Skip to content

Commit

Permalink
Add support for fetching VSM details of multiple pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Sep 12, 2023
1 parent b852266 commit e448c69
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ func getConfigRepoPreflightCheckCommand() *cobra.Command {
var pathAndPattern []string

if len(configRepoPreflightObj.pipelineFiles) != 0 {
for _, pipelinefile := range configRepoPreflightObj.pipelineFiles {
file, err := client.GetPipelineFiles(pipelinefile)
for _, pipelineFile := range configRepoPreflightObj.pipelineFiles {
file, err := client.GetPipelineFiles(pipelineFile)
if err != nil {
return err
}
Expand Down
87 changes: 59 additions & 28 deletions cmd/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ var (
goCDPipelinePause bool
goCDPipelineUnPause bool
numberOfDays time.Duration
downStreamPipeline bool
upStreamPipeline bool
)

func registerPipelinesCommand() *cobra.Command {
Expand Down Expand Up @@ -108,59 +106,92 @@ func getPipelinesCommand() *cobra.Command {
}

func getPipelineVSMCommand() *cobra.Command {
var (
downStreamPipeline bool
upStreamPipeline bool
goCDPipelines []string
)

type pipelineVSM struct {
Pipeline string `json:"pipeline,omitempty" yaml:"pipeline,omitempty"`
DownstreamPipelines []string `json:"downstream_pipelines,omitempty" yaml:"downstream_pipelines,omitempty"`
UpstreamPipelines []string `json:"upstream_pipelines,omitempty" yaml:"upstream_pipelines,omitempty"`
}

getPipelineVSMCmd := &cobra.Command{
Use: "vsm",
Short: "Command to GET downstream pipelines of a specified pipeline present in GoCD [https://api.gocd.org/current/#get-pipeline-config]",
Args: cobra.RangeArgs(1, 1),
Args: cobra.NoArgs,
PreRunE: setCLIClient,
Example: `gocd-cli pipeline get sample-pipeline --query "[*] | name eq sample-group"`,
RunE: func(cmd *cobra.Command, args []string) error {
pipelineName := args[0]
vsm := make([]pipelineVSM, 0)

pipelineHistory, err := client.GetLimitedPipelineRunHistory(args[0], "10", "0")
if err != nil {
return err
}
for _, goCDPipeline := range goCDPipelines {
pipelineHistory, err := client.GetLimitedPipelineRunHistory(goCDPipeline, "10", "0")
if err != nil {
return err
}

cliLogger.Debugf("run history for pipeline '%s' was fetched successfully", pipelineName)
cliLogger.Debugf("run history for pipeline '%s' was fetched successfully", goCDPipeline)

response, err := client.GetPipelineVSM(pipelineHistory[0].Name, fmt.Sprintf("%d", pipelineHistory[0].Counter))
if err != nil {
return err
}
response, err := client.GetPipelineVSM(pipelineHistory[0].Name, fmt.Sprintf("%d", pipelineHistory[0].Counter))
if err != nil {
return err
}

cliLogger.Debugf("VSM details for pipeline '%s' instace '%d' was fetched successfully", pipelineName, pipelineHistory[0].Counter)
cliLogger.Debugf("VSM details for pipeline '%s' instace '%d' was fetched successfully", goCDPipeline, pipelineHistory[0].Counter)

var pipelineStreams []string
var pipelineStreams []string

if downStreamPipeline {
cliLogger.Debugf("since --down-stream is set fetching downstream pipelines")
if downStreamPipeline {
cliLogger.Debugf("since --down-stream is set fetching downstream pipelines")

pipelineStreams = findDownStreamPipelines(pipelineName, response)
}
pipelineStreams = findDownStreamPipelines(goCDPipeline, response)
}

if upStreamPipeline {
cliLogger.Debugf("since --up-stream is set fetching upstream pipelines")
if upStreamPipeline {
cliLogger.Debugf("since --up-stream is set fetching upstream pipelines")

pipelineStreams = findUpStreamPipelines(pipelineName, response)
}
pipelineStreams = findUpStreamPipelines(goCDPipeline, response)
}

fmt.Println(pipelineStreams)
pipelineDependencies, err := parsePipelineConfig(pipelineName, pipelineStreams)
if err != nil {
return err
pipelineDependencies, err := parsePipelineConfig(goCDPipeline, pipelineStreams)
if err != nil {
return err
}

if upStreamPipeline {
vsm = append(vsm, pipelineVSM{
Pipeline: goCDPipeline,
UpstreamPipelines: pipelineDependencies,
})
}

if downStreamPipeline {
vsm = append(vsm, pipelineVSM{
Pipeline: goCDPipeline,
DownstreamPipelines: pipelineDependencies,
})
}
}

return cliRenderer.Render(pipelineDependencies)
return cliRenderer.Render(vsm)
},
}

getPipelineVSMCmd.PersistentFlags().StringSliceVarP(&goCDPipelines, "pipeline", "", nil,
"name of the pipeline for which the VSM has to be retrieved")
getPipelineVSMCmd.PersistentFlags().BoolVarP(&downStreamPipeline, "down-stream", "", false,
"when enabled, will fetch all downstream pipelines of a specified pipeline.")
getPipelineVSMCmd.PersistentFlags().BoolVarP(&upStreamPipeline, "up-stream", "", false,
"when enabled, will fetch all upstream pipelines of a specified pipeline. (NOTE: flag up-stream is still in experimental phase)")
getPipelineVSMCmd.MarkFlagsMutuallyExclusive("down-stream", "up-stream")

if err := getPipelineVSMCmd.MarkPersistentFlagRequired("pipeline"); err != nil {
cliLogger.Fatalf("%v", err)
}

return getPipelineVSMCmd
}

Expand Down

0 comments on commit e448c69

Please sign in to comment.