Skip to content

Commit 396cd62

Browse files
PuneetPunamiyachmouel
authored andcommitted
Handles branch delete event on github
When a branch is deleted via repository UI, a push event is triggered and the payload will have a zero hash value Hence, this patch handles the deletion of branch deletion and returns early from ParsePayload function without further processing which in turn also avoids unnecessary log noise Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
1 parent 47ba586 commit 396cd62

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

pkg/provider/github/parse_payload.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
259259
if gitEvent.GetRepo() == nil {
260260
return nil, errors.New("error parsing payload the repository should not be nil")
261261
}
262+
263+
// When a branch is deleted via repository UI, it triggers a push event.
264+
// However, Pipelines as Code does not support handling branch delete events,
265+
// so we return an error here to indicate this unsupported operation.
266+
if gitEvent.After != nil {
267+
if provider.IsZeroSHA(*gitEvent.After) {
268+
return nil, fmt.Errorf("branch %s has been deleted, exiting", gitEvent.GetRef())
269+
}
270+
}
271+
262272
processedEvent.Organization = gitEvent.GetRepo().GetOwner().GetLogin()
263273
processedEvent.Repository = gitEvent.GetRepo().GetName()
264274
processedEvent.DefaultBranch = gitEvent.GetRepo().GetDefaultBranch()

pkg/provider/github/parse_payload_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ func TestParsePayLoad(t *testing.T) {
204204
payloadEventStruct: github.PushEvent{},
205205
wantErrString: "error parsing payload the repository should not be nil",
206206
},
207+
{
208+
name: "branch/deleted",
209+
eventType: "push",
210+
triggerTarget: triggertype.Push.String(),
211+
payloadEventStruct: github.PushEvent{
212+
Repo: &github.PushEventRepository{
213+
Owner: &github.User{Login: github.Ptr("foo")},
214+
Name: github.Ptr("pushRepo"),
215+
},
216+
Ref: github.Ptr("test"),
217+
After: github.Ptr("0000000000000000000000000000000000000000"),
218+
},
219+
wantErrString: "branch test has been deleted, exiting",
220+
},
207221
{
208222
// specific run from a check_suite
209223
name: "good/rerequest check_run on pull request",

pkg/provider/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,7 @@ func GetCheckName(status StatusOpts, pacopts *info.PacOpts) string {
186186
}
187187
return status.OriginalPipelineRunName
188188
}
189+
190+
func IsZeroSHA(sha string) bool {
191+
return sha == "0000000000000000000000000000000000000000"
192+
}

0 commit comments

Comments
 (0)