1// Copyright 2023 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package actions
5
6import (
7 webhook_module "forgejo.org/modules/webhook"
8)
9
10const (
11 GithubEventPullRequest = "pull_request"
12 GithubEventPullRequestTarget = "pull_request_target"
13 GithubEventPullRequestReviewComment = "pull_request_review_comment"
14 GithubEventPullRequestReview = "pull_request_review"
15 GithubEventRegistryPackage = "registry_package"
16 GithubEventCreate = "create"
17 GithubEventDelete = "delete"
18 GithubEventFork = "fork"
19 GithubEventPush = "push"
20 GithubEventIssues = "issues"
21 GithubEventIssueComment = "issue_comment"
22 GithubEventRelease = "release"
23 GithubEventPullRequestComment = "pull_request_comment"
24 GithubEventGollum = "gollum"
25 GithubEventSchedule = "schedule"
26 GithubEventWorkflowDispatch = "workflow_dispatch"
27)
28
29// IsDefaultBranchWorkflow returns true if the event only triggers workflows on the default branch
30func IsDefaultBranchWorkflow(triggedEvent webhook_module.HookEventType) bool {
31 switch triggedEvent {
32 case webhook_module.HookEventDelete:
33 // GitHub "delete" event
34 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#delete
35 return true
36 case webhook_module.HookEventFork:
37 // GitHub "fork" event
38 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#fork
39 return true
40 case webhook_module.HookEventIssueComment:
41 // GitHub "issue_comment" event
42 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment
43 return true
44 case webhook_module.HookEventPullRequestComment:
45 // GitHub "pull_request_comment" event
46 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
47 return true
48 case webhook_module.HookEventWiki:
49 // GitHub "gollum" event
50 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
51 return true
52 case webhook_module.HookEventSchedule:
53 // GitHub "schedule" event
54 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
55 return true
56 case webhook_module.HookEventWorkflowDispatch:
57 // GitHub "workflow_dispatch" event
58 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch
59 return true
60 case webhook_module.HookEventIssues,
61 webhook_module.HookEventIssueAssign,
62 webhook_module.HookEventIssueLabel,
63 webhook_module.HookEventIssueMilestone:
64 // Github "issues" event
65 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues
66 return true
67 }
68
69 return false
70}
71
72// canGithubEventMatch check if the input Github event can match any Gitea event.
73func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
74 switch eventName {
75 case GithubEventRegistryPackage:
76 return triggedEvent == webhook_module.HookEventPackage
77
78 // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum
79 case GithubEventGollum:
80 return triggedEvent == webhook_module.HookEventWiki
81
82 case GithubEventWorkflowDispatch:
83 return triggedEvent == webhook_module.HookEventWorkflowDispatch
84
85 case GithubEventIssues:
86 switch triggedEvent {
87 case webhook_module.HookEventIssues,
88 webhook_module.HookEventIssueAssign,
89 webhook_module.HookEventIssueLabel,
90 webhook_module.HookEventIssueMilestone:
91 return true
92
93 default:
94 return false
95 }
96
97 case GithubEventPullRequest, GithubEventPullRequestTarget:
98 switch triggedEvent {
99 case webhook_module.HookEventPullRequest,
100 webhook_module.HookEventPullRequestSync,
101 webhook_module.HookEventPullRequestAssign,
102 webhook_module.HookEventPullRequestLabel,
103 webhook_module.HookEventPullRequestReviewRequest,
104 webhook_module.HookEventPullRequestMilestone:
105 return true
106
107 default:
108 return false
109 }
110
111 case GithubEventPullRequestReview:
112 switch triggedEvent {
113 case webhook_module.HookEventPullRequestReviewApproved,
114 webhook_module.HookEventPullRequestReviewComment,
115 webhook_module.HookEventPullRequestReviewRejected:
116 return true
117
118 default:
119 return false
120 }
121
122 case GithubEventIssueComment:
123 // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment
124 return triggedEvent == webhook_module.HookEventIssueComment ||
125 triggedEvent == webhook_module.HookEventPullRequestComment
126
127 case GithubEventSchedule:
128 return triggedEvent == webhook_module.HookEventSchedule
129
130 default:
131 return eventName == string(triggedEvent)
132 }
133}