Skip to content

Commit 2ae004a

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 16234d8 commit 2ae004a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

pkg/provider/github/parse_payload.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ 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+
if gitEvent.After != nil {
264+
if isZeroSHA(*gitEvent.After) {
265+
return nil, fmt.Errorf("branch %s has been deleted, exiting", gitEvent.GetRef())
266+
}
267+
}
268+
262269
processedEvent.Organization = gitEvent.GetRepo().GetOwner().GetLogin()
263270
processedEvent.Repository = gitEvent.GetRepo().GetName()
264271
processedEvent.DefaultBranch = gitEvent.GetRepo().GetDefaultBranch()
@@ -326,6 +333,10 @@ func (v *Provider) processEvent(ctx context.Context, event *info.Event, eventInt
326333
return processedEvent, nil
327334
}
328335

336+
func isZeroSHA(sha string) bool {
337+
return sha == "0000000000000000000000000000000000000000"
338+
}
339+
329340
func (v *Provider) handleReRequestEvent(ctx context.Context, event *github.CheckRunEvent) (*info.Event, error) {
330341
runevent := info.NewEvent()
331342
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)