Skip to content

Commit 2e6d978

Browse files
Zettat123GiteaBot
authored andcommitted
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]> (cherry picked from commit bd1946e)
1 parent 28ab56b commit 2e6d978

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

modules/actions/workflows.go

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -302,44 +302,47 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j
302302
}
303303

304304
func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool {
305-
// with no special filter parameters
306-
if len(evt.Acts()) == 0 {
305+
acts := evt.Acts()
306+
activityTypeMatched := false
307+
matchTimes := 0
308+
309+
if vals, ok := acts["types"]; !ok {
307310
// defaultly, only pull request `opened`, `reopened` and `synchronized` will trigger workflow
308311
// See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
309-
return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
312+
activityTypeMatched = prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened
313+
} else {
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+
activityTypeMatched = true
337+
matchTimes++
338+
break
339+
}
340+
}
310341
}
311342

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

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

modules/actions/workflows_test.go

Lines changed: 19 additions & 0 deletions
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)