From ca58ecd8f4414da9f987c0eff737d77c3b73cadd Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 21 Mar 2023 18:26:22 +0800 Subject: [PATCH 1/5] add canGithubEventMatch --- modules/actions/github.go | 124 +++++++++++++++++++++++++++++++++++ modules/actions/workflows.go | 2 +- 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index bcde9a0f552ec..cb2d78fcf50e0 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -4,6 +4,7 @@ package actions import ( + "code.gitea.io/gitea/modules/util" webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/nektos/act/pkg/jobparser" @@ -25,6 +26,25 @@ const ( githubEventPullRequestComment = "pull_request_comment" ) +const ( + githubActivityTypeOpened = "opened" + githubActivityTypeClosed = "closed" + githubActivityTypeReopened = "reopened" + githubActivityTypeEdited = "edited" + githubActivityTypeAssigned = "assigned" + githubActivityTypeUnassigned = "unassigned" + githubActivityTypeLabeled = "labeled" + githubActivityTypeUnlabeled = "unlabeled" + githubActivityTypeMilestoned = "milestoned" + githubActivityTypeDemilestoned = "demilestoned" + + githubActivityTypeSynchronize = "synchronize" + + githubActivityTypePublished = "published" + githubActivityTypeCreated = "created" + githubActivityTypeDeleted = "deleted" +) + func convertFromGithubEvent(evt *jobparser.Event) string { switch evt.Name { case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview, @@ -39,3 +59,107 @@ func convertFromGithubEvent(evt *jobparser.Event) string { return evt.Name } } + +// canGithubEventMatch check if the input Github event can match any Gitea event. +func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookEventType) bool { + switch evt.Name { + case githubEventCreate: + return triggedEvent == webhook_module.HookEventCreate + case githubEventDelete: + return triggedEvent == webhook_module.HookEventDelete + case githubEventFork: + return triggedEvent == webhook_module.HookEventFork + case githubEventPush: + return triggedEvent == webhook_module.HookEventPush + case githubEventRelease: + return triggedEvent == webhook_module.HookEventRelease + + case githubEventIssues: + // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues + if isEventActsTypesEmpty(evt.Acts) { + return util.SliceContains([]webhook_module.HookEventType{ + webhook_module.HookEventIssues, + webhook_module.HookEventIssueAssign, + webhook_module.HookEventIssueLabel, + webhook_module.HookEventIssueMilestone, + }, triggedEvent) + } + switch triggedEvent { + case webhook_module.HookEventIssues: + return matchByActivityTypes(evt.Acts, githubActivityTypeEdited, githubActivityTypeOpened, githubActivityTypeClosed, githubActivityTypeReopened) + case webhook_module.HookEventIssueAssign: + return matchByActivityTypes(evt.Acts, githubActivityTypeAssigned, githubActivityTypeUnassigned) + case webhook_module.HookEventIssueLabel: + return matchByActivityTypes(evt.Acts, githubActivityTypeLabeled, githubActivityTypeUnlabeled) + case webhook_module.HookEventIssueMilestone: + return matchByActivityTypes(evt.Acts, githubActivityTypeMilestoned, githubActivityTypeDemilestoned) + } + return false + + case githubEventIssueComment: + return triggedEvent == webhook_module.HookEventIssueComment + + case githubEventPullRequest: + // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request + if isEventActsTypesEmpty(evt.Acts) { + return util.SliceContains([]webhook_module.HookEventType{ + webhook_module.HookEventPullRequest, + webhook_module.HookEventPullRequestSync, + }, triggedEvent) + } + switch triggedEvent { + case webhook_module.HookEventPullRequest: + return matchByActivityTypes(evt.Acts, githubActivityTypeEdited, githubActivityTypeOpened, githubActivityTypeClosed, githubActivityTypeReopened) + case webhook_module.HookEventPullRequestAssign: + return matchByActivityTypes(evt.Acts, githubActivityTypeAssigned, githubActivityTypeUnassigned) + case webhook_module.HookEventPullRequestLabel: + return matchByActivityTypes(evt.Acts, githubActivityTypeLabeled, githubActivityTypeUnlabeled) + case webhook_module.HookEventPullRequestSync: + return matchByActivityTypes(evt.Acts, githubActivityTypeSynchronize) + } + return false + + case githubEventPullRequestComment: + return triggedEvent == webhook_module.HookEventPullRequestComment + + case githubEventPullRequestReview: + return util.SliceContains([]webhook_module.HookEventType{ + webhook_module.HookEventPullRequestReviewApproved, + webhook_module.HookEventPullRequestReviewComment, + webhook_module.HookEventPullRequestReviewRejected, + }, triggedEvent) + + case githubEventPullRequestReviewComment: + // TODO + + case githubEventPullRequestTarget: + // TODO + + default: + return false + } + + return false +} + +func isEventActsTypesEmpty(evtActs map[string][]string) bool { + if len(evtActs) == 0 || len(evtActs["types"]) == 0 { + return true + } + return false +} + +func matchByActivityTypes(evtActs map[string][]string, actTypes ...string) bool { + if isEventActsTypesEmpty(evtActs) { + return false + } + + evtActTypes := evtActs["types"] + for _, actType := range actTypes { + if util.SliceContainsString(evtActTypes, actType) { + return true + } + } + + return false +} diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 67c3b12427adb..0d7c82cb909d5 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -99,7 +99,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 convertFromGithubEvent(evt) != string(triggedEvent) { + if !canGithubEventMatch(evt, triggedEvent) { return false } From f698417fa01acc7b3c751a94cdff4e8164b2b461 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 21 Mar 2023 18:50:00 +0800 Subject: [PATCH 2/5] remove unnecessary code --- modules/actions/github.go | 62 +-------------------------------------- 1 file changed, 1 insertion(+), 61 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index cb2d78fcf50e0..e7bbc521a9fdf 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -74,72 +74,12 @@ func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookE case githubEventRelease: return triggedEvent == webhook_module.HookEventRelease - case githubEventIssues: - // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#issues - if isEventActsTypesEmpty(evt.Acts) { - return util.SliceContains([]webhook_module.HookEventType{ - webhook_module.HookEventIssues, - webhook_module.HookEventIssueAssign, - webhook_module.HookEventIssueLabel, - webhook_module.HookEventIssueMilestone, - }, triggedEvent) - } - switch triggedEvent { - case webhook_module.HookEventIssues: - return matchByActivityTypes(evt.Acts, githubActivityTypeEdited, githubActivityTypeOpened, githubActivityTypeClosed, githubActivityTypeReopened) - case webhook_module.HookEventIssueAssign: - return matchByActivityTypes(evt.Acts, githubActivityTypeAssigned, githubActivityTypeUnassigned) - case webhook_module.HookEventIssueLabel: - return matchByActivityTypes(evt.Acts, githubActivityTypeLabeled, githubActivityTypeUnlabeled) - case webhook_module.HookEventIssueMilestone: - return matchByActivityTypes(evt.Acts, githubActivityTypeMilestoned, githubActivityTypeDemilestoned) - } - return false - - case githubEventIssueComment: - return triggedEvent == webhook_module.HookEventIssueComment - - case githubEventPullRequest: - // https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request - if isEventActsTypesEmpty(evt.Acts) { - return util.SliceContains([]webhook_module.HookEventType{ - webhook_module.HookEventPullRequest, - webhook_module.HookEventPullRequestSync, - }, triggedEvent) - } - switch triggedEvent { - case webhook_module.HookEventPullRequest: - return matchByActivityTypes(evt.Acts, githubActivityTypeEdited, githubActivityTypeOpened, githubActivityTypeClosed, githubActivityTypeReopened) - case webhook_module.HookEventPullRequestAssign: - return matchByActivityTypes(evt.Acts, githubActivityTypeAssigned, githubActivityTypeUnassigned) - case webhook_module.HookEventPullRequestLabel: - return matchByActivityTypes(evt.Acts, githubActivityTypeLabeled, githubActivityTypeUnlabeled) - case webhook_module.HookEventPullRequestSync: - return matchByActivityTypes(evt.Acts, githubActivityTypeSynchronize) - } - return false - - case githubEventPullRequestComment: - return triggedEvent == webhook_module.HookEventPullRequestComment - - case githubEventPullRequestReview: - return util.SliceContains([]webhook_module.HookEventType{ - webhook_module.HookEventPullRequestReviewApproved, - webhook_module.HookEventPullRequestReviewComment, - webhook_module.HookEventPullRequestReviewRejected, - }, triggedEvent) - - case githubEventPullRequestReviewComment: - // TODO - - case githubEventPullRequestTarget: - // TODO + // TODO: handle rest events default: return false } - return false } func isEventActsTypesEmpty(evtActs map[string][]string) bool { From 91a5fbc1ecd740c3405b14044dc201a77dbb0aa2 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 22 Mar 2023 11:35:00 +0800 Subject: [PATCH 3/5] replace convertFromGithubEvent with canGithubEventMatch --- modules/actions/github.go | 91 ++++++++++++++------------------------- 1 file changed, 33 insertions(+), 58 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index e7bbc521a9fdf..69eff5c338fac 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -26,40 +26,6 @@ const ( githubEventPullRequestComment = "pull_request_comment" ) -const ( - githubActivityTypeOpened = "opened" - githubActivityTypeClosed = "closed" - githubActivityTypeReopened = "reopened" - githubActivityTypeEdited = "edited" - githubActivityTypeAssigned = "assigned" - githubActivityTypeUnassigned = "unassigned" - githubActivityTypeLabeled = "labeled" - githubActivityTypeUnlabeled = "unlabeled" - githubActivityTypeMilestoned = "milestoned" - githubActivityTypeDemilestoned = "demilestoned" - - githubActivityTypeSynchronize = "synchronize" - - githubActivityTypePublished = "published" - githubActivityTypeCreated = "created" - githubActivityTypeDeleted = "deleted" -) - -func convertFromGithubEvent(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 - } -} - // canGithubEventMatch check if the input Github event can match any Gitea event. func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookEventType) bool { switch evt.Name { @@ -71,35 +37,44 @@ func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookE return triggedEvent == webhook_module.HookEventFork case githubEventPush: return triggedEvent == webhook_module.HookEventPush + case githubEventRegistryPackage: + return triggedEvent == webhook_module.HookEventPackage case githubEventRelease: return triggedEvent == webhook_module.HookEventRelease - // TODO: handle rest events + case githubEventIssues: + return util.SliceContains([]webhook_module.HookEventType{ + webhook_module.HookEventIssues, + webhook_module.HookEventIssueAssign, + webhook_module.HookEventIssueLabel, + webhook_module.HookEventIssueMilestone, + }, triggedEvent) + + case githubEventIssueComment: + return triggedEvent == webhook_module.HookEventIssueComment + + case githubEventPullRequest, githubEventPullRequestTarget: + return util.SliceContains([]webhook_module.HookEventType{ + webhook_module.HookEventPullRequest, + webhook_module.HookEventPullRequestSync, + webhook_module.HookEventPullRequestAssign, + webhook_module.HookEventPullRequestLabel, + }, triggedEvent) + + case githubEventPullRequestComment: + return triggedEvent == webhook_module.HookEventPullRequestComment + + case githubEventPullRequestReview: + return util.SliceContains([]webhook_module.HookEventType{ + webhook_module.HookEventPullRequestReviewApproved, + webhook_module.HookEventPullRequestComment, + webhook_module.HookEventPullRequestReviewRejected, + }, triggedEvent) + + case githubEventPullRequestReviewComment: + return triggedEvent == webhook_module.HookEventPullRequestComment default: return false } - -} - -func isEventActsTypesEmpty(evtActs map[string][]string) bool { - if len(evtActs) == 0 || len(evtActs["types"]) == 0 { - return true - } - return false -} - -func matchByActivityTypes(evtActs map[string][]string, actTypes ...string) bool { - if isEventActsTypesEmpty(evtActs) { - return false - } - - evtActTypes := evtActs["types"] - for _, actType := range actTypes { - if util.SliceContainsString(evtActTypes, actType) { - return true - } - } - - return false } From 9ad11e16811d0e493c777fc4c02502b49cce693b Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 22 Mar 2023 11:48:09 +0800 Subject: [PATCH 4/5] improve canGithubEventMatch --- modules/actions/github.go | 54 +++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/modules/actions/github.go b/modules/actions/github.go index 69eff5c338fac..88dd7e44256ae 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -4,7 +4,6 @@ package actions import ( - "code.gitea.io/gitea/modules/util" webhook_module "code.gitea.io/gitea/modules/webhook" "github.com/nektos/act/pkg/jobparser" @@ -29,52 +28,45 @@ const ( // canGithubEventMatch check if the input Github event can match any Gitea event. func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookEventType) bool { switch evt.Name { - case githubEventCreate: - return triggedEvent == webhook_module.HookEventCreate - case githubEventDelete: - return triggedEvent == webhook_module.HookEventDelete - case githubEventFork: - return triggedEvent == webhook_module.HookEventFork - case githubEventPush: - return triggedEvent == webhook_module.HookEventPush case githubEventRegistryPackage: return triggedEvent == webhook_module.HookEventPackage - case githubEventRelease: - return triggedEvent == webhook_module.HookEventRelease case githubEventIssues: - return util.SliceContains([]webhook_module.HookEventType{ - webhook_module.HookEventIssues, + switch triggedEvent { + case webhook_module.HookEventIssues, webhook_module.HookEventIssueAssign, webhook_module.HookEventIssueLabel, - webhook_module.HookEventIssueMilestone, - }, triggedEvent) + webhook_module.HookEventIssueMilestone: + return true - case githubEventIssueComment: - return triggedEvent == webhook_module.HookEventIssueComment + default: + return false + } case githubEventPullRequest, githubEventPullRequestTarget: - return util.SliceContains([]webhook_module.HookEventType{ - webhook_module.HookEventPullRequest, + switch triggedEvent { + case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestAssign, - webhook_module.HookEventPullRequestLabel, - }, triggedEvent) + webhook_module.HookEventPullRequestLabel: + return true - case githubEventPullRequestComment: - return triggedEvent == webhook_module.HookEventPullRequestComment + default: + return false + } case githubEventPullRequestReview: - return util.SliceContains([]webhook_module.HookEventType{ - webhook_module.HookEventPullRequestReviewApproved, - webhook_module.HookEventPullRequestComment, - webhook_module.HookEventPullRequestReviewRejected, - }, triggedEvent) + switch triggedEvent { + case webhook_module.HookEventPullRequestReviewApproved, + webhook_module.HookEventPullRequestReviewComment, + webhook_module.HookEventPullRequestReviewRejected: + return true - case githubEventPullRequestReviewComment: - return triggedEvent == webhook_module.HookEventPullRequestComment + default: + return false + } default: - return false + return evt.Name == string(triggedEvent) } } From a50fdc0f8096e78c2c937aaf51e4f9a747797084 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Thu, 23 Mar 2023 10:49:51 +0800 Subject: [PATCH 5/5] add unit tests --- modules/actions/github.go | 8 +-- modules/actions/github_test.go | 113 +++++++++++++++++++++++++++++++++ modules/actions/workflows.go | 2 +- 3 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 modules/actions/github_test.go diff --git a/modules/actions/github.go b/modules/actions/github.go index 88dd7e44256ae..1148554139cd1 100644 --- a/modules/actions/github.go +++ b/modules/actions/github.go @@ -5,8 +5,6 @@ package actions import ( webhook_module "code.gitea.io/gitea/modules/webhook" - - "github.com/nektos/act/pkg/jobparser" ) const ( @@ -26,8 +24,8 @@ const ( ) // canGithubEventMatch check if the input Github event can match any Gitea event. -func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookEventType) bool { - switch evt.Name { +func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool { + switch eventName { case githubEventRegistryPackage: return triggedEvent == webhook_module.HookEventPackage @@ -67,6 +65,6 @@ func canGithubEventMatch(evt *jobparser.Event, triggedEvent webhook_module.HookE } default: - return evt.Name == string(triggedEvent) + return eventName == string(triggedEvent) } } diff --git a/modules/actions/github_test.go b/modules/actions/github_test.go new file mode 100644 index 0000000000000..e7f4158ae2d1e --- /dev/null +++ b/modules/actions/github_test.go @@ -0,0 +1,113 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package actions + +import ( + "testing" + + webhook_module "code.gitea.io/gitea/modules/webhook" + + "github.com/stretchr/testify/assert" +) + +func TestCanGithubEventMatch(t *testing.T) { + testCases := []struct { + desc string + eventName string + triggeredEvent webhook_module.HookEventType + expected bool + }{ + // registry_package event + { + "registry_package matches", + githubEventRegistryPackage, + webhook_module.HookEventPackage, + true, + }, + { + "registry_package cannot match", + githubEventRegistryPackage, + webhook_module.HookEventPush, + false, + }, + // issues event + { + "issue matches", + githubEventIssues, + webhook_module.HookEventIssueLabel, + true, + }, + { + "issue cannot match", + githubEventIssues, + webhook_module.HookEventIssueComment, + false, + }, + // issue_comment event + { + "issue_comment matches", + githubEventIssueComment, + webhook_module.HookEventIssueComment, + true, + }, + { + "issue_comment cannot match", + githubEventIssueComment, + webhook_module.HookEventIssues, + false, + }, + // pull_request event + { + "pull_request matches", + githubEventPullRequest, + webhook_module.HookEventPullRequestSync, + true, + }, + { + "pull_request cannot match", + githubEventPullRequest, + webhook_module.HookEventPullRequestComment, + false, + }, + // pull_request_target event + { + "pull_request_target matches", + githubEventPullRequest, + webhook_module.HookEventPullRequest, + true, + }, + { + "pull_request_target cannot match", + githubEventPullRequest, + webhook_module.HookEventPullRequestComment, + false, + }, + // pull_request_review event + { + "pull_request_review matches", + githubEventPullRequestReview, + webhook_module.HookEventPullRequestReviewComment, + true, + }, + { + "pull_request_review cannot match", + githubEventPullRequestReview, + webhook_module.HookEventPullRequestComment, + false, + }, + // other events + { + "create event", + githubEventCreate, + webhook_module.HookEventCreate, + true, + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + assert.Equalf(t, tc.expected, canGithubEventMatch(tc.eventName, tc.triggeredEvent), "canGithubEventMatch(%v, %v)", tc.eventName, tc.triggeredEvent) + }) + } +} diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 0d7c82cb909d5..738026142b94f 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -99,7 +99,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 !canGithubEventMatch(evt, triggedEvent) { + if !canGithubEventMatch(evt.Name, triggedEvent) { return false }