From 7f03ef69d8f118749a3679c1cabfdad01d63cb1c Mon Sep 17 00:00:00 2001 From: Krzysztof Kotula Date: Tue, 13 Feb 2024 10:12:21 +0100 Subject: [PATCH] feat(bb): when repo branch is not configured, both the main and master branch will be checked --- go.sum | 10 +------- pkg/git/bbcloud/push.go | 2 +- pkg/git/bbcloud/push_test.go | 47 +++++++++++++++++++++++++++++++++++ pkg/git/stash/push.go | 2 +- pkg/git/stash/push_test.go | 48 ++++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 11 deletions(-) diff --git a/go.sum b/go.sum index a95fa989..e3e71822 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/pkg/git/bbcloud/push.go b/pkg/git/bbcloud/push.go index f071ec9c..ad5ae4ec 100644 --- a/pkg/git/bbcloud/push.go +++ b/pkg/git/bbcloud/push.go @@ -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, diff --git a/pkg/git/bbcloud/push_test.go b/pkg/git/bbcloud/push_test.go index f89d6f63..8a28a198 100644 --- a/pkg/git/bbcloud/push_test.go +++ b/pkg/git/bbcloud/push_test.go @@ -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) + }) + } +} diff --git a/pkg/git/stash/push.go b/pkg/git/stash/push.go index 002d1556..795116c6 100644 --- a/pkg/git/stash/push.go +++ b/pkg/git/stash/push.go @@ -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 diff --git a/pkg/git/stash/push_test.go b/pkg/git/stash/push_test.go index 6f366b69..51c4ec74 100644 --- a/pkg/git/stash/push_test.go +++ b/pkg/git/stash/push_test.go @@ -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) + }) + } +}