Skip to content

Commit 178fd7a

Browse files
committed
fix(bitbucketdatacenter): ignore branch deletion events in ParsePayload
When a branch is deleted in Bitbucket Data Center, the repo:refs_changed webhook sends a change with toHash set to all-zeros. Previously, Pipelines as Code treated this like a push event, which caused errors when trying to validate a missing commit. This patch detects and ignores such events early in ParsePayload. Fixes #2035
1 parent 46067b2 commit 178fd7a

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

pkg/provider/bitbucketdatacenter/parse_payload.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketdatacenter/types"
1616
)
1717

18-
// checkValidPayload checks if the payload is valid.
19-
func checkValidPayload(e *types.PullRequestEvent) error {
18+
func checkValidPullRequestPayload(e *types.PullRequestEvent) error {
2019
if e.PullRequest.ToRef.Repository.Project == nil {
2120
return fmt.Errorf("bitbucket toRef project is nil")
2221
}
@@ -132,7 +131,7 @@ func (v *Provider) ParsePayload(_ context.Context, run *params.Run, request *htt
132131
processedEvent.TriggerComment = e.Comment.Text
133132
}
134133

135-
if err := checkValidPayload(e); err != nil {
134+
if err := checkValidPullRequestPayload(e); err != nil {
136135
return nil, err
137136
}
138137

@@ -164,6 +163,13 @@ func (v *Provider) ParsePayload(_ context.Context, run *params.Run, request *htt
164163
return nil, fmt.Errorf("push event contains no commits under 'changes'; cannot proceed")
165164
}
166165

166+
// Check for branch deletion - if any change is a DELETE type with zero hash, skip processing
167+
for _, change := range e.Changes {
168+
if provider.IsZeroSHA(change.ToHash) && change.Type == "DELETE" {
169+
return nil, nil
170+
}
171+
}
172+
167173
if len(e.Commits) == 0 {
168174
return nil, fmt.Errorf("push event contains no commits; cannot proceed")
169175
}

pkg/provider/bitbucketdatacenter/parse_payload_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ func TestCheckValidPayload(t *testing.T) {
545545
}
546546
for _, tt := range tests {
547547
t.Run(tt.name, func(t *testing.T) {
548-
err := checkValidPayload(&tt.payloadEvent)
548+
err := checkValidPullRequestPayload(&tt.payloadEvent)
549549
if tt.wantErrString != "" {
550550
assert.ErrorContains(t, err, tt.wantErrString)
551551
return
@@ -709,6 +709,19 @@ func TestParsePayload(t *testing.T) {
709709
expEvent: ev1,
710710
wantSHA: "abcd",
711711
},
712+
{
713+
name: "branch/deleted with zero hash",
714+
eventType: "repo:refs_changed",
715+
payloadEvent: bbv1test.MakePushEvent(ev1, []types.PushRequestEventChange{
716+
{
717+
ToHash: "0000000000000000000000000000000000000000",
718+
RefID: "refs/heads/feature-branch",
719+
Type: "DELETE",
720+
},
721+
}, []types.Commit{},
722+
),
723+
expEvent: nil,
724+
},
712725
}
713726
for _, tt := range tests {
714727
t.Run(tt.name, func(t *testing.T) {
@@ -735,10 +748,15 @@ func TestParsePayload(t *testing.T) {
735748
return
736749
}
737750
assert.NilError(t, err)
738-
751+
// Handle case where expEvent is nil (e.g., branch deletion)
752+
if tt.expEvent == nil {
753+
if got != nil {
754+
t.Fatalf("expected event to be nil, got: %+v", got)
755+
}
756+
return
757+
}
739758
// assert SHA ID
740759
assert.Equal(t, tt.wantSHA, got.SHA)
741-
742760
assert.Equal(t, got.AccountID, tt.expEvent.AccountID)
743761

744762
// test that we got slashed

0 commit comments

Comments
 (0)