forked from vijaykramesh/pr-lint-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
329 lines (263 loc) · 10.9 KB
/
index.test.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const nock = require('nock')
const fs = require('fs')
const { Toolkit } = require('actions-toolkit')
nock.disableNetConnect()
describe('jira-check-action', () => {
let action, tools
// Mock Toolkit.run to define `action` so we can call it
Toolkit.run = jest.fn(actionFn => {
action = actionFn
})
// Load up our entrypoint file
require('.')
const bad_title_and_branch = { title: 'no ticket in me', ref_name: 'no-ticket-in-me' }
const good_title_and_branch = { title: '[PROJ-1234] a good PR title', ref_name: 'bug/PROJ-1234/a_good_branch' }
const good_title_and_bad_branch = { title: '[PROJ-1234] a good PR title', ref_name: 'fix_things' }
const bad_title_and_good_branch = { title: 'no ticket in me', ref_name: 'bug/PROJ_1234/a_good_branch' }
const lower_case_good_title_and_branch = { title: '[proj-1234] a lower case good title', ref_name: 'bug/proj_1234/a_good_lowercase_branch' }
const good_commits = [
{ commit: { message: "PROJ-1234 Commit 1" } },
{ commit: { message: "PROJ-1234 Commit 2" } },
{ commit: { message: "PROJ-1234 Commit 3" } }
];
const lower_case_good_commits = [
{ commit: { message: "PROJ-1234 Commit 1" } },
{ commit: { message: "PROJ-1234 Commit 2" } },
{ commit: { message: "PROJ-1234 Commit 3" } }
];
const bad_commits = [
{ commit: { message: "PRJ-123 Commit 1" } },
{ commit: { message: "PROJ-1234 Commit 2" } },
{ commit: { message: "Commit 3" } }
];
beforeEach(() => {
// Create a new Toolkit instance
tools = new Toolkit()
// Mock methods on it!
tools.exit.success = jest.fn()
tools.exit.failure = jest.fn()
})
it('fails if check_title is true and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))
tools.context.payload = pullRequestOpenedFixture(bad_title_and_good_branch)
await action(tools)
expect(tools.exit.failure).toHaveBeenCalledWith("PR Linting Failed")
expect.assertions(1)
})
it('passes if check_title is false and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('branch.yml'))
tools.context.payload = pullRequestOpenedFixture(bad_title_and_good_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('passes if check_title is true and title matches', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))
tools.context.payload = pullRequestOpenedFixture(good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('fails if check_branch is true and branch does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('branch.yml'))
tools.context.payload = pullRequestOpenedFixture(good_title_and_bad_branch)
await action(tools)
expect(tools.exit.failure).toHaveBeenCalledWith("PR Linting Failed")
expect.assertions(1)
})
it('passes if check_branch is false and branch does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))
tools.context.payload = pullRequestOpenedFixture(good_title_and_bad_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('passes if check_branch is true and branch matches', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('branch.yml'))
tools.context.payload = pullRequestOpenedFixture(bad_title_and_good_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('passes if check_commits is true and all commits match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('commits.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('fails if check_commits is true and some commits do not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('commits.yml'))
mockGetPRCommitListRequest(bad_commits)
tools.context.payload = pullRequestOpenedFixture(good_title_and_branch)
await action(tools)
expect(tools.exit.failure).toHaveBeenCalledWith("PR Linting Failed")
expect.assertions(1)
})
it('passes if check_commits is false and all commits match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('passes if check_commits is false and some commits do not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('title.yml'))
mockGetPRCommitListRequest(bad_commits)
tools.context.payload = pullRequestOpenedFixture(good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('fails if check_branch and check_title is true and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(bad_title_and_good_branch)
await action(tools)
expect(tools.exit.failure).toHaveBeenCalledWith("PR Linting Failed")
expect.assertions(1)
})
it('fails if check_branch and check_title is true and title does not match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(bad_title_and_good_branch)
await action(tools)
expect(tools.exit.failure).toHaveBeenCalledWith("PR Linting Failed")
expect.assertions(1)
})
it('passes if check_branch and check_title is true and both match', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('passes if ignore_case and lower case title/branch', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(lower_case_good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('passes if ignore_case and lower case commits', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('all.yml'))
mockGetPRCommitListRequest(lower_case_good_commits);
tools.context.payload = pullRequestOpenedFixture(lower_case_good_title_and_branch)
await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(1)
})
it('fails if not ignore_case and lower case title/branch', async () => {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/contents/.github/pr-lint.yml')
.query(true)
.reply(200, configFixture('no-ignore-case.yml'))
mockGetPRCommitListRequest(good_commits);
tools.context.payload = pullRequestOpenedFixture(lower_case_good_title_and_branch)
await action(tools)
expect(tools.exit.failure).toHaveBeenCalledWith("PR Linting Failed")
expect.assertions(1)
})
})
function mockGetPRCommitListRequest(commits) {
nock('https://api.github.com')
.get('/repos/vijaykramesh/pr-lint-action-test/pulls/1/commits')
.query(true)
.reply(200, commits);
}
function encodeContent(content) {
return Buffer.from(content).toString('base64')
}
function configFixture(fileName = 'all.yml') {
return {
type: 'file',
encoding: 'base64',
size: 5362,
name: fileName,
path: `.github/${fileName}`,
content: encodeContent(fs.readFileSync(`./fixtures/${fileName}`)),
sha: '3d21ec53a331a6f037a91c368710b99387d012c1',
url:
'https://api.github.com/repos/vijaykramesh/pr-lint-action/contents/.github/pr-lint.yml',
git_url:
'https://api.github.com/repos/vijaykramesh/pr-lint-action/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
html_url:
'https://github.com/vijaykramesh/pr-lint-action/blob/master/.github/pr-lint.yml',
download_url:
'https://raw.githubusercontent.com/vijaykramesh/pr-lint-action/master/.github/pr-lint.yml',
_links: {
git:
'https://api.github.com/repos/vijaykramesh/pr-lint-action/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
self:
'https://api.github.com/repos/vijaykramesh/pr-lint-action/contents/.github/pr-lint.yml',
html:
'https://github.com/vijaykramesh/pr-lint-action/blob/master/.github/pr-lint.yml'
}
}
}
function pullRequestOpenedFixture({ title, ref_name }) {
return {
pull_request: {
number: 1,
title: title,
head: {
ref: ref_name
}
},
repository: {
name: 'pr-lint-action-test',
owner: {
login: 'vijaykramesh'
}
}
}
}