]> source.dussan.org Git - gitea.git/commitdiff
Improve workflow event triggers (#23613) (#23648)
authorGiteabot <teabot@gitea.io>
Thu, 23 Mar 2023 08:34:05 +0000 (04:34 -0400)
committerGitHub <noreply@github.com>
Thu, 23 Mar 2023 08:34:05 +0000 (16:34 +0800)
modules/actions/github.go
modules/actions/github_test.go [new file with mode: 0644]
modules/actions/workflows.go

index bcde9a0f552ec76eea77d7f0f7083fedcf689874..1148554139cd103d8633eebadd90020640f7b988 100644 (file)
@@ -5,8 +5,6 @@ package actions
 
 import (
        webhook_module "code.gitea.io/gitea/modules/webhook"
-
-       "github.com/nektos/act/pkg/jobparser"
 )
 
 const (
@@ -25,17 +23,48 @@ const (
        githubEventPullRequestComment       = "pull_request_comment"
 )
 
-func convertFromGithubEvent(evt *jobparser.Event) string {
-       switch evt.Name {
-       case githubEventPullRequest, githubEventPullRequestTarget, githubEventPullRequestReview,
-               githubEventPullRequestReviewComment:
-               return string(webhook_module.HookEventPullRequest)
+// canGithubEventMatch check if the input Github event can match any Gitea event.
+func canGithubEventMatch(eventName string, triggedEvent webhook_module.HookEventType) bool {
+       switch eventName {
        case githubEventRegistryPackage:
-               return string(webhook_module.HookEventPackage)
-       case githubEventCreate, githubEventDelete, githubEventFork, githubEventPush,
-               githubEventIssues, githubEventIssueComment, githubEventRelease, githubEventPullRequestComment:
-               fallthrough
+               return triggedEvent == webhook_module.HookEventPackage
+
+       case githubEventIssues:
+               switch triggedEvent {
+               case webhook_module.HookEventIssues,
+                       webhook_module.HookEventIssueAssign,
+                       webhook_module.HookEventIssueLabel,
+                       webhook_module.HookEventIssueMilestone:
+                       return true
+
+               default:
+                       return false
+               }
+
+       case githubEventPullRequest, githubEventPullRequestTarget:
+               switch triggedEvent {
+               case webhook_module.HookEventPullRequest,
+                       webhook_module.HookEventPullRequestSync,
+                       webhook_module.HookEventPullRequestAssign,
+                       webhook_module.HookEventPullRequestLabel:
+                       return true
+
+               default:
+                       return false
+               }
+
+       case githubEventPullRequestReview:
+               switch triggedEvent {
+               case webhook_module.HookEventPullRequestReviewApproved,
+                       webhook_module.HookEventPullRequestReviewComment,
+                       webhook_module.HookEventPullRequestReviewRejected:
+                       return true
+
+               default:
+                       return false
+               }
+
        default:
-               return evt.Name
+               return eventName == string(triggedEvent)
        }
 }
diff --git a/modules/actions/github_test.go b/modules/actions/github_test.go
new file mode 100644 (file)
index 0000000..e7f4158
--- /dev/null
@@ -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)
+               })
+       }
+}
index 6f4d98d52d3983c59d00f6bb2602bd820bfbbbd5..7ce05582ea04e932aea53e391ccfb2aa7979e862 100644 (file)
@@ -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 convertFromGithubEvent(evt) != string(triggedEvent) {
+       if !canGithubEventMatch(evt.Name, triggedEvent) {
                return false
        }