Skip to content

Commit

Permalink
plugins/environments.js: Fix creation of new environments
Browse files Browse the repository at this point in the history
Without this change, environments that do not exist will
not be created by safe-settings.

New tests are included.

Signed-off-by: Kyle Harding <kyle@balena.io>
  • Loading branch information
klutchell committed Apr 4, 2024
1 parent 7927530 commit 6b0d187
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/plugins/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ module.exports = class Environments extends Diffable {
wait_timer: attrs.wait_timer,
prevent_self_review: attrs.prevent_self_review,
reviewers: attrs.reviewers,
deployment_branch_policy: attrs.deployment_branch_policy === null
deployment_branch_policy: attrs.deployment_branch_policy == null
? null
: {
protected_branches: attrs.deployment_branch_policy.protected_branches,
Expand All @@ -248,7 +248,7 @@ module.exports = class Environments extends Diffable {
}
}

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

for (const rule of attrs.deployment_protection_rules) {
for (const rule of attrs.deployment_protection_rules || []) {
await this.github.request('POST /repos/:org/:repo/environments/:environment_name/deployment_protection_rules', {
org: this.repo.owner,
repo: this.repo.repo,
Expand Down
65 changes: 60 additions & 5 deletions test/unit/lib/plugins/environments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('Environments', () => {
let github
const org = 'bkeepers'
const repo = 'test'

function fillEnvironment(attrs) {
if (!attrs.wait_timer) attrs.wait_timer = 0;
if (!attrs.prevent_self_review) attrs.prevent_self_review = false;
Expand Down Expand Up @@ -80,7 +80,62 @@ describe('Environments', () => {
app_id: 1
}
]
}
},
{
name: 'new-wait-timer',
wait_timer: 1
},
{
name: 'new-reviewers',
reviewers: [
{
type: 'User',
id: 1
},
{
type: 'Team',
id: 2
}
]
},
{
name: 'new-prevent-self-review',
prevent_self_review: true
},
{
name: 'new-deployment-branch-policy',
deployment_branch_policy: {
protected_branches: true,
custom_branch_policies: false
}
},
{
name: 'new-deployment-branch-policy-custom',
deployment_branch_policy: {
protected_branches: false,
custom_branch_policies: [
'master',
'dev'
]
}
},
{
name: 'new-variables',
variables: [
{
name: 'test',
value: 'test'
}
]
},
{
name: 'new-deployment-protection-rules',
deployment_protection_rules: [
{
app_id: 1
}
]
},
], {
debug: function() {}
});
Expand Down Expand Up @@ -121,7 +176,7 @@ describe('Environments', () => {
]
}
});

['wait-timer', 'reviewers', 'prevent-self-review', 'deployment-branch-policy', 'deployment-branch-policy-custom', 'variables', 'deployment-protection-rules'].forEach((environment_name) => {
when(github.request)
.calledWith('GET /repos/:org/:repo/environments/:environment_name/variables', { org, repo, environment_name })
Expand All @@ -139,7 +194,7 @@ describe('Environments', () => {
data: {
custom_deployment_protection_rules: []
}
})
})
});

when(github.request)
Expand Down Expand Up @@ -271,4 +326,4 @@ describe('Environments', () => {
}));
})
})
})
})

0 comments on commit 6b0d187

Please sign in to comment.