Refactor webhook events (#33337)

Extract from #33320

This PR uses a map instead of a struct to store webhook event
information. It removes many duplicated functions and makes the logic
clearer.
pull/33074/merge
Lunny Xiao 1 week ago committed by GitHub
parent 594b8350b1
commit e94f37f95e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 201
      models/webhook/webhook.go
  2. 12
      models/webhook/webhook_test.go
  3. 20
      modules/webhook/events.go
  4. 39
      modules/webhook/structs.go
  5. 53
      modules/webhook/type.go
  6. 81
      routers/api/v1/utils/hook.go
  7. 42
      routers/web/repo/setting/webhook.go
  8. 11
      services/forms/repo_form.go
  9. 10
      services/webhook/webhook.go
  10. 42
      templates/repo/settings/webhook/settings.tmpl

@ -167,186 +167,39 @@ func (w *Webhook) UpdateEvent() error {
return err return err
} }
// HasCreateEvent returns true if hook enabled create event. func (w *Webhook) HasEvent(evt webhook_module.HookEventType) bool {
func (w *Webhook) HasCreateEvent() bool { if w.SendEverything {
return w.SendEverything || return true
(w.ChooseEvents && w.HookEvents.Create)
}
// HasDeleteEvent returns true if hook enabled delete event.
func (w *Webhook) HasDeleteEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Delete)
}
// HasForkEvent returns true if hook enabled fork event.
func (w *Webhook) HasForkEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Fork)
}
// HasIssuesEvent returns true if hook enabled issues event.
func (w *Webhook) HasIssuesEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Issues)
}
// HasIssuesAssignEvent returns true if hook enabled issues assign event.
func (w *Webhook) HasIssuesAssignEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.IssueAssign)
}
// HasIssuesLabelEvent returns true if hook enabled issues label event.
func (w *Webhook) HasIssuesLabelEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.IssueLabel)
}
// HasIssuesMilestoneEvent returns true if hook enabled issues milestone event.
func (w *Webhook) HasIssuesMilestoneEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.IssueMilestone)
}
// HasIssueCommentEvent returns true if hook enabled issue_comment event.
func (w *Webhook) HasIssueCommentEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.IssueComment)
}
// HasPushEvent returns true if hook enabled push event.
func (w *Webhook) HasPushEvent() bool {
return w.PushOnly || w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Push)
}
// HasPullRequestEvent returns true if hook enabled pull request event.
func (w *Webhook) HasPullRequestEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequest)
}
// HasPullRequestAssignEvent returns true if hook enabled pull request assign event.
func (w *Webhook) HasPullRequestAssignEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestAssign)
}
// HasPullRequestLabelEvent returns true if hook enabled pull request label event.
func (w *Webhook) HasPullRequestLabelEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestLabel)
}
// HasPullRequestMilestoneEvent returns true if hook enabled pull request milestone event.
func (w *Webhook) HasPullRequestMilestoneEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestMilestone)
}
// HasPullRequestCommentEvent returns true if hook enabled pull_request_comment event.
func (w *Webhook) HasPullRequestCommentEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestComment)
}
// HasPullRequestApprovedEvent returns true if hook enabled pull request review event.
func (w *Webhook) HasPullRequestApprovedEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestReview)
}
// HasPullRequestRejectedEvent returns true if hook enabled pull request review event.
func (w *Webhook) HasPullRequestRejectedEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestReview)
}
// HasPullRequestReviewCommentEvent returns true if hook enabled pull request review event.
func (w *Webhook) HasPullRequestReviewCommentEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestReview)
}
// HasPullRequestSyncEvent returns true if hook enabled pull request sync event.
func (w *Webhook) HasPullRequestSyncEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestSync)
}
// HasWikiEvent returns true if hook enabled wiki event.
func (w *Webhook) HasWikiEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvent.Wiki)
}
// HasReleaseEvent returns if hook enabled release event.
func (w *Webhook) HasReleaseEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Release)
}
// HasRepositoryEvent returns if hook enabled repository event.
func (w *Webhook) HasRepositoryEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Repository)
}
// HasPackageEvent returns if hook enabled package event.
func (w *Webhook) HasPackageEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.Package)
}
// HasPullRequestReviewRequestEvent returns true if hook enabled pull request review request event.
func (w *Webhook) HasPullRequestReviewRequestEvent() bool {
return w.SendEverything ||
(w.ChooseEvents && w.HookEvents.PullRequestReviewRequest)
}
// EventCheckers returns event checkers
func (w *Webhook) EventCheckers() []struct {
Has func() bool
Type webhook_module.HookEventType
} {
return []struct {
Has func() bool
Type webhook_module.HookEventType
}{
{w.HasCreateEvent, webhook_module.HookEventCreate},
{w.HasDeleteEvent, webhook_module.HookEventDelete},
{w.HasForkEvent, webhook_module.HookEventFork},
{w.HasPushEvent, webhook_module.HookEventPush},
{w.HasIssuesEvent, webhook_module.HookEventIssues},
{w.HasIssuesAssignEvent, webhook_module.HookEventIssueAssign},
{w.HasIssuesLabelEvent, webhook_module.HookEventIssueLabel},
{w.HasIssuesMilestoneEvent, webhook_module.HookEventIssueMilestone},
{w.HasIssueCommentEvent, webhook_module.HookEventIssueComment},
{w.HasPullRequestEvent, webhook_module.HookEventPullRequest},
{w.HasPullRequestAssignEvent, webhook_module.HookEventPullRequestAssign},
{w.HasPullRequestLabelEvent, webhook_module.HookEventPullRequestLabel},
{w.HasPullRequestMilestoneEvent, webhook_module.HookEventPullRequestMilestone},
{w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestComment},
{w.HasPullRequestApprovedEvent, webhook_module.HookEventPullRequestReviewApproved},
{w.HasPullRequestRejectedEvent, webhook_module.HookEventPullRequestReviewRejected},
{w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestReviewComment},
{w.HasPullRequestSyncEvent, webhook_module.HookEventPullRequestSync},
{w.HasWikiEvent, webhook_module.HookEventWiki},
{w.HasRepositoryEvent, webhook_module.HookEventRepository},
{w.HasReleaseEvent, webhook_module.HookEventRelease},
{w.HasPackageEvent, webhook_module.HookEventPackage},
{w.HasPullRequestReviewRequestEvent, webhook_module.HookEventPullRequestReviewRequest},
} }
if w.PushOnly {
return evt == webhook_module.HookEventPush
}
checkEvt := evt
switch evt {
case webhook_module.HookEventPullRequestReviewApproved, webhook_module.HookEventPullRequestReviewRejected, webhook_module.HookEventPullRequestReviewComment:
checkEvt = webhook_module.HookEventPullRequestReview
}
return w.HookEvents[checkEvt]
} }
// EventsArray returns an array of hook events // EventsArray returns an array of hook events
func (w *Webhook) EventsArray() []string { func (w *Webhook) EventsArray() []string {
events := make([]string, 0, 7) if w.SendEverything {
events := make([]string, 0, len(webhook_module.AllEvents()))
for _, evt := range webhook_module.AllEvents() {
events = append(events, string(evt))
}
return events
}
if w.PushOnly {
return []string{string(webhook_module.HookEventPush)}
}
for _, c := range w.EventCheckers() { events := make([]string, 0, len(w.HookEvents))
if c.Has() { for event, enabled := range w.HookEvents {
events = append(events, string(c.Type)) if enabled {
events = append(events, string(event))
} }
} }
return events return events

@ -54,9 +54,9 @@ func TestWebhook_UpdateEvent(t *testing.T) {
SendEverything: false, SendEverything: false,
ChooseEvents: false, ChooseEvents: false,
HookEvents: webhook_module.HookEvents{ HookEvents: webhook_module.HookEvents{
Create: false, webhook_module.HookEventCreate: false,
Push: true, webhook_module.HookEventPush: true,
PullRequest: false, webhook_module.HookEventPullRequest: false,
}, },
} }
webhook.HookEvent = hookEvent webhook.HookEvent = hookEvent
@ -68,13 +68,13 @@ func TestWebhook_UpdateEvent(t *testing.T) {
} }
func TestWebhook_EventsArray(t *testing.T) { func TestWebhook_EventsArray(t *testing.T) {
assert.Equal(t, []string{ assert.EqualValues(t, []string{
"create", "delete", "fork", "push", "create", "delete", "fork", "push",
"issues", "issue_assign", "issue_label", "issue_milestone", "issue_comment", "issues", "issue_assign", "issue_label", "issue_milestone", "issue_comment",
"pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone", "pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone",
"pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected", "pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected",
"pull_request_review_comment", "pull_request_sync", "wiki", "repository", "release", "pull_request_review_comment", "pull_request_sync", "pull_request_review_request", "wiki", "repository", "release",
"package", "pull_request_review_request", "package", "status",
}, },
(&Webhook{ (&Webhook{
HookEvent: &webhook_module.HookEvent{SendEverything: true}, HookEvent: &webhook_module.HookEvent{SendEverything: true},

@ -0,0 +1,20 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package webhook
type HookEvents map[HookEventType]bool
func (he HookEvents) Get(evt HookEventType) bool {
return he[evt]
}
// HookEvent represents events that will delivery hook.
type HookEvent struct {
PushOnly bool `json:"push_only"`
SendEverything bool `json:"send_everything"`
ChooseEvents bool `json:"choose_events"`
BranchFilter string `json:"branch_filter"`
HookEvents `json:"events"`
}

@ -1,39 +0,0 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package webhook
// HookEvents is a set of web hook events
type HookEvents struct {
Create bool `json:"create"`
Delete bool `json:"delete"`
Fork bool `json:"fork"`
Issues bool `json:"issues"`
IssueAssign bool `json:"issue_assign"`
IssueLabel bool `json:"issue_label"`
IssueMilestone bool `json:"issue_milestone"`
IssueComment bool `json:"issue_comment"`
Push bool `json:"push"`
PullRequest bool `json:"pull_request"`
PullRequestAssign bool `json:"pull_request_assign"`
PullRequestLabel bool `json:"pull_request_label"`
PullRequestMilestone bool `json:"pull_request_milestone"`
PullRequestComment bool `json:"pull_request_comment"`
PullRequestReview bool `json:"pull_request_review"`
PullRequestSync bool `json:"pull_request_sync"`
PullRequestReviewRequest bool `json:"pull_request_review_request"`
Wiki bool `json:"wiki"`
Repository bool `json:"repository"`
Release bool `json:"release"`
Package bool `json:"package"`
}
// HookEvent represents events that will delivery hook.
type HookEvent struct {
PushOnly bool `json:"push_only"`
SendEverything bool `json:"send_everything"`
ChooseEvents bool `json:"choose_events"`
BranchFilter string `json:"branch_filter"`
HookEvents `json:"events"`
}

@ -31,21 +31,47 @@ const (
HookEventRepository HookEventType = "repository" HookEventRepository HookEventType = "repository"
HookEventRelease HookEventType = "release" HookEventRelease HookEventType = "release"
HookEventPackage HookEventType = "package" HookEventPackage HookEventType = "package"
HookEventSchedule HookEventType = "schedule"
HookEventStatus HookEventType = "status" HookEventStatus HookEventType = "status"
// once a new event added here, please also added to AllEvents() function
// FIXME: This event should be a group of pull_request_review_xxx events
HookEventPullRequestReview HookEventType = "pull_request_review"
// Actions event only
HookEventSchedule HookEventType = "schedule"
) )
func AllEvents() []HookEventType {
return []HookEventType{
HookEventCreate,
HookEventDelete,
HookEventFork,
HookEventPush,
HookEventIssues,
HookEventIssueAssign,
HookEventIssueLabel,
HookEventIssueMilestone,
HookEventIssueComment,
HookEventPullRequest,
HookEventPullRequestAssign,
HookEventPullRequestLabel,
HookEventPullRequestMilestone,
HookEventPullRequestComment,
HookEventPullRequestReviewApproved,
HookEventPullRequestReviewRejected,
HookEventPullRequestReviewComment,
HookEventPullRequestSync,
HookEventPullRequestReviewRequest,
HookEventWiki,
HookEventRepository,
HookEventRelease,
HookEventPackage,
HookEventStatus,
}
}
// Event returns the HookEventType as an event string // Event returns the HookEventType as an event string
func (h HookEventType) Event() string { func (h HookEventType) Event() string {
switch h { switch h {
case HookEventCreate:
return "create"
case HookEventDelete:
return "delete"
case HookEventFork:
return "fork"
case HookEventPush:
return "push"
case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone: case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone:
return "issues" return "issues"
case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone, case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone,
@ -59,14 +85,9 @@ func (h HookEventType) Event() string {
return "pull_request_rejected" return "pull_request_rejected"
case HookEventPullRequestReviewComment: case HookEventPullRequestReviewComment:
return "pull_request_comment" return "pull_request_comment"
case HookEventWiki: default:
return "wiki" return string(h)
case HookEventRepository:
return "repository"
case HookEventRelease:
return "release"
} }
return ""
} }
func (h HookEventType) IsPullRequest() bool { func (h HookEventType) IsPullRequest() bool {

@ -185,26 +185,27 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
HookEvent: &webhook_module.HookEvent{ HookEvent: &webhook_module.HookEvent{
ChooseEvents: true, ChooseEvents: true,
HookEvents: webhook_module.HookEvents{ HookEvents: webhook_module.HookEvents{
Create: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true), webhook_module.HookEventCreate: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true),
Delete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true), webhook_module.HookEventDelete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true),
Fork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true), webhook_module.HookEventFork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true),
Issues: issuesHook(form.Events, "issues_only"), webhook_module.HookEventIssues: issuesHook(form.Events, "issues_only"),
IssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)), webhook_module.HookEventIssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)),
IssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)), webhook_module.HookEventIssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)),
IssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)), webhook_module.HookEventIssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)),
IssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)), webhook_module.HookEventIssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)),
Push: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true), webhook_module.HookEventPush: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true),
PullRequest: pullHook(form.Events, "pull_request_only"), webhook_module.HookEventPullRequest: pullHook(form.Events, "pull_request_only"),
PullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)), webhook_module.HookEventPullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)),
PullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)), webhook_module.HookEventPullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)),
PullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)), webhook_module.HookEventPullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)),
PullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)), webhook_module.HookEventPullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)),
PullRequestReview: pullHook(form.Events, "pull_request_review"), webhook_module.HookEventPullRequestReview: pullHook(form.Events, "pull_request_review"),
PullRequestReviewRequest: pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)), webhook_module.HookEventPullRequestReviewRequest: pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)),
PullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)), webhook_module.HookEventPullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)),
Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true), webhook_module.HookEventWiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true),
Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true), webhook_module.HookEventRepository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true),
Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true), webhook_module.HookEventRelease: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true),
webhook_module.HookEventStatus: util.SliceContainsString(form.Events, string(webhook_module.HookEventStatus), true),
}, },
BranchFilter: form.BranchFilter, BranchFilter: form.BranchFilter,
}, },
@ -356,14 +357,13 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
w.PushOnly = false w.PushOnly = false
w.SendEverything = false w.SendEverything = false
w.ChooseEvents = true w.ChooseEvents = true
w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) w.HookEvents[webhook_module.HookEventCreate] = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true)
w.Push = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true) w.HookEvents[webhook_module.HookEventPush] = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true)
w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true) w.HookEvents[webhook_module.HookEventDelete] = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true)
w.Delete = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true) w.HookEvents[webhook_module.HookEventFork] = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true)
w.Fork = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true) w.HookEvents[webhook_module.HookEventRepository] = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true)
w.Repository = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true) w.HookEvents[webhook_module.HookEventWiki] = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true)
w.Wiki = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true) w.HookEvents[webhook_module.HookEventRelease] = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true)
w.Release = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true)
w.BranchFilter = form.BranchFilter w.BranchFilter = form.BranchFilter
err := w.SetHeaderAuthorization(form.AuthorizationHeader) err := w.SetHeaderAuthorization(form.AuthorizationHeader)
@ -373,21 +373,20 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
} }
// Issues // Issues
w.Issues = issuesHook(form.Events, "issues_only") w.HookEvents[webhook_module.HookEventIssues] = issuesHook(form.Events, "issues_only")
w.IssueAssign = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)) w.HookEvents[webhook_module.HookEventIssueAssign] = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign))
w.IssueLabel = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)) w.HookEvents[webhook_module.HookEventIssueLabel] = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel))
w.IssueMilestone = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)) w.HookEvents[webhook_module.HookEventIssueMilestone] = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone))
w.IssueComment = issuesHook(form.Events, string(webhook_module.HookEventIssueComment)) w.HookEvents[webhook_module.HookEventIssueComment] = issuesHook(form.Events, string(webhook_module.HookEventIssueComment))
// Pull requests // Pull requests
w.PullRequest = pullHook(form.Events, "pull_request_only") w.HookEvents[webhook_module.HookEventPullRequest] = pullHook(form.Events, "pull_request_only")
w.PullRequestAssign = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)) w.HookEvents[webhook_module.HookEventPullRequestAssign] = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign))
w.PullRequestLabel = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)) w.HookEvents[webhook_module.HookEventPullRequestLabel] = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel))
w.PullRequestMilestone = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)) w.HookEvents[webhook_module.HookEventPullRequestMilestone] = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone))
w.PullRequestComment = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)) w.HookEvents[webhook_module.HookEventPullRequestReview] = pullHook(form.Events, "pull_request_review")
w.PullRequestReview = pullHook(form.Events, "pull_request_review") w.HookEvents[webhook_module.HookEventPullRequestReviewRequest] = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest))
w.PullRequestReviewRequest = pullHook(form.Events, string(webhook_module.HookEventPullRequestReviewRequest)) w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))
w.PullRequestSync = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))
if err := w.UpdateEvent(); err != nil { if err := w.UpdateEvent(); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err) ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)

