From 5886ec82d5c57213578781eda621b7b1ed12c1ba Mon Sep 17 00:00:00 2001 From: Damien Coraboeuf Date: Sun, 21 Mar 2021 16:34:29 +0100 Subject: [PATCH] Always normalizing the branch name --- cmd/branchSetPropertyGeneric.go | 1 + cmd/branchSetPropertyGit.go | 6 +- cmd/branchSetup.go | 96 +++++++++++++-------------- cmd/branches.go | 10 +++ cmd/buildSetPropertyGeneric.go | 1 + cmd/buildSetPropertyGitCommit.go | 1 + cmd/buildSetup.go | 47 ++++++++----- cmd/promotionLevelSetup.go | 1 + cmd/validate.go | 1 + cmd/validateCHML.go | 1 + cmd/validateMetrics.go | 1 + cmd/validatePercentage.go | 1 + cmd/validateTests.go | 1 + cmd/validationStampSetupCHML.go | 1 + cmd/validationStampSetupGeneric.go | 1 + cmd/validationStampSetupMetrics.go | 1 + cmd/validationStampSetupPercentage.go | 1 + cmd/validationStampSetupTests.go | 1 + 18 files changed, 104 insertions(+), 69 deletions(-) create mode 100644 cmd/branches.go diff --git a/cmd/branchSetPropertyGeneric.go b/cmd/branchSetPropertyGeneric.go index 3e615e8..bfec00f 100644 --- a/cmd/branchSetPropertyGeneric.go +++ b/cmd/branchSetPropertyGeneric.go @@ -46,6 +46,7 @@ ontrack-cli branch set-property --project PROJECT --branch BRANCH generic --prop if err != nil { return err } + branch = NormalizeBranchName(branch) property, err := cmd.Flags().GetString("property") if err != nil { diff --git a/cmd/branchSetPropertyGit.go b/cmd/branchSetPropertyGit.go index b12ee70..dfeea1c 100644 --- a/cmd/branchSetPropertyGit.go +++ b/cmd/branchSetPropertyGit.go @@ -55,6 +55,10 @@ As of now, this also sets the "GitCommitPropertyLink" property by default (build if err != nil { return err } + if gitBranch == "" { + gitBranch = branch + } + branch = NormalizeBranchName(branch) cfg, err := config.GetSelectedConfiguration() if err != nil { @@ -114,6 +118,4 @@ func init() { // is called directly, e.g.: // branchSetPropertyGitCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") branchSetPropertyGitCmd.Flags().StringP("git-branch", "g", "", "Git branch to associate with the branch") - - branchSetPropertyGitCmd.MarkFlagRequired("git-branch") } diff --git a/cmd/branchSetup.go b/cmd/branchSetup.go index 04500f9..6c67cba 100644 --- a/cmd/branchSetup.go +++ b/cmd/branchSetup.go @@ -22,17 +22,12 @@ THE SOFTWARE. package cmd import ( - "regexp" - "github.com/spf13/cobra" client "ontrack-cli/client" config "ontrack-cli/config" ) -var branchSetupProject string -var branchSetupBranch string - // branchSetupCmd represents the branchSetup command var branchSetupCmd = &cobra.Command{ Use: "setup", @@ -45,38 +40,40 @@ The BRANCH name will be adapted to fit Ontrack naming conventions, so you can directly give the name of the Git branch. `, RunE: func(cmd *cobra.Command, args []string) error { - return branchSetup() - }, -} + project, err := cmd.Flags().GetString("project") + if err != nil { + return err + } + branch, err := cmd.Flags().GetString("branch") + if err != nil { + return err + } + branch = NormalizeBranchName(branch) -func branchSetup() error { - config, err := config.GetSelectedConfiguration() - if err != nil { - return err - } - // Normalizing the name of the branch - re := regexp.MustCompile("[^A-Za-z0-9\\._-]") - normalizedBranchName := re.ReplaceAllString(branchSetupBranch, "-") - // Creates or get the project - var data struct { - CreateProjectOrGet struct { - Project struct { - ID int - } - Errors []struct { - Message string - } + config, err := config.GetSelectedConfiguration() + if err != nil { + return err } - CreateBranchOrGet struct { - Branch struct { - ID int + // Creates or get the project + var data struct { + CreateProjectOrGet struct { + Project struct { + ID int + } + Errors []struct { + Message string + } } - Errors []struct { - Message string + CreateBranchOrGet struct { + Branch struct { + ID int + } + Errors []struct { + Message string + } } } - } - if err := client.GraphQLCall(config, ` + if err := client.GraphQLCall(config, ` mutation ProjectSetup($project: String!, $branch: String!) { createProjectOrGet(input: {name: $project}) { project { @@ -96,23 +93,24 @@ func branchSetup() error { } } `, map[string]interface{}{ - "project": branchSetupProject, - "branch": normalizedBranchName, - }, &data); err != nil { - return err - } + "project": project, + "branch": branch, + }, &data); err != nil { + return err + } - // Checks errors for the project - if err := client.CheckDataErrors(data.CreateProjectOrGet.Errors); err != nil { - return err - } - // Checks errors for the branch - if err := client.CheckDataErrors(data.CreateBranchOrGet.Errors); err != nil { - return err - } + // Checks errors for the project + if err := client.CheckDataErrors(data.CreateProjectOrGet.Errors); err != nil { + return err + } + // Checks errors for the branch + if err := client.CheckDataErrors(data.CreateBranchOrGet.Errors); err != nil { + return err + } - // OK - return nil + // OK + return nil + }, } func init() { @@ -126,8 +124,8 @@ func init() { // Cobra supports local flags which will only run when this command // is called directly, e.g.: - branchSetupCmd.Flags().StringVarP(&branchSetupProject, "project", "p", "", "Project name") + branchSetupCmd.Flags().StringP("project", "p", "", "Project name") branchSetupCmd.MarkFlagRequired("project") - branchSetupCmd.Flags().StringVarP(&branchSetupBranch, "branch", "b", "", "Branch name or Git branch name") + branchSetupCmd.Flags().StringP("branch", "b", "", "Branch name or Git branch name") branchSetupCmd.MarkFlagRequired("branch") } diff --git a/cmd/branches.go b/cmd/branches.go new file mode 100644 index 0000000..955b80f --- /dev/null +++ b/cmd/branches.go @@ -0,0 +1,10 @@ +package cmd + +import ( + "regexp" +) + +func NormalizeBranchName(name string) string { + re := regexp.MustCompile("[^A-Za-z0-9\\._-]") + return re.ReplaceAllString(name, "-") +} diff --git a/cmd/buildSetPropertyGeneric.go b/cmd/buildSetPropertyGeneric.go index c841765..58c7fe6 100644 --- a/cmd/buildSetPropertyGeneric.go +++ b/cmd/buildSetPropertyGeneric.go @@ -46,6 +46,7 @@ Example: if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/buildSetPropertyGitCommit.go b/cmd/buildSetPropertyGitCommit.go index 30f4c9d..816304d 100644 --- a/cmd/buildSetPropertyGitCommit.go +++ b/cmd/buildSetPropertyGitCommit.go @@ -49,6 +49,7 @@ Example: if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/buildSetup.go b/cmd/buildSetup.go index 6c47d99..dccf3c2 100644 --- a/cmd/buildSetup.go +++ b/cmd/buildSetup.go @@ -22,19 +22,12 @@ THE SOFTWARE. package cmd import ( - "regexp" - "github.com/spf13/cobra" client "ontrack-cli/client" config "ontrack-cli/config" ) -var buildSetupProject string -var buildSetupBranch string -var buildSetupBuild string -var buildSetupDescription string - // buildSetupCmd represents the buildSetup command var buildSetupCmd = &cobra.Command{ Use: "setup", @@ -53,6 +46,26 @@ and the same command run a second time won't do anything. } func buildSetup(cmd *cobra.Command) error { + project, err := cmd.Flags().GetString("project") + if err != nil { + return err + } + + branch, err := cmd.Flags().GetString("branch") + if err != nil { + return err + } + branch = NormalizeBranchName(branch) + + build, err := cmd.Flags().GetString("build") + if err != nil { + return err + } + + description, err := cmd.Flags().GetString("description") + if err != nil { + return err + } // Run info runInfo, err := GetRunInfo(cmd) @@ -64,9 +77,7 @@ func buildSetup(cmd *cobra.Command) error { if err != nil { return err } - // Normalizing the name of the branch - re := regexp.MustCompile("[^A-Za-z0-9\\._-]") - normalizedBranchName := re.ReplaceAllString(buildSetupBranch, "-") + // Creates or get the build var data struct { CreateBuildOrGet struct { @@ -84,10 +95,10 @@ func buildSetup(cmd *cobra.Command) error { } } `, map[string]interface{}{ - "project": buildSetupProject, - "branch": normalizedBranchName, - "build": buildSetupBuild, - "description": buildSetupDescription, + "project": project, + "branch": branch, + "build": build, + "description": description, "runInfo": runInfo, }, &data); err != nil { return err @@ -113,10 +124,10 @@ func init() { // Cobra supports local flags which will only run when this command // is called directly, e.g.: - buildSetupCmd.Flags().StringVarP(&buildSetupProject, "project", "p", "", "Project name (required)") - buildSetupCmd.Flags().StringVarP(&buildSetupBranch, "branch", "b", "", "Branch name (required)") - buildSetupCmd.Flags().StringVarP(&buildSetupBuild, "build", "n", "", "Build name (required)") - buildSetupCmd.Flags().StringVarP(&buildSetupDescription, "description", "d", "", "Build description") + buildSetupCmd.Flags().StringP("project", "p", "", "Project name (required)") + buildSetupCmd.Flags().StringP("branch", "b", "", "Branch name (required)") + buildSetupCmd.Flags().StringP("build", "n", "", "Build name (required)") + buildSetupCmd.Flags().StringP("description", "d", "", "Build description") // Run info parameters InitRunInfoCommandFlags(buildSetupCmd) diff --git a/cmd/promotionLevelSetup.go b/cmd/promotionLevelSetup.go index 836dea9..dc6d3ba 100644 --- a/cmd/promotionLevelSetup.go +++ b/cmd/promotionLevelSetup.go @@ -53,6 +53,7 @@ The promotion can be set to be in "auto promotion" mode by using addtional optio if err != nil { return err } + branch = NormalizeBranchName(branch) promotion, err := cmd.Flags().GetString("promotion") if err != nil { return err diff --git a/cmd/validate.go b/cmd/validate.go index 60026d1..eb7218a 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -68,6 +68,7 @@ Type 'ontrack-cli validate --help' to get a list of all options. if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/validateCHML.go b/cmd/validateCHML.go index ca5d094..9d21f26 100644 --- a/cmd/validateCHML.go +++ b/cmd/validateCHML.go @@ -48,6 +48,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/validateMetrics.go b/cmd/validateMetrics.go index 103d7d6..3a248b5 100644 --- a/cmd/validateMetrics.go +++ b/cmd/validateMetrics.go @@ -57,6 +57,7 @@ An alternative syntax is: if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/validatePercentage.go b/cmd/validatePercentage.go index 6addda2..2bf6eb3 100644 --- a/cmd/validatePercentage.go +++ b/cmd/validatePercentage.go @@ -49,6 +49,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/validateTests.go b/cmd/validateTests.go index 5ae664d..6cf02f6 100644 --- a/cmd/validateTests.go +++ b/cmd/validateTests.go @@ -48,6 +48,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) build, err := cmd.Flags().GetString("build") if err != nil { diff --git a/cmd/validationStampSetupCHML.go b/cmd/validationStampSetupCHML.go index 03fc3fb..d02f33f 100644 --- a/cmd/validationStampSetupCHML.go +++ b/cmd/validationStampSetupCHML.go @@ -55,6 +55,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) validation, err := cmd.Flags().GetString("validation") if err != nil { diff --git a/cmd/validationStampSetupGeneric.go b/cmd/validationStampSetupGeneric.go index 78f02b2..da0e002 100644 --- a/cmd/validationStampSetupGeneric.go +++ b/cmd/validationStampSetupGeneric.go @@ -59,6 +59,7 @@ Note that specific commands per type are also available, see 'ontrack-cli vs set if err != nil { return err } + branch = NormalizeBranchName(branch) validation, err := cmd.Flags().GetString("validation") if err != nil { diff --git a/cmd/validationStampSetupMetrics.go b/cmd/validationStampSetupMetrics.go index 83682eb..88115ab 100644 --- a/cmd/validationStampSetupMetrics.go +++ b/cmd/validationStampSetupMetrics.go @@ -48,6 +48,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) validation, err := cmd.Flags().GetString("validation") if err != nil { diff --git a/cmd/validationStampSetupPercentage.go b/cmd/validationStampSetupPercentage.go index f0f0429..73b1950 100644 --- a/cmd/validationStampSetupPercentage.go +++ b/cmd/validationStampSetupPercentage.go @@ -50,6 +50,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) validation, err := cmd.Flags().GetString("validation") if err != nil { diff --git a/cmd/validationStampSetupTests.go b/cmd/validationStampSetupTests.go index 6f0b5d5..9f0da28 100644 --- a/cmd/validationStampSetupTests.go +++ b/cmd/validationStampSetupTests.go @@ -49,6 +49,7 @@ For example: if err != nil { return err } + branch = NormalizeBranchName(branch) validation, err := cmd.Flags().GetString("validation") if err != nil {