Skip to content

Commit 684efda

Browse files
riddler7Conrad Dobbschmouel
authored
Fix tag ref for Bitbucket Cloud (#1782)
Fix tag ref when using Bitbucket Cloud as the provider. Also reverted changes to "old" change, as this would be a change in behavior Co-authored-by: Conrad Dobbs <[email protected]> Co-authored-by: Chmouel Boudjnah <[email protected]>
1 parent e4a3b86 commit 684efda

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

pkg/provider/bitbucketcloud/parse_payload.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,14 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
155155
processedEvent.Repository = e.Repository.Name
156156
processedEvent.SHA = e.Push.Changes[0].New.Target.Hash
157157
processedEvent.URL = e.Repository.Links.HTML.HRef
158-
processedEvent.BaseBranch = e.Push.Changes[0].New.Name
159158
processedEvent.HeadBranch = e.Push.Changes[0].Old.Name
160159
processedEvent.BaseURL = e.Push.Changes[0].New.Target.Links.HTML.HRef
161160
processedEvent.HeadURL = e.Push.Changes[0].Old.Target.Links.HTML.HRef
161+
if e.Push.Changes[0].New.Type == "tag" {
162+
processedEvent.BaseBranch = fmt.Sprintf("refs/tags/%s", e.Push.Changes[0].New.Name)
163+
} else {
164+
processedEvent.BaseBranch = e.Push.Changes[0].New.Name
165+
}
162166
processedEvent.AccountID = e.Actor.AccountID
163167
processedEvent.Sender = e.Actor.Nickname
164168
default:

pkg/provider/bitbucketcloud/parse_payload_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestParsePayload(t *testing.T) {
2525
expectedEventType string
2626
expectedAccountID string
2727
expectedSHA string
28+
expectedRef string
2829
eventType string
2930
sourceIP string
3031
allowedConfig map[string]map[string]string
@@ -34,10 +35,21 @@ func TestParsePayload(t *testing.T) {
3435
}{
3536
{
3637
name: "parse push request",
37-
payloadEvent: bbcloudtest.MakePushEvent("PushAccountID", "Barbie", "slighlyhashed"),
38+
payloadEvent: bbcloudtest.MakePushEvent("PushAccountID", "Barbie", "slighlyhashed", "branch"),
3839
expectedSender: "Barbie",
3940
expectedAccountID: "PushAccountID",
4041
expectedSHA: "slighlyhashed",
42+
expectedRef: "mychange",
43+
eventType: "repo:push",
44+
expectedEventType: triggertype.Push.String(),
45+
},
46+
{
47+
name: "parse push tag",
48+
payloadEvent: bbcloudtest.MakePushEvent("PushAccountID", "Barbie", "slighlyhashed", "tag"),
49+
expectedSender: "Barbie",
50+
expectedAccountID: "PushAccountID",
51+
expectedSHA: "slighlyhashed",
52+
expectedRef: "refs/tags/mychange",
4153
eventType: "repo:push",
4254
expectedEventType: triggertype.Push.String(),
4355
},
@@ -253,6 +265,10 @@ func TestParsePayload(t *testing.T) {
253265
assert.Equal(t, tt.expectedSender, got.Sender)
254266
assert.Equal(t, tt.expectedSHA, got.SHA, "%s != %s", tt.expectedSHA, got.SHA)
255267
assert.Equal(t, tt.expectedEventType, got.EventType, "%s != %s", tt.expectedEventType, got.EventType)
268+
269+
if tt.expectedRef != "" {
270+
assert.Equal(t, tt.expectedRef, got.BaseBranch, tt.expectedRef, got.BaseBranch)
271+
}
256272
if tt.targetPipelinerun != "" {
257273
assert.Equal(t, tt.targetPipelinerun, got.TargetTestPipelineRun, tt.targetPipelinerun, got.TargetTestPipelineRun)
258274
}

pkg/provider/bitbucketcloud/test/bbcloudtest.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func MakePREvent(accountid, nickname, sha, comment string) types.PullRequestEven
280280
return pr
281281
}
282282

283-
func MakePushEvent(accountid, nickname, sha string) types.PushRequestEvent {
283+
func MakePushEvent(accountid, nickname, sha, changeType string) types.PushRequestEvent {
284284
if accountid == "" {
285285
accountid = "countlady"
286286
}
@@ -299,9 +299,11 @@ func MakePushEvent(accountid, nickname, sha string) types.PushRequestEvent {
299299
Changes: []types.Change{
300300
{
301301
New: types.ChangeType{
302+
Name: "mychange",
302303
Target: types.Commit{
303304
Hash: sha,
304305
},
306+
Type: changeType,
305307
},
306308
Old: types.ChangeType{
307309
Target: types.Commit{

pkg/provider/bitbucketcloud/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type PushRequestEvent struct {
7575
type ChangeType struct {
7676
Name string
7777
Target Commit
78+
Type string
7879
}
7980

8081
type Change struct {

0 commit comments

Comments
 (0)