@ -163,27 +163,27 @@ func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent {
SendEverything: form.SendEverything(), SendEverything: form.SendEverything(),
ChooseEvents: form.ChooseEvents(), ChooseEvents: form.ChooseEvents(),
HookEvents: webhook_module.HookEvents{ HookEvents: webhook_module.HookEvents{
Create: form.Create, webhook_module.HookEventCreate: form.Create,
Delete: form.Delete, webhook_module.HookEventDelete: form.Delete,
Fork: form.Fork, webhook_module.HookEventFork: form.Fork,
Issues: form.Issues, webhook_module.HookEventIssues: form.Issues,
IssueAssign: form.IssueAssign, webhook_module.HookEventIssueAssign: form.IssueAssign,
IssueLabel: form.IssueLabel, webhook_module.HookEventIssueLabel: form.IssueLabel,
IssueMilestone: form.IssueMilestone, webhook_module.HookEventIssueMilestone: form.IssueMilestone,
IssueComment: form.IssueComment, webhook_module.HookEventIssueComment: form.IssueComment,
Release: form.Release, webhook_module.HookEventRelease: form.Release,
Push: form.Push, webhook_module.HookEventPush: form.Push,
PullRequest: form.PullRequest, webhook_module.HookEventPullRequest: form.PullRequest,
PullRequestAssign: form.PullRequestAssign, webhook_module.HookEventPullRequestAssign: form.PullRequestAssign,
PullRequestLabel: form.PullRequestLabel, webhook_module.HookEventPullRequestLabel: form.PullRequestLabel,
PullRequestMilestone: form.PullRequestMilestone, webhook_module.HookEventPullRequestMilestone: form.PullRequestMilestone,
PullRequestComment: form.PullRequestComment, webhook_module.HookEventPullRequestComment: form.PullRequestComment,
PullRequestReview: form.PullRequestReview, webhook_module.HookEventPullRequestReview: form.PullRequestReview,
PullRequestSync: form.PullRequestSync, webhook_module.HookEventPullRequestSync: form.PullRequestSync,
PullRequestReviewRequest: form.PullRequestReviewRequest, webhook_module.HookEventPullRequestReviewRequest: form.PullRequestReviewRequest,
Wiki: form.Wiki, webhook_module.HookEventWiki: form.Wiki,
Repository: form.Repository, webhook_module.HookEventRepository: form.Repository,
Package: form.Package, webhook_module.HookEventPackage: form.Package,
}, },
BranchFilter: form.BranchFilter, BranchFilter: form.BranchFilter,
} }

