summaryrefslogtreecommitdiffstats
path: root/modules/actions/workflows_test.go
blob: 965d01f134ed2b6514feaef2e654ad7392c37303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
	"testing"

	"code.gitea.io/gitea/modules/git"
	api "code.gitea.io/gitea/modules/structs"
	webhook_module "code.gitea.io/gitea/modules/webhook"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestDetectMatched(t *testing.T) {
	testCases := []struct {
		desc           string
		commit         *git.Commit
		triggeredEvent webhook_module.HookEventType
		payload        api.Payloader
		yamlOn         string
		expected       bool
	}{
		{
			desc:           "HookEventCreate(create) matches GithubEventCreate(create)",
			triggeredEvent: webhook_module.HookEventCreate,
			payload:        nil,
			yamlOn:         "on: create",
			expected:       true,
		},
		{
			desc:           "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)",
			triggeredEvent: webhook_module.HookEventIssues,
			payload:        &api.IssuePayload{Action: api.HookIssueOpened},
			yamlOn:         "on: issues",
			expected:       true,
		},
		{
			desc:           "HookEventIssueComment(issue_comment) `created` action matches GithubEventIssueComment(issue_comment)",
			triggeredEvent: webhook_module.HookEventIssueComment,
			payload:        &api.IssueCommentPayload{Action: api.HookIssueCommentCreated},
			yamlOn:         "on:\n  issue_comment:\n    types: [created]",
			expected:       true,
		},

		{
			desc:           "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)",
			triggeredEvent: webhook_module.HookEventIssues,
			payload:        &api.IssuePayload{Action: api.HookIssueMilestoned},
			yamlOn:         "on: issues",
			expected:       true,
		},

		{
			desc:           "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)",
			triggeredEvent: webhook_module.HookEventPullRequestSync,
			payload:        &api.PullRequestPayload{Action: api.HookIssueSynchronized},
			yamlOn:         "on: pull_request",
			expected:       true,
		},
		{
			desc:           "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
			triggeredEvent: webhook_module.HookEventPullRequest,
			payload:        &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
			yamlOn:         "on: pull_request",
			expected:       false,
		},
		{
			desc:           "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
			triggeredEvent: webhook_module.HookEventPullRequest,
			payload:        &api.PullRequestPayload{Action: api.HookIssueClosed},
			yamlOn:         "on: pull_request",
			expected:       false,
		},
		{
			desc:           "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
			triggeredEvent: webhook_module.HookEventPullRequest,
			payload: &api.PullRequestPayload{
				Action: api.HookIssueClosed,
				PullRequest: &api.PullRequest{
					Base: &api.PRBranchInfo{},
				},
			},
			yamlOn:   "on:\n  pull_request:\n    branches: [main]",
			expected: false,
		},
		{
			desc:           "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
			triggeredEvent: webhook_module.HookEventPullRequest,
			payload:        &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
			yamlOn:         "on:\n  pull_request:\n    types: [labeled]",
			expected:       true,
		},
		{
			desc:           "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)",
			triggeredEvent: webhook_module.HookEventPullRequestReviewComment,
			payload:        &api.PullRequestPayload{Action: api.HookIssueReviewed},
			yamlOn:         "on:\n  pull_request_review_comment:\n    types: [created]",
			expected:       true,
		},
		{
			desc:           "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)",
			triggeredEvent: webhook_module.HookEventPullRequestReviewRejected,
			payload:        &api.PullRequestPayload{Action: api.HookIssueReviewed},
			yamlOn:         "on:\n  pull_request_review:\n    types: [dismissed]",
			expected:       false,
		},
		{
			desc:           "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type",
			triggeredEvent: webhook_module.HookEventRelease,
			payload:        &api.ReleasePayload{Action: api.HookReleasePublished},
			yamlOn:         "on:\n  release:\n    types: [published]",
			expected:       true,
		},
		{
			desc:           "HookEventRelease(updated) `updated` action matches GithubEventRelease(edited) with `edited` activity type",
			triggeredEvent: webhook_module.HookEventRelease,
			payload:        &api.ReleasePayload{Action: api.HookReleaseUpdated},
			yamlOn:         "on:\n  release:\n    types: [edited]",
			expected:       true,
		},

		{
			desc:           "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type",
			triggeredEvent: webhook_module.HookEventPackage,
			payload:        &api.PackagePayload{Action: api.HookPackageCreated},
			yamlOn:         "on:\n  registry_package:\n    types: [updated]",
			expected:       false,
		},
		{
			desc:           "HookEventWiki(wiki) matches GithubEventGollum(gollum)",
			triggeredEvent: webhook_module.HookEventWiki,
			payload:        nil,
			yamlOn:         "on: gollum",
			expected:       true,
		},
		{
			desc:           "HookEventSchedule(schedule) matches GithubEventSchedule(schedule)",
			triggeredEvent: webhook_module.HookEventSchedule,
			payload:        nil,
			yamlOn:         "on: schedule",
			expected:       true,
		},
		{
			desc:           "HookEventWorkflowDispatch(workflow_dispatch) matches GithubEventWorkflowDispatch(workflow_dispatch)",
			triggeredEvent: webhook_module.HookEventWorkflowDispatch,
			payload:        nil,
			yamlOn:         "on: workflow_dispatch",
			expected:       true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			evts, err := GetEventsFromContent([]byte(tc.yamlOn))
			require.NoError(t, err)
			assert.Len(t, evts, 1)
			assert.Equal(t, tc.expected, detectMatched(nil, tc.commit, tc.triggeredEvent, tc.payload, evts[0]))
		})
	}
}