Skip to content

Commit 7af2ccd

Browse files
S7evinKlafrikszeripathtechknowlogick
authored
Avoid sending "0 new commits" webhooks (#12212)
* Avoid sending "0 new commits" webhook Signed-off-by: Till Faelligen <[email protected]> * Revert "Avoid sending "0 new commits" webhook" This reverts commit 1f47ccf. * Move commit count check to more central place * Make tests pass * Update modules/webhook/webhook.go Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 514201a commit 7af2ccd

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

modules/webhook/webhook.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
7676
}
7777
}
7878

79+
// Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.).
80+
// Integration webhooks (e.g. drone) still receive the required data.
81+
if pushEvent, ok := p.(*api.PushPayload); ok &&
82+
w.HookTaskType != models.GITEA && w.HookTaskType != models.GOGS &&
83+
len(pushEvent.Commits) == 0 {
84+
return nil
85+
}
86+
7987
// If payload has no associated branch (e.g. it's a new tag, issue, etc.),
8088
// branch filter has no effect.
8189
if branch := getPayloadBranch(p); branch != "" {

modules/webhook/webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestPrepareWebhooks(t *testing.T) {
3434
for _, hookTask := range hookTasks {
3535
models.AssertNotExistsBean(t, hookTask)
3636
}
37-
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{}))
37+
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
3838
for _, hookTask := range hookTasks {
3939
models.AssertExistsAndLoadBean(t, hookTask)
4040
}
@@ -51,7 +51,7 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
5151
models.AssertNotExistsBean(t, hookTask)
5252
}
5353
// this test also ensures that * doesn't handle / in any special way (like shell would)
54-
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791"}))
54+
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}}))
5555
for _, hookTask := range hookTasks {
5656
models.AssertExistsAndLoadBean(t, hookTask)
5757
}

0 commit comments

Comments
 (0)