Skip to content

Commit b3b864e

Browse files
Zettat123lunny
authored andcommitted
Improve workflow event triggers (go-gitea#23613)
Follow go-gitea#23037 Fix [go-gitea#22598 comment](go-gitea#22958 (comment)) Workflows with `pull_request` trigger event can't be triggered by `pull_request_sync` event. This PR adds the `canGithubEventMatch` function to check if a Github event can match any Gitea event. If the Github event matches a Gitea event, the related workflows should be triggered. --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent 622d216 commit b3b864e

File tree

3 files changed

+155
-13
lines changed

3 files changed

+155
-13
lines changed

modules/actions/github.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package actions
55

66
import (
77
webhook_module "code.gitea.io/gitea/modules/webhook"
8-
9-
"github.com/nektos/act/pkg/jobparser"
108
)
119

1210
const (
@@ -25,17 +23,48 @@ const (
2523
githubEventPullRequestComment = "pull_request_comment"
2624
)
2725

28-
func convertFromGithubEvent(evt *jobparser.Event) string {
29-
switch evt.Name {
30-
case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview,
31-
githubEventPullRequestReviewComment:
32-
return string(webhook_module.HookEventPullRequest)
26+
// canGithubEventMatch check if the input Github event can match any Gitea event.
27+
func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
28+
switch eventName {
3329
case githubEventRegistryPackage:
34-
return string(webhook_module.HookEventPackage)
35-
case githubEventCreate, githubEventDelete, githubEventFork, githubEventPush,
36-
githubEventIssues, githubEventIssueComment, githubEventRelease, githubEventPullRequestComment:
37-
fallthrough
30+
return triggedEvent == webhook_module.HookEventPackage
31+
32+
case githubEventIssues:
33+
switch triggedEvent {
34+
case webhook_module.HookEventIssues,
35+
webhook_module.HookEventIssueAssign,
36+
webhook_module.HookEventIssueLabel,
37+
webhook_module.HookEventIssueMilestone:
38+
return true
39+
40+
default:
41+
return false
42+
}
43+
44+
case githubEventPullRequest, githubEventPullRequestTarget:
45+
switch triggedEvent {
46+
case webhook_module.HookEventPullRequest,
47+
webhook_module.HookEventPullRequestSync,
48+
webhook_module.HookEventPullRequestAssign,
49+
webhook_module.HookEventPullRequestLabel:
50+
return true
51+
52+
default:
53+
return false
54+
}
55+
56+
case githubEventPullRequestReview:
57+
switch triggedEvent {
58+
case webhook_module.HookEventPullRequestReviewApproved,
59+
webhook_module.HookEventPullRequestReviewComment,
60+
webhook_module.HookEventPullRequestReviewRejected:
61+
return true
62+
63+
default:
64+
return false
65+
}
66+
3867
default:
39-
return evt.Name
68+
return eventName == string(triggedEvent)
4069
}
4170
}

modules/actions/github_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
webhook_module "code.gitea.io/gitea/modules/webhook"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCanGithubEventMatch(t *testing.T) {
15+
testCases := []struct {
16+
desc string
17+
eventName string
18+
triggeredEvent webhook_module.HookEventType
19+
expected bool
20+
}{
21+
// registry_package event
22+
{
23+
"registry_package matches",
24+
githubEventRegistryPackage,
25+
webhook_module.HookEventPackage,
26+
true,
27+
},
28+
{
29+
"registry_package cannot match",
30+
githubEventRegistryPackage,
31+
webhook_module.HookEventPush,
32+
false,
33+
},
34+
// issues event
35+
{
36+
"issue matches",
37+
githubEventIssues,
38+
webhook_module.HookEventIssueLabel,
39+
true,
40+
},
41+
{
42+
"issue cannot match",
43+
githubEventIssues,
44+
webhook_module.HookEventIssueComment,
45+
false,
46+
},
47+
// issue_comment event
48+
{
49+
"issue_comment matches",
50+
githubEventIssueComment,
51+
webhook_module.HookEventIssueComment,
52+
true,
53+
},
54+
{
55+
"issue_comment cannot match",
56+
githubEventIssueComment,
57+
webhook_module.HookEventIssues,
58+
false,
59+
},
60+
// pull_request event
61+
{
62+
"pull_request matches",
63+
githubEventPullRequest,
64+
webhook_module.HookEventPullRequestSync,
65+
true,
66+
},
67+
{
68+
"pull_request cannot match",
69+
githubEventPullRequest,
70+
webhook_module.HookEventPullRequestComment,
71+
false,
72+
},
73+
// pull_request_target event
74+
{
75+
"pull_request_target matches",
76+
githubEventPullRequest,
77+
webhook_module.HookEventPullRequest,
78+
true,
79+
},
80+
{
81+
"pull_request_target cannot match",
82+
githubEventPullRequest,
83+
webhook_module.HookEventPullRequestComment,
84+
false,
85+
},
86+
// pull_request_review event
87+
{
88+
"pull_request_review matches",
89+
githubEventPullRequestReview,
90+
webhook_module.HookEventPullRequestReviewComment,
91+
true,
92+
},
93+
{
94+
"pull_request_review cannot match",
95+
githubEventPullRequestReview,
96+
webhook_module.HookEventPullRequestComment,
97+
false,
98+
},
99+
// other events
100+
{
101+
"create event",
102+
githubEventCreate,
103+
webhook_module.HookEventCreate,
104+
true,
105+
},
106+
}
107+
108+
for _, tc := range testCases {
109+
t.Run(tc.desc, func(t *testing.T) {
110+
assert.Equalf(t, tc.expected, canGithubEventMatch(tc.eventName, tc.triggeredEvent), "canGithubEventMatch(%v, %v)", tc.eventName, tc.triggeredEvent)
111+
})
112+
}
113+
}

modules/actions/workflows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy
8383
}
8484

8585
func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool {
86-
if convertFromGithubEvent(evt) != string(triggedEvent) {
86+
if !canGithubEventMatch(evt.Name, triggedEvent) {
8787
return false
8888
}
8989

0 commit comments

Comments
 (0)