Skip to content

Commit

Permalink
feat(bb): when repo branch is not configured, both the main and maste…
Browse files Browse the repository at this point in the history
…r branch will be checked (#186)

When the `repoConfig` section is not populated, Dinghy will by default treat both the `master` and `main` branch as the source branch.
  • Loading branch information
kkotula committed Feb 13, 2024
1 parent 2840222 commit d0dc889
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
10 changes: 1 addition & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.1.0 h1:Y2lUDsFKVRSYGojLJ1yLxSXdMmMYTYls0rCvoqmMUQk=
github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/sprig/v3 v3.1.0 h1:j7GpgZ7PdFqNsmncycTHsLmVPf5/3wJtlgW9TNDYD9Y=
github.com/Masterminds/sprig/v3 v3.1.0/go.mod h1:ONGMf7UfYGAbMXCZmQLy8x3lCDIPrEZE/rU8pmrbihA=
github.com/Masterminds/sprig/v3 v3.2.0 h1:P1ekkbuU73Ui/wS0nK1HOM37hh4xdfZo485UPf8rc+Y=
github.com/Masterminds/sprig/v3 v3.2.0/go.mod h1:tWhwTbUTndesPNeF0C900vKoq283u6zp4APT9vaF3SI=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand All @@ -37,8 +33,6 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armory/go-yaml-tools v0.0.0-20180620164822-5d0947924d8e/go.mod h1:rHIvkswnJs3bWHmIDC8l+JHHpChXI/jsJBjl2JNck2w=
github.com/armory/go-yaml-tools v0.0.2 h1:IYAbeGQJQKFwBAdl4u9IVrENgHjjUsOxZRSTvypgYk4=
github.com/armory/go-yaml-tools v0.0.2/go.mod h1:LasFFVo6zuV334Pmx7Diq9L3muOUUI8MF79QUFC1jq0=
github.com/armory/plank/v4 v4.1.1 h1:z/+PCCtra5EW0tfOCeE5K5ewRTIOAsYMxP8zqhdXcS8=
github.com/armory/plank/v4 v4.1.1/go.mod h1:xH8nrFDGCXEL+zZpLMxrdTq395DHvtrHgJmEdg9Ygo4=
github.com/armory/plank/v4 v4.1.2 h1:FwDazjvSE5S6BUZ/Pmr6wnis8/OixhDz2LTnGiOp/g0=
github.com/armory/plank/v4 v4.1.2/go.mod h1:xH8nrFDGCXEL+zZpLMxrdTq395DHvtrHgJmEdg9Ygo4=
github.com/aws/aws-sdk-go v1.28.9 h1:grIuBQc+p3dTRXerh5+2OxSuWFi0iXuxbFdTSg0jaW0=
Expand Down Expand Up @@ -164,8 +158,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huandu/xstrings v1.3.1 h1:4jgBlKK6tLKFvO8u5pmYjG91cqytmDCDvGh7ECVFfFs=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/jinzhu/copier v0.0.0-20180308034124-7e38e58719c3 h1:sHsPfNMAG70QAvKbddQ0uScZCHQoZsT5NykGRCeeeIs=
Expand Down Expand Up @@ -468,8 +460,8 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/bbcloud/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *WebhookChange) Branch() string {

// Find in the webhook payload if the change was done in "master" branch
func (c *WebhookChange) IsMaster() bool {
return c.New.Name == "master"
return c.New.Name == "master" || c.New.Name == "refs/heads/master" || c.New.Name == "main" || c.New.Name == "refs/heads/main"
}

func getFilesChanged(fromCommitHash, toCommitHash string, page int, cfg Config,
Expand Down
47 changes: 47 additions & 0 deletions pkg/git/bbcloud/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,50 @@ func TestIsBranch(t *testing.T) {
})
}
}

func TestIsMaster(t *testing.T) {

testCases := map[string]struct {
payload string
expected bool
}{
"master": {
payload: fmt.Sprintf(webhookPayloadOneChange, "master", "master"),
expected: true,
},
"refs/heads/master": {
payload: fmt.Sprintf(webhookPayloadOneChange, "refs/heads/master", "refs/heads/master"),
expected: true,
},
"main": {
payload: fmt.Sprintf(webhookPayloadOneChange, "main", "main"),
expected: true,
},
"refs/heads/main": {
payload: fmt.Sprintf(webhookPayloadOneChange, "refs/heads/main", "refs/heads/main"),
expected: true,
},
"somethingelse": {
payload: fmt.Sprintf(webhookPayloadOneChange, "somethingelse", "somethingelse"),
expected: false,
},
"refs/heads/somethingelse": {
payload: fmt.Sprintf(webhookPayloadOneChange, "refs/heads/somethingelse", "refs/heads/somethingelse"),
expected: false,
},
}

for desc, tc := range testCases {
t.Run(desc, func(t *testing.T) {
webhookPayload := WebhookPayload{}
if err := json.NewDecoder(bytes.NewBufferString(tc.payload)).Decode(&webhookPayload); err != nil {
t.Fatalf(err.Error())
}

p := &Push{Payload: webhookPayload}

actual := p.IsMaster()
assert.Equal(t, tc.expected, actual)
})
}
}
2 changes: 1 addition & 1 deletion pkg/git/stash/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type WebhookChange struct {

// IsMaster detects if a change was on master
func (c *WebhookChange) IsMaster() bool {
return c.RefID == "refs/heads/master"
return c.RefID == "master" || c.RefID == "refs/heads/master" || c.RefID == "main" || c.RefID == "refs/heads/main"
}

// Find the branch in the webhook payload
Expand Down
48 changes: 48 additions & 0 deletions pkg/git/stash/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,51 @@ func TestIsBranch(t *testing.T) {
})
}
}

func TestIsMaster(t *testing.T) {

testCases := map[string]struct {
webhookBranchName string
expected bool
}{
"master": {
webhookBranchName: "master",
expected: true,
},
"refs/heads/master": {
webhookBranchName: "refs/heads/master",
expected: true,
},
"main": {
webhookBranchName: "main",
expected: true,
},
"refs/heads/main": {
webhookBranchName: "refs/heads/main",
expected: true,
},
"somethingelse": {
webhookBranchName: "somethingelse",
expected: false,
},
"refs/heads/somethingelse": {
webhookBranchName: "refs/heads/somethingelse",
expected: false,
},
}

for desc, tc := range testCases {
t.Run(desc, func(t *testing.T) {
payload := fmt.Sprintf(`{"changes": [{"refId": "%s"}]}`, tc.webhookBranchName)
webhookPayload := WebhookPayload{}
if err := json.NewDecoder(bytes.NewBufferString(payload)).Decode(&webhookPayload); err != nil {
t.Fatalf(err.Error())
}

p := &Push{Payload: webhookPayload}

actual := p.IsMaster()
assert.Equal(t, tc.expected, actual)
})
}
}

0 comments on commit d0dc889

Please sign in to comment.