1
1
import { promises as fs } from 'fs' ;
2
- import * as nock from 'nock' ;
3
2
import { posix as path } from 'path' ;
3
+ import { execSync } from 'child_process' ;
4
4
5
+ import nock from 'nock' ;
5
6
import { Probot , ProbotOctokit } from 'probot' ;
7
+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
6
8
7
9
import { SKIP_CHECK_LABEL } from '../src/constants' ;
8
10
import { CheckRunStatus , PRChange } from '../src/enums' ;
9
- import { ProbotHandler } from '../src/index' ;
11
+ import { default as trop } from '../src/index' ;
10
12
import {
11
13
backportToBranch ,
12
14
backportToLabel ,
@@ -16,8 +18,6 @@ import { updateManualBackport } from '../src/operations/update-manual-backport';
16
18
import { labelClosedPR , getPRNumbersFromPRBody } from '../src/utils' ;
17
19
import * as checkUtils from '../src/utils/checks-util' ;
18
20
19
- const trop : ProbotHandler = require ( '../src/index' ) ;
20
-
21
21
// event fixtures
22
22
const prClosedEvent = require ( './fixtures/pull_request.closed.json' ) ;
23
23
const issueCommentBackportCreatedEvent = require ( './fixtures/issue_comment_backport.created.json' ) ;
@@ -53,51 +53,51 @@ const targetLabel = {
53
53
color : 'fff' ,
54
54
} ;
55
55
56
- jest . mock ( '../src/utils' , ( ) => ( {
57
- labelClosedPR : jest . fn ( ) ,
58
- isAuthorizedUser : jest . fn ( ) . mockResolvedValue ( [ true ] ) ,
59
- getPRNumbersFromPRBody : jest . fn ( ) . mockReturnValue ( [ 12345 ] ) ,
56
+ vi . mock ( '../src/utils' , ( ) => ( {
57
+ labelClosedPR : vi . fn ( ) ,
58
+ isAuthorizedUser : vi . fn ( ) . mockResolvedValue ( [ true ] ) ,
59
+ getPRNumbersFromPRBody : vi . fn ( ) . mockReturnValue ( [ 12345 ] ) ,
60
60
} ) ) ;
61
61
62
- jest . mock ( '../src/utils/env-util' , ( ) => ( {
63
- getEnvVar : jest . fn ( ) ,
62
+ vi . mock ( '../src/utils/env-util' , ( ) => ( {
63
+ getEnvVar : vi . fn ( ) ,
64
64
} ) ) ;
65
65
66
- jest . mock ( '../src/operations/update-manual-backport' , ( ) => ( {
67
- updateManualBackport : jest . fn ( ) ,
66
+ vi . mock ( '../src/operations/update-manual-backport' , ( ) => ( {
67
+ updateManualBackport : vi . fn ( ) ,
68
68
} ) ) ;
69
69
70
- jest . mock ( '../src/operations/backport-to-location' , ( ) => ( {
71
- backportToBranch : jest . fn ( ) ,
72
- backportToLabel : jest . fn ( ) ,
70
+ vi . mock ( '../src/operations/backport-to-location' , ( ) => ( {
71
+ backportToBranch : vi . fn ( ) ,
72
+ backportToLabel : vi . fn ( ) ,
73
73
} ) ) ;
74
74
75
- jest . mock ( '../src/utils/checks-util' , ( ) => ( {
76
- updateBackportValidityCheck : jest . fn ( ) ,
77
- getBackportInformationCheck : jest . fn ( ) . mockResolvedValue ( { status : 'thing' } ) ,
78
- updateBackportInformationCheck : jest . fn ( ) . mockResolvedValue ( undefined ) ,
79
- queueBackportInformationCheck : jest . fn ( ) . mockResolvedValue ( undefined ) ,
75
+ vi . mock ( '../src/utils/checks-util' , ( ) => ( {
76
+ updateBackportValidityCheck : vi . fn ( ) ,
77
+ getBackportInformationCheck : vi . fn ( ) . mockResolvedValue ( { status : 'thing' } ) ,
78
+ updateBackportInformationCheck : vi . fn ( ) . mockResolvedValue ( undefined ) ,
79
+ queueBackportInformationCheck : vi . fn ( ) . mockResolvedValue ( undefined ) ,
80
80
} ) ) ;
81
81
82
82
describe ( 'trop' , ( ) => {
83
83
let robot : Probot ;
84
84
let octokit : any ;
85
- process . env = { BOT_USER_NAME : 'trop[bot]' } ;
85
+ process . env = { ... process . env , BOT_USER_NAME : 'trop[bot]' } ;
86
86
87
87
beforeEach ( ( ) => {
88
88
nock . disableNetConnect ( ) ;
89
89
octokit = {
90
90
repos : {
91
- getBranch : jest . fn ( ) . mockResolvedValue ( undefined ) ,
92
- listBranches : jest . fn ( ) . mockResolvedValue ( {
91
+ getBranch : vi . fn ( ) . mockResolvedValue ( undefined ) ,
92
+ listBranches : vi . fn ( ) . mockResolvedValue ( {
93
93
data : [ { name : '8-x-y' } , { name : '7-1-x' } ] ,
94
94
} ) ,
95
95
} ,
96
96
git : {
97
- deleteRef : jest . fn ( ) . mockResolvedValue ( undefined ) ,
97
+ deleteRef : vi . fn ( ) . mockResolvedValue ( undefined ) ,
98
98
} ,
99
99
pulls : {
100
- get : jest . fn ( ) . mockResolvedValue ( {
100
+ get : vi . fn ( ) . mockResolvedValue ( {
101
101
data : {
102
102
merged : true ,
103
103
head : {
@@ -114,11 +114,11 @@ describe('trop', () => {
114
114
} ) ,
115
115
} ,
116
116
issues : {
117
- addLabels : jest . fn ( ) . mockResolvedValue ( { } ) ,
118
- removeLabel : jest . fn ( ) . mockResolvedValue ( { } ) ,
119
- createLabel : jest . fn ( ) . mockResolvedValue ( { } ) ,
120
- createComment : jest . fn ( ) . mockResolvedValue ( { } ) ,
121
- listLabelsOnIssue : jest . fn ( ) . mockResolvedValue ( {
117
+ addLabels : vi . fn ( ) . mockResolvedValue ( { } ) ,
118
+ removeLabel : vi . fn ( ) . mockResolvedValue ( { } ) ,
119
+ createLabel : vi . fn ( ) . mockResolvedValue ( { } ) ,
120
+ createComment : vi . fn ( ) . mockResolvedValue ( { } ) ,
121
+ listLabelsOnIssue : vi . fn ( ) . mockResolvedValue ( {
122
122
data : [
123
123
{
124
124
id : 208045946 ,
@@ -131,8 +131,8 @@ describe('trop', () => {
131
131
} ) ,
132
132
} ,
133
133
checks : {
134
- listForRef : jest . fn ( ) . mockResolvedValue ( { data : { check_runs : [ ] } } ) ,
135
- create : jest . fn ( ) . mockResolvedValue ( { data : jest . fn ( ) } ) ,
134
+ listForRef : vi . fn ( ) . mockResolvedValue ( { data : { check_runs : [ ] } } ) ,
135
+ create : vi . fn ( ) . mockResolvedValue ( { data : vi . fn ( ) } ) ,
136
136
} ,
137
137
} ;
138
138
@@ -163,7 +163,7 @@ describe('trop', () => {
163
163
} ) ;
164
164
165
165
it ( 'does not trigger the backport on comment if the PR is not merged' , async ( ) => {
166
- octokit . pulls . get = jest
166
+ octokit . pulls . get = vi
167
167
. fn ( )
168
168
. mockResolvedValue ( { data : { merged : false } } ) ;
169
169
@@ -199,7 +199,7 @@ describe('trop', () => {
199
199
} ) ;
200
200
201
201
it ( 'does not trigger the backport on comment to a targeted branch if the branch does not exist' , async ( ) => {
202
- octokit . repos . getBranch = jest
202
+ octokit . repos . getBranch = vi
203
203
. fn ( )
204
204
. mockReturnValue ( Promise . reject ( new Error ( '404' ) ) ) ;
205
205
await robot . receive ( issueCommentBackportToCreatedEvent ) ;
@@ -230,7 +230,7 @@ describe('trop', () => {
230
230
} ) ;
231
231
232
232
it ( 'fails the check if there is conflicting backport information in a new PR' , async ( ) => {
233
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
233
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
234
234
235
235
const event = JSON . parse (
236
236
await fs . readFile ( newPROpenedEventPath , 'utf-8' ) ,
@@ -240,9 +240,8 @@ describe('trop', () => {
240
240
241
241
await robot . receive ( event ) ;
242
242
243
- const updatePayload = jest . mocked (
244
- checkUtils . updateBackportInformationCheck ,
245
- ) . mock . calls [ 0 ] [ 2 ] ;
243
+ const updatePayload = vi . mocked ( checkUtils . updateBackportInformationCheck )
244
+ . mock . calls [ 0 ] [ 2 ] ;
246
245
247
246
expect ( updatePayload ) . toMatchObject ( {
248
247
title : 'Conflicting Backport Information' ,
@@ -253,7 +252,7 @@ describe('trop', () => {
253
252
} ) ;
254
253
255
254
it ( 'passes the check if there is a "no-backport" label and no "target/" label in a new PR' , async ( ) => {
256
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
255
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
257
256
258
257
const event = JSON . parse (
259
258
await fs . readFile ( newPROpenedEventPath , 'utf-8' ) ,
@@ -263,9 +262,8 @@ describe('trop', () => {
263
262
264
263
await robot . receive ( event ) ;
265
264
266
- const updatePayload = jest . mocked (
267
- checkUtils . updateBackportInformationCheck ,
268
- ) . mock . calls [ 0 ] [ 2 ] ;
265
+ const updatePayload = vi . mocked ( checkUtils . updateBackportInformationCheck )
266
+ . mock . calls [ 0 ] [ 2 ] ;
269
267
270
268
expect ( updatePayload ) . toMatchObject ( {
271
269
title : 'Backport Information Provided' ,
@@ -275,7 +273,7 @@ describe('trop', () => {
275
273
} ) ;
276
274
277
275
it ( 'passes the check if there is no "no-backport" label and a "target/" label in a new PR' , async ( ) => {
278
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
276
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
279
277
280
278
const event = JSON . parse (
281
279
await fs . readFile ( newPROpenedEventPath , 'utf-8' ) ,
@@ -285,9 +283,8 @@ describe('trop', () => {
285
283
286
284
await robot . receive ( event ) ;
287
285
288
- const updatePayload = jest . mocked (
289
- checkUtils . updateBackportInformationCheck ,
290
- ) . mock . calls [ 0 ] [ 2 ] ;
286
+ const updatePayload = vi . mocked ( checkUtils . updateBackportInformationCheck )
287
+ . mock . calls [ 0 ] [ 2 ] ;
291
288
292
289
expect ( updatePayload ) . toMatchObject ( {
293
290
title : 'Backport Information Provided' ,
@@ -334,7 +331,7 @@ Notes: <!-- One-line Change Summary Here-->`,
334
331
} ,
335
332
} ;
336
333
337
- expect ( jest . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
334
+ expect ( vi . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
338
335
expect . anything ( ) ,
339
336
pr ,
340
337
'5-0-x' ,
@@ -372,7 +369,7 @@ Notes: <!-- One-line Change Summary Here-->`,
372
369
} ,
373
370
} ;
374
371
375
- expect ( jest . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
372
+ expect ( vi . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
376
373
expect . anything ( ) ,
377
374
pr ,
378
375
'4-0-x' ,
@@ -418,7 +415,7 @@ Notes: <!-- One-line Change Summary Here-->`,
418
415
419
416
expect ( updateManualBackport ) . toHaveBeenCalled ( ) ;
420
417
421
- expect ( jest . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
418
+ expect ( vi . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
422
419
expect . anything ( ) ,
423
420
pr ,
424
421
'4-0-x' ,
@@ -464,7 +461,7 @@ Notes: <!-- One-line Change Summary Here-->`,
464
461
465
462
expect ( updateManualBackport ) . toHaveBeenCalled ( ) ;
466
463
467
- expect ( jest . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
464
+ expect ( vi . mocked ( labelClosedPR ) ) . toHaveBeenCalledWith (
468
465
expect . anything ( ) ,
469
466
pr ,
470
467
'4-0-x' ,
@@ -475,7 +472,7 @@ Notes: <!-- One-line Change Summary Here-->`,
475
472
476
473
describe ( 'updateBackportValidityCheck from pull_request events' , ( ) => {
477
474
it ( 'skips the backport validity check if there is skip check label in a new PR' , async ( ) => {
478
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
475
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
479
476
octokit . issues . listLabelsOnIssue . mockResolvedValue ( {
480
477
data : [
481
478
{
@@ -490,7 +487,7 @@ Notes: <!-- One-line Change Summary Here-->`,
490
487
event . payload . pull_request . base . ref = '30-x-y' ;
491
488
await robot . receive ( event ) ;
492
489
493
- const updatePayload = jest . mocked ( checkUtils . updateBackportValidityCheck )
490
+ const updatePayload = vi . mocked ( checkUtils . updateBackportValidityCheck )
494
491
. mock . calls [ 0 ] [ 2 ] ;
495
492
496
493
expect ( updatePayload ) . toMatchObject ( {
@@ -501,15 +498,15 @@ Notes: <!-- One-line Change Summary Here-->`,
501
498
} ) ;
502
499
503
500
it ( 'cancels the backport validity check if branch is targeting main' , async ( ) => {
504
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
501
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ ] ) ;
505
502
506
503
const event = JSON . parse (
507
504
await fs . readFile ( newPRBackportOpenedEventPath , 'utf-8' ) ,
508
505
) ;
509
506
510
507
await robot . receive ( event ) ;
511
508
512
- const updatePayload = jest . mocked ( checkUtils . updateBackportValidityCheck )
509
+ const updatePayload = vi . mocked ( checkUtils . updateBackportValidityCheck )
513
510
. mock . calls [ 0 ] [ 2 ] ;
514
511
515
512
expect ( updatePayload ) . toMatchObject ( {
@@ -520,7 +517,7 @@ Notes: <!-- One-line Change Summary Here-->`,
520
517
} ) ;
521
518
522
519
it ( 'fails the backport validity check if old PR was not merged to a supported release branch' , async ( ) => {
523
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ 1234 ] ) ;
520
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ 1234 ] ) ;
524
521
octokit . pulls . get . mockResolvedValueOnce ( {
525
522
data : {
526
523
merged : true ,
@@ -536,7 +533,7 @@ Notes: <!-- One-line Change Summary Here-->`,
536
533
event . payload . action = 'synchronize' ;
537
534
await robot . receive ( event ) ;
538
535
539
- const updatePayload = jest . mocked ( checkUtils . updateBackportValidityCheck )
536
+ const updatePayload = vi . mocked ( checkUtils . updateBackportValidityCheck )
540
537
. mock . calls [ 0 ] [ 2 ] ;
541
538
542
539
expect ( updatePayload ) . toMatchObject ( {
@@ -548,7 +545,7 @@ Notes: <!-- One-line Change Summary Here-->`,
548
545
} ) ;
549
546
550
547
it ( 'fails the backport validity check if old PR has not been merged yet' , async ( ) => {
551
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ 1234 ] ) ;
548
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ 1234 ] ) ;
552
549
octokit . pulls . get . mockResolvedValueOnce ( {
553
550
data : {
554
551
merged : false ,
@@ -564,7 +561,7 @@ Notes: <!-- One-line Change Summary Here-->`,
564
561
event . payload . action = 'synchronize' ;
565
562
await robot . receive ( event ) ;
566
563
567
- const updatePayload = jest . mocked ( checkUtils . updateBackportValidityCheck )
564
+ const updatePayload = vi . mocked ( checkUtils . updateBackportValidityCheck )
568
565
. mock . calls [ 0 ] [ 2 ] ;
569
566
570
567
expect ( updatePayload ) . toMatchObject ( {
@@ -576,7 +573,7 @@ Notes: <!-- One-line Change Summary Here-->`,
576
573
} ) ;
577
574
578
575
it ( 'succeeds the backport validity check if all checks pass' , async ( ) => {
579
- jest . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ 1234 ] ) ;
576
+ vi . mocked ( getPRNumbersFromPRBody ) . mockReturnValueOnce ( [ 1234 ] ) ;
580
577
octokit . pulls . get . mockResolvedValueOnce ( {
581
578
data : {
582
579
merged : true ,
@@ -592,7 +589,7 @@ Notes: <!-- One-line Change Summary Here-->`,
592
589
event . payload . action = 'synchronize' ;
593
590
await robot . receive ( event ) ;
594
591
595
- const updatePayload = jest . mocked ( checkUtils . updateBackportValidityCheck )
592
+ const updatePayload = vi . mocked ( checkUtils . updateBackportValidityCheck )
596
593
. mock . calls [ 0 ] [ 2 ] ;
597
594
598
595
expect ( updatePayload ) . toMatchObject ( {
0 commit comments