Skip to content

Commit cfdfa82

Browse files
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 6974ba9 commit cfdfa82

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/provider/github/parse_payload.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ 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, a push event is triggered,
264+
// hence returning an error from here to avoid further processing
265+
if gitEvent.After != nil {
266+
if isZeroSHA(*gitEvent.After) {
267+
return nil, fmt.Errorf("branch %s has been deleted, exiting", gitEvent.GetRef())
268+
}
269+
}
270+
262271
processedEvent.Organization = gitEvent.GetRepo().GetOwner().GetLogin()
263272
processedEvent.Repository = gitEvent.GetRepo().GetName()
264273
processedEvent.DefaultBranch = gitEvent.GetRepo().GetDefaultBranch()
@@ -326,6 +335,10 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
326335
return processedEvent, nil
327336
}
328337

338+
func isZeroSHA(sha string) bool {
339+
return sha == "0000000000000000000000000000000000000000"
340+
}
341+
329342
func (v *Provider) handleReRequestEvent(ctx context.Context, event *github.CheckRunEvent) (*info.Event, error) {
330343
runevent := info.NewEvent()
331344
if event.GetRepo() == nil {

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",

0 commit comments

Comments
 (0)