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 GitLab from './index.js' ;
7
7
@@ -18,34 +18,128 @@ describe('GitLab', function () {
18
18
} ) ;
19
19
20
20
describe ( '#initialize' , ( ) => {
21
- const scopes = [ ] ;
21
+ context ( 'when some labels are missing' , ( ) => {
22
+ const scopes = [ ] ;
22
23
23
- before ( async ( ) => {
24
- const existingLabels = MANAGED_LABELS . slice ( 0 , - 2 ) ;
24
+ before ( async ( ) => {
25
+ const existingLabels = MANAGED_LABELS . slice ( 0 , - 2 ) . map ( label => ( {
26
+ ...label ,
27
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
28
+ } ) ) ;
25
29
26
- nock ( gitlab . apiBaseURL )
27
- . get ( `/projects/${ encodeURIComponent ( 'owner/repo' ) } ` )
28
- . reply ( 200 , { id : PROJECT_ID } ) ;
30
+ nock ( gitlab . apiBaseURL )
31
+ . get ( `/projects/${ encodeURIComponent ( 'owner/repo' ) } ` )
32
+ . reply ( 200 , { id : PROJECT_ID } ) ;
29
33
30
- nock ( gitlab . apiBaseURL )
31
- . get ( `/projects/${ PROJECT_ID } /labels?with_counts=true` )
32
- . reply ( 200 , existingLabels ) ;
34
+ nock ( gitlab . apiBaseURL )
35
+ . get ( `/projects/${ PROJECT_ID } /labels?with_counts=true` )
36
+ . reply ( 200 , existingLabels ) ;
33
37
34
- const missingLabels = MANAGED_LABELS . slice ( - 2 ) ;
38
+ const missingLabels = MANAGED_LABELS . slice ( - 2 ) ;
35
39
36
- for ( const label of missingLabels ) {
37
- scopes . push ( nock ( gitlab . apiBaseURL )
38
- . post ( `/projects/${ PROJECT_ID } /labels` )
39
- . reply ( 200 , { name : label . name } ) ) ;
40
- }
40
+ for ( const label of missingLabels ) {
41
+ scopes . push ( nock ( gitlab . apiBaseURL )
42
+ . post ( `/projects/${ PROJECT_ID } /labels` )
43
+ . reply ( 200 , { name : label . name } ) ) ;
44
+ }
41
45
42
- await gitlab . initialize ( ) ;
46
+ await gitlab . initialize ( ) ;
47
+ } ) ;
48
+
49
+ after ( nock . cleanAll ) ;
50
+
51
+ it ( 'should create missing labels' , ( ) => {
52
+ scopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
53
+ } ) ;
43
54
} ) ;
44
55
45
- after ( nock . cleanAll ) ;
56
+ context ( 'when some labels are obsolete' , ( ) => {
57
+ const deleteScopes = [ ] ;
58
+
59
+ before ( async ( ) => {
60
+ const existingLabels = [
61
+ ...MANAGED_LABELS . map ( label => ( {
62
+ ...label ,
63
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
64
+ } ) ) ,
65
+ // Add an obsolete label that should be removed
66
+ {
67
+ name : 'obsolete label' ,
68
+ color : '#FF0000' ,
69
+ description : `This label is no longer used ${ MANAGED_BY_OTA_MARKER } ` ,
70
+ } ,
71
+ ] ;
72
+
73
+ nock ( gitlab . apiBaseURL )
74
+ . get ( `/projects/${ encodeURIComponent ( 'owner/repo' ) } ` )
75
+ . reply ( 200 , { id : PROJECT_ID } ) ;
76
+
77
+ nock ( gitlab . apiBaseURL )
78
+ . get ( `/projects/${ PROJECT_ID } /labels?with_counts=true` )
79
+ . reply ( 200 , existingLabels ) ;
46
80
47
- it ( 'should create missing labels' , ( ) => {
48
- scopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
81
+ // Mock the delete call for the obsolete label
82
+ deleteScopes . push ( nock ( gitlab . apiBaseURL )
83
+ . delete ( `/projects/${ PROJECT_ID } /labels/${ encodeURIComponent ( 'obsolete label' ) } ` )
84
+ . reply ( 200 ) ) ;
85
+
86
+ // Mock the second getRepositoryLabels call after deletion
87
+ nock ( gitlab . apiBaseURL )
88
+ . get ( `/projects/${ PROJECT_ID } /labels?with_counts=true` )
89
+ . reply ( 200 , MANAGED_LABELS . map ( label => ( {
90
+ ...label ,
91
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
92
+ } ) ) ) ;
93
+
94
+ await gitlab . initialize ( ) ;
95
+ } ) ;
96
+
97
+ after ( nock . cleanAll ) ;
98
+
99
+ it ( 'should remove obsolete managed labels' , ( ) => {
100
+ deleteScopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
101
+ } ) ;
102
+ } ) ;
103
+
104
+ context ( 'when some labels have changed descriptions' , ( ) => {
105
+ const updateScopes = [ ] ;
106
+
107
+ before ( async ( ) => {
108
+ const originalTestLabels = MANAGED_LABELS . slice ( - 2 ) ;
109
+ const testLabels = originalTestLabels . map ( label => ( {
110
+ ...label ,
111
+ description : `${ label . description } - obsolete description` ,
112
+ } ) ) ;
113
+
114
+ nock ( gitlab . apiBaseURL )
115
+ . get ( `/projects/${ encodeURIComponent ( 'owner/repo' ) } ` )
116
+ . reply ( 200 , { id : PROJECT_ID } ) ;
117
+
118
+ nock ( gitlab . apiBaseURL )
119
+ . persist ( )
120
+ . get ( `/projects/${ PROJECT_ID } /labels?with_counts=true` )
121
+ . reply ( 200 , [ ...MANAGED_LABELS . slice ( 0 , - 2 ) , ...testLabels ] . map ( label => ( {
122
+ ...label ,
123
+ description : `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` ,
124
+ } ) ) ) ;
125
+
126
+ for ( const label of originalTestLabels ) {
127
+ updateScopes . push ( nock ( gitlab . apiBaseURL )
128
+ . put ( `/projects/${ PROJECT_ID } /labels/${ encodeURIComponent ( label . name ) } ` , body =>
129
+ body . description === `${ label . description } ${ MANAGED_BY_OTA_MARKER } ` )
130
+ . reply ( 200 , { name : label . name , color : `#${ label . color } ` } ) ) ;
131
+ }
132
+
133
+ await gitlab . initialize ( ) ;
134
+ } ) ;
135
+
136
+ after ( ( ) => {
137
+ nock . cleanAll ( ) ;
138
+ } ) ;
139
+
140
+ it ( 'should update labels with changed descriptions' , ( ) => {
141
+ updateScopes . forEach ( scope => expect ( scope . isDone ( ) ) . to . be . true ) ;
142
+ } ) ;
49
143
} ) ;
50
144
} ) ;
51
145
0 commit comments