Skip to content

Commit

Permalink
Add support for fetching mappings of a selected pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Sep 13, 2023
1 parent 0825f02 commit 5670919
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
82 changes: 82 additions & 0 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ GET/PAUSE/UNPAUSE/UNLOCK/SCHEDULE and comment on a GoCD pipeline`,
pipelineCommand.AddCommand(validatePipelinesCommand())
pipelineCommand.AddCommand(exportPipelineToConfigRepoFormatCommand())
pipelineCommand.AddCommand(getPipelineVSMCommand())
pipelineCommand.AddCommand(getPipelineMapping())

for _, command := range pipelineCommand.Commands() {
command.SilenceUsage = true
Expand Down Expand Up @@ -902,6 +903,87 @@ func exportPipelineToConfigRepoFormatCommand() *cobra.Command {
return exportPipelineToConfigRepoFormatCmd
}

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),
PreRunE: setCLIClient,
RunE: func(cmd *cobra.Command, args []string) error {
pipelineName := args[0]
var goCDConfigRepoName, goCDEnvironmentName string

cliLogger.Debugf("all configrepo's information would be fetched, to identify pipeline is part of which config repos")
configRepos, err := client.GetConfigRepos()
if err != nil {
return err
}

cliLogger.Debugf("all configrepo's information was fetched 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
}
}
}
}

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
}

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
}
}
}

output := map[string]string{
"pipeline": pipelineName,
"config_repo": goCDConfigRepoName,
"environment": goCDEnvironmentName,
}

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

baseQuery, err := render.SetQuery(output, jsonQuery)
if err != nil {
return err
}

cliLogger.Debugf(baseQuery.Print())

return cliRenderer.Render(baseQuery.RunQuery())
}

return cliRenderer.Render(output)
},
}

getPipelineMappingCmd.SetUsageTemplate(getUsageTemplate())

return getPipelineMappingCmd
}

func findDownStreamPipelines(pipelineName string, resp gocd.VSM) []string {
newParents := []string{pipelineName}
for _, level := range resp.Level {
Expand Down
3 changes: 2 additions & 1 deletion docs/doc/gocd-cli_pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ gocd-cli pipeline [flags]
* [gocd-cli pipeline export-format](gocd-cli_pipeline_export-format.md) - Command to export specified pipeline present in GoCD to appropriate config repo format [https://api.gocd.org/current/#export-pipeline-config-to-config-repo-format]
* [gocd-cli pipeline get](gocd-cli_pipeline_get.md) - Command to GET pipeline config of a specified pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-config]
* [gocd-cli pipeline get-all](gocd-cli_pipeline_get-all.md) - Command to GET all pipelines present in GoCD [https://api.gocd.org/current/#get-feed-of-all-stages-in-a-pipeline]
* [gocd-cli pipeline get-mappings](gocd-cli_pipeline_get-mappings.md) - Command to Identify the given pipeline is part of which config-repo/environment of GoCD
* [gocd-cli pipeline history](gocd-cli_pipeline_history.md) - Command to GET pipeline run history present in GoCD [https://api.gocd.org/current/#get-pipeline-history]
* [gocd-cli pipeline instance](gocd-cli_pipeline_instance.md) - Command to GET instance of a specific pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-instance]
* [gocd-cli pipeline last-schedule](gocd-cli_pipeline_last-schedule.md) - Command to GET last scheduled time of the pipeline present in GoCD [/pipelineHistory.json?pipelineName=nameOfThePipeline]
Expand All @@ -60,4 +61,4 @@ gocd-cli pipeline [flags]
* [gocd-cli pipeline validate-syntax](gocd-cli_pipeline_validate-syntax.md) - Command validate pipeline syntax by running it against appropriate GoCD plugin
* [gocd-cli pipeline vsm](gocd-cli_pipeline_vsm.md) - Command to GET downstream pipelines of a specified pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-config]

###### Auto generated by spf13/cobra on 11-Sep-2023
###### Auto generated by spf13/cobra on 13-Sep-2023
44 changes: 44 additions & 0 deletions docs/doc/gocd-cli_pipeline_get-mappings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## gocd-cli pipeline get-mappings

Command to Identify the given pipeline is part of which config-repo/environment of GoCD

```
gocd-cli pipeline get-mappings [flags]
```

### Examples

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

### Options

```
-h, --help help for get-mappings
```

### Options inherited from parent commands

```
-t, --auth-token string token to authenticate with GoCD server, should not be co-used with basic auth (username/password)
--ca-file-path string path to file containing CA cert used to authenticate GoCD server, if you have one
--from-file string file containing configurations of objects that needs to be created in GoCD, config-repo/pipeline-group/environment and etc.
--json enable this to Render output in JSON format
-l, --log-level string log level for gocd cli, log levels supported by [https://github.com/sirupsen/logrus] will work (default "info")
--no-color enable this to Render output in YAML format
-p, --password string password to authenticate with GoCD server
-q, --query string query to filter the results, ex: '.material.attributes.type | id eq git'. this uses library gojsonq beneath
more queries can be found here https://github.com/thedevsaddam/gojsonq/wiki/Queries
--server-url string GoCD server URL base path (default "http://localhost:8153/go")
--skip-cache-config if enabled locally save auth configs would not be used to authenticate GoCD server (path: $HOME/.gocd/auth_config.yaml)
--to-file string file to which the output needs to be written to
-u, --username string username to authenticate with GoCD server
--yaml enable this to Render output in YAML format
```

### SEE ALSO

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

###### Auto generated by spf13/cobra on 13-Sep-2023

0 comments on commit 5670919

Please sign in to comment.