Skip to content

Commit 617843c

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 5e98945 commit 617843c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pkg/provider/bitbucketdatacenter/parse_payload.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ func (v *Provider) ParsePayload(_ context.Context, run *params.Run, request *htt
164164
return nil, fmt.Errorf("push event contains no commits under 'changes'; cannot proceed")
165165
}
166166

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

pkg/provider/bitbucketdatacenter/parse_payload_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,20 @@ 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: ev1,
724+
wantErrSubstr: "branch delete event is not supported; cannot proceed",
725+
},
712726
}
713727
for _, tt := range tests {
714728
t.Run(tt.name, func(t *testing.T) {
@@ -735,10 +749,15 @@ func TestParsePayload(t *testing.T) {
735749
return
736750
}
737751
assert.NilError(t, err)
738-
752+
// Handle case where expEvent is nil (e.g., branch deletion)
753+
if tt.expEvent == nil {
754+
if got != nil {
755+
t.Fatalf("expected event to be nil, got: %+v", got)
756+
}
757+
return
758+
}
739759
// assert SHA ID
740760
assert.Equal(t, tt.wantSHA, got.SHA)
741-
742761
assert.Equal(t, got.AccountID, tt.expEvent.AccountID)
743762

744763
// test that we got slashed

0 commit comments

Comments
 (0)