generated from ExpediaGroup/new-project
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmanage-merge-queue.test.ts
More file actions
748 lines (680 loc) · 26.9 KB
/
manage-merge-queue.test.ts
File metadata and controls
748 lines (680 loc) · 26.9 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
Copyright 2021 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { JUMP_THE_QUEUE_PR_LABEL, MERGE_QUEUE_STATUS, READY_FOR_MERGE_PR_LABEL } from '../../src/constants';
import { Mocktokit } from '../types';
import { context } from '@actions/github';
import { manageMergeQueue } from '../../src/helpers/manage-merge-queue';
import { notifyUser } from '../../src/utils/notify-user';
import { octokit, octokitGraphql } from '../../src/octokit';
import { removeLabel, removeLabelIfExists } from '../../src/helpers/remove-label';
import { setCommitStatus } from '../../src/helpers/set-commit-status';
import { updateMergeQueue } from '../../src/utils/update-merge-queue';
import { updatePrWithDefaultBranch } from '../../src/helpers/prepare-queued-pr-for-merge';
import { approvalsSatisfied } from '../../src/helpers/approvals-satisfied';
import { createPrComment } from '../../src/helpers/create-pr-comment';
import { isUserInTeam } from '../../src/helpers/is-user-in-team';
jest.mock('../../src/helpers/remove-label');
jest.mock('../../src/helpers/set-commit-status');
jest.mock('../../src/utils/notify-user');
jest.mock('../../src/utils/update-merge-queue');
jest.mock('../../src/helpers/is-user-in-team');
jest.mock('../../src/helpers/approvals-satisfied');
jest.mock('../../src/helpers/create-pr-comment');
jest.mock('../../src/utils/../../src/helpers/prepare-queued-pr-for-merge');
jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 }, serverUrl: 'sampleUrl' },
getOctokit: jest.fn(() => ({
rest: {
pulls: { get: jest.fn(), list: jest.fn() },
issues: {
addLabels: jest.fn()
},
users: {
getByUsername: jest.fn()
}
},
graphql: jest.fn(() => ({ catch: jest.fn() }))
}))
}));
(isUserInTeam as jest.Mock).mockImplementation(({ team }) => {
return team === 'team';
});
(octokit.users.getByUsername as unknown as Mocktokit).mockImplementation(async () => ({
data: { email: 'user@github.com' }
}));
describe('manageMergeQueue', () => {
describe('pr merged case', () => {
const prUnderTest = {
number: 123,
merged: true,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const openPr = {
number: 456,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }]
};
const openPrs = [prUnderTest, openPr];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
await manageMergeQueue();
});
it('should call remove label with correct params', () => {
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #1', 123);
});
it('should call updateMergeQueue with correct params', () => {
expect(updateMergeQueue).toHaveBeenCalledWith(openPrs);
});
});
describe('pr not core approved case', () => {
const prUnderTest = {
merged: false,
head: { sha: 'sha' },
number: 123,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPr = {
number: 456,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const openPrs = [prUnderTest, openPr];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(false);
await manageMergeQueue();
});
it('should call remove label with correct params', () => {
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
});
it('should set commit status with correct params', () => {
expect(setCommitStatus).toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'pending',
description: 'This PR is not in the merge queue.'
});
});
it('should call updateMergeQueue with correct params', () => {
expect(updateMergeQueue).toHaveBeenCalledWith([openPr]);
});
});
describe('pr without ready for merge label case', () => {
const prUnderTest = {
merged: false,
head: { sha: 'sha' },
number: 123,
labels: [{ name: 'QUEUED FOR MERGE #2' }]
};
const openPr = {
number: 456,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const openPrs = [prUnderTest, openPr];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should call remove label with correct params', () => {
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #2', 123);
});
it('should set commit status with correct params', () => {
expect(setCommitStatus).toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'pending',
description: 'This PR is not in the merge queue.'
});
});
it('should call updateMergeQueue with correct params', () => {
expect(updateMergeQueue).toHaveBeenCalledWith([openPr]);
});
});
describe('pr ready for merge with one PR in the queue', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPr = {
number: 456,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const openPrs = [openPr];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should call setCommitStatus with correct params', () => {
expect(setCommitStatus).toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'pending',
description: 'This PR is in line to merge.'
});
});
it('should call addLabels with correct params', () => {
expect(octokit.issues.addLabels).toHaveBeenCalledWith({
labels: ['QUEUED FOR MERGE #2'],
issue_number: 123,
...context.repo
});
});
it('should not update PR with default branch', () => {
expect(updatePrWithDefaultBranch).not.toHaveBeenCalled();
});
it('should enable auto-merge', () => {
expect(octokitGraphql).toHaveBeenCalled();
});
});
describe('pr ready for merge with a PR that has ready for merge label only', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPr1 = {
number: 456,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPr2 = {
number: 789,
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const openPrs = [prUnderTest, openPr1, openPr2];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should call setCommitStatus with correct params', () => {
expect(setCommitStatus).toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'pending',
description: 'This PR is in line to merge.'
});
});
it('should call addLabels with correct params', () => {
expect(octokit.issues.addLabels).toHaveBeenCalledWith({
labels: ['QUEUED FOR MERGE #2'],
issue_number: 123,
...context.repo
});
});
it('should not update PR with default branch', () => {
expect(updatePrWithDefaultBranch).not.toHaveBeenCalled();
});
it('should enable auto-merge', () => {
expect(octokitGraphql).toHaveBeenCalled();
});
});
describe('pr ready for merge with empty queue', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [prUnderTest, { labels: [{ name: 'some random label' }] }];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should call setCommitStatus', () => {
expect(setCommitStatus).toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'success',
description: 'This PR is next to merge.'
});
});
it('should call addLabels with correct params', () => {
expect(octokit.issues.addLabels).toHaveBeenCalledWith({
labels: ['QUEUED FOR MERGE #1'],
issue_number: 123,
...context.repo
});
});
it('should update PR with default branch', () => {
expect(updatePrWithDefaultBranch).toHaveBeenCalledWith(prUnderTest);
});
it('should enable auto-merge', () => {
expect(octokitGraphql).toHaveBeenCalled();
});
});
describe('pr ready for merge case where repo has disabled auto merge', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [prUnderTest];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(octokitGraphql as unknown as jest.Mock).mockRejectedValue(new Error('Auto merge is not allowed for this repo'));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should call setCommitStatus', () => {
expect(setCommitStatus).toHaveBeenCalledWith({
sha: 'sha',
context: MERGE_QUEUE_STATUS,
state: 'success',
description: 'This PR is next to merge.'
});
});
it('should call addLabels with correct params', () => {
expect(octokit.issues.addLabels).toHaveBeenCalledWith({
labels: ['QUEUED FOR MERGE #1'],
issue_number: 123,
...context.repo
});
});
it('should attempt to enable auto-merge', () => {
expect(octokitGraphql).toHaveBeenCalled();
});
});
describe('pr already in the queue case', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #5' }]
};
const openPrs = [
prUnderTest,
{ number: 1, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }] },
{ number: 2, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] },
{ number: 3, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #3' }] },
{ number: 4, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #4' }] }
];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should do nothing', () => {
expect(octokit.issues.addLabels).not.toHaveBeenCalled();
expect(octokitGraphql).not.toHaveBeenCalled();
expect(updateMergeQueue).not.toHaveBeenCalled();
});
});
describe('skip_auto_merge is used', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [prUnderTest];
it('should not enable auto-merge on PR if skip_auto_merge is true', async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ skip_auto_merge: 'true' });
expect(octokitGraphql).not.toHaveBeenCalled();
});
it('should enable auto-merge on PR if skip_auto_merge is false', async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ skip_auto_merge: 'false' });
expect(octokitGraphql).toHaveBeenCalled();
});
});
describe('jump the queue case', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: JUMP_THE_QUEUE_PR_LABEL }, { name: 'QUEUED FOR MERGE #5' }]
};
const openPrs = [
prUnderTest,
{ number: 1, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }] },
{ number: 2, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] },
{ number: 3, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #3' }] },
{ number: 4, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #4' }] }
];
beforeEach(() => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
});
it('should call updateMergeQueue with correct params', async () => {
await manageMergeQueue();
expect(isUserInTeam).toHaveBeenCalledTimes(0);
expect(removeLabel).toHaveBeenCalledTimes(0);
expect(createPrComment).toHaveBeenCalledTimes(0);
expect(updateMergeQueue).toHaveBeenCalledWith(openPrs);
});
it('should call updateMergeQueue with correct params when not checking maintainers group', async () => {
await manageMergeQueue({ allow_only_for_maintainers: 'false' });
expect(isUserInTeam).toHaveBeenCalledTimes(0);
expect(removeLabel).toHaveBeenCalledTimes(0);
expect(createPrComment).toHaveBeenCalledTimes(0);
expect(updateMergeQueue).toHaveBeenCalledWith(openPrs);
});
it('should call updateMergeQueue when user in maintainers group', async () => {
await manageMergeQueue({ team: 'team', allow_only_for_maintainers: 'true' });
expect(isUserInTeam).toHaveBeenCalled();
expect(removeLabel).toHaveBeenCalledTimes(0);
expect(createPrComment).toHaveBeenCalledTimes(0);
expect(updateMergeQueue).toHaveBeenCalledWith(openPrs);
});
it('should not call updateMergeQueue when user not in maintainers group', async () => {
await manageMergeQueue({ team: 'not_team', allow_only_for_maintainers: 'true' });
expect(isUserInTeam).toHaveBeenCalled();
expect(removeLabelIfExists).toHaveBeenCalled();
expect(createPrComment).toHaveBeenCalled();
expect(updateMergeQueue).toHaveBeenCalledTimes(0);
});
});
describe('slack reminder integration', () => {
const login = 'test';
const slack_webhook_url = 'https://hooks.slack.com/workflows/1234567890';
it('should notify user if pr is becoming queue position 1', async () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [prUnderTest];
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login, slack_webhook_url });
expect(notifyUser).toHaveBeenCalled();
});
it('should notify user if pr is already queue position 1', async () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const openPrs = [prUnderTest, { labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] }];
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login, slack_webhook_url });
expect(notifyUser).toHaveBeenCalled();
});
it('should not notify user if queue position greater than 2', async () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #5' }]
};
const openPrs = [
{ number: 1, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }] },
{ number: 2, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] },
{ number: 3, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #3' }] },
{ number: 4, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #4' }] }
];
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login, slack_webhook_url });
expect(notifyUser).not.toHaveBeenCalled();
});
it('should not notify user if slack_webhook_url not provided', async () => {
const openPrs = [{ labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }];
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: {
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login });
expect(notifyUser).not.toHaveBeenCalled();
});
it('should not notify user if login not provided', async () => {
const openPrs = [{ labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }];
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: {
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
}
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ slack_webhook_url });
expect(notifyUser).not.toHaveBeenCalled();
});
it('should remove user from the queue if email not set on user profile and slack_webhook_url/login are provided', async () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [prUnderTest];
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(octokit.users.getByUsername as unknown as Mocktokit).mockImplementation(async () => ({
data: { email: null }
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({ login, slack_webhook_url });
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
expect(createPrComment).toHaveBeenCalled();
});
});
describe('filters queued prs when there are multiple pages', () => {
const prUnderTest = {
number: 123,
merged: true,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }]
};
const queuedPr = { number: 999, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] };
const openPrsPage1 = [prUnderTest, { number: 456, labels: [{ name: READY_FOR_MERGE_PR_LABEL }] }];
const openPrsPage2 = [{ number: 789, labels: [] }, queuedPr];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrsPage1 : page === 2 ? openPrsPage2 : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue();
});
it('should call pulls.list for multiple pages', () => {
expect(octokit.pulls.list).toHaveBeenCalledWith({
state: 'open',
sort: 'updated',
direction: 'desc',
per_page: 100,
page: 1,
...context.repo
});
expect(octokit.pulls.list).toHaveBeenCalledWith({
state: 'open',
sort: 'updated',
direction: 'desc',
per_page: 100,
page: 2,
...context.repo
});
expect(octokit.pulls.list).toHaveBeenCalledWith({
state: 'open',
sort: 'updated',
direction: 'desc',
per_page: 100,
page: 3,
...context.repo
});
});
it('should call remove label with correct params', () => {
expect(removeLabelIfExists).toHaveBeenCalledWith(READY_FOR_MERGE_PR_LABEL, 123);
expect(removeLabelIfExists).toHaveBeenCalledWith('QUEUED FOR MERGE #1', 123);
});
it('should call updateMergeQueue with correct params', () => {
expect(updateMergeQueue).toHaveBeenCalledWith([prUnderTest, queuedPr]);
});
});
describe('more than max prs in the queue', () => {
const prUnderTest = {
number: 123,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [
prUnderTest,
{ number: 1, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }] },
{ number: 2, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] },
{ number: 3, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #3' }] }
];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({
max_queue_size: '3'
});
});
it('should not add pr to the queue when it is full', () => {
expect(octokit.issues.addLabels).not.toHaveBeenCalled();
expect(octokitGraphql).not.toHaveBeenCalled();
expect(createPrComment).toHaveBeenCalled();
});
});
describe('fewer than max prs in the queue', () => {
const prUnderTest = {
number: 1,
merged: false,
head: { sha: 'sha' },
labels: [{ name: READY_FOR_MERGE_PR_LABEL }]
};
const openPrs = [
prUnderTest,
{ number: 1, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #1' }] },
{ number: 2, labels: [{ name: READY_FOR_MERGE_PR_LABEL }, { name: 'QUEUED FOR MERGE #2' }] }
];
beforeEach(async () => {
(octokit.pulls.list as unknown as Mocktokit).mockImplementation(async ({ page }) => ({
data: page === 1 ? openPrs : []
}));
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: prUnderTest
}));
(approvalsSatisfied as jest.Mock).mockResolvedValue(true);
await manageMergeQueue({
max_queue_size: '3'
});
});
it('should add pr to the queue when it is not yet full', () => {
expect(octokit.issues.addLabels).toHaveBeenCalledWith({
labels: ['QUEUED FOR MERGE #3'],
issue_number: 123,
...context.repo
});
expect(octokitGraphql).toHaveBeenCalled();
expect(createPrComment).not.toHaveBeenCalled();
});
});
});