Skip to content

Commit

Permalink
chore(pipelines): add --name flag to pipeline deploy (#3247)
Browse files Browse the repository at this point in the history
Part of #3231.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
  • Loading branch information
huanjani authored Feb 7, 2022
1 parent b5361f5 commit d9b5027
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 194 deletions.
46 changes: 34 additions & 12 deletions internal/pkg/cli/pipeline_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const connectionsURL = "https://console.aws.amazon.com/codesuite/settings/connec

type deployPipelineVars struct {
appName string
name string
skipConfirmation bool
}

Expand All @@ -60,7 +61,6 @@ type deployPipelineOpts struct {
ws wsPipelineReader
codestar codestar

pipelineName string
shouldPromptUpdateConnection bool
}

Expand Down Expand Up @@ -100,6 +100,11 @@ func newDeployPipelineOpts(vars deployPipelineVars) (*deployPipelineOpts, error)

// Validate returns an error if the flag values passed by the user are invalid.
func (o *deployPipelineOpts) Validate() error {
if o.name != "" {
if err := o.validatePipelineName(); err != nil {
return err
}
}
return nil
}

Expand All @@ -114,7 +119,7 @@ func (o *deployPipelineOpts) Execute() error {
}
o.prog.Stop(log.Ssuccessf(fmtPipelineDeployResourcesComplete, color.HighlightUserInput(o.appName)))

// read pipeline manifest
// Read pipeline manifest.
data, err := o.ws.ReadPipelineManifest()
if err != nil {
return fmt.Errorf("read pipeline manifest: %w", err)
Expand All @@ -123,10 +128,11 @@ func (o *deployPipelineOpts) Execute() error {
if err != nil {
return fmt.Errorf("unmarshal pipeline manifest: %w", err)
}
if len(pipeline.Name) > 100 {
return fmt.Errorf(`pipeline name '%s' must be shorter than 100 characters`, pipeline.Name)
if err := pipeline.Validate(); err != nil {
return fmt.Errorf("validate pipeline: %w", err)
}
o.pipelineName = pipeline.Name
// Set pipeline name; if passed in with flag, should be identical.
o.name = pipeline.Name

// If the source has an existing connection, get the correlating ConnectionARN .
connection, ok := pipeline.Source.Properties["connection_name"]
Expand Down Expand Up @@ -173,6 +179,21 @@ func (o *deployPipelineOpts) Execute() error {
return nil
}

func (o *deployPipelineOpts) validatePipelineName() error {
data, err := o.ws.ReadPipelineManifest()
if err != nil {
return fmt.Errorf("read pipeline manifest: %w", err)
}
pipeline, err := manifest.UnmarshalPipeline(data)
if err != nil {
return fmt.Errorf("unmarshal pipeline manifest: %w", err)
}
if pipeline.Name != o.name {
return fmt.Errorf(`pipeline %s not found in the workspace`, color.HighlightUserInput(o.name))
}
return nil
}

func (o *deployPipelineOpts) convertStages(manifestStages []manifest.PipelineStage) ([]deploy.PipelineStage, error) {
var stages []deploy.PipelineStage
workloads, err := o.ws.ListWorkloads()
Expand Down Expand Up @@ -233,7 +254,7 @@ func (o *deployPipelineOpts) shouldUpdate() (bool, error) {
return true, nil
}

shouldUpdate, err := o.prompt.Confirm(fmt.Sprintf(fmtPipelineDeployExistPrompt, o.pipelineName), "")
shouldUpdate, err := o.prompt.Confirm(fmt.Sprintf(fmtPipelineDeployExistPrompt, o.name), "")
if err != nil {
return false, fmt.Errorf("prompt for pipeline deploy: %w", err)
}
Expand All @@ -252,7 +273,7 @@ func (o *deployPipelineOpts) deployPipeline(in *deploy.CreatePipelineInput) erro
return fmt.Errorf("get bucket name: %w", err)
}
if !exist {
o.prog.Start(fmt.Sprintf(fmtPipelineDeployStart, color.HighlightUserInput(o.pipelineName)))
o.prog.Start(fmt.Sprintf(fmtPipelineDeployStart, color.HighlightUserInput(o.name)))

// If the source requires CodeStar Connections, the user is prompted to update the connection status.
if o.shouldPromptUpdateConnection {
Expand All @@ -273,11 +294,11 @@ func (o *deployPipelineOpts) deployPipeline(in *deploy.CreatePipelineInput) erro
if err := o.pipelineDeployer.CreatePipeline(in, bucketName); err != nil {
var alreadyExists *cloudformation.ErrStackAlreadyExists
if !errors.As(err, &alreadyExists) {
o.prog.Stop(log.Serrorf(fmtPipelineDeployFailed, color.HighlightUserInput(o.pipelineName)))
o.prog.Stop(log.Serrorf(fmtPipelineDeployFailed, color.HighlightUserInput(o.name)))
return fmt.Errorf("create pipeline: %w", err)
}
}
o.prog.Stop(log.Ssuccessf(fmtPipelineDeployComplete, color.HighlightUserInput(o.pipelineName)))
o.prog.Stop(log.Ssuccessf(fmtPipelineDeployComplete, color.HighlightUserInput(o.name)))
return nil
}

Expand All @@ -289,12 +310,12 @@ func (o *deployPipelineOpts) deployPipeline(in *deploy.CreatePipelineInput) erro
if !shouldUpdate {
return nil
}
o.prog.Start(fmt.Sprintf(fmtPipelineDeployProposalStart, color.HighlightUserInput(o.pipelineName)))
o.prog.Start(fmt.Sprintf(fmtPipelineDeployProposalStart, color.HighlightUserInput(o.name)))
if err := o.pipelineDeployer.UpdatePipeline(in, bucketName); err != nil {
o.prog.Stop(log.Serrorf(fmtPipelineDeployProposalFailed, color.HighlightUserInput(o.pipelineName)))
o.prog.Stop(log.Serrorf(fmtPipelineDeployProposalFailed, color.HighlightUserInput(o.name)))
return fmt.Errorf("update pipeline: %w", err)
}
o.prog.Stop(log.Ssuccessf(fmtPipelineDeployProposalComplete, color.HighlightUserInput(o.pipelineName)))
o.prog.Stop(log.Ssuccessf(fmtPipelineDeployProposalComplete, color.HighlightUserInput(o.name)))
return nil
}

Expand Down Expand Up @@ -338,6 +359,7 @@ func buildPipelineDeployCmd() *cobra.Command {
}),
}
cmd.Flags().StringVarP(&vars.appName, appFlag, appFlagShort, tryReadingAppName(), appFlagDescription)
cmd.Flags().StringVarP(&vars.name, nameFlag, nameFlagShort, "", pipelineFlagDescription)
cmd.Flags().BoolVar(&vars.skipConfirmation, yesFlag, false, yesFlagDescription)
return cmd
}
Loading

0 comments on commit d9b5027

Please sign in to comment.