Skip to content

Commit 50b6c5f

Browse files
committed
plugins/environments.js: Fix creation of new environments
Without this change, environments that do not exist will not be created by safe-settings. New tests are included. Signed-off-by: Kyle Harding <[email protected]>
1 parent 239e547 commit 50b6c5f

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

lib/plugins/environments.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ module.exports = class Environments extends Diffable {
229229
wait_timer: attrs.wait_timer,
230230
prevent_self_review: attrs.prevent_self_review,
231231
reviewers: attrs.reviewers,
232-
deployment_branch_policy: attrs.deployment_branch_policy === null
232+
deployment_branch_policy: attrs.deployment_branch_policy == null
233233
? null
234234
: {
235235
protected_branches: attrs.deployment_branch_policy.protected_branches,
@@ -248,7 +248,7 @@ module.exports = class Environments extends Diffable {
248248
}
249249
}
250250

251-
for (const variable of attrs.variables) {
251+
for (const variable of attrs.variables || []) {
252252
await this.github.request('POST /repos/:org/:repo/environments/:environment_name/variables', {
253253
org: this.repo.owner,
254254
repo: this.repo.repo,
@@ -258,7 +258,7 @@ module.exports = class Environments extends Diffable {
258258
})
259259
}
260260

261-
for (const rule of attrs.deployment_protection_rules) {
261+
for (const rule of attrs.deployment_protection_rules || []) {
262262
await this.github.request('POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', {
263263
org: this.repo.owner,
264264
repo: this.repo.repo,

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

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Environments', () => {
55
let github
66
const org = 'bkeepers'
77
const repo = 'test'
8-
8+
99
function fillEnvironment(attrs) {
1010
if (!attrs.wait_timer) attrs.wait_timer = 0;
1111
if (!attrs.prevent_self_review) attrs.prevent_self_review = false;
@@ -80,7 +80,62 @@ describe('Environments', () => {
8080
app_id: 1
8181
}
8282
]
83-
}
83+
},
84+
{
85+
name: 'new-wait-timer',
86+
wait_timer: 1
87+
},
88+
{
89+
name: 'new-reviewers',
90+
reviewers: [
91+
{
92+
type: 'User',
93+
id: 1
94+
},
95+
{
96+
type: 'Team',
97+
id: 2
98+
}
99+
]
100+
},
101+
{
102+
name: 'new-prevent-self-review',
103+
prevent_self_review: true
104+
},
105+
{
106+
name: 'new-deployment-branch-policy',
107+
deployment_branch_policy: {
108+
protected_branches: true,
109+
custom_branch_policies: false
110+
}
111+
},
112+
{
113+
name: 'new-deployment-branch-policy-custom',
114+
deployment_branch_policy: {
115+
protected_branches: false,
116+
custom_branch_policies: [
117+
'master',
118+
'dev'
119+
]
120+
}
121+
},
122+
{
123+
name: 'new-variables',
124+
variables: [
125+
{
126+
name: 'test',
127+
value: 'test'
128+
}
129+
]
130+
},
131+
{
132+
name: 'new-deployment-protection-rules',
133+
deployment_protection_rules: [
134+
{
135+
app_id: 1
136+
}
137+
]
138+
},
84139
], {
85140
debug: function() {}
86141
});
@@ -121,7 +176,7 @@ describe('Environments', () => {
121176
]
122177
}
123178
});
124-
179+
125180
['wait-timer', 'reviewers', 'prevent-self-review', 'deployment-branch-policy', 'deployment-branch-policy-custom', 'variables', 'deployment-protection-rules'].forEach((environment_name) => {
126181
when(github.request)
127182
.calledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name })
@@ -139,7 +194,7 @@ describe('Environments', () => {
139194
data: {
140195
custom_deployment_protection_rules: []
141196
}
142-
})
197+
})
143198
});
144199

145200
when(github.request)
@@ -271,4 +326,4 @@ describe('Environments', () => {
271326
}));
272327
})
273328
})
274-
})
329+
})

0 commit comments

Comments
 (0)