@@ -19,6 +19,16 @@ module.exports = class Environments extends Diffable {
19
19
}
20
20
}
21
21
22
+ async nopifyRequest ( url , options , description ) {
23
+ if ( ! this . nop ) {
24
+ await this . github . request ( url , options ) ;
25
+ } else {
26
+ return Promise . resolve ( [
27
+ new NopCommand ( this . constructor . name , this . repo , url , description )
28
+ ] )
29
+ }
30
+ }
31
+
22
32
async find ( ) {
23
33
const { data : { environments } } = await this . github . request ( 'GET /repos/:org/:repo/environments' , {
24
34
org : this . repo . owner ,
@@ -63,7 +73,6 @@ module.exports = class Environments extends Diffable {
63
73
environmentsMapped . push ( mapped )
64
74
// console.log(mapped);
65
75
}
66
-
67
76
return environmentsMapped
68
77
}
69
78
@@ -117,9 +126,14 @@ module.exports = class Environments extends Diffable {
117
126
118
127
async update ( existing , attrs ) {
119
128
const { waitTimer, preventSelfReview, reviewers, deploymentBranchPolicy, variables, deploymentProtectionRules } = this . getChanged ( existing , attrs )
129
+ const baseRequestOptions = {
130
+ org : this . repo . owner ,
131
+ repo : this . repo . repo ,
132
+ environment_name : attrs . name
133
+ }
120
134
121
135
if ( waitTimer || preventSelfReview || reviewers || deploymentBranchPolicy ) {
122
- await this . github . request ( 'PUT /repos/:org/:repo/environments/:environment_name' , {
136
+ const options = {
123
137
org : this . repo . owner ,
124
138
repo : this . repo . repo ,
125
139
environment_name : attrs . name ,
@@ -132,32 +146,26 @@ module.exports = class Environments extends Diffable {
132
146
protected_branches : attrs . deployment_branch_policy . protected_branches ,
133
147
custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
134
148
}
135
- } )
149
+ }
150
+ await this . nopifyRequest ( `PUT /repos/:org/:repo/environments/:environment_name` , options , 'Update environment settings' ) ;
136
151
}
137
152
138
153
if ( deploymentBranchPolicy && attrs . deployment_branch_policy && attrs . deployment_branch_policy . custom_branch_policies ) {
139
- const existingPolicies = ( await this . github . request ( 'GET /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
140
- org : this . repo . owner ,
141
- repo : this . repo . repo ,
142
- environment_name : attrs . name
143
- } ) ) . data . branch_policies
154
+ const existingPolicies = ( await this . github . request ( 'GET /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , baseRequestOptions ) ) . data . branch_policies
144
155
145
156
for ( const policy of existingPolicies ) {
146
- await this . github . request ( 'DELETE /repos/:org/:repo/environments/:environment_name/deployment-branch-policies/:branch_policy_id' , {
147
- org : this . repo . owner ,
148
- repo : this . repo . repo ,
149
- environment_name : attrs . name ,
157
+ await this . nopifyRequest ( 'DELETE /repos/:org/:repo/environments/:environment_name/deployment-branch-policies/:branch_policy_id' , {
158
+ ...baseRequestOptions ,
150
159
branch_policy_id : policy . id
151
- } )
160
+ } , 'Delete deployment branch policy' )
152
161
}
153
162
154
163
for ( const policy of attrs . deployment_branch_policy . custom_branch_policies ) {
155
- await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
156
- org : this . repo . owner ,
157
- repo : this . repo . repo ,
158
- environment_name : attrs . name ,
159
- name : policy
160
- } )
164
+ await this . nopifyRequest (
165
+ 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
166
+ ...baseRequestOptions ,
167
+ name : policy
168
+ } , 'Create deployment branch policy' )
161
169
}
162
170
}
163
171
@@ -169,32 +177,32 @@ module.exports = class Environments extends Diffable {
169
177
if ( existingVariable ) {
170
178
existingVariables = existingVariables . filter ( _var => _var . name === variable . name )
171
179
if ( existingVariable . value !== variable . value ) {
172
- await this . github . request ( 'PATCH /repos/:org/:repo/environments/:environment_name/variables/:variable_name' , {
173
- org : this . repo . owner ,
174
- repo : this . repo . repo ,
175
- environment_name : attrs . name ,
176
- variable_name : variable . name ,
177
- value : variable . value
178
- } )
180
+ await this . nopifyRequest (
181
+ 'PATCH /repos/: org/: repo/environments/:environment_name/variables/:variable_name' , {
182
+ ... baseRequestOptions ,
183
+ variable_name : variable . name ,
184
+ value : variable . value
185
+ } , 'Update environment variable'
186
+ )
179
187
}
180
188
} else {
181
- await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/variables' , {
182
- org : this . repo . owner ,
183
- repo : this . repo . repo ,
184
- environment_name : attrs . name ,
185
- name : variable . name ,
186
- value : variable . value
187
- } )
189
+ await this . nopifyRequest (
190
+ 'POST /repos/: org/: repo/environments/:environment_name/variables' , {
191
+ ... baseRequestOptions ,
192
+ name : variable . name ,
193
+ value : variable . value
194
+ } , 'Create environment variable'
195
+ )
188
196
}
189
197
}
190
198
191
199
for ( const variable of existingVariables ) {
192
- await this . github . request ( 'DELETE /repos/:org/:repo/environments/:environment_name/variables/:variable_name' , {
193
- org : this . repo . owner ,
194
- repo : this . repo . repo ,
195
- environment_name : attrs . name ,
196
- variable_name : variable . name
197
- } )
200
+ await this . nopifyRequest (
201
+ 'DELETE /repos/: org/: repo/environments/:environment_name/variables/:variable_name' , {
202
+ ... baseRequestOptions ,
203
+ variable_name : variable . name
204
+ } , 'Delete environment variable'
205
+ )
198
206
}
199
207
}
200
208
@@ -205,84 +213,91 @@ module.exports = class Environments extends Diffable {
205
213
const existingRule = existingRules . find ( ( _rule ) => _rule . id === rule . id )
206
214
207
215
if ( ! existingRule ) {
208
- await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules' , {
209
- org : this . repo . owner ,
210
- repo : this . repo . repo ,
211
- environment_name : attrs . name ,
212
- integration_id : rule . app_id
213
- } )
216
+ await this . nopifyRequest (
217
+ 'POST /repos/: org/: repo/environments/:environment_name/deployment_protection_rules' , {
218
+ ... baseRequestOptions ,
219
+ integration_id : rule . app_id
220
+ } , 'Create deployment protection rule'
221
+ )
214
222
}
215
223
}
216
-
217
224
for ( const rule of existingRules ) {
218
- await this . github . request ( 'DELETE /repos/:org/:repo/environments/:environment_name/deployment_protection_rules/:rule_id' , {
219
- org : this . repo . owner ,
220
- repo : this . repo . repo ,
221
- environment_name : attrs . name ,
225
+ await this . nopifyRequest ( 'DELETE /repos/:org/:repo/environments/:environment_name/deployment_protection_rules/:rule_id' , {
226
+ ...baseRequestOptions ,
222
227
rule_id : rule . id
223
- } )
228
+ } , "Delete deployment protection rule" )
224
229
}
230
+
225
231
}
226
232
}
227
233
228
234
async add ( attrs ) {
229
- await this . github . request ( 'PUT /repos/:org/:repo/environments/:environment_name' , {
235
+ const baseRequestOptions = {
230
236
org : this . repo . owner ,
231
237
repo : this . repo . repo ,
232
- environment_name : attrs . name ,
233
- wait_timer : attrs . wait_timer ,
234
- prevent_self_review : attrs . prevent_self_review ,
235
- reviewers : attrs . reviewers ,
236
- deployment_branch_policy : attrs . deployment_branch_policy == null
237
- ? null
238
- : {
239
- protected_branches : ! ! attrs . deployment_branch_policy . protected_branches ,
240
- custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
238
+ environment_name : attrs . name
239
+ }
240
+
241
+ await this . nopifyRequest (
242
+ '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 : {
248
+ protected_branches : ! ! attrs . deployment_branch_policy . protected_branches ,
249
+ custom_branch_policies : ! ! attrs . deployment_branch_policy . custom_branch_policies
241
250
}
242
- } )
251
+ } , 'Update environment settings' )
243
252
244
253
if ( attrs . deployment_branch_policy && attrs . deployment_branch_policy . custom_branch_policies ) {
245
254
for ( const policy of attrs . deployment_branch_policy . custom_branch_policies ) {
246
- await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies' , {
247
- org : this . repo . owner ,
248
- repo : this . repo . repo ,
249
- environment_name : attrs . name ,
250
- name : policy . name
251
- } )
255
+ await this . nopifyRequest (
256
+ 'POST /repos/: org/: repo/environments/:environment_name/deployment-branch-policies' , {
257
+ ... baseRequestOptions ,
258
+ name : policy . name
259
+ } , 'Create deployment branch policy'
260
+ )
252
261
}
253
262
}
254
263
255
264
if ( attrs . variables ) {
256
- for ( const variable of attrs . variables ) {
257
- await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/variables' , {
258
- org : this . repo . owner ,
259
- repo : this . repo . repo ,
260
- environment_name : attrs . name ,
261
- name : variable . name ,
262
- value : variable . value
263
- } )
265
+ for ( const variable of attrs . variables ) {
266
+ await this . nopifyRequest (
267
+ 'POST /repos/:org/:repo/environments/:environment_name/variables' , {
268
+ ...baseRequestOptions ,
269
+ name : variable . name ,
270
+ value : variable . value
271
+ } , 'Create environment variable'
272
+ )
273
+ }
264
274
}
265
- }
266
275
267
276
if ( attrs . deployment_protection_rules ) {
268
277
for ( const rule of attrs . deployment_protection_rules ) {
269
- await this . github . request ( 'POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules' , {
270
- org : this . repo . owner ,
271
- repo : this . repo . repo ,
272
- environment_name : attrs . name ,
273
- integration_id : rule . app_id
274
- } )
278
+ await this . nopifyRequest (
279
+ 'POST /repos/: org/: repo/environments/:environment_name/deployment_protection_rules' , {
280
+ ... baseRequestOptions ,
281
+ integration_id : rule . app_id
282
+ } , 'Create deployment protection rule'
283
+ )
275
284
}
276
285
}
277
286
}
278
287
279
288
async remove ( existing ) {
280
- await this . github . request ( 'DELETE /repos/:org/:repo/environments/:environment_name' , {
289
+ const baseRequestOptions = {
281
290
org : this . repo . owner ,
282
291
repo : this . repo . repo ,
283
292
environment_name : existing . name
284
- } )
285
- }
293
+ }
294
+
295
+ await this . nopifyRequest (
296
+ 'DELETE /repos/:org/:repo/environments/:environment_name' , {
297
+ ...baseRequestOptions
298
+ } , 'Delete environment'
299
+ )
300
+ }
286
301
287
302
sync ( ) {
288
303
const resArray = [ ]
0 commit comments