1// Copyright 2023 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package actions
5
6import (
7 "testing"
8
9 webhook_module "forgejo.org/modules/webhook"
10
11 "github.com/stretchr/testify/assert"
12)
13
14func TestCanGithubEventMatch(t *testing.T) {
15 testCases := []struct {
16 desc string
17 eventName string
18 triggeredEvent webhook_module.HookEventType
19 expected bool
20 }{
21 // registry_package event
22 {
23 "registry_package matches",
24 GithubEventRegistryPackage,
25 webhook_module.HookEventPackage,
26 true,
27 },
28 {
29 "registry_package cannot match",
30 GithubEventRegistryPackage,
31 webhook_module.HookEventPush,
32 false,
33 },
34 // issues event
35 {
36 "issue matches",
37 GithubEventIssues,
38 webhook_module.HookEventIssueLabel,
39 true,
40 },
41 {
42 "issue cannot match",
43 GithubEventIssues,
44 webhook_module.HookEventIssueComment,
45 false,
46 },
47 // issue_comment event
48 {
49 "issue_comment matches",
50 GithubEventIssueComment,
51 webhook_module.HookEventIssueComment,
52 true,
53 },
54 {
55 "issue_comment cannot match",
56 GithubEventIssueComment,
57 webhook_module.HookEventIssues,
58 false,
59 },
60 // pull_request event
61 {
62 "pull_request matches",
63 GithubEventPullRequest,
64 webhook_module.HookEventPullRequestSync,
65 true,
66 },
67 {
68 "pull_request cannot match",
69 GithubEventPullRequest,
70 webhook_module.HookEventPullRequestComment,
71 false,
72 },
73 // pull_request_target event
74 {
75 "pull_request_target matches",
76 GithubEventPullRequest,
77 webhook_module.HookEventPullRequest,
78 true,
79 },
80 {
81 "pull_request_target cannot match",
82 GithubEventPullRequest,
83 webhook_module.HookEventPullRequestComment,
84 false,
85 },
86 // pull_request_review event
87 {
88 "pull_request_review matches",
89 GithubEventPullRequestReview,
90 webhook_module.HookEventPullRequestReviewComment,
91 true,
92 },
93 {
94 "pull_request_review cannot match",
95 GithubEventPullRequestReview,
96 webhook_module.HookEventPullRequestComment,
97 false,
98 },
99 // other events
100 {
101 "create event",
102 GithubEventCreate,
103 webhook_module.HookEventCreate,
104 true,
105 },
106 {
107 "create pull request comment",
108 GithubEventIssueComment,
109 webhook_module.HookEventPullRequestComment,
110 true,
111 },
112 }
113
114 for _, tc := range testCases {
115 t.Run(tc.desc, func(t *testing.T) {
116 assert.Equalf(t, tc.expected, canGithubEventMatch(tc.eventName, tc.triggeredEvent), "canGithubEventMatch(%v, %v)", tc.eventName, tc.triggeredEvent)
117 })
118 }
119}