1// Copyright 2023 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package actions
5
6import (
7 "testing"
8
9 "forgejo.org/modules/git"
10 api "forgejo.org/modules/structs"
11 webhook_module "forgejo.org/modules/webhook"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15)
16
17func TestDetectMatched(t *testing.T) {
18 testCases := []struct {
19 desc string
20 commit *git.Commit
21 triggeredEvent webhook_module.HookEventType
22 payload api.Payloader
23 yamlOn string
24 expected bool
25 }{
26 {
27 desc: "HookEventCreate(create) matches GithubEventCreate(create)",
28 triggeredEvent: webhook_module.HookEventCreate,
29 payload: nil,
30 yamlOn: "on: create",
31 expected: true,
32 },
33 {
34 desc: "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)",
35 triggeredEvent: webhook_module.HookEventIssues,
36 payload: &api.IssuePayload{Action: api.HookIssueOpened},
37 yamlOn: "on: issues",
38 expected: true,
39 },
40 {
41 desc: "HookEventIssueComment(issue_comment) `created` action matches GithubEventIssueComment(issue_comment)",
42 triggeredEvent: webhook_module.HookEventIssueComment,
43 payload: &api.IssueCommentPayload{Action: api.HookIssueCommentCreated},
44 yamlOn: "on:\n issue_comment:\n types: [created]",
45 expected: true,
46 },
47
48 {
49 desc: "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)",
50 triggeredEvent: webhook_module.HookEventIssues,
51 payload: &api.IssuePayload{Action: api.HookIssueMilestoned},
52 yamlOn: "on: issues",
53 expected: true,
54 },
55
56 {
57 desc: "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)",
58 triggeredEvent: webhook_module.HookEventPullRequestSync,
59 payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized},
60 yamlOn: "on: pull_request",
61 expected: true,
62 },
63 {
64 desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
65 triggeredEvent: webhook_module.HookEventPullRequest,
66 payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
67 yamlOn: "on: pull_request",
68 expected: false,
69 },
70 {
71 desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
72 triggeredEvent: webhook_module.HookEventPullRequest,
73 payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
74 yamlOn: "on: pull_request",
75 expected: false,
76 },
77 {
78 desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
79 triggeredEvent: webhook_module.HookEventPullRequest,
80 payload: &api.PullRequestPayload{
81 Action: api.HookIssueClosed,
82 PullRequest: &api.PullRequest{
83 Base: &api.PRBranchInfo{},
84 },
85 },
86 yamlOn: "on:\n pull_request:\n branches: [main]",
87 expected: false,
88 },
89 {
90 desc: "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
91 triggeredEvent: webhook_module.HookEventPullRequest,
92 payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
93 yamlOn: "on:\n pull_request:\n types: [labeled]",
94 expected: true,
95 },
96 {
97 desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)",
98 triggeredEvent: webhook_module.HookEventPullRequestReviewComment,
99 payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
100 yamlOn: "on:\n pull_request_review_comment:\n types: [created]",
101 expected: true,
102 },
103 {
104 desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)",
105 triggeredEvent: webhook_module.HookEventPullRequestReviewRejected,
106 payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
107 yamlOn: "on:\n pull_request_review:\n types: [dismissed]",
108 expected: false,
109 },
110 {
111 desc: "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type",
112 triggeredEvent: webhook_module.HookEventRelease,
113 payload: &api.ReleasePayload{Action: api.HookReleasePublished},
114 yamlOn: "on:\n release:\n types: [published]",
115 expected: true,
116 },
117 {
118 desc: "HookEventRelease(updated) `updated` action matches GithubEventRelease(edited) with `edited` activity type",
119 triggeredEvent: webhook_module.HookEventRelease,
120 payload: &api.ReleasePayload{Action: api.HookReleaseUpdated},
121 yamlOn: "on:\n release:\n types: [edited]",
122 expected: true,
123 },
124
125 {
126 desc: "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type",
127 triggeredEvent: webhook_module.HookEventPackage,
128 payload: &api.PackagePayload{Action: api.HookPackageCreated},
129 yamlOn: "on:\n registry_package:\n types: [updated]",
130 expected: false,
131 },
132 {
133 desc: "HookEventWiki(wiki) matches GithubEventGollum(gollum)",
134 triggeredEvent: webhook_module.HookEventWiki,
135 payload: nil,
136 yamlOn: "on: gollum",
137 expected: true,
138 },
139 {
140 desc: "HookEventSchedule(schedule) matches GithubEventSchedule(schedule)",
141 triggeredEvent: webhook_module.HookEventSchedule,
142 payload: nil,
143 yamlOn: "on: schedule",
144 expected: true,
145 },
146 {
147 desc: "HookEventWorkflowDispatch(workflow_dispatch) matches GithubEventWorkflowDispatch(workflow_dispatch)",
148 triggeredEvent: webhook_module.HookEventWorkflowDispatch,
149 payload: nil,
150 yamlOn: "on: workflow_dispatch",
151 expected: true,
152 },
153 }
154
155 for _, tc := range testCases {
156 t.Run(tc.desc, func(t *testing.T) {
157 evts, err := GetEventsFromContent([]byte(tc.yamlOn))
158 require.NoError(t, err)
159 assert.Len(t, evts, 1)
160 assert.Equal(t, tc.expected, detectMatched(nil, tc.commit, tc.triggeredEvent, tc.payload, evts[0]))
161 })
162 }
163}