1
1
const Diffable = require ( './diffable' )
2
+ const MergeDeep = require ( '../mergeDeep' )
3
+ const NopCommand = require ( '../nopcommand' )
2
4
3
5
module . exports = class Environments extends Diffable {
4
6
constructor ( ...args ) {
@@ -14,7 +16,7 @@ module.exports = class Environments extends Diffable {
14
16
} ) ;
15
17
}
16
18
} )
17
- }
19
+ }
18
20
}
19
21
20
22
async find ( ) {
@@ -78,7 +80,7 @@ module.exports = class Environments extends Diffable {
78
80
const wait_timer = existing . wait_timer !== attrs . wait_timer ;
79
81
const prevent_self_review = existing . prevent_self_review !== attrs . prevent_self_review ;
80
82
const reviewers = JSON . stringify ( existing . reviewers . sort ( ( x1 , x2 ) => x1 . id - x2 . id ) ) !== JSON . stringify ( attrs . reviewers . sort ( ( x1 , x2 ) => x1 . id - x2 . id ) ) ;
81
-
83
+
82
84
let existing_custom_branch_policies = existing . deployment_branch_policy === null ? null : existing . deployment_branch_policy . custom_branch_policies ;
83
85
if ( typeof ( existing_custom_branch_policies ) === 'object' && existing_custom_branch_policies !== null ) {
84
86
existing_custom_branch_policies = existing_custom_branch_policies . sort ( ) ;
@@ -158,6 +160,7 @@ module.exports = class Environments extends Diffable {
158
160
159
161
if ( variables ) {
160
162
let existingVariables = [ ...existing . variables ] ;
163
+
161
164
for ( let variable of attrs . variables ) {
162
165
const existingVariable = existingVariables . find ( ( _var ) => _var . name === variable . name ) ;
163
166
if ( existingVariable ) {
@@ -195,6 +198,7 @@ module.exports = class Environments extends Diffable {
195
198
196
199
if ( deployment_protection_rules ) {
197
200
let existingRules = [ ...existing . deployment_protection_rules ] ;
201
+
198
202
for ( let rule of attrs . deployment_protection_rules ) {
199
203
const existingRule = existingRules . find ( ( _rule ) => _rule . id === rule . id ) ;
200
204
@@ -227,13 +231,14 @@ module.exports = class Environments extends Diffable {
227
231
wait_timer : attrs . wait_timer ,
228
232
prevent_self_review : attrs . prevent_self_review ,
229
233
reviewers : attrs . reviewers ,
230
- deployment_branch_policy : attrs . deployment_branch_policy === null ? null : {
231
- protected_branches : attrs . deployment_branch_policy . protected_branches ,
234
+ deployment_branch_policy : attrs . deployment_branch_policy == null ? null : {
235
+ protected_branches : ! ! attrs . deployment_branch_policy . protected_branches ,
232
236
custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
233
237
}
234
238
} ) ;
235
239
236
240
if ( attrs . deployment_branch_policy && attrs . deployment_branch_policy . custom_branch_policies ) {
241
+
237
242
for ( let policy of attrs . deployment_branch_policy . custom_branch_policies ) {
238
243
await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
239
244
org : this . repo . owner ,
@@ -242,26 +247,34 @@ module.exports = class Environments extends Diffable {
242
247
name : policy . name
243
248
} ) ;
244
249
}
245
- }
246
-
247
250
248
- for ( let variable of attrs . variables ) {
249
- await this . github . request ( `POST /repos/:org/:repo/environments/:environment_name/variables` , {
250
- org : this . repo . owner ,
251
- repo : this . repo . repo ,
252
- environment_name : attrs . name ,
253
- name : variable . name ,
254
- value : variable . value
255
- } ) ;
256
251
}
257
252
258
- for ( let rule of attrs . deployment_protection_rules ) {
259
- await this . github . request ( `POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules` , {
260
- org : this . repo . owner ,
261
- repo : this . repo . repo ,
262
- environment_name : attrs . name ,
263
- integration_id : rule . app_id
264
- } ) ;
253
+ if ( attrs . variables ) {
254
+
255
+ for ( let variable of attrs . variables ) {
256
+ await this . github . request ( `POST /repos/:org/:repo/environments/:environment_name/variables` , {
257
+ org : this . repo . owner ,
258
+ repo : this . repo . repo ,
259
+ environment_name : attrs . name ,
260
+ name : variable . name ,
261
+ value : variable . value
262
+ } ) ;
263
+ }
264
+
265
+ }
266
+
267
+ if ( attrs . deployment_protection_rules ) {
268
+
269
+ for ( let rule of attrs . deployment_protection_rules ) {
270
+ await this . github . request ( `POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules` , {
271
+ org : this . repo . owner ,
272
+ repo : this . repo . repo ,
273
+ environment_name : attrs . name ,
274
+ integration_id : rule . app_id
275
+ } ) ;
276
+ }
277
+
265
278
}
266
279
}
267
280
@@ -272,4 +285,79 @@ module.exports = class Environments extends Diffable {
272
285
environment_name : existing . name
273
286
} ) ;
274
287
}
275
- }
288
+
289
+ sync ( ) {
290
+ const resArray = [ ]
291
+ if ( this . entries ) {
292
+ let filteredEntries = this . filterEntries ( )
293
+ return this . find ( ) . then ( existingRecords => {
294
+
295
+ // Remove any null or undefined values from the diffables (usually comes from repo override)
296
+ for ( const entry of filteredEntries ) {
297
+ for ( const key of Object . keys ( entry ) ) {
298
+ if ( entry [ key ] === null || entry [ key ] === undefined ) {
299
+ delete entry [ key ]
300
+ }
301
+ }
302
+ }
303
+ // For environments, we want to keep the entries with only name defined.
304
+
305
+ const changes = [ ]
306
+
307
+ existingRecords . forEach ( x => {
308
+ if ( ! filteredEntries . find ( y => this . comparator ( x , y ) ) ) {
309
+ const change = this . remove ( x ) . then ( res => {
310
+ if ( this . nop ) {
311
+ return resArray . push ( res )
312
+ }
313
+ return res
314
+ } )
315
+ changes . push ( change )
316
+ }
317
+ } )
318
+
319
+ filteredEntries . forEach ( attrs => {
320
+ const existing = existingRecords . find ( record => {
321
+ return this . comparator ( record , attrs )
322
+ } )
323
+
324
+ if ( ! existing ) {
325
+ const change = this . add ( attrs ) . then ( res => {
326
+ if ( this . nop ) {
327
+ return resArray . push ( res )
328
+ }
329
+ return res
330
+ } )
331
+ changes . push ( change )
332
+ } else if ( this . changed ( existing , attrs ) ) {
333
+ const change = this . update ( existing , attrs ) . then ( res => {
334
+ if ( this . nop ) {
335
+ return resArray . push ( res )
336
+ }
337
+ return res
338
+ } )
339
+ changes . push ( change )
340
+ }
341
+ } )
342
+
343
+ if ( this . nop ) {
344
+ return Promise . resolve ( resArray )
345
+ }
346
+ return Promise . all ( changes )
347
+ } ) . catch ( e => {
348
+ if ( this . nop ) {
349
+ if ( e . status === 404 ) {
350
+ // Ignore 404s which can happen in dry-run as the repo may not exist.
351
+ return Promise . resolve ( resArray )
352
+ } else {
353
+ resArray . push ( new NopCommand ( this . constructor . name , this . repo , null , `error ${ e } in ${ this . constructor . name } for repo: ${ JSON . stringify ( this . repo ) } entries ${ JSON . stringify ( this . entries ) } ` , 'ERROR' ) )
354
+ return Promise . resolve ( resArray )
355
+ }
356
+ } else {
357
+ this . logError ( `Error ${ e } in ${ this . constructor . name } for repo: ${ JSON . stringify ( this . repo ) } entries ${ JSON . stringify ( this . entries ) } ` )
358
+ }
359
+ } )
360
+ }
361
+ }
362
+
363
+ }
0 commit comments