From c9bb31ca4055df0fa0e57027b0f4ac5afe77559a Mon Sep 17 00:00:00 2001 From: Scott Merchant Date: Wed, 23 Oct 2024 11:51:48 +0200 Subject: [PATCH] feat: update cli to retrieve applications for product release steps (#758) ## What Adding promotion commits to product release steps if the version of codefresh is compatible. I've added a fall back in case the cli is connected to an old version of codefresh (on prem). ## Why A customer wanted to know the commit created by promotions for each step of a product release for each application. See Jira ticket for details. ## Notes Jira: https://codefresh-io.atlassian.net/browse/CR-25720 --- Makefile | 2 +- cmd/commands/product-release.go | 80 ++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 7053a567..06d4c83b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.1.65 +VERSION=v0.1.66 OUT_DIR=dist YEAR?=$(shell date +"%Y") diff --git a/cmd/commands/product-release.go b/cmd/commands/product-release.go index 2cbad81b..80c6ed66 100644 --- a/cmd/commands/product-release.go +++ b/cmd/commands/product-release.go @@ -22,6 +22,7 @@ import ( "github.com/codefresh-io/cli-v2/pkg/util" + "github.com/codefresh-io/cli-v2/pkg/log" "github.com/codefresh-io/go-sdk/pkg/client" platmodel "github.com/codefresh-io/go-sdk/pkg/model/promotion-orchestrator" "github.com/spf13/cobra" @@ -37,6 +38,55 @@ type ( } ) +const latest_query = ` +query getProductReleasesList( + $productName: String! + $filters: ProductReleaseFiltersArgs! + $pagination: SlicePaginationArgs +) { + productReleases(productName: $productName, filters: $filters, pagination: $pagination) { + edges { + node { + releaseId + steps { + environmentName + status + applications { + applicationId { + runtime + namespace + name + } + commitSha + } + } + status + } + } + } +} +` + +const pre_v1_3120_1_query = ` +query getProductReleasesList( + $productName: String! + $filters: ProductReleaseFiltersArgs! + $pagination: SlicePaginationArgs +) { + productReleases(productName: $productName, filters: $filters, pagination: $pagination) { + edges { + node { + releaseId + steps { + environmentName + status + } + status + } + } + } +}` + func NewProductReleaseCommand() *cobra.Command { cmd := &cobra.Command{ Use: "product-release", @@ -98,25 +148,7 @@ func newProductReleaseListCommand() *cobra.Command { // client here is for mock testings usage func runProductReleaseList(ctx context.Context, filterArgs platmodel.ProductReleaseFiltersArgs, productName string, pageLimit int) error { - query := ` -query getProductReleasesList( - $productName: String! - $filters: ProductReleaseFiltersArgs! - $pagination: SlicePaginationArgs -) { - productReleases(productName: $productName, filters: $filters, pagination: $pagination) { - edges { - node { - releaseId - steps { - environmentName - status - } - status - } - } - } -}` + // add pagination - default for now is last 20 variables := map[string]any{ "filters": filterArgs, @@ -126,9 +158,15 @@ query getProductReleasesList( }, } - productReleasesPage, err := client.GraphqlAPI[productReleaseSlice](ctx, cfConfig.NewClient().InternalClient(), query, variables) + productReleasesPage, err := client.GraphqlAPI[productReleaseSlice](ctx, cfConfig.NewClient().InternalClient(), latest_query, variables) if err != nil { - return fmt.Errorf("failed to get product releases: %w", err) + if strings.Contains(err.Error(), "Cannot query field \\\"applications\\\" on type \\\"ProductReleaseStep\\\".") { + log.G().Warn("codefresh version older than v1.3120.1 detected. Using pre v1.3120.1 query which excludes applications.") + productReleasesPage, err = client.GraphqlAPI[productReleaseSlice](ctx, cfConfig.NewClient().InternalClient(), pre_v1_3120_1_query, variables) + } + if err != nil { + return fmt.Errorf("failed to get product releases: %s", err.Error()) + } } if len(productReleasesPage.Edges) == 0 {