Skip to content

Commit fc179df

Browse files
committed
Allow concise config for Environments
This commit combines PR [616](github#616) and [646](github#646) environments.js Add defensive code to prevent the GitHub API from being called with undefined data. In the UI, and API an environment can be added with just an name. Now, safe-settings permits this as well. In the UI, and API an environment can be added without variables. Now, safe-settings permits this as well. In the UI, and API an environment can be added without deployment_protection_rules. Now, safe-settings permits this as well. environments.test.js Add a test case for the scenario when there are zero existing environments Add a test case for an environment name change Add a test case inspired by PR 616 which adds 7 new environments with various attributes Move expect statements out of aftereach() as there is now variability in what is expected across test cases. Specifically, when there is no existing environment, that environment should NOT be queried for variables nor deployment_protection_rules
1 parent f1d5036 commit fc179df

File tree

2 files changed

+423
-26
lines changed

2 files changed

+423
-26
lines changed

lib/plugins/environments.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ module.exports = class Environments extends Diffable {
1616
});
1717
}
1818
})
19-
}
19+
}
20+
21+
// Remove 'name' from filtering list so Environments with only a name defined are processed.
22+
MergeDeep.NAME_FIELDS.splice(MergeDeep.NAME_FIELDS.indexOf('name'), 1)
23+
2024
}
2125

2226
async find() {
@@ -160,6 +164,7 @@ module.exports = class Environments extends Diffable {
160164

161165
if(variables) {
162166
let existingVariables = [...existing.variables];
167+
163168
for(let variable of attrs.variables) {
164169
const existingVariable = existingVariables.find((_var) => _var.name === variable.name);
165170
if(existingVariable) {
@@ -197,6 +202,7 @@ module.exports = class Environments extends Diffable {
197202

198203
if(deployment_protection_rules) {
199204
let existingRules = [...existing.deployment_protection_rules];
205+
200206
for(let rule of attrs.deployment_protection_rules) {
201207
const existingRule = existingRules.find((_rule) => _rule.id === rule.id);
202208

@@ -229,13 +235,14 @@ module.exports = class Environments extends Diffable {
229235
wait_timer: attrs.wait_timer,
230236
prevent_self_review: attrs.prevent_self_review,
231237
reviewers: attrs.reviewers,
232-
deployment_branch_policy: attrs.deployment_branch_policy === null ? null : {
233-
protected_branches: attrs.deployment_branch_policy.protected_branches,
238+
deployment_branch_policy: attrs.deployment_branch_policy == null ? null : {
239+
protected_branches: !!attrs.deployment_branch_policy.protected_branches,
234240
custom_branch_policies: !!attrs.deployment_branch_policy.custom_branch_policies
235241
}
236242
});
237243

238244
if(attrs.deployment_branch_policy && attrs.deployment_branch_policy.custom_branch_policies) {
245+
239246
for(let policy of attrs.deployment_branch_policy.custom_branch_policies) {
240247
await this.github.request('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', {
241248
org: this.repo.owner,
@@ -244,26 +251,34 @@ module.exports = class Environments extends Diffable {
244251
name: policy.name
245252
});
246253
}
254+
247255
}
248256

257+
if(attrs.variables) {
249258

250-
for(let variable of attrs.variables) {
251-
await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/variables`, {
252-
org: this.repo.owner,
253-
repo: this.repo.repo,
254-
environment_name: attrs.name,
255-
name: variable.name,
256-
value: variable.value
257-
});
258-
}
259+
for(let variable of attrs.variables) {
260+
await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/variables`, {
261+
org: this.repo.owner,
262+
repo: this.repo.repo,
263+
environment_name: attrs.name,
264+
name: variable.name,
265+
value: variable.value
266+
});
267+
}
268+
269+
}
270+
271+
if(attrs.deployment_protection_rules) {
272+
273+
for(let rule of attrs.deployment_protection_rules) {
274+
await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules`, {
275+
org: this.repo.owner,
276+
repo: this.repo.repo,
277+
environment_name: attrs.name,
278+
integration_id: rule.app_id
279+
});
280+
}
259281

260-
for(let rule of attrs.deployment_protection_rules) {
261-
await this.github.request(`POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules`, {
262-
org: this.repo.owner,
263-
repo: this.repo.repo,
264-
environment_name: attrs.name,
265-
integration_id: rule.app_id
266-
});
267282
}
268283
}
269284

0 commit comments

Comments
 (0)