Skip to content

Commit 3b7eb05

Browse files
fix: made envs application actually NOP (#691)
* feat: add nop to environments * fix: corrected endpoint parameter in nopifyRequest * fix: include updated test * Update environments.test.js
1 parent 1fff61c commit 3b7eb05

File tree

2 files changed

+138
-86
lines changed

2 files changed

+138
-86
lines changed

lib/plugins/environments.js

Lines changed: 101 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ module.exports = class Environments extends Diffable {
1919
}
2020
}
2121

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+
2232
async find () {
2333
const { data: { environments } } = await this.github.request('GET /repos/:org/:repo/environments', {
2434
org: this.repo.owner,
@@ -63,7 +73,6 @@ module.exports = class Environments extends Diffable {
6373
environmentsMapped.push(mapped)
6474
// console.log(mapped);
6575
}
66-
6776
return environmentsMapped
6877
}
6978

@@ -117,9 +126,14 @@ module.exports = class Environments extends Diffable {
117126

118127
async update (existing, attrs) {
119128
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+
}
120134

121135
if (waitTimer || preventSelfReview || reviewers || deploymentBranchPolicy) {
122-
await this.github.request('PUT /repos/:org/:repo/environments/:environment_name', {
136+
const options = {
123137
org: this.repo.owner,
124138
repo: this.repo.repo,
125139
environment_name: attrs.name,
@@ -132,32 +146,26 @@ module.exports = class Environments extends Diffable {
132146
protected_branches: attrs.deployment_branch_policy.protected_branches,
133147
custom_branch_policies: !!attrs.deployment_branch_policy.custom_branch_policies
134148
}
135-
})
149+
}
150+
await this.nopifyRequest(`PUT /repos/:org/:repo/environments/:environment_name`, options, 'Update environment settings');
136151
}
137152

138153
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
144155

145156
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,
150159
branch_policy_id: policy.id
151-
})
160+
}, 'Delete deployment branch policy')
152161
}
153162

154163
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')
161169
}
162170
}
163171

@@ -169,32 +177,32 @@ module.exports = class Environments extends Diffable {
169177
if (existingVariable) {
170178
existingVariables = existingVariables.filter(_var => _var.name === variable.name)
171179
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+
)
179187
}
180188
} 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+
)
188196
}
189197
}
190198

191199
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+
)
198206
}
199207
}
200208

@@ -205,84 +213,91 @@ module.exports = class Environments extends Diffable {
205213
const existingRule = existingRules.find((_rule) => _rule.id === rule.id)
206214

207215
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+
)
214222
}
215223
}
216-
217224
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,
222227
rule_id: rule.id
223-
})
228+
}, "Delete deployment protection rule")
224229
}
230+
225231
}
226232
}
227233

228234
async add (attrs) {
229-
await this.github.request('PUT /repos/:org/:repo/environments/:environment_name', {
235+
const baseRequestOptions = {
230236
org: this.repo.owner,
231237
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
241250
}
242-
})
251+
}, 'Update environment settings')
243252

244253
if (attrs.deployment_branch_policy && attrs.deployment_branch_policy.custom_branch_policies) {
245254
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+
)
252261
}
253262
}
254263

255264
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+
}
264274
}
265-
}
266275

267276
if (attrs.deployment_protection_rules) {
268277
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+
)
275284
}
276285
}
277286
}
278287

279288
async remove (existing) {
280-
await this.github.request('DELETE /repos/:org/:repo/environments/:environment_name', {
289+
const baseRequestOptions = {
281290
org: this.repo.owner,
282291
repo: this.repo.repo,
283292
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+
}
286301

287302
sync () {
288303
const resArray = []

test/unit/lib/plugins/environments.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { when } = require('jest-when')
22
const Environments = require('../../../../lib/plugins/environments')
3+
const NopCommand = require('../../../../lib/nopcommand');
34

45
describe('Environments Plugin test suite', () => {
56
let github
@@ -1060,3 +1061,39 @@ describe('Environments Plugin test suite', () => {
10601061
})
10611062
})
10621063
})
1064+
1065+
describe('nopifyRequest', () => {
1066+
let github;
1067+
let plugin;
1068+
const org = 'bkeepers';
1069+
const repo = 'test';
1070+
const environment_name = 'test-environment';
1071+
const url = 'PUT /repos/:org/:repo/environments/:environment_name';
1072+
const options = { org, repo, environment_name, wait_timer: 1 };
1073+
const description = 'Update environment wait timer';
1074+
1075+
beforeEach(() => {
1076+
github = {
1077+
request: jest.fn(() => Promise.resolve(true))
1078+
};
1079+
plugin = new Environments(undefined, github, { owner: org, repo }, [], { debug: jest.fn(), error: console.error }, []);
1080+
});
1081+
1082+
it('should make a request when nop is false', async () => {
1083+
plugin.nop = false;
1084+
1085+
await plugin.nopifyRequest(url, options, description);
1086+
1087+
expect(github.request).toHaveBeenCalledWith(url, options);
1088+
});
1089+
1090+
it('should return NopCommand when nop is true', async () => {
1091+
plugin.nop = true;
1092+
1093+
const result = await plugin.nopifyRequest(url, options, description);
1094+
1095+
expect(result).toEqual([
1096+
new NopCommand('Environments', { owner: org, repo }, url, description)
1097+
]);
1098+
});
1099+
});

0 commit comments

Comments
 (0)