From cc20c66949fedd5615e44f960fc4bd6bc23c3921 Mon Sep 17 00:00:00 2001 From: infernus01 Date: Thu, 12 Jun 2025 22:49:43 +0530 Subject: [PATCH] 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 --- .../bitbucketdatacenter/parse_payload.go | 7 ++++++ .../bitbucketdatacenter/parse_payload_test.go | 23 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/provider/bitbucketdatacenter/parse_payload.go b/pkg/provider/bitbucketdatacenter/parse_payload.go index bd7ede332..3afe788bb 100644 --- a/pkg/provider/bitbucketdatacenter/parse_payload.go +++ b/pkg/provider/bitbucketdatacenter/parse_payload.go @@ -164,6 +164,13 @@ func (v *Provider) ParsePayload(_ context.Context, run *params.Run, request *htt return nil, fmt.Errorf("push event contains no commits under 'changes'; cannot proceed") } + // Check for branch deletion - if any change is a DELETE type with zero hash, skip processing + for _, change := range e.Changes { + if provider.IsZeroSHA(change.ToHash) && change.Type == "DELETE" { + return nil, fmt.Errorf("branch delete event is not supported; cannot proceed") + } + } + if len(e.Commits) == 0 { return nil, fmt.Errorf("push event contains no commits; cannot proceed") } diff --git a/pkg/provider/bitbucketdatacenter/parse_payload_test.go b/pkg/provider/bitbucketdatacenter/parse_payload_test.go index 09c9b6ca4..cb7fa1575 100644 --- a/pkg/provider/bitbucketdatacenter/parse_payload_test.go +++ b/pkg/provider/bitbucketdatacenter/parse_payload_test.go @@ -709,6 +709,20 @@ func TestParsePayload(t *testing.T) { expEvent: ev1, wantSHA: "abcd", }, + { + name: "branch/deleted with zero hash", + eventType: "repo:refs_changed", + payloadEvent: bbv1test.MakePushEvent(ev1, []types.PushRequestEventChange{ + { + ToHash: "0000000000000000000000000000000000000000", + RefID: "refs/heads/feature-branch", + Type: "DELETE", + }, + }, []types.Commit{}, + ), + expEvent: ev1, + wantErrSubstr: "branch delete event is not supported; cannot proceed", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -735,10 +749,15 @@ func TestParsePayload(t *testing.T) { return } assert.NilError(t, err) - + // Handle case where expEvent is nil (e.g., branch deletion) + if tt.expEvent == nil { + if got != nil { + t.Fatalf("expected event to be nil, got: %+v", got) + } + return + } // assert SHA ID assert.Equal(t, tt.wantSHA, got.SHA) - assert.Equal(t, got.AccountID, tt.expEvent.AccountID) // test that we got slashed