diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2025-01-23 10:53:06 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-23 18:53:06 +0000 |
commit | e94f37f95e286ba3b982700744e7f55fdb31f046 (patch) | |
tree | 72b0d58c6710e302333f089a6615a846518645d3 /modules/webhook | |
parent | 594b8350b174e5832c1f91c55028ac76246bbef7 (diff) | |
download | gitea-e94f37f95e286ba3b982700744e7f55fdb31f046.tar.gz gitea-e94f37f95e286ba3b982700744e7f55fdb31f046.zip |
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.
Diffstat (limited to 'modules/webhook')
-rw-r--r-- | modules/webhook/events.go | 20 | ||||
-rw-r--r-- | modules/webhook/structs.go | 39 | ||||
-rw-r--r-- | modules/webhook/type.go | 53 |
3 files changed, 57 insertions, 55 deletions
diff --git a/modules/webhook/events.go b/modules/webhook/events.go new file mode 100644 index 0000000000..f4dfff0294 --- /dev/null +++ b/modules/webhook/events.go @@ -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"` +} diff --git a/modules/webhook/structs.go b/modules/webhook/structs.go deleted file mode 100644 index 927a91a74c..0000000000 --- a/modules/webhook/structs.go +++ /dev/null @@ -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"` -} diff --git a/modules/webhook/type.go b/modules/webhook/type.go index aa4de45eb4..b244bb0cff 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -31,21 +31,47 @@ const ( HookEventRepository HookEventType = "repository" HookEventRelease HookEventType = "release" HookEventPackage HookEventType = "package" - HookEventSchedule HookEventType = "schedule" 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 func (h HookEventType) Event() string { switch h { - case HookEventCreate: - return "create" - case HookEventDelete: - return "delete" - case HookEventFork: - return "fork" - case HookEventPush: - return "push" case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone: return "issues" case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone, @@ -59,14 +85,9 @@ func (h HookEventType) Event() string { return "pull_request_rejected" case HookEventPullRequestReviewComment: return "pull_request_comment" - case HookEventWiki: - return "wiki" - case HookEventRepository: - return "repository" - case HookEventRelease: - return "release" + default: + return string(h) } - return "" } func (h HookEventType) IsPullRequest() bool { |