@ -219,26 +219,18 @@ type ProtectBranchPriorityForm struct {
IDs []int64 IDs []int64
} }
// __ __ ___. .__ __
// / \ / \ ____\_ |__ | |__ ____ ____ | | __
// \ \/\/ // __ \| __ \| | \ / _ \ / _ \| |/ /
// \ /\ ___/| \_\ \ Y ( <_> | <_> ) <
// \__/\ / \___ >___ /___| /\____/ \____/|__|_ \
// \/ \/ \/ \/ \/
// WebhookForm form for changing web hook // WebhookForm form for changing web hook
type WebhookForm struct { type WebhookForm struct {
Events string Events string
Create bool Create bool
Delete bool Delete bool
Fork bool Fork bool
Push bool
Issues bool Issues bool
IssueAssign bool IssueAssign bool
IssueLabel bool IssueLabel bool
IssueMilestone bool IssueMilestone bool
IssueComment bool IssueComment bool
Release bool
Push bool
PullRequest bool PullRequest bool
PullRequestAssign bool PullRequestAssign bool
PullRequestLabel bool PullRequestLabel bool
@ -249,6 +241,7 @@ type WebhookForm struct {
PullRequestReviewRequest bool PullRequestReviewRequest bool
Wiki bool Wiki bool
Repository bool Repository bool
Release bool
Package bool Package bool
Active bool Active bool
BranchFilter string `binding:"GlobPattern"` BranchFilter string `binding:"GlobPattern"`

@ -137,14 +137,8 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook
return nil return nil
} }
for _, e := range w.EventCheckers() { if !w.HasEvent(event) {
if event == e.Type { return nil
if !e.Has() {
return nil
}
break
}
} }
// Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.). // Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.).

@ -31,7 +31,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="create" type="checkbox" {{if .Webhook.Create}}checked{{end}}> <input name="create" type="checkbox" {{if .Webhook.HookEvents.Get "create"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_create"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_create"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_create_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_create_desc"}}</span>
</div> </div>
@ -41,7 +41,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="delete" type="checkbox" {{if .Webhook.Delete}}checked{{end}}> <input name="delete" type="checkbox" {{if .Webhook.HookEvents.Get "delete"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_delete"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_delete"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_delete_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_delete_desc"}}</span>
</div> </div>
@ -51,7 +51,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="fork" type="checkbox" {{if .Webhook.Fork}}checked{{end}}> <input name="fork" type="checkbox" {{if .Webhook.HookEvents.Get "fork"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_fork"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_fork"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_fork_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_fork_desc"}}</span>
</div> </div>
@ -61,7 +61,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="push" type="checkbox" {{if .Webhook.Push}}checked{{end}}> <input name="push" type="checkbox" {{if .Webhook.HookEvents.Get "push"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_push"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_push"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_push_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_push_desc"}}</span>
</div> </div>
@ -71,7 +71,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="repository" type="checkbox" {{if .Webhook.Repository}}checked{{end}}> <input name="repository" type="checkbox" {{if .Webhook.HookEvents.Get "repository"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_repository"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_repository"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_repository_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_repository_desc"}}</span>
</div> </div>
@ -81,7 +81,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="release" type="checkbox" {{if .Webhook.Release}}checked{{end}}> <input name="release" type="checkbox" {{if .Webhook.HookEvents.Get "release"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_release"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_release"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_release_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_release_desc"}}</span>
</div> </div>
@ -91,7 +91,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="package" type="checkbox" {{if .Webhook.Package}}checked{{end}}> <input name="package" type="checkbox" {{if .Webhook.HookEvents.Get "package"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_package"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_package"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_package_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_package_desc"}}</span>
</div> </div>
@ -102,7 +102,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="wiki" type="checkbox" {{if .Webhook.Wiki}}checked{{end}}> <input name="wiki" type="checkbox" {{if .Webhook.HookEvents.Get "wiki"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_wiki"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_wiki"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_wiki_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_wiki_desc"}}</span>
</div> </div>
@ -117,7 +117,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="issues" type="checkbox" {{if .Webhook.Issues}}checked{{end}}> <input name="issues" type="checkbox" {{if .Webhook.HookEvents.Get "issues"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_issues"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_issues"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_issues_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_issues_desc"}}</span>
</div> </div>
@ -127,7 +127,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="issue_assign" type="checkbox" {{if .Webhook.IssueAssign}}checked{{end}}> <input name="issue_assign" type="checkbox" {{if .Webhook.HookEvents.Get "issue_assign"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_issue_assign"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_issue_assign"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_assign_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_assign_desc"}}</span>
</div> </div>
@ -137,7 +137,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="issue_label" type="checkbox" {{if .Webhook.IssueLabel}}checked{{end}}> <input name="issue_label" type="checkbox" {{if .Webhook.HookEvents.Get "issue_label"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_issue_label"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_issue_label"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_label_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_label_desc"}}</span>
</div> </div>
@ -147,7 +147,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="issue_milestone" type="checkbox" {{if .Webhook.IssueMilestone}}checked{{end}}> <input name="issue_milestone" type="checkbox" {{if .Webhook.HookEvents.Get "issue_milestone"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_issue_milestone"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_issue_milestone"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_milestone_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_milestone_desc"}}</span>
</div> </div>
@ -157,7 +157,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="issue_comment" type="checkbox" {{if .Webhook.IssueComment}}checked{{end}}> <input name="issue_comment" type="checkbox" {{if .Webhook.HookEvents.Get "issue_comment"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_issue_comment"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_issue_comment"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_comment_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_issue_comment_desc"}}</span>
</div> </div>
@ -172,7 +172,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request" type="checkbox" {{if .Webhook.PullRequest}}checked{{end}}> <input name="pull_request" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_desc"}}</span>
</div> </div>
@ -182,7 +182,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_assign" type="checkbox" {{if .Webhook.PullRequestAssign}}checked{{end}}> <input name="pull_request_assign" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_assign"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_assign"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_assign"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_assign_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_assign_desc"}}</span>
</div> </div>
@ -192,7 +192,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_label" type="checkbox" {{if .Webhook.PullRequestLabel}}checked{{end}}> <input name="pull_request_label" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_label"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_label"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_label"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_label_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_label_desc"}}</span>
</div> </div>
@ -202,7 +202,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_milestone" type="checkbox" {{if .Webhook.PullRequestMilestone}}checked{{end}}> <input name="pull_request_milestone" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_milestone"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_milestone"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_milestone"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_milestone_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_milestone_desc"}}</span>
</div> </div>
@ -212,7 +212,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_comment" type="checkbox" {{if .Webhook.PullRequestComment}}checked{{end}}> <input name="pull_request_comment" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_comment"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_comment"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_comment"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_comment_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_comment_desc"}}</span>
</div> </div>
@ -222,7 +222,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_review" type="checkbox" {{if .Webhook.PullRequestReview}}checked{{end}}> <input name="pull_request_review" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_review"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_review"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_review"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_review_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_review_desc"}}</span>
</div> </div>
@ -232,7 +232,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_sync" type="checkbox" {{if .Webhook.PullRequestSync}}checked{{end}}> <input name="pull_request_sync" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_sync"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_sync"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_sync"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_sync_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_sync_desc"}}</span>
</div> </div>
@ -242,7 +242,7 @@
<div class="seven wide column"> <div class="seven wide column">
<div class="field"> <div class="field">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_request_review_request" type="checkbox" {{if .Webhook.PullRequestReviewRequest}}checked{{end}}> <input name="pull_request_review_request" type="checkbox" {{if .Webhook.HookEvents.Get "pull_request_review_request"}}checked{{end}}>
<label>{{ctx.Locale.Tr "repo.settings.event_pull_request_review_request"}}</label> <label>{{ctx.Locale.Tr "repo.settings.event_pull_request_review_request"}}</label>
<span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_review_request_desc"}}</span> <span class="help">{{ctx.Locale.Tr "repo.settings.event_pull_request_review_request_desc"}}</span>
</div> </div>

Loading…
Cancel
Save