11const Diffable = require ( './diffable' )
2+ const MergeDeep = require ( '../mergeDeep' )
3+ const NopCommand = require ( '../nopcommand' )
24
35module . exports = class Environments extends Diffable {
46 constructor ( ...args ) {
@@ -14,7 +16,7 @@ module.exports = class Environments extends Diffable {
1416 } ) ;
1517 }
1618 } )
17- }
19+ }
1820 }
1921
2022 async find ( ) {
@@ -78,7 +80,7 @@ module.exports = class Environments extends Diffable {
7880 const wait_timer = existing . wait_timer !== attrs . wait_timer ;
7981 const prevent_self_review = existing . prevent_self_review !== attrs . prevent_self_review ;
8082 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+
8284 let existing_custom_branch_policies = existing . deployment_branch_policy === null ? null : existing . deployment_branch_policy . custom_branch_policies ;
8385 if ( typeof ( existing_custom_branch_policies ) === 'object' && existing_custom_branch_policies !== null ) {
8486 existing_custom_branch_policies = existing_custom_branch_policies . sort ( ) ;
@@ -158,6 +160,7 @@ module.exports = class Environments extends Diffable {
158160
159161 if ( variables ) {
160162 let existingVariables = [ ...existing . variables ] ;
163+
161164 for ( let variable of attrs . variables ) {
162165 const existingVariable = existingVariables . find ( ( _var ) => _var . name === variable . name ) ;
163166 if ( existingVariable ) {
@@ -195,6 +198,7 @@ module.exports = class Environments extends Diffable {
195198
196199 if ( deployment_protection_rules ) {
197200 let existingRules = [ ...existing . deployment_protection_rules ] ;
201+
198202 for ( let rule of attrs . deployment_protection_rules ) {
199203 const existingRule = existingRules . find ( ( _rule ) => _rule . id === rule . id ) ;
200204
@@ -227,13 +231,14 @@ module.exports = class Environments extends Diffable {
227231 wait_timer : attrs . wait_timer ,
228232 prevent_self_review : attrs . prevent_self_review ,
229233 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 ,
232236 custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
233237 }
234238 } ) ;
235239
236240 if ( attrs . deployment_branch_policy && attrs . deployment_branch_policy . custom_branch_policies ) {
241+
237242 for ( let policy of attrs . deployment_branch_policy . custom_branch_policies ) {
238243 await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
239244 org : this . repo . owner ,
@@ -242,26 +247,34 @@ module.exports = class Environments extends Diffable {
242247 name : policy . name
243248 } ) ;
244249 }
245- }
246-
247250
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- } ) ;
256251 }
257252
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+
265278 }
266279 }
267280
@@ -272,4 +285,79 @@ module.exports = class Environments extends Diffable {
272285 environment_name : existing . name
273286 } ) ;
274287 }
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