Skip to content

Commit

Permalink
Always normalizing the branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Mar 21, 2021
1 parent a9c3f3c commit 5886ec8
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 69 deletions.
1 change: 1 addition & 0 deletions cmd/branchSetPropertyGeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions cmd/branchSetPropertyGit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
96 changes: 47 additions & 49 deletions cmd/branchSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand All @@ -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() {
Expand All @@ -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")
}
10 changes: 10 additions & 0 deletions cmd/branches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmd

import (
"regexp"
)

func NormalizeBranchName(name string) string {
re := regexp.MustCompile("[^A-Za-z0-9\\._-]")
return re.ReplaceAllString(name, "-")
}
1 change: 1 addition & 0 deletions cmd/buildSetPropertyGeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

build, err := cmd.Flags().GetString("build")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/buildSetPropertyGitCommit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

build, err := cmd.Flags().GetString("build")
if err != nil {
Expand Down
47 changes: 29 additions & 18 deletions cmd/buildSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/promotionLevelSetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/validateCHML.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

build, err := cmd.Flags().GetString("build")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/validateMetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/validatePercentage.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

build, err := cmd.Flags().GetString("build")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/validateTests.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

build, err := cmd.Flags().GetString("build")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/validationStampSetupCHML.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

validation, err := cmd.Flags().GetString("validation")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/validationStampSetupGeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/validationStampSetupMetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

validation, err := cmd.Flags().GetString("validation")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/validationStampSetupPercentage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

validation, err := cmd.Flags().GetString("validation")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/validationStampSetupTests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ For example:
if err != nil {
return err
}
branch = NormalizeBranchName(branch)

validation, err := cmd.Flags().GetString("validation")
if err != nil {
Expand Down

0 comments on commit 5886ec8

Please sign in to comment.