Skip to content

Commit bd1946e

Browse files
Zettat123GiteaBot
andauthored
Fix activity type match in matchPullRequestEvent (go-gitea#25746) (go-gitea#25797)
Backport go-gitea#25746 Fix go-gitea#25736 Caused by go-gitea#24048 Right now we only check the activity type for `pull_request` event when `types` is specified or there are no `types` and filter. If a workflow only specifies filters but no `types` like this: ``` on: pull_request: branches: [main] ``` the workflow will be triggered even if the activity type is not one of `[opened, reopened, sync]`. We need to check the activity type in this case. Co-authored-by: Giteabot <[email protected]>
1 parent 5bfe469 commit bd1946e

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

modules/actions/workflows.go

+36-33
Original file line numberDiff line numberDiff line change
@@ -299,44 +299,47 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j
299299
}
300300

301301
func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool {
302-
// with no special filter parameters
303-
if len(evt.Acts()) == 0 {
302+
acts := evt.Acts()
303+
activityTypeMatched := false
304+
matchTimes := 0
305+
306+
if vals, ok := acts["types"]; !ok {
304307
// defaultly, only pull request `opened`, `reopened` and `synchronized` will trigger workflow
305308
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
306-
return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
309+
activityTypeMatched = prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
310+
} else {
311+
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
312+
// Actions with the same name:
313+
// opened, edited, closed, reopened, assigned, unassigned
314+
// Actions need to be converted:
315+
// synchronized -> synchronize
316+
// label_updated -> labeled
317+
// label_cleared -> unlabeled
318+
// Unsupported activity types:
319+
// converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled
320+
321+
action := prPayload.Action
322+
switch action {
323+
case api.HookIssueSynchronized:
324+
action = "synchronize"
325+
case api.HookIssueLabelUpdated:
326+
action = "labeled"
327+
case api.HookIssueLabelCleared:
328+
action = "unlabeled"
329+
}
330+
log.Trace("matching pull_request %s with %v", action, vals)
331+
for _, val := range vals {
332+
if glob.MustCompile(val, '/').Match(string(action)) {
333+
activityTypeMatched = true
334+
matchTimes++
335+
break
336+
}
337+
}
307338
}
308339

309-
matchTimes := 0
310340
// all acts conditions should be satisfied
311-
for cond, vals := range evt.Acts() {
341+
for cond, vals := range acts {
312342
switch cond {
313-
case "types":
314-
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
315-
// Actions with the same name:
316-
// opened, edited, closed, reopened, assigned, unassigned
317-
// Actions need to be converted:
318-
// synchronized -> synchronize
319-
// label_updated -> labeled
320-
// label_cleared -> unlabeled
321-
// Unsupported activity types:
322-
// converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled
323-
324-
action := prPayload.Action
325-
switch action {
326-
case api.HookIssueSynchronized:
327-
action = "synchronize"
328-
case api.HookIssueLabelUpdated:
329-
action = "labeled"
330-
case api.HookIssueLabelCleared:
331-
action = "unlabeled"
332-
}
333-
log.Trace("matching pull_request %s with %v", action, vals)
334-
for _, val := range vals {
335-
if glob.MustCompile(val, '/').Match(string(action)) {
336-
matchTimes++
337-
break
338-
}
339-
}
340343
case "branches":
341344
refName := git.RefName(prPayload.PullRequest.Base.Ref)
342345
patterns, err := workflowpattern.CompilePatterns(vals...)
@@ -385,7 +388,7 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload
385388
log.Warn("pull request event unsupported condition %q", cond)
386389
}
387390
}
388-
return matchTimes == len(evt.Acts())
391+
return activityTypeMatched && matchTimes == len(evt.Acts())
389392
}
390393

391394
func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCommentPayload, evt *jobparser.Event) bool {

modules/actions/workflows_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ func TestDetectMatched(t *testing.T) {
6060
yamlOn: "on: pull_request",
6161
expected: false,
6262
},
63+
{
64+
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
65+
triggedEvent: webhook_module.HookEventPullRequest,
66+
payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
67+
yamlOn: "on: pull_request",
68+
expected: false,
69+
},
70+
{
71+
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
72+
triggedEvent: webhook_module.HookEventPullRequest,
73+
payload: &api.PullRequestPayload{
74+
Action: api.HookIssueClosed,
75+
PullRequest: &api.PullRequest{
76+
Base: &api.PRBranchInfo{},
77+
},
78+
},
79+
yamlOn: "on:\n pull_request:\n branches: [main]",
80+
expected: false,
81+
},
6382
{
6483
desc: "HookEventPullRequest(pull_request) `label_updated` action matches githubEventPullRequest(pull_request) with `label` activity type",
6584
triggedEvent: webhook_module.HookEventPullRequest,

0 commit comments

Comments
 (0)