11const Diffable = require ( './diffable' )
2- const MergeDeep = require ( '../mergeDeep' )
32const NopCommand = require ( '../nopcommand' )
43
54module . exports = class Environments extends Diffable {
@@ -19,13 +18,13 @@ module.exports = class Environments extends Diffable {
1918 }
2019 }
2120
22- async nopifyRequest ( url , options , description ) {
21+ async nopifyRequest ( url , options , description ) {
2322 if ( ! this . nop ) {
24- await this . github . request ( url , options ) ;
23+ await this . github . request ( url , options )
2524 } else {
26- return Promise . resolve ( [
27- new NopCommand ( this . constructor . name , this . repo , url , description )
28- ] )
25+ return Promise . resolve ( [
26+ new NopCommand ( this . constructor . name , this . repo , url , description )
27+ ] )
2928 }
3029 }
3130
@@ -147,7 +146,7 @@ module.exports = class Environments extends Diffable {
147146 custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
148147 }
149148 }
150- await this . nopifyRequest ( ` PUT /repos/:org/:repo/environments/:environment_name` , options , 'Update environment settings' ) ;
149+ await this . nopifyRequest ( ' PUT /repos/:org/:repo/environments/:environment_name' , options , 'Update environment settings' )
151150 }
152151
153152 if ( deploymentBranchPolicy && attrs . deployment_branch_policy && attrs . deployment_branch_policy . custom_branch_policies ) {
@@ -157,14 +156,14 @@ module.exports = class Environments extends Diffable {
157156 await this . nopifyRequest ( 'DELETE /repos/:org/:repo/environments/:environment_name/deployment-branch-policies/:branch_policy_id' , {
158157 ...baseRequestOptions ,
159158 branch_policy_id : policy . id
160- } , 'Delete deployment branch policy' )
159+ } , 'Delete deployment branch policy' )
161160 }
162161
163162 for ( const policy of attrs . deployment_branch_policy . custom_branch_policies ) {
164163 await this . nopifyRequest (
165- 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
166- ...baseRequestOptions ,
167- name : policy
164+ 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
165+ ...baseRequestOptions ,
166+ name : policy
168167 } , 'Create deployment branch policy' )
169168 }
170169 }
@@ -175,22 +174,23 @@ module.exports = class Environments extends Diffable {
175174 for ( const variable of attrs . variables ) {
176175 const existingVariable = existingVariables . find ( ( _var ) => _var . name === variable . name )
177176 if ( existingVariable ) {
178- existingVariables = existingVariables . filter ( _var => _var . name === variable . name )
177+ // Maintain a list of existing variables that will be deleted after the patch/post loop
178+ existingVariables = existingVariables . filter ( _var => _var . name !== variable . name )
179179 if ( existingVariable . value !== variable . value ) {
180180 await this . nopifyRequest (
181181 'PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name' , {
182- ...baseRequestOptions ,
183- variable_name : variable . name ,
184- value : variable . value
182+ ...baseRequestOptions ,
183+ variable_name : variable . name ,
184+ value : variable . value
185185 } , 'Update environment variable'
186- )
186+ )
187187 }
188188 } else {
189189 await this . nopifyRequest (
190190 'POST /repos/:org/:repo/environments/:environment_name/variables' , {
191- ...baseRequestOptions ,
192- name : variable . name ,
193- value : variable . value
191+ ...baseRequestOptions ,
192+ name : variable . name ,
193+ value : variable . value
194194 } , 'Create environment variable'
195195 )
196196 }
@@ -199,8 +199,8 @@ module.exports = class Environments extends Diffable {
199199 for ( const variable of existingVariables ) {
200200 await this . nopifyRequest (
201201 'DELETE /repos/:org/:repo/environments/:environment_name/variables/:variable_name' , {
202- ...baseRequestOptions ,
203- variable_name : variable . name
202+ ...baseRequestOptions ,
203+ variable_name : variable . name
204204 } , 'Delete environment variable'
205205 )
206206 }
@@ -215,19 +215,18 @@ module.exports = class Environments extends Diffable {
215215 if ( ! existingRule ) {
216216 await this . nopifyRequest (
217217 'POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules' , {
218- ...baseRequestOptions ,
219- integration_id : rule . app_id
218+ ...baseRequestOptions ,
219+ integration_id : rule . app_id
220220 } , 'Create deployment protection rule'
221- )
221+ )
222222 }
223223 }
224224 for ( const rule of existingRules ) {
225225 await this . nopifyRequest ( 'DELETE /repos/:org/:repo/environments/:environment_name/deployment_protection_rules/:rule_id' , {
226226 ...baseRequestOptions ,
227227 rule_id : rule . id
228- } , " Delete deployment protection rule" )
228+ } , ' Delete deployment protection rule' )
229229 }
230-
231230 }
232231 }
233232
@@ -237,48 +236,50 @@ module.exports = class Environments extends Diffable {
237236 repo : this . repo . repo ,
238237 environment_name : attrs . name
239238 }
240-
239+
241240 await this . nopifyRequest (
242241 'PUT /repos/:org/:repo/environments/:environment_name' , {
243- ...baseRequestOptions ,
244- wait_timer : attrs . wait_timer ,
245- prevent_self_review : attrs . prevent_self_review ,
246- reviewers : attrs . reviewers ,
247- deployment_branch_policy : attrs . deployment_branch_policy == null ? null : {
242+ ...baseRequestOptions ,
243+ wait_timer : attrs . wait_timer ,
244+ prevent_self_review : attrs . prevent_self_review ,
245+ reviewers : attrs . reviewers ,
246+ deployment_branch_policy : attrs . deployment_branch_policy == null
247+ ? null
248+ : {
248249 protected_branches : ! ! attrs . deployment_branch_policy . protected_branches ,
249250 custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
250- }
251+ }
251252 } , 'Update environment settings' )
252253
253254 if ( attrs . deployment_branch_policy && attrs . deployment_branch_policy . custom_branch_policies ) {
254255 for ( const policy of attrs . deployment_branch_policy . custom_branch_policies ) {
255256 await this . nopifyRequest (
256257 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
257- ...baseRequestOptions ,
258- name : policy . name
258+ ...baseRequestOptions ,
259+ name : policy . name
259260 } , 'Create deployment branch policy'
260261 )
261262 }
262263 }
263264
264265 if ( attrs . variables ) {
265- for ( const variable of attrs . variables ) {
266+ for ( const variable of attrs . variables ) {
266267 await this . nopifyRequest (
267268 'POST /repos/:org/:repo/environments/:environment_name/variables' , {
268269 ...baseRequestOptions ,
269270 name : variable . name ,
270271 value : variable . value
271272 } , 'Create environment variable'
272- )
273- }
273+ )
274274 }
275+ }
275276
276277 if ( attrs . deployment_protection_rules ) {
277278 for ( const rule of attrs . deployment_protection_rules ) {
278279 await this . nopifyRequest (
279280 'POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules' , {
280- ...baseRequestOptions ,
281- integration_id : rule . app_id
281+ ...baseRequestOptions ,
282+ integration_id : rule . app_id
282283 } , 'Create deployment protection rule'
283284 )
284285 }
@@ -293,16 +294,16 @@ module.exports = class Environments extends Diffable {
293294 }
294295
295296 await this . nopifyRequest (
296- 'DELETE /repos/:org/:repo/environments/:environment_name' , {
297- ...baseRequestOptions
298- } , 'Delete environment'
297+ 'DELETE /repos/:org/:repo/environments/:environment_name' , {
298+ ...baseRequestOptions
299+ } , 'Delete environment'
299300 )
300- }
301+ }
301302
302303 sync ( ) {
303304 const resArray = [ ]
304305 if ( this . entries ) {
305- let filteredEntries = this . filterEntries ( )
306+ const filteredEntries = this . filterEntries ( )
306307 return this . find ( ) . then ( existingRecords => {
307308 // Remove any null or undefined values from the diffables (usually comes from repo override)
308309 for ( const entry of filteredEntries ) {
0 commit comments