Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid sending "0 new commits" webhooks #12212

Merged
merged 7 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
}
}

pushEvent, ok := p.(*api.PushPayload)
// Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.).
// Integration webhooks (e.g. drone) still receive the required data.
if ok && w.HookTaskType != models.GITEA && w.HookTaskType != models.GOGS {
if len(pushEvent.Commits) == 0 {
return nil
}
}
S7evinK marked this conversation as resolved.
Show resolved Hide resolved

// If payload has no associated branch (e.g. it's a new tag, issue, etc.),
// branch filter has no effect.
if branch := getPayloadBranch(p); branch != "" {
Expand Down
4 changes: 2 additions & 2 deletions modules/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestPrepareWebhooks(t *testing.T) {
for _, hookTask := range hookTasks {
models.AssertNotExistsBean(t, hookTask)
}
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{}))
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Commits: []*api.PayloadCommit{{}}}))
for _, hookTask := range hookTasks {
models.AssertExistsAndLoadBean(t, hookTask)
}
Expand All @@ -51,7 +51,7 @@ func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
models.AssertNotExistsBean(t, hookTask)
}
// this test also ensures that * doesn't handle / in any special way (like shell would)
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791"}))
assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791", Commits: []*api.PayloadCommit{{}}}))
for _, hookTask := range hookTasks {
models.AssertExistsAndLoadBean(t, hookTask)
}
Expand Down