Skip to content

Commit

Permalink
Add support for passing multiple pipelines for getting pipeline mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Sep 14, 2023
1 parent bb2c233 commit 1547066
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
74 changes: 39 additions & 35 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
goCDPipelineMessage string
goCDPipelineETAG string
goCDPipelineTemplateName string
goCDPipelines []string
goCDPipelineGroups []string
goCDEnvironments []string
goCDPausePipelineAtStart bool
Expand Down Expand Up @@ -112,7 +113,6 @@ func getPipelineVSMCommand() *cobra.Command {
downStreamPipeline bool
upStreamPipeline bool
goCDPipelineInstanceNumber []string
goCDPipelines []string
)

type pipelineVSM struct {
Expand All @@ -126,7 +126,7 @@ func getPipelineVSMCommand() *cobra.Command {
Short: "Command to GET downstream pipelines of a specified pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-config]",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline get sample-pipeline --query "[*] | name eq sample-group"`,
Example: `gocd-cli pipeline vsm --pipeline animation-movies --pipeline animation-and-action-movies --down-stream --instance animation-movies=14 --yaml"`,
RunE: func(cmd *cobra.Command, args []string) error {
vsm := make([]pipelineVSM, 0)

Expand Down Expand Up @@ -944,53 +944,56 @@ func getPipelineMapping() *cobra.Command {
getPipelineMappingCmd := &cobra.Command{
Use: "get-mappings",
Short: "Command to Identify the given pipeline is part of which config-repo/environment of GoCD",
Example: "gocd-cli pipeline get-mappings helm-images --yaml",
Args: cobra.RangeArgs(1, 1),
Example: "gocd-cli pipeline get-mappings --pipeline helm-images --yaml",
Args: cobra.NoArgs,
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
pipelineName := args[0]
var goCDConfigRepoName, goCDEnvironmentName, originGoCD string
pipelineMappings := make([]map[string]string, 0)

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
}
for _, goCDPipeline := range goCDPipelines {
var goCDConfigRepoName, goCDEnvironmentName, originGoCD string

cliLogger.Debugf("pipeline config was retrieved successfully")
cliLogger.Debugf("fetching pipeline config to identify which config repo this pipeline is part of")
pipelineConfig, err := client.GetPipelineConfig(goCDPipeline)
if err != nil {
return err
}

originGoCD = "true"
if pipelineConfig.Origin.Type != "gocd" {
goCDConfigRepoName = pipelineConfig.Origin.ID
originGoCD = "false"
}
cliLogger.Debugf("pipeline config was retrieved successfully")

environmentNames, err := client.GetEnvironments()
if err != nil {
return err
}
originGoCD = "true"
if pipelineConfig.Origin.Type != "gocd" {
goCDConfigRepoName = pipelineConfig.Origin.ID
originGoCD = "false"
}

environmentNames, err := client.GetEnvironments()
if err != nil {
return err
}

cliLogger.Debugf("all GoCD environment information was fetched successfully")
cliLogger.Debugf("all GoCD environment information was fetched successfully")

for _, environmentName := range environmentNames {
for _, pipeline := range environmentName.Pipelines {
if pipeline.Name == pipelineName {
goCDEnvironmentName = environmentName.Name
for _, environmentName := range environmentNames {
for _, pipeline := range environmentName.Pipelines {
if pipeline.Name == goCDPipeline {
goCDEnvironmentName = environmentName.Name
}
}
}
}

output := map[string]string{
"pipeline": pipelineName,
"config_repo": goCDConfigRepoName,
"environment": goCDEnvironmentName,
"origin_gocd": originGoCD,
pipelineMappings = append(pipelineMappings, map[string]string{
"pipeline": goCDPipeline,
"config_repo": goCDConfigRepoName,
"environment": goCDEnvironmentName,
"origin_gocd": originGoCD,
})
}

if len(jsonQuery) != 0 {
cliLogger.Debugf(queryEnabledMessage, jsonQuery)

baseQuery, err := render.SetQuery(output, jsonQuery)
baseQuery, err := render.SetQuery(pipelineMappings, jsonQuery)
if err != nil {
return err
}
Expand All @@ -1000,11 +1003,12 @@ func getPipelineMapping() *cobra.Command {
return cliRenderer.Render(baseQuery.RunQuery())
}

return cliRenderer.Render(output)
return cliRenderer.Render(pipelineMappings)
},
}

getPipelineMappingCmd.SetUsageTemplate(getUsageTemplate())
getPipelineMappingCmd.PersistentFlags().StringSliceVarP(&goCDPipelines, "pipeline", "", nil,
"name of the pipeline for which the environment and config repo mappings to be fetched")

return getPipelineMappingCmd
}
Expand Down
7 changes: 4 additions & 3 deletions docs/doc/gocd-cli_pipeline_get-mappings.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ gocd-cli pipeline get-mappings [flags]
### Examples

```
gocd-cli pipeline get-mappings helm-images --yaml
gocd-cli pipeline get-mappings --pipeline helm-images --yaml
```

### Options

```
-h, --help help for get-mappings
-h, --help help for get-mappings
--pipeline strings name of the pipeline for which the environment and config repo mappings to be fetched
```

### Options inherited from parent commands
Expand All @@ -41,4 +42,4 @@ gocd-cli pipeline get-mappings helm-images --yaml

* [gocd-cli pipeline](gocd-cli_pipeline.md) - Command to operate on pipelines present in GoCD

###### Auto generated by spf13/cobra on 13-Sep-2023
###### Auto generated by spf13/cobra on 14-Sep-2023
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_pipeline_vsm.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ gocd-cli pipeline vsm [flags]
### Examples

```
gocd-cli pipeline get sample-pipeline --query "[*] | name eq sample-group"
gocd-cli pipeline vsm --pipeline animation-movies --pipeline animation-and-action-movies --down-stream --instance animation-movies=14 --yaml"
```

### Options
Expand Down
2 changes: 1 addition & 1 deletion docs/doc/gocd-cli_plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ gocd-cli plugin [flags]
* [gocd-cli plugin list](gocd-cli_plugin_list.md) - Command to LIST all plugins present in GoCD
* [gocd-cli plugin update-settings](gocd-cli_plugin_update-settings.md) - Command to UPDATE settings of a specified plugin present in GoCD [https://api.gocd.org/current/#update-plugin-settings]

###### Auto generated by spf13/cobra on 11-Sep-2023
###### Auto generated by spf13/cobra on 14-Sep-2023

0 comments on commit 1547066

Please sign in to comment.