From 9ac1c837802ba783fb74cddbb8321d89050974cf Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 11 Apr 2023 14:55:35 +0800 Subject: [PATCH 1/9] fix pr_label and pr_assign --- modules/actions/workflows.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index d30982a46bda..a302bcb67a51 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -121,8 +121,6 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventIssueAssign, webhook_module.HookEventIssueLabel, webhook_module.HookEventIssueMilestone, - webhook_module.HookEventPullRequestAssign, - webhook_module.HookEventPullRequestLabel, webhook_module.HookEventPullRequestMilestone, webhook_module.HookEventPullRequestComment, webhook_module.HookEventPullRequestReviewApproved, @@ -144,7 +142,10 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType case webhook_module.HookEventIssues: return matchIssuesEvent(commit, payload.(*api.IssuePayload), evt) - case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync: + case webhook_module.HookEventPullRequest, + webhook_module.HookEventPullRequestSync, + webhook_module.HookEventPullRequestAssign, + webhook_module.HookEventPullRequestLabel: return matchPullRequestEvent(commit, payload.(*api.PullRequestPayload), evt) case webhook_module.HookEventIssueComment: @@ -281,8 +282,9 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { // with no special filter parameters if len(evt.Acts()) == 0 { - // defaultly, only pull request opened and synchronized will trigger workflow - return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened + // defaultly, only pull request opened, reopened and synchronized will trigger workflow + // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request + return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened } matchTimes := 0 @@ -291,8 +293,13 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload switch cond { case "types": action := prPayload.Action - if prPayload.Action == api.HookIssueSynchronized { + switch action { + case api.HookIssueSynchronized: action = "synchronize" + case api.HookIssueLabelUpdated: + action = "labeled" + case api.HookIssueLabelCleared: + action = "unlabeled" } log.Trace("matching pull_request %s with %v", action, vals) for _, val := range vals { From d315f02051e54698d0eda272c13c279288813d25 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 11 Apr 2023 17:42:04 +0800 Subject: [PATCH 2/9] add matchPullRequestReviewEvent --- modules/actions/github.go | 5 ++ modules/actions/workflows.go | 94 ++++++++++++++++++++++++++++-------- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index 1148554139cd..75c31f8d0f1d 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -21,6 +21,7 @@ const ( githubEventIssueComment = "issue_comment" githubEventRelease = "release" githubEventPullRequestComment = "pull_request_comment" + githubEventGollum = "gollum" ) // canGithubEventMatch check if the input Github event can match any Gitea event. @@ -29,6 +30,10 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent case githubEventRegistryPackage: return triggedEvent == webhook_module.HookEventPackage + case githubEventGollum: + // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum + return triggedEvent == webhook_module.HookEventWiki + case githubEventIssues: switch triggedEvent { case webhook_module.HookEventIssues, diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index a302bcb67a51..884bfcb0144b 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -115,21 +115,14 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType } switch triggedEvent { - case webhook_module.HookEventCreate, - webhook_module.HookEventDelete, - webhook_module.HookEventFork, - webhook_module.HookEventIssueAssign, - webhook_module.HookEventIssueLabel, - webhook_module.HookEventIssueMilestone, - webhook_module.HookEventPullRequestMilestone, - webhook_module.HookEventPullRequestComment, - webhook_module.HookEventPullRequestReviewApproved, - webhook_module.HookEventPullRequestReviewRejected, - webhook_module.HookEventPullRequestReviewComment, - webhook_module.HookEventWiki, - webhook_module.HookEventRepository, - webhook_module.HookEventRelease, - webhook_module.HookEventPackage: + case webhook_module.HookEventCreate, // no activity types + webhook_module.HookEventDelete, // no activity types + webhook_module.HookEventFork, // no activity types + webhook_module.HookEventWiki, // no activity types + + webhook_module.HookEventPullRequestReviewComment, // TODO + webhook_module.HookEventRelease, // TODO + webhook_module.HookEventPackage: // TODO if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) } @@ -139,17 +132,31 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType case webhook_module.HookEventPush: return matchPushEvent(commit, payload.(*api.PushPayload), evt) - case webhook_module.HookEventIssues: + case // issues + webhook_module.HookEventIssues, + webhook_module.HookEventIssueAssign, + webhook_module.HookEventIssueLabel, + webhook_module.HookEventIssueMilestone: return matchIssuesEvent(commit, payload.(*api.IssuePayload), evt) - case webhook_module.HookEventPullRequest, + case // issue_comment + webhook_module.HookEventIssueComment, + // use issue_comment for pull_request_comment + // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment + webhook_module.HookEventPullRequestComment: + return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) + + case // pull_request + webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestLabel: return matchPullRequestEvent(commit, payload.(*api.PullRequestPayload), evt) - case webhook_module.HookEventIssueComment: - return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) + case // pull_request_review + webhook_module.HookEventPullRequestReviewApproved, + webhook_module.HookEventPullRequestReviewRejected: + return matchPullRequestReviewEvent(commit, payload.(*api.PullRequestPayload), evt) default: log.Warn("unsupported event %q", triggedEvent) @@ -266,8 +273,15 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j for cond, vals := range evt.Acts() { switch cond { case "types": + action := issuePayload.Action + switch action { + case api.HookIssueLabelUpdated: + action = "labeled" + case api.HookIssueLabelCleared: + action = "unlabeled" + } for _, val := range vals { - if glob.MustCompile(val, '/').Match(string(issuePayload.Action)) { + if glob.MustCompile(val, '/').Match(string(action)) { matchTimes++ break } @@ -382,3 +396,43 @@ func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCo } return matchTimes == len(evt.Acts()) } + +func matchPullRequestReviewEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { + // with no special filter parameters + if len(evt.Acts()) == 0 { + return true + } + + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts() { + switch cond { + case "types": + actions := make([]string, 0) + if prPayload.Action == api.HookIssueReviewed { + // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review + actions = append(actions, "submitted", "edited") + } + // TODO: support dismissed activity type + + matched := false + for _, val := range vals { + for _, action := range actions { + if glob.MustCompile(val, '/').Match(action) { + matched = true + break + } + } + if matched { + break + } + } + if matched { + matchTimes++ + } + default: + log.Warn("pull request review event unsupported condition %q", cond) + } + } + return matchTimes == len(evt.Acts()) +} From a8210c3c47f4b3a6bbce708d4ccb65f946c64801 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 11 Apr 2023 18:37:17 +0800 Subject: [PATCH 3/9] add matchReleaseEvent --- modules/actions/workflows.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 884bfcb0144b..f03dd402d78a 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -121,7 +121,6 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventWiki, // no activity types webhook_module.HookEventPullRequestReviewComment, // TODO - webhook_module.HookEventRelease, // TODO webhook_module.HookEventPackage: // TODO if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) @@ -158,6 +157,9 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventPullRequestReviewRejected: return matchPullRequestReviewEvent(commit, payload.(*api.PullRequestPayload), evt) + case webhook_module.HookEventRelease: + return matchReleaseEvent(commit, payload.(*api.ReleasePayload), evt) + default: log.Warn("unsupported event %q", triggedEvent) return false @@ -436,3 +438,32 @@ func matchPullRequestReviewEvent(commit *git.Commit, prPayload *api.PullRequestP } return matchTimes == len(evt.Acts()) } + +func matchReleaseEvent(commit *git.Commit, payload *api.ReleasePayload, evt *jobparser.Event) bool { + // with no special filter parameters + if len(evt.Acts()) == 0 { + return true + } + + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts() { + switch cond { + case "types": + action := payload.Action + switch action { + case api.HookReleaseUpdated: + action = "edited" + } + for _, val := range vals { + if glob.MustCompile(val, '/').Match(string(action)) { + matchTimes++ + break + } + } + default: + log.Warn("release event unsupported condition %q", cond) + } + } + return matchTimes == len(evt.Acts()) +} From cb5edcfb2ed9c283074778095034581d1d9c4e94 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 12 Apr 2023 11:46:41 +0800 Subject: [PATCH 4/9] add matchPackageEvent --- modules/actions/workflows.go | 43 +++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index f03dd402d78a..d3a5549297f5 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -120,8 +120,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventFork, // no activity types webhook_module.HookEventWiki, // no activity types - webhook_module.HookEventPullRequestReviewComment, // TODO - webhook_module.HookEventPackage: // TODO + webhook_module.HookEventPullRequestReviewComment: // TODO if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) } @@ -140,7 +139,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType case // issue_comment webhook_module.HookEventIssueComment, - // use issue_comment for pull_request_comment + // `pull_request_comment` is same as `issue_comment` // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment webhook_module.HookEventPullRequestComment: return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) @@ -160,6 +159,9 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType case webhook_module.HookEventRelease: return matchReleaseEvent(commit, payload.(*api.ReleasePayload), evt) + case webhook_module.HookEventPackage: + return matchPackageEvent(commit, payload.(*api.PackagePayload), evt) + default: log.Warn("unsupported event %q", triggedEvent) return false @@ -298,7 +300,7 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { // with no special filter parameters if len(evt.Acts()) == 0 { - // defaultly, only pull request opened, reopened and synchronized will trigger workflow + // defaultly, only pull request `opened`, `reopened`` and `synchronized` will trigger workflow // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened } @@ -412,10 +414,11 @@ func matchPullRequestReviewEvent(commit *git.Commit, prPayload *api.PullRequestP case "types": actions := make([]string, 0) if prPayload.Action == api.HookIssueReviewed { + // the `reviewed` HookIssueAction can match the two activity types: `submitted` and `edited` // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review actions = append(actions, "submitted", "edited") } - // TODO: support dismissed activity type + // TODO: support the `dismissed` activity type matched := false for _, val := range vals { @@ -467,3 +470,33 @@ func matchReleaseEvent(commit *git.Commit, payload *api.ReleasePayload, evt *job } return matchTimes == len(evt.Acts()) } + +func matchPackageEvent(commit *git.Commit, payload *api.PackagePayload, evt *jobparser.Event) bool { + // with no special filter parameters + if len(evt.Acts()) == 0 { + return true + } + + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts() { + switch cond { + case "types": + action := payload.Action + switch action { + case api.HookPackageCreated: + action = "published" + // TODO: support the `updated` activity type + } + for _, val := range vals { + if glob.MustCompile(val, '/').Match(string(action)) { + matchTimes++ + break + } + } + default: + log.Warn("release event unsupported condition %q", cond) + } + } + return matchTimes == len(evt.Acts()) +} From 78f04bbb74c003e578dbb1195c54f15b3275d06c Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 12 Apr 2023 12:11:03 +0800 Subject: [PATCH 5/9] add matchPullRequestReviewCommentEvent --- modules/actions/workflows.go | 67 ++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index d3a5549297f5..76884e8e6004 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -115,19 +115,19 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType } switch triggedEvent { - case webhook_module.HookEventCreate, // no activity types - webhook_module.HookEventDelete, // no activity types - webhook_module.HookEventFork, // no activity types - webhook_module.HookEventWiki, // no activity types - - webhook_module.HookEventPullRequestReviewComment: // TODO + case // events with no activity types + webhook_module.HookEventCreate, + webhook_module.HookEventDelete, + webhook_module.HookEventFork, + webhook_module.HookEventWiki: if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) } // no special filter parameters for these events, just return true if name matched return true - case webhook_module.HookEventPush: + case // push + webhook_module.HookEventPush: return matchPushEvent(commit, payload.(*api.PushPayload), evt) case // issues @@ -156,10 +156,16 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventPullRequestReviewRejected: return matchPullRequestReviewEvent(commit, payload.(*api.PullRequestPayload), evt) - case webhook_module.HookEventRelease: + case // pull + webhook_module.HookEventPullRequestReviewComment: + return matchPullRequestReviewCommentEvent(commit, payload.(*api.PullRequestPayload), evt) + + case // release + webhook_module.HookEventRelease: return matchReleaseEvent(commit, payload.(*api.ReleasePayload), evt) - case webhook_module.HookEventPackage: + case // registry_package + webhook_module.HookEventPackage: return matchPackageEvent(commit, payload.(*api.PackagePayload), evt) default: @@ -442,6 +448,47 @@ func matchPullRequestReviewEvent(commit *git.Commit, prPayload *api.PullRequestP return matchTimes == len(evt.Acts()) } +func matchPullRequestReviewCommentEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { + // with no special filter parameters + if len(evt.Acts()) == 0 { + return true + } + + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts() { + switch cond { + case "types": + actions := make([]string, 0) + if prPayload.Action == api.HookIssueReviewed { + // the `reviewed` HookIssueAction can match the two activity types: `created` and `edited` + // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment + actions = append(actions, "created", "edited") + } + // TODO: support the `deleted` activity type + + matched := false + for _, val := range vals { + for _, action := range actions { + if glob.MustCompile(val, '/').Match(action) { + matched = true + break + } + } + if matched { + break + } + } + if matched { + matchTimes++ + } + default: + log.Warn("pull request review comment event unsupported condition %q", cond) + } + } + return matchTimes == len(evt.Acts()) +} + func matchReleaseEvent(commit *git.Commit, payload *api.ReleasePayload, evt *jobparser.Event) bool { // with no special filter parameters if len(evt.Acts()) == 0 { @@ -495,7 +542,7 @@ func matchPackageEvent(commit *git.Commit, payload *api.PackagePayload, evt *job } } default: - log.Warn("release event unsupported condition %q", cond) + log.Warn("package event unsupported condition %q", cond) } } return matchTimes == len(evt.Acts()) From cfbcb0e91f26c4d2cbbe258c89cb771d49183443 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 12 Apr 2023 13:39:09 +0800 Subject: [PATCH 6/9] add FIXME --- modules/actions/github.go | 5 ----- modules/actions/workflows.go | 2 ++ 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index 75c31f8d0f1d..1148554139cd 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -21,7 +21,6 @@ const ( githubEventIssueComment = "issue_comment" githubEventRelease = "release" githubEventPullRequestComment = "pull_request_comment" - githubEventGollum = "gollum" ) // canGithubEventMatch check if the input Github event can match any Gitea event. @@ -30,10 +29,6 @@ func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEvent case githubEventRegistryPackage: return triggedEvent == webhook_module.HookEventPackage - case githubEventGollum: - // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum - return triggedEvent == webhook_module.HookEventWiki - case githubEventIssues: switch triggedEvent { case webhook_module.HookEventIssues, diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 76884e8e6004..0e039520173c 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -119,6 +119,8 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventCreate, webhook_module.HookEventDelete, webhook_module.HookEventFork, + // FIXME: `wiki` event should match `gollum` event. + // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum webhook_module.HookEventWiki: if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) From ce2c67aa3094fd31c4e86af092ef180340807646 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 12 Apr 2023 15:04:21 +0800 Subject: [PATCH 7/9] unit tests --- modules/actions/workflows.go | 6 +- modules/actions/workflows_test.go | 105 ++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 modules/actions/workflows_test.go diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 0e039520173c..0f951832fab2 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -119,7 +119,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventCreate, webhook_module.HookEventDelete, webhook_module.HookEventFork, - // FIXME: `wiki` event should match `gollum` event. + // FIXME: `wiki` event should match `gollum` event // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum webhook_module.HookEventWiki: if len(evt.Acts()) != 0 { @@ -158,7 +158,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventPullRequestReviewRejected: return matchPullRequestReviewEvent(commit, payload.(*api.PullRequestPayload), evt) - case // pull + case // pull_request_review_comment webhook_module.HookEventPullRequestReviewComment: return matchPullRequestReviewCommentEvent(commit, payload.(*api.PullRequestPayload), evt) @@ -308,7 +308,7 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { // with no special filter parameters if len(evt.Acts()) == 0 { - // defaultly, only pull request `opened`, `reopened`` and `synchronized` will trigger workflow + // defaultly, only pull request `opened`, `reopened` and `synchronized` will trigger workflow // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened } diff --git a/modules/actions/workflows_test.go b/modules/actions/workflows_test.go new file mode 100644 index 000000000000..6724abafd859 --- /dev/null +++ b/modules/actions/workflows_test.go @@ -0,0 +1,105 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package actions + +import ( + "testing" + + "code.gitea.io/gitea/modules/git" + api "code.gitea.io/gitea/modules/structs" + webhook_module "code.gitea.io/gitea/modules/webhook" + + "github.com/stretchr/testify/assert" +) + +func TestDetectMatched(t *testing.T) { + testCases := []struct { + desc string + commit *git.Commit + triggedEvent webhook_module.HookEventType + payload api.Payloader + yamlOn string + expected bool + }{ + { + desc: "HookEventCreate(create) matches githubEventCreate(create)", + triggedEvent: webhook_module.HookEventCreate, + payload: nil, + yamlOn: "on: create", + expected: true, + }, + { + desc: "HookEventIssues(issues) `opened` action matches githubEventIssues(issues)", + triggedEvent: webhook_module.HookEventIssues, + payload: &api.IssuePayload{Action: api.HookIssueOpened}, + yamlOn: "on: issues", + expected: true, + }, + { + desc: "HookEventIssues(issues) `milestoned` action matches githubEventIssues(issues)", + triggedEvent: webhook_module.HookEventIssues, + payload: &api.IssuePayload{Action: api.HookIssueMilestoned}, + yamlOn: "on: issues", + expected: true, + }, + { + desc: "HookEventPullRequestSync(pull_request_sync) matches githubEventPullRequest(pull_request)", + triggedEvent: webhook_module.HookEventPullRequestSync, + payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized}, + yamlOn: "on: pull_request", + expected: true, + }, + { + desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match githubEventPullRequest(pull_request) with no activity type", + triggedEvent: webhook_module.HookEventPullRequest, + payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated}, + yamlOn: "on: pull_request", + expected: false, + }, + { + desc: "HookEventPullRequest(pull_request) `label_updated` action matches githubEventPullRequest(pull_request) with `label` activity type", + triggedEvent: webhook_module.HookEventPullRequest, + payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated}, + yamlOn: "on:\n pull_request:\n types: [labeled]", + expected: true, + }, + { + desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches githubEventPullRequestReviewComment(pull_request_review_comment)", + triggedEvent: webhook_module.HookEventPullRequestReviewComment, + payload: &api.PullRequestPayload{Action: api.HookIssueReviewed}, + yamlOn: "on:\n pull_request_review_comment:\n types: [created]", + expected: true, + }, + { + desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match githubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)", + triggedEvent: webhook_module.HookEventPullRequestReviewRejected, + payload: &api.PullRequestPayload{Action: api.HookIssueReviewed}, + yamlOn: "on:\n pull_request_review:\n types: [dismissed]", + expected: false, + }, + { + desc: "HookEventRelease(release) `published` action matches githubEventRelease(release) with `published` activity type", + triggedEvent: webhook_module.HookEventRelease, + payload: &api.ReleasePayload{Action: api.HookReleasePublished}, + yamlOn: "on:\n release:\n types: [published]", + expected: true, + }, + { + desc: "HookEventPackage(package) `created` action doesn't match githubEventRegistryPackage(registry_package) with `updated` activity type", + triggedEvent: webhook_module.HookEventPackage, + payload: &api.PackagePayload{Action: api.HookPackageCreated}, + yamlOn: "on:\n registry_package:\n types: [updated]", + expected: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + evts, err := GetEventsFromContent([]byte(tc.yamlOn)) + assert.NoError(t, err) + assert.Len(t, evts, 1) + assert.Equal(t, tc.expected, detectMatched(tc.commit, tc.triggedEvent, tc.payload, evts[0])) + }) + } +} From 7a9f777c49511c027c5b25cd787e8c9771241eed Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 12 Apr 2023 16:04:23 +0800 Subject: [PATCH 8/9] update comments --- modules/actions/workflows.go | 74 ++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 8 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 0f951832fab2..2695e034f9d4 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -120,7 +120,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventDelete, webhook_module.HookEventFork, // FIXME: `wiki` event should match `gollum` event - // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#gollum webhook_module.HookEventWiki: if len(evt.Acts()) != 0 { log.Warn("Ignore unsupported %s event arguments %v", triggedEvent, evt.Acts()) @@ -142,7 +142,7 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType case // issue_comment webhook_module.HookEventIssueComment, // `pull_request_comment` is same as `issue_comment` - // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_comment-use-issue_comment webhook_module.HookEventPullRequestComment: return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) @@ -285,6 +285,15 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues + // Actions with the same name: + // opened, edited, closed, reopened, assigned, unassigned, milestoned, demilestoned + // Actions need to be converted: + // label_updated -> labeled + // label_cleared -> unlabeled + // Unsupported activity types: + // deleted, transferred, pinned, unpinned, locked, unlocked + action := issuePayload.Action switch action { case api.HookIssueLabelUpdated: @@ -309,7 +318,7 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload // with no special filter parameters if len(evt.Acts()) == 0 { // defaultly, only pull request `opened`, `reopened` and `synchronized` will trigger workflow - // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened || prPayload.Action == api.HookIssueReOpened } @@ -318,6 +327,16 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request + // Actions with the same name: + // opened, edited, closed, reopened, assigned, unassigned + // Actions need to be converted: + // synchronized -> synchronize + // label_updated -> labeled + // label_cleared -> unlabeled + // Unsupported activity types: + // converted_to_draft, ready_for_review, locked, unlocked, review_requested, review_request_removed, auto_merge_enabled, auto_merge_disabled + action := prPayload.Action switch action { case api.HookIssueSynchronized: @@ -396,6 +415,14 @@ func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCo for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issue_comment + // Actions with the same name: + // created, edited, deleted + // Actions need to be converted: + // NONE + // Unsupported activity types: + // NONE + for _, val := range vals { if glob.MustCompile(val, '/').Match(string(issueCommentPayload.Action)) { matchTimes++ @@ -420,13 +447,21 @@ func matchPullRequestReviewEvent(commit *git.Commit, prPayload *api.PullRequestP for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review + // Activity types with the same name: + // NONE + // Activity types need to be converted: + // reviewed -> submitted + // reviewed -> edited + // Unsupported activity types: + // dismissed + actions := make([]string, 0) if prPayload.Action == api.HookIssueReviewed { // the `reviewed` HookIssueAction can match the two activity types: `submitted` and `edited` - // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review actions = append(actions, "submitted", "edited") } - // TODO: support the `dismissed` activity type matched := false for _, val := range vals { @@ -461,13 +496,21 @@ func matchPullRequestReviewCommentEvent(commit *git.Commit, prPayload *api.PullR for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment + // Activity types with the same name: + // NONE + // Activity types need to be converted: + // reviewed -> created + // reviewed -> edited + // Unsupported activity types: + // deleted + actions := make([]string, 0) if prPayload.Action == api.HookIssueReviewed { // the `reviewed` HookIssueAction can match the two activity types: `created` and `edited` - // See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_review_comment actions = append(actions, "created", "edited") } - // TODO: support the `deleted` activity type matched := false for _, val := range vals { @@ -502,6 +545,14 @@ func matchReleaseEvent(commit *git.Commit, payload *api.ReleasePayload, evt *job for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#release + // Activity types with the same name: + // published + // Activity types need to be converted: + // updated -> edited + // Unsupported activity types: + // unpublished, created, deleted, prereleased, released + action := payload.Action switch action { case api.HookReleaseUpdated: @@ -531,11 +582,18 @@ func matchPackageEvent(commit *git.Commit, payload *api.PackagePayload, evt *job for cond, vals := range evt.Acts() { switch cond { case "types": + // See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#registry_package + // Activity types with the same name: + // NONE + // Activity types need to be converted: + // created -> published + // Unsupported activity types: + // updated + action := payload.Action switch action { case api.HookPackageCreated: action = "published" - // TODO: support the `updated` activity type } for _, val := range vals { if glob.MustCompile(val, '/').Match(string(action)) { From 19c8cea912bc00fac5bca0e2b91ec6b6fca76236 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 12 Apr 2023 16:21:32 +0800 Subject: [PATCH 9/9] fix log --- modules/actions/workflows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 2695e034f9d4..d21dc1d809c0 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -430,7 +430,7 @@ func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCo } } default: - log.Warn("issue comment unsupported condition %q", cond) + log.Warn("issue comment event unsupported condition %q", cond) } } return matchTimes == len(evt.Acts())