1
1
import { expect } from 'chai' ;
2
2
import nock from 'nock' ;
3
3
4
- import { LABELS } from '../labels.js' ;
4
+ import { LABELS , MANAGED_BY_OTA_MARKER } from '../labels.js' ;
5
5
6
6
import GitHub from './index.js' ;
7
7
@@ -10,8 +10,8 @@ describe('GitHub', function () {
10
10
11
11
let MANAGED_LABELS ;
12
12
let github ;
13
- const EXISTING_OPEN_ISSUE = { number : 1 , title : 'Opened issue' , description : 'Issue description' , state : GitHub . ISSUE_STATE_OPEN , labels : [ { name : 'page access restriction' } , { name : 'server error' } ] } ;
14
- const EXISTING_CLOSED_ISSUE = { number : 2 , title : 'Closed issue' , description : 'Issue description' , state : GitHub . ISSUE_STATE_CLOSED , labels : [ { name : 'empty content' } ] } ;
13
+ const EXISTING_OPEN_ISSUE = { number : 1 , title : 'Opened issue' , description : 'Issue description' , state : GitHub . ISSUE_STATE_OPEN , labels : [ { name : LABELS . HTTP_403 . name } ] } ;
14
+ const EXISTING_CLOSED_ISSUE = { number : 2 , title : 'Closed issue' , description : 'Issue description' , state : GitHub . ISSUE_STATE_CLOSED , labels : [ { name : LABELS . EMPTY_CONTENT . name } ] } ;
15
15
16
16
before ( async ( ) => {
17
17
MANAGED_LABELS = Object . values ( LABELS ) ;
@@ -24,31 +24,119 @@ describe('GitHub', function () {
24
24
} ) ;
25
25
26
26
describe ( '#initialize' , ( ) => {
27
- const scopes = [ ] ;
27
+ context ( 'when some labels are missing' , ( ) => {
28
+ const scopes = [ ] ;
28
29
29
- before ( async ( ) => {
30
- const existingLabels = MANAGED_LABELS . slice ( 0 , - 2 ) ;
30
+ before ( async ( ) => {
31
+ const existingLabels = MANAGED_LABELS . slice ( 0 , - 2 ) . map ( label => ( {
32
+ ...label ,
33
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
34
+ } ) ) ;
31
35
32
- nock ( 'https://api.github.com' )
33
- . get ( '/repos/owner/repo/labels' )
34
- . query ( true )
35
- . reply ( 200 , existingLabels ) ;
36
+ nock ( 'https://api.github.com' )
37
+ . get ( '/repos/owner/repo/labels' )
38
+ . query ( true )
39
+ . reply ( 200 , existingLabels ) ;
40
+
41
+ const missingLabels = MANAGED_LABELS . slice ( - 2 ) ;
36
42
37
- const missingLabels = MANAGED_LABELS . slice ( - 2 ) ;
43
+ for ( const label of missingLabels ) {
44
+ scopes . push ( nock ( 'https://api.github.com' )
45
+ . post ( '/repos/owner/repo/labels' , body => body . name === label . name )
46
+ . reply ( 200 , label ) ) ;
47
+ }
38
48
39
- for ( const label of missingLabels ) {
40
- scopes . push ( nock ( 'https://api.github.com' )
41
- . post ( '/repos/owner/repo/labels' , body => body . name === label . name )
42
- . reply ( 200 , label ) ) ;
43
- }
49
+ await github . initialize ( ) ;
50
+ } ) ;
51
+
52
+ after ( nock . cleanAll ) ;
44
53
45
- await github . initialize ( ) ;
54
+ it ( 'should create missing labels' , ( ) => {
55
+ scopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
56
+ } ) ;
46
57
} ) ;
47
58
48
- after ( nock . cleanAll ) ;
59
+ context ( 'when some labels are obsolete' , ( ) => {
60
+ const deleteScopes = [ ] ;
61
+
62
+ before ( async ( ) => {
63
+ const existingLabels = [
64
+ ...MANAGED_LABELS . map ( label => ( {
65
+ ...label ,
66
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
67
+ } ) ) ,
68
+ // Add an obsolete label that should be removed
69
+ {
70
+ name : 'obsolete label' ,
71
+ color : 'FF0000' ,
72
+ description : `This label is no longer used ${ MANAGED_BY_OTA_MARKER } ` ,
73
+ } ,
74
+ ] ;
75
+
76
+ nock ( 'https://api.github.com' )
77
+ . get ( '/repos/owner/repo/labels' )
78
+ . query ( true )
79
+ . reply ( 200 , existingLabels ) ;
80
+
81
+ // Mock the delete call for the obsolete label
82
+ deleteScopes . push ( nock ( 'https://api.github.com' )
83
+ . delete ( '/repos/owner/repo/labels/obsolete%20label' )
84
+ . reply ( 200 ) ) ;
85
+
86
+ // Mock the second getRepositoryLabels call after deletion
87
+ nock ( 'https://api.github.com' )
88
+ . get ( '/repos/owner/repo/labels' )
89
+ . query ( true )
90
+ . reply ( 200 , MANAGED_LABELS . map ( label => ( {
91
+ ...label ,
92
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
93
+ } ) ) ) ;
94
+
95
+ await github . initialize ( ) ;
96
+ } ) ;
97
+
98
+ after ( nock . cleanAll ) ;
99
+
100
+ it ( 'should remove obsolete managed labels' , ( ) => {
101
+ deleteScopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
102
+ } ) ;
103
+ } ) ;
104
+
105
+ context ( 'when some labels have changed descriptions' , ( ) => {
106
+ const updateScopes = [ ] ;
49
107
50
- it ( 'should create missing labels' , ( ) => {
51
- scopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
108
+ before ( async ( ) => {
109
+ const originalTestLabels = MANAGED_LABELS . slice ( - 2 ) ;
110
+ const testLabels = originalTestLabels . map ( label => ( {
111
+ ...label ,
112
+ description : `${ label . description } - obsolete description` ,
113
+ } ) ) ;
114
+
115
+ nock ( 'https://api.github.com' )
116
+ . persist ( )
117
+ . get ( '/repos/owner/repo/labels' )
118
+ . query ( true )
119
+ . reply ( 200 , [ ...MANAGED_LABELS . slice ( 0 , - 2 ) , ...testLabels ] . map ( label => ( {
120
+ ...label ,
121
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
122
+ } ) ) ) ;
123
+
124
+ for ( const label of originalTestLabels ) {
125
+ updateScopes . push ( nock ( 'https://api.github.com' )
126
+ . patch ( `/repos/owner/repo/labels/${ encodeURIComponent ( label . name ) } ` , body =>
127
+ body . description === `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` )
128
+ . reply ( 200 , label ) ) ;
129
+ }
130
+ await github . initialize ( ) ;
131
+ } ) ;
132
+
133
+ after ( ( ) => {
134
+ nock . cleanAll ( ) ;
135
+ } ) ;
136
+
137
+ it ( 'should update labels with changed descriptions' , ( ) => {
138
+ updateScopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
139
+ } ) ;
52
140
} ) ;
53
141
} ) ;
54
142
@@ -280,7 +368,7 @@ describe('GitHub', function () {
280
368
const ISSUE_TO_CREATE = {
281
369
title : 'New Issue' ,
282
370
description : 'Description of the new issue' ,
283
- labels : [ 'empty response' ] ,
371
+ labels : [ LABELS . EMPTY_RESPONSE . name ] ,
284
372
} ;
285
373
286
374
before ( async ( ) => {
@@ -311,14 +399,14 @@ describe('GitHub', function () {
311
399
312
400
before ( async ( ) => {
313
401
updateIssueScope = nock ( 'https://api.github.com' )
314
- . patch ( `/repos/owner/repo/issues/${ EXISTING_CLOSED_ISSUE . number } ` , { state : GitHub . ISSUE_STATE_OPEN , labels : [ 'page access restriction' ] } )
402
+ . patch ( `/repos/owner/repo/issues/${ EXISTING_CLOSED_ISSUE . number } ` , { state : GitHub . ISSUE_STATE_OPEN , labels : [ LABELS . HTTP_403 . name ] } )
315
403
. reply ( 200 ) ;
316
404
317
405
addCommentScope = nock ( 'https://api.github.com' )
318
406
. post ( `/repos/owner/repo/issues/${ EXISTING_CLOSED_ISSUE . number } /comments` , { body : EXISTING_CLOSED_ISSUE . description } )
319
407
. reply ( 200 ) ;
320
408
321
- await github . createOrUpdateIssue ( { title : EXISTING_CLOSED_ISSUE . title , description : EXISTING_CLOSED_ISSUE . description , labels : [ 'page access restriction' ] } ) ;
409
+ await github . createOrUpdateIssue ( { title : EXISTING_CLOSED_ISSUE . title , description : EXISTING_CLOSED_ISSUE . description , labels : [ LABELS . HTTP_403 . name ] } ) ;
322
410
} ) ;
323
411
324
412
after ( ( ) => {
@@ -342,14 +430,14 @@ describe('GitHub', function () {
342
430
343
431
before ( async ( ) => {
344
432
updateIssueScope = nock ( 'https://api.github.com' )
345
- . patch ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } ` , { state : GitHub . ISSUE_STATE_OPEN , labels : [ 'empty content' ] } )
433
+ . patch ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } ` , { state : GitHub . ISSUE_STATE_OPEN , labels : [ LABELS . EMPTY_CONTENT . name ] } )
346
434
. reply ( 200 ) ;
347
435
348
436
addCommentScope = nock ( 'https://api.github.com' )
349
437
. post ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } /comments` , { body : EXISTING_OPEN_ISSUE . description } )
350
438
. reply ( 200 ) ;
351
439
352
- await github . createOrUpdateIssue ( { title : EXISTING_OPEN_ISSUE . title , description : EXISTING_OPEN_ISSUE . description , labels : [ 'empty content' ] } ) ;
440
+ await github . createOrUpdateIssue ( { title : EXISTING_OPEN_ISSUE . title , description : EXISTING_OPEN_ISSUE . description , labels : [ LABELS . EMPTY_CONTENT . name ] } ) ;
353
441
} ) ;
354
442
355
443
after ( ( ) => {
@@ -379,7 +467,7 @@ describe('GitHub', function () {
379
467
. post ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } /comments` )
380
468
. reply ( 200 ) ;
381
469
382
- await github . createOrUpdateIssue ( { title : EXISTING_OPEN_ISSUE . title , description : EXISTING_OPEN_ISSUE . description , labels : [ 'page access restriction' , 'server error' ] } ) ;
470
+ await github . createOrUpdateIssue ( { title : EXISTING_OPEN_ISSUE . title , description : EXISTING_OPEN_ISSUE . description , labels : [ LABELS . HTTP_403 . name ] } ) ;
383
471
} ) ;
384
472
385
473
after ( ( ) => {
@@ -403,14 +491,14 @@ describe('GitHub', function () {
403
491
404
492
before ( async ( ) => {
405
493
updateIssueScope = nock ( 'https://api.github.com' )
406
- . patch ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } ` , { state : GitHub . ISSUE_STATE_OPEN , labels : [ 'page access restriction' , 'empty content' ] } )
494
+ . patch ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } ` , { state : GitHub . ISSUE_STATE_OPEN , labels : [ LABELS . HTTP_403 . name , LABELS . NEEDS_INTERVENTION . name ] } )
407
495
. reply ( 200 ) ;
408
496
409
497
addCommentScope = nock ( 'https://api.github.com' )
410
498
. post ( `/repos/owner/repo/issues/${ EXISTING_OPEN_ISSUE . number } /comments` , { body : EXISTING_OPEN_ISSUE . description } )
411
499
. reply ( 200 ) ;
412
500
413
- await github . createOrUpdateIssue ( { title : EXISTING_OPEN_ISSUE . title , description : EXISTING_OPEN_ISSUE . description , labels : [ 'page access restriction' , 'empty content' ] } ) ;
501
+ await github . createOrUpdateIssue ( { title : EXISTING_OPEN_ISSUE . title , description : EXISTING_OPEN_ISSUE . description , labels : [ LABELS . HTTP_403 . name , LABELS . NEEDS_INTERVENTION . name ] } ) ;
414
502
} ) ;
415
503
416
504
after ( ( ) => {
0 commit comments