Skip to content

Commit ac3c6f3

Browse files
committed
Refactor webhook events
1 parent 6cc1067 commit ac3c6f3

File tree

10 files changed

+123
-330
lines changed

10 files changed

+123
-330
lines changed

models/webhook/webhook.go

Lines changed: 10 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -167,186 +167,23 @@ func (w *Webhook) UpdateEvent() error {
167167
return err
168168
}
169169

170-
// HasCreateEvent returns true if hook enabled create event.
171-
func (w *Webhook) HasCreateEvent() bool {
172-
return w.SendEverything ||
173-
(w.ChooseEvents && w.HookEvents.Create)
174-
}
175-
176-
// HasDeleteEvent returns true if hook enabled delete event.
177-
func (w *Webhook) HasDeleteEvent() bool {
178-
return w.SendEverything ||
179-
(w.ChooseEvents && w.HookEvents.Delete)
180-
}
181-
182-
// HasForkEvent returns true if hook enabled fork event.
183-
func (w *Webhook) HasForkEvent() bool {
184-
return w.SendEverything ||
185-
(w.ChooseEvents && w.HookEvents.Fork)
186-
}
187-
188-
// HasIssuesEvent returns true if hook enabled issues event.
189-
func (w *Webhook) HasIssuesEvent() bool {
190-
return w.SendEverything ||
191-
(w.ChooseEvents && w.HookEvents.Issues)
192-
}
193-
194-
// HasIssuesAssignEvent returns true if hook enabled issues assign event.
195-
func (w *Webhook) HasIssuesAssignEvent() bool {
196-
return w.SendEverything ||
197-
(w.ChooseEvents && w.HookEvents.IssueAssign)
198-
}
199-
200-
// HasIssuesLabelEvent returns true if hook enabled issues label event.
201-
func (w *Webhook) HasIssuesLabelEvent() bool {
202-
return w.SendEverything ||
203-
(w.ChooseEvents && w.HookEvents.IssueLabel)
204-
}
205-
206-
// HasIssuesMilestoneEvent returns true if hook enabled issues milestone event.
207-
func (w *Webhook) HasIssuesMilestoneEvent() bool {
208-
return w.SendEverything ||
209-
(w.ChooseEvents && w.HookEvents.IssueMilestone)
210-
}
211-
212-
// HasIssueCommentEvent returns true if hook enabled issue_comment event.
213-
func (w *Webhook) HasIssueCommentEvent() bool {
214-
return w.SendEverything ||
215-
(w.ChooseEvents && w.HookEvents.IssueComment)
216-
}
217-
218-
// HasPushEvent returns true if hook enabled push event.
219-
func (w *Webhook) HasPushEvent() bool {
220-
return w.PushOnly || w.SendEverything ||
221-
(w.ChooseEvents && w.HookEvents.Push)
222-
}
223-
224-
// HasPullRequestEvent returns true if hook enabled pull request event.
225-
func (w *Webhook) HasPullRequestEvent() bool {
226-
return w.SendEverything ||
227-
(w.ChooseEvents && w.HookEvents.PullRequest)
228-
}
229-
230-
// HasPullRequestAssignEvent returns true if hook enabled pull request assign event.
231-
func (w *Webhook) HasPullRequestAssignEvent() bool {
232-
return w.SendEverything ||
233-
(w.ChooseEvents && w.HookEvents.PullRequestAssign)
234-
}
235-
236-
// HasPullRequestLabelEvent returns true if hook enabled pull request label event.
237-
func (w *Webhook) HasPullRequestLabelEvent() bool {
238-
return w.SendEverything ||
239-
(w.ChooseEvents && w.HookEvents.PullRequestLabel)
240-
}
241-
242-
// HasPullRequestMilestoneEvent returns true if hook enabled pull request milestone event.
243-
func (w *Webhook) HasPullRequestMilestoneEvent() bool {
244-
return w.SendEverything ||
245-
(w.ChooseEvents && w.HookEvents.PullRequestMilestone)
246-
}
247-
248-
// HasPullRequestCommentEvent returns true if hook enabled pull_request_comment event.
249-
func (w *Webhook) HasPullRequestCommentEvent() bool {
250-
return w.SendEverything ||
251-
(w.ChooseEvents && w.HookEvents.PullRequestComment)
252-
}
253-
254-
// HasPullRequestApprovedEvent returns true if hook enabled pull request review event.
255-
func (w *Webhook) HasPullRequestApprovedEvent() bool {
256-
return w.SendEverything ||
257-
(w.ChooseEvents && w.HookEvents.PullRequestReview)
258-
}
259-
260-
// HasPullRequestRejectedEvent returns true if hook enabled pull request review event.
261-
func (w *Webhook) HasPullRequestRejectedEvent() bool {
262-
return w.SendEverything ||
263-
(w.ChooseEvents && w.HookEvents.PullRequestReview)
264-
}
265-
266-
// HasPullRequestReviewCommentEvent returns true if hook enabled pull request review event.
267-
func (w *Webhook) HasPullRequestReviewCommentEvent() bool {
268-
return w.SendEverything ||
269-
(w.ChooseEvents && w.HookEvents.PullRequestReview)
270-
}
271-
272-
// HasPullRequestSyncEvent returns true if hook enabled pull request sync event.
273-
func (w *Webhook) HasPullRequestSyncEvent() bool {
274-
return w.SendEverything ||
275-
(w.ChooseEvents && w.HookEvents.PullRequestSync)
276-
}
277-
278-
// HasWikiEvent returns true if hook enabled wiki event.
279-
func (w *Webhook) HasWikiEvent() bool {
280-
return w.SendEverything ||
281-
(w.ChooseEvents && w.HookEvent.Wiki)
282-
}
283-
284-
// HasReleaseEvent returns if hook enabled release event.
285-
func (w *Webhook) HasReleaseEvent() bool {
286-
return w.SendEverything ||
287-
(w.ChooseEvents && w.HookEvents.Release)
288-
}
289-
290-
// HasRepositoryEvent returns if hook enabled repository event.
291-
func (w *Webhook) HasRepositoryEvent() bool {
292-
return w.SendEverything ||
293-
(w.ChooseEvents && w.HookEvents.Repository)
294-
}
295-
296-
// HasPackageEvent returns if hook enabled package event.
297-
func (w *Webhook) HasPackageEvent() bool {
298-
return w.SendEverything ||
299-
(w.ChooseEvents && w.HookEvents.Package)
300-
}
301-
302-
// HasPullRequestReviewRequestEvent returns true if hook enabled pull request review request event.
303-
func (w *Webhook) HasPullRequestReviewRequestEvent() bool {
304-
return w.SendEverything ||
305-
(w.ChooseEvents && w.HookEvents.PullRequestReviewRequest)
306-
}
307-
308-
// EventCheckers returns event checkers
309-
func (w *Webhook) EventCheckers() []struct {
310-
Has func() bool
311-
Type webhook_module.HookEventType
312-
} {
313-
return []struct {
314-
Has func() bool
315-
Type webhook_module.HookEventType
316-
}{
317-
{w.HasCreateEvent, webhook_module.HookEventCreate},
318-
{w.HasDeleteEvent, webhook_module.HookEventDelete},
319-
{w.HasForkEvent, webhook_module.HookEventFork},
320-
{w.HasPushEvent, webhook_module.HookEventPush},
321-
{w.HasIssuesEvent, webhook_module.HookEventIssues},
322-
{w.HasIssuesAssignEvent, webhook_module.HookEventIssueAssign},
323-
{w.HasIssuesLabelEvent, webhook_module.HookEventIssueLabel},
324-
{w.HasIssuesMilestoneEvent, webhook_module.HookEventIssueMilestone},
325-
{w.HasIssueCommentEvent, webhook_module.HookEventIssueComment},
326-
{w.HasPullRequestEvent, webhook_module.HookEventPullRequest},
327-
{w.HasPullRequestAssignEvent, webhook_module.HookEventPullRequestAssign},
328-
{w.HasPullRequestLabelEvent, webhook_module.HookEventPullRequestLabel},
329-
{w.HasPullRequestMilestoneEvent, webhook_module.HookEventPullRequestMilestone},
330-
{w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestComment},
331-
{w.HasPullRequestApprovedEvent, webhook_module.HookEventPullRequestReviewApproved},
332-
{w.HasPullRequestRejectedEvent, webhook_module.HookEventPullRequestReviewRejected},
333-
{w.HasPullRequestCommentEvent, webhook_module.HookEventPullRequestReviewComment},
334-
{w.HasPullRequestSyncEvent, webhook_module.HookEventPullRequestSync},
335-
{w.HasWikiEvent, webhook_module.HookEventWiki},
336-
{w.HasRepositoryEvent, webhook_module.HookEventRepository},
337-
{w.HasReleaseEvent, webhook_module.HookEventRelease},
338-
{w.HasPackageEvent, webhook_module.HookEventPackage},
339-
{w.HasPullRequestReviewRequestEvent, webhook_module.HookEventPullRequestReviewRequest},
170+
func (w *Webhook) HasEvent(evt webhook_module.HookEventType) bool {
171+
if w.SendEverything {
172+
return true
173+
}
174+
if w.PushOnly {
175+
return evt == webhook_module.HookEventPush
340176
}
177+
return w.HookEvents[evt]
341178
}
342179

343180
// EventsArray returns an array of hook events
344181
func (w *Webhook) EventsArray() []string {
345182
events := make([]string, 0, 7)
346183

347-
for _, c := range w.EventCheckers() {
348-
if c.Has() {
349-
events = append(events, string(c.Type))
184+
for event, enabled := range w.HookEvents {
185+
if enabled {
186+
events = append(events, string(event))
350187
}
351188
}
352189
return events

models/webhook/webhook_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func TestWebhook_UpdateEvent(t *testing.T) {
5454
SendEverything: false,
5555
ChooseEvents: false,
5656
HookEvents: webhook_module.HookEvents{
57-
Create: false,
58-
Push: true,
59-
PullRequest: false,
57+
webhook_module.HookEventCreate: false,
58+
webhook_module.HookEventPush: true,
59+
webhook_module.HookEventPullRequest: false,
6060
},
6161
}
6262
webhook.HookEvent = hookEvent

modules/webhook/events.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package webhook
5+
6+
type HookEvents map[HookEventType]bool
7+
8+
func (he HookEvents) Get(evt HookEventType) bool {
9+
return he[evt]
10+
}
11+
12+
// HookEvent represents events that will delivery hook.
13+
type HookEvent struct {
14+
PushOnly bool `json:"push_only"`
15+
SendEverything bool `json:"send_everything"`
16+
ChooseEvents bool `json:"choose_events"`
17+
BranchFilter string `json:"branch_filter"`
18+
19+
HookEvents `json:"events"`
20+
}

modules/webhook/structs.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

modules/webhook/type.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
HookEventPullRequestLabel HookEventType = "pull_request_label"
2323
HookEventPullRequestMilestone HookEventType = "pull_request_milestone"
2424
HookEventPullRequestComment HookEventType = "pull_request_comment"
25+
HookEventPullRequestReview HookEventType = "pull_request_review"
2526
HookEventPullRequestReviewApproved HookEventType = "pull_request_review_approved"
2627
HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected"
2728
HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment"
@@ -38,14 +39,6 @@ const (
3839
// Event returns the HookEventType as an event string
3940
func (h HookEventType) Event() string {
4041
switch h {
41-
case HookEventCreate:
42-
return "create"
43-
case HookEventDelete:
44-
return "delete"
45-
case HookEventFork:
46-
return "fork"
47-
case HookEventPush:
48-
return "push"
4942
case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone:
5043
return "issues"
5144
case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone,
@@ -59,14 +52,9 @@ func (h HookEventType) Event() string {
5952
return "pull_request_rejected"
6053
case HookEventPullRequestReviewComment:
6154
return "pull_request_comment"
62-
case HookEventWiki:
63-
return "wiki"
64-
case HookEventRepository:
65-
return "repository"
66-
case HookEventRelease:
67-
return "release"
55+
default:
56+
return string(h)
6857
}
69-
return ""
7058
}
7159

7260
func (h HookEventType) IsPullRequest() bool {

0 commit comments

Comments
 (0)