From 0c0ea8a83c1b67cadafdda05e6076b51228a1148 Mon Sep 17 00:00:00 2001 From: nikhilsbhat Date: Thu, 14 Sep 2023 13:04:12 +0530 Subject: [PATCH] Update the way of identifying the config-repo for a given pipeline --- cmd/pipelines.go | 30 +++++++++--------------------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/cmd/pipelines.go b/cmd/pipelines.go index 6ce20f6..a97a1d6 100644 --- a/cmd/pipelines.go +++ b/cmd/pipelines.go @@ -912,35 +912,22 @@ func getPipelineMapping() *cobra.Command { PreRunE: setCLIClient, RunE: func(cmd *cobra.Command, args []string) error { pipelineName := args[0] - var goCDConfigRepoName, goCDEnvironmentName string + var goCDConfigRepoName, goCDEnvironmentName, originGoCD string - cliLogger.Debugf("all configrepo's information would be fetched, to identify pipeline is part of which config repos") - configRepos, err := client.GetConfigRepos() + cliLogger.Debugf("fetching pipeline config to identify which config repo this pipeline is part of") + pipelineConfig, err := client.GetPipelineConfig(pipelineName) if err != nil { return err } - cliLogger.Debugf("all configrepo's information was fetched successfully") + cliLogger.Debugf("pipeline config was retrieved successfully") - for _, configRepo := range configRepos { - cliLogger.Debugf("fetching config repo definition, to identify pipeline is part of which config repo") - configRepoDefinition, err := client.GetConfigRepoDefinitions(configRepo.ID) - if err != nil { - if !strings.Contains(err.Error(), "got 404 from GoCD") { - return err - } - } - for _, pipelineGroup := range configRepoDefinition.Groups { - for _, pipeline := range pipelineGroup.Pipelines { - if pipeline.Name == pipelineName { - goCDConfigRepoName = configRepo.ID - } - } - } + originGoCD = "true" + if pipelineConfig.Origin.Type != "gocd" { + goCDConfigRepoName = pipelineConfig.Origin.ID + originGoCD = "false" } - cliLogger.Debugf("all GoCD environment information would be fetched, to identify pipeline is part of which environment") - environmentNames, err := client.GetEnvironments() if err != nil { return err @@ -960,6 +947,7 @@ func getPipelineMapping() *cobra.Command { "pipeline": pipelineName, "config_repo": goCDConfigRepoName, "environment": goCDEnvironmentName, + "origin_gocd": originGoCD, } if len(jsonQuery) != 0 { diff --git a/go.mod b/go.mod index 7d30b03..d8ab930 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/ghodss/yaml v1.0.0 - github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230911084833-1ceb6cff8ea8 + github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230914073201-732c431eb78e github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.6.1 github.com/stretchr/testify v1.8.2 diff --git a/go.sum b/go.sum index 28a5fda..f06219d 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230911084833-1ceb6cff8ea8 h1:64XraEHr9B9gUsBxjQq7BFQyfJwqViBnvq6t90Ks4H0= -github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230911084833-1ceb6cff8ea8/go.mod h1:ubwvA7YgcWDUXiCghrJGRKTdeVuXpDmNzIp6ZvVzdfk= +github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230914073201-732c431eb78e h1:mNeqFt0S4/j+1BEglKPspSH1PrD7jaA0TmcbE8MTQP4= +github.com/nikhilsbhat/gocd-sdk-go v0.1.9-0.20230914073201-732c431eb78e/go.mod h1:ubwvA7YgcWDUXiCghrJGRKTdeVuXpDmNzIp6ZvVzdfk= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=