From 14b253fd0e51be5f79916e8db810e48e98787d0c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 21 Feb 2023 14:44:41 +0800 Subject: [PATCH 1/9] Fix pull request sync event and supports issues and issue_comment event --- modules/actions/workflows.go | 320 ++++++++++++++++++++++------------- 1 file changed, 203 insertions(+), 117 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 7f0e6e456436b..c2816788a3547 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -44,6 +44,59 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) { return ret, nil } +func converGithubEvent2GiteaEvent(evt *jobparser.Event) string { + switch evt.Name { + case "create": + return string(webhook_module.HookEventCreate) + case "delete": + return string(webhook_module.HookEventDelete) + case "fork": + return string(webhook_module.HookEventFork) + case "issue_comment": + return string(webhook_module.HookEventIssueComment) + case "issues": + for _, tp := range evt.Acts["types"] { + switch tp { + case "assigned": + return string(webhook_module.HookEventIssueAssign) + case "milestoned": + return string(webhook_module.HookEventIssueMilestone) + case "labeled": + return string(webhook_module.HookEventIssueLabel) + } + } + return string(webhook_module.HookEventIssues) + case "pull_request", "pull_request_target": + for _, tp := range evt.Acts["types"] { + switch tp { + case "assigned": + return string(webhook_module.HookEventPullRequestAssign) + case "milestoned": + return string(webhook_module.HookEventPullRequestMilestone) + case "labeled": + return string(webhook_module.HookEventPullRequestLabel) + case "synchronize": + return string(webhook_module.HookEventPullRequestSync) + } + } + return string(webhook_module.HookEventPullRequest) + case "pull_request_comment": + return string(webhook_module.HookEventPullRequestComment) + case "pull_request_review_comment": + return string(webhook_module.HookEventPullRequestReviewComment) + case "push": + return string(webhook_module.HookEventPush) + case "registry_package": + return string(webhook_module.HookEventPackage) + case "release": + return string(webhook_module.HookEventRelease) + case "pull_request_review", "milestone", "label", "project", "project_card", "project_column": + fallthrough + default: + return evt.Name + } +} + func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader) (map[string][]byte, error) { entries, err := ListWorkflows(commit) if err != nil { @@ -72,9 +125,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy continue } for _, evt := range events { - if evt.Name != triggedEvent.Event() { - continue - } + log.Trace("detect workflow %q for event %q matching %q", entry.Name(), evt.Name, triggedEvent) if detectMatched(commit, triggedEvent, payload, evt) { workflows[entry.Name()] = content } @@ -84,139 +135,174 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy return workflows, nil } -func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { - if len(evt.Acts) == 0 { - return true - } - - switch triggedEvent { - case webhook_module.HookEventCreate: - fallthrough - case webhook_module.HookEventDelete: - fallthrough - case webhook_module.HookEventFork: - log.Warn("unsupported event %q", triggedEvent.Event()) - return false - case webhook_module.HookEventPush: - pushPayload := payload.(*api.PushPayload) - matchTimes := 0 - // all acts conditions should be satisfied - for cond, vals := range evt.Acts { - switch cond { - case "branches", "tags": - refShortName := git.RefName(pushPayload.Ref).ShortName() +func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobparser.Event) bool { + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts { + switch cond { + case "branches", "tags": + refShortName := git.RefName(pushPayload.Ref).ShortName() + for _, val := range vals { + if glob.MustCompile(val, '/').Match(refShortName) { + matchTimes++ + break + } + } + case "paths": + filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) + if err != nil { + log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) + } else { for _, val := range vals { - if glob.MustCompile(val, '/').Match(refShortName) { + matched := false + for _, file := range filesChanged { + if glob.MustCompile(val, '/').Match(file) { + matched = true + break + } + } + if matched { matchTimes++ break } } - case "paths": - filesChanged, err := commit.GetFilesChangedSinceCommit(pushPayload.Before) - if err != nil { - log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) - } else { - for _, val := range vals { - matched := false - for _, file := range filesChanged { - if glob.MustCompile(val, '/').Match(file) { - matched = true - break - } - } - if matched { - matchTimes++ - break - } - } + } + default: + log.Warn("unsupported condition %q", cond) + } + } + return matchTimes == len(evt.Acts) +} + +func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *jobparser.Event) bool { + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts { + switch cond { + case "types": + for _, val := range vals { + if glob.MustCompile(val, '/').Match(string(issuePayload.Action)) { + matchTimes++ + break } - default: - log.Warn("unsupported condition %q", cond) } + default: + log.Warn("unsupported condition %q", cond) } - return matchTimes == len(evt.Acts) + } + return matchTimes == len(evt.Acts) +} - case webhook_module.HookEventIssues: - fallthrough - case webhook_module.HookEventIssueAssign: - fallthrough - case webhook_module.HookEventIssueLabel: - fallthrough - case webhook_module.HookEventIssueMilestone: - fallthrough - case webhook_module.HookEventIssueComment: - fallthrough - case webhook_module.HookEventPullRequest: - prPayload := payload.(*api.PullRequestPayload) - matchTimes := 0 - // all acts conditions should be satisfied - for cond, vals := range evt.Acts { - switch cond { - case "types": - for _, val := range vals { - if glob.MustCompile(val, '/').Match(string(prPayload.Action)) { - matchTimes++ - break - } +func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts { + switch cond { + case "types": + for _, val := range vals { + if glob.MustCompile(val, '/').Match(string(prPayload.Action)) { + matchTimes++ + break } - case "branches": - refShortName := git.RefName(prPayload.PullRequest.Base.Ref).ShortName() + } + case "branches": + refShortName := git.RefName(prPayload.PullRequest.Base.Ref).ShortName() + for _, val := range vals { + if glob.MustCompile(val, '/').Match(refShortName) { + matchTimes++ + break + } + } + case "paths": + filesChanged, err := commit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref) + if err != nil { + log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) + } else { for _, val := range vals { - if glob.MustCompile(val, '/').Match(refShortName) { + matched := false + for _, file := range filesChanged { + if glob.MustCompile(val, '/').Match(file) { + matched = true + break + } + } + if matched { matchTimes++ break } } - case "paths": - filesChanged, err := commit.GetFilesChangedSinceCommit(prPayload.PullRequest.Base.Ref) - if err != nil { - log.Error("GetFilesChangedSinceCommit [commit_sha1: %s]: %v", commit.ID.String(), err) - } else { - for _, val := range vals { - matched := false - for _, file := range filesChanged { - if glob.MustCompile(val, '/').Match(file) { - matched = true - break - } - } - if matched { - matchTimes++ - break - } - } + } + default: + log.Warn("unsupported condition %q", cond) + } + } + return matchTimes == len(evt.Acts) +} + +func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCommentPayload, evt *jobparser.Event) bool { + matchTimes := 0 + // all acts conditions should be satisfied + for cond, vals := range evt.Acts { + switch cond { + case "types": + for _, val := range vals { + if glob.MustCompile(val, '/').Match(string(issueCommentPayload.Action)) { + matchTimes++ + break } - default: - log.Warn("unsupported condition %q", cond) } + default: + log.Warn("unsupported condition %q", cond) } - return matchTimes == len(evt.Acts) - case webhook_module.HookEventPullRequestAssign: - fallthrough - case webhook_module.HookEventPullRequestLabel: - fallthrough - case webhook_module.HookEventPullRequestMilestone: - fallthrough - case webhook_module.HookEventPullRequestComment: - fallthrough - case webhook_module.HookEventPullRequestReviewApproved: - fallthrough - case webhook_module.HookEventPullRequestReviewRejected: - fallthrough - case webhook_module.HookEventPullRequestReviewComment: - fallthrough - case webhook_module.HookEventPullRequestSync: - fallthrough - case webhook_module.HookEventWiki: - fallthrough - case webhook_module.HookEventRepository: - fallthrough - case webhook_module.HookEventRelease: - fallthrough - case webhook_module.HookEventPackage: - fallthrough + } + return matchTimes == len(evt.Acts) +} + +func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { + if converGithubEvent2GiteaEvent(evt) != triggedEvent.Event() { + return false + } + + // with no special filter parameters + if len(evt.Acts) == 0 { + return true + } + + switch triggedEvent { + case webhook_module.HookEventCreate, + webhook_module.HookEventDelete, + webhook_module.HookEventFork, + webhook_module.HookEventIssueAssign, + webhook_module.HookEventIssueLabel, + webhook_module.HookEventIssueMilestone, + webhook_module.HookEventPullRequestAssign, + webhook_module.HookEventPullRequestLabel, + 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: + // no special filter parameters for these events, just return true if name matched + return true + + case webhook_module.HookEventPush: + return matchPushEvent(commit, payload.(*api.PushPayload), evt) + + case webhook_module.HookEventIssues: + return matchIssuesEvent(commit, payload.(*api.IssuePayload), evt) + + case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync: + return matchPullRequestEvent(commit, payload.(*api.PullRequestPayload), evt) + + case webhook_module.HookEventIssueComment: + return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) + default: log.Warn("unsupported event %q", triggedEvent.Event()) + return false } - return false } From 0b6d6ff782b77fb4443355bf98f371270cdd1a3c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 21 Feb 2023 15:40:13 +0800 Subject: [PATCH 2/9] Fix pull request sync event --- modules/actions/workflows.go | 53 ++++++------------------------------ 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index c2816788a3547..589f38ff9090a 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -46,51 +46,11 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) { func converGithubEvent2GiteaEvent(evt *jobparser.Event) string { switch evt.Name { - case "create": - return string(webhook_module.HookEventCreate) - case "delete": - return string(webhook_module.HookEventDelete) - case "fork": - return string(webhook_module.HookEventFork) - case "issue_comment": - return string(webhook_module.HookEventIssueComment) - case "issues": - for _, tp := range evt.Acts["types"] { - switch tp { - case "assigned": - return string(webhook_module.HookEventIssueAssign) - case "milestoned": - return string(webhook_module.HookEventIssueMilestone) - case "labeled": - return string(webhook_module.HookEventIssueLabel) - } - } - return string(webhook_module.HookEventIssues) - case "pull_request", "pull_request_target": - for _, tp := range evt.Acts["types"] { - switch tp { - case "assigned": - return string(webhook_module.HookEventPullRequestAssign) - case "milestoned": - return string(webhook_module.HookEventPullRequestMilestone) - case "labeled": - return string(webhook_module.HookEventPullRequestLabel) - case "synchronize": - return string(webhook_module.HookEventPullRequestSync) - } - } + case "pull_request", "pull_request_target", "pull_request_review_comment", "pull_request_review": return string(webhook_module.HookEventPullRequest) - case "pull_request_comment": - return string(webhook_module.HookEventPullRequestComment) - case "pull_request_review_comment": - return string(webhook_module.HookEventPullRequestReviewComment) - case "push": - return string(webhook_module.HookEventPush) case "registry_package": return string(webhook_module.HookEventPackage) - case "release": - return string(webhook_module.HookEventRelease) - case "pull_request_review", "milestone", "label", "project", "project_card", "project_column": + case "create", "delete", "fork", "push", "issues", "issue_comment", "release", "pull_request_comment": fallthrough default: return evt.Name @@ -125,7 +85,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy continue } for _, evt := range events { - log.Trace("detect workflow %q for event %q matching %q", entry.Name(), evt.Name, triggedEvent) + log.Trace("detect workflow %q for event %#v matching %q", entry.Name(), evt, triggedEvent) if detectMatched(commit, triggedEvent, payload, evt) { workflows[entry.Name()] = content } @@ -199,8 +159,13 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload for cond, vals := range evt.Acts { switch cond { case "types": + action := prPayload.Action + if prPayload.Action == api.HookIssueSynchronized { + action = "synchronize" + } + log.Trace("matching pull_request %s with %v", action, vals) for _, val := range vals { - if glob.MustCompile(val, '/').Match(string(prPayload.Action)) { + if glob.MustCompile(val, '/').Match(string(action)) { matchTimes++ break } From d8402f5507443cbed27bdc09cef19e136c11f56c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 21 Feb 2023 15:44:02 +0800 Subject: [PATCH 3/9] Make review easier --- modules/actions/workflows.go | 98 ++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 589f38ff9090a..5734c39dfb3c2 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -95,6 +95,55 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy return workflows, nil } +func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { + if converGithubEvent2GiteaEvent(evt) != triggedEvent.Event() { + return false + } + + // with no special filter parameters + if len(evt.Acts) == 0 { + return true + } + + switch triggedEvent { + case webhook_module.HookEventCreate, + webhook_module.HookEventDelete, + webhook_module.HookEventFork, + webhook_module.HookEventIssueAssign, + webhook_module.HookEventIssueLabel, + webhook_module.HookEventIssueMilestone, + webhook_module.HookEventPullRequestAssign, + webhook_module.HookEventPullRequestLabel, + 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: + // no special filter parameters for these events, just return true if name matched + return true + + case webhook_module.HookEventPush: + return matchPushEvent(commit, payload.(*api.PushPayload), evt) + + case webhook_module.HookEventIssues: + return matchIssuesEvent(commit, payload.(*api.IssuePayload), evt) + + case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync: + return matchPullRequestEvent(commit, payload.(*api.PullRequestPayload), evt) + + case webhook_module.HookEventIssueComment: + return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) + + default: + log.Warn("unsupported event %q", triggedEvent.Event()) + return false + } +} + func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobparser.Event) bool { matchTimes := 0 // all acts conditions should be satisfied @@ -222,52 +271,3 @@ func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCo } return matchTimes == len(evt.Acts) } - -func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { - if converGithubEvent2GiteaEvent(evt) != triggedEvent.Event() { - return false - } - - // with no special filter parameters - if len(evt.Acts) == 0 { - return true - } - - switch triggedEvent { - case webhook_module.HookEventCreate, - webhook_module.HookEventDelete, - webhook_module.HookEventFork, - webhook_module.HookEventIssueAssign, - webhook_module.HookEventIssueLabel, - webhook_module.HookEventIssueMilestone, - webhook_module.HookEventPullRequestAssign, - webhook_module.HookEventPullRequestLabel, - 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: - // no special filter parameters for these events, just return true if name matched - return true - - case webhook_module.HookEventPush: - return matchPushEvent(commit, payload.(*api.PushPayload), evt) - - case webhook_module.HookEventIssues: - return matchIssuesEvent(commit, payload.(*api.IssuePayload), evt) - - case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync: - return matchPullRequestEvent(commit, payload.(*api.PullRequestPayload), evt) - - case webhook_module.HookEventIssueComment: - return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) - - default: - log.Warn("unsupported event %q", triggedEvent.Event()) - return false - } -} From fa063d0db59125e8e63fadb4a3644f283d273ae6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 7 Mar 2023 12:05:06 +0800 Subject: [PATCH 4/9] Follow reviewers' suggestion --- modules/actions/github.go | 40 ++++++++++++++++++++++++++++++++++++ modules/actions/workflows.go | 15 +------------- 2 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 modules/actions/github.go diff --git a/modules/actions/github.go b/modules/actions/github.go new file mode 100644 index 0000000000000..e969583713435 --- /dev/null +++ b/modules/actions/github.go @@ -0,0 +1,40 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package actions + +import ( + webhook_module "code.gitea.io/gitea/modules/webhook" + "github.com/nektos/act/pkg/jobparser" +) + +const ( + githubEventPullRequest = "pull_request" + githubEventPullRequestTarget = "pull_request_target" + githubEventPullRequestReviewComment = "pull_request_review_comment" + githubEventPullRequestReview = "pull_request_review" + githubEventRegistryPackage = "registry_package" + githubEventCreate = "create" + githubEventDelete = "delete" + githubEventFork = "fork" + githubEventPush = "push" + githubEventIssues = "issues" + githubEventIssueComment = "issue_comment" + githubEventRelease = "release" + githubEventPullRequestComment = "pull_request_comment" +) + +func converFromGithubEvent(evt *jobparser.Event) string { + switch evt.Name { + case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview, + githubEventPullRequestReviewComment: + return string(webhook_module.HookEventPullRequest) + case githubEventRegistryPackage: + return string(webhook_module.HookEventPackage) + case githubEventCreate, githubEventDelete, githubEventFork, githubEventPush, + githubEventIssues, githubEventIssueComment, githubEventRelease, githubEventPullRequestComment: + fallthrough + default: + return evt.Name + } +} diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 5734c39dfb3c2..794e0b978b2d6 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -44,19 +44,6 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) { return ret, nil } -func converGithubEvent2GiteaEvent(evt *jobparser.Event) string { - switch evt.Name { - case "pull_request", "pull_request_target", "pull_request_review_comment", "pull_request_review": - return string(webhook_module.HookEventPullRequest) - case "registry_package": - return string(webhook_module.HookEventPackage) - case "create", "delete", "fork", "push", "issues", "issue_comment", "release", "pull_request_comment": - fallthrough - default: - return evt.Name - } -} - func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader) (map[string][]byte, error) { entries, err := ListWorkflows(commit) if err != nil { @@ -96,7 +83,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy } func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { - if converGithubEvent2GiteaEvent(evt) != triggedEvent.Event() { + if converFromGithubEvent(evt) != triggedEvent.Event() { return false } From 93bdf5603a0dd9791c9f29fddb407252145433e1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 7 Mar 2023 15:50:23 +0800 Subject: [PATCH 5/9] Fix check --- modules/actions/github.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/actions/github.go b/modules/actions/github.go index e969583713435..8ede4d912bc53 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -5,6 +5,7 @@ package actions import ( webhook_module "code.gitea.io/gitea/modules/webhook" + "github.com/nektos/act/pkg/jobparser" ) From bcd05dca2201a3eed78d79d1226913694c511eee Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 7 Mar 2023 20:51:15 +0800 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: yp05327 <576951401@qq.com> --- modules/actions/github.go | 2 +- modules/actions/workflows.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index 8ede4d912bc53..bcde9a0f552ec 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -25,7 +25,7 @@ const ( githubEventPullRequestComment = "pull_request_comment" ) -func converFromGithubEvent(evt *jobparser.Event) string { +func convertFromGithubEvent(evt *jobparser.Event) string { switch evt.Name { case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview, githubEventPullRequestReviewComment: diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 794e0b978b2d6..65615d8571ba2 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -83,7 +83,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy } func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { - if converFromGithubEvent(evt) != triggedEvent.Event() { + if convertFromGithubEvent(evt) != triggedEvent.Event() { return false } From 6000d2c93877cf324fac63f2943f4adff19a77a6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 14 Mar 2023 17:01:12 +0800 Subject: [PATCH 7/9] Fix event match bug --- 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 794e0b978b2d6..d846a9fb2d641 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -83,7 +83,7 @@ func DetectWorkflows(commit *git.Commit, triggedEvent webhook_module.HookEventTy } func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType, payload api.Payloader, evt *jobparser.Event) bool { - if converFromGithubEvent(evt) != triggedEvent.Event() { + if converFromGithubEvent(evt) != string(triggedEvent) { return false } From ffa35719100c68c08ade6682808cc04d0e115a16 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 14 Mar 2023 17:36:20 +0800 Subject: [PATCH 8/9] don't trigger event when pull request close --- modules/actions/workflows.go | 39 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index f4f679fc4567c..529e5b9c2ea38 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -103,11 +103,6 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType return false } - // with no special filter parameters - if len(evt.Acts) == 0 { - return true - } - switch triggedEvent { case webhook_module.HookEventCreate, webhook_module.HookEventDelete, @@ -126,6 +121,9 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType webhook_module.HookEventRepository, webhook_module.HookEventRelease, webhook_module.HookEventPackage: + if len(evt.Acts) != 0 { + log.Warn("Ignore unsupported %s event arguments %q", triggedEvent, evt.Acts) + } // no special filter parameters for these events, just return true if name matched return true @@ -142,12 +140,17 @@ func detectMatched(commit *git.Commit, triggedEvent webhook_module.HookEventType return matchIssueCommentEvent(commit, payload.(*api.IssueCommentPayload), evt) default: - log.Warn("unsupported event %q", triggedEvent.Event()) + log.Warn("unsupported event %q", triggedEvent) return false } } func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, 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 { @@ -180,13 +183,18 @@ func matchPushEvent(commit *git.Commit, pushPayload *api.PushPayload, evt *jobpa } } default: - log.Warn("unsupported condition %q", cond) + log.Warn("push event unsupported condition %q", cond) } } return matchTimes == len(evt.Acts) } func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, 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 { @@ -199,13 +207,19 @@ func matchIssuesEvent(commit *git.Commit, issuePayload *api.IssuePayload, evt *j } } default: - log.Warn("unsupported condition %q", cond) + log.Warn("issue event unsupported condition %q", cond) } } return matchTimes == len(evt.Acts) } func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload, evt *jobparser.Event) bool { + // with no special filter parameters + if len(evt.Acts) == 0 { + // defautly, only pull request opened and synchronized will not trigger workflow + return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened + } + matchTimes := 0 // all acts conditions should be satisfied for cond, vals := range evt.Acts { @@ -250,13 +264,18 @@ func matchPullRequestEvent(commit *git.Commit, prPayload *api.PullRequestPayload } } default: - log.Warn("unsupported condition %q", cond) + log.Warn("pull request event unsupported condition %q", cond) } } return matchTimes == len(evt.Acts) } func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCommentPayload, 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 { @@ -269,7 +288,7 @@ func matchIssueCommentEvent(commit *git.Commit, issueCommentPayload *api.IssueCo } } default: - log.Warn("unsupported condition %q", cond) + log.Warn("issue comment unsupported condition %q", cond) } } return matchTimes == len(evt.Acts) From 7616b679cf6f2529cf68553cb3495f1335d249a9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 14 Mar 2023 20:10:19 +0800 Subject: [PATCH 9/9] Update modules/actions/workflows.go Co-authored-by: yp05327 <576951401@qq.com> --- 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 529e5b9c2ea38..67c3b12427adb 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -216,7 +216,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 { - // defautly, only pull request opened and synchronized will not trigger workflow + // defaultly, only pull request opened and synchronized will trigger workflow return prPayload.Action == api.HookIssueSynchronized || prPayload.Action == api.HookIssueOpened }