Skip to content

Commit

Permalink
feat(environments): custom branch policies support type ('branch', 't…
Browse files Browse the repository at this point in the history
…ag') (#910)
  • Loading branch information
goruha committed Jun 28, 2024
1 parent d8387f9 commit 7c25368
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/plugins/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ environments:
custom_branches:
- main
- dev/*
- name: release/*
type: branch
- name: v*
type: tag
```
22 changes: 18 additions & 4 deletions lib/plugins/environments.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ function deploymentBranchPolicyToString (attrs) {
return JSON.stringify(
shouldUseProtectedBranches(attrs.protected_branches, attrs.custom_branches)
? { protected_branches: true }
: { custom_branches: attrs.custom_branches.sort() }
: {
custom_branches: attrs.custom_branches.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)))
}
)
}
}
Expand Down Expand Up @@ -66,6 +68,14 @@ module.exports = class Environments extends Diffable {
// Force all names to lowercase to avoid comparison issues.
this.entries.forEach(environment => {
environment.name = environment.name.toLowerCase()
if (environment.deployment_branch_policy && environment.deployment_branch_policy.custom_branches) {
environment.deployment_branch_policy.custom_branches = environment.deployment_branch_policy.custom_branches.map(
rule => ({
name: rule.name || rule,
type: rule.type || 'branch'
})
)
}
})
}
}
Expand All @@ -88,7 +98,10 @@ module.exports = class Environments extends Diffable {
environment.name
)
environment.deployment_branch_policy = {
custom_branches: branchPolicies.map(_ => _.name)
custom_branches: branchPolicies.map(_ => ({
name: _.name,
type: _.type
}))
}
} else {
environment.deployment_branch_policy = {
Expand Down Expand Up @@ -147,12 +160,13 @@ module.exports = class Environments extends Diffable {

if (attrs.deployment_branch_policy && attrs.deployment_branch_policy.custom_branches) {
await Promise.all(
attrs.deployment_branch_policy.custom_branches.map(name =>
attrs.deployment_branch_policy.custom_branches.map(rule =>
this.github.request('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', {
org: this.repo.owner,
repo: this.repo.repo,
environment_name: attrs.name,
name
name: rule.name,
type: rule.type
})
)
)
Expand Down
15 changes: 12 additions & 3 deletions test/integration/features/step_definitions/environments-steps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ Given('an environment exists with a {string} branches deployment branch policy',
)

if (policyType === 'custom') {
this.customBranches = any.listOf(() => ({ name: any.word(), id: any.integer() }))
this.customBranches = any.listOf(() => ({
name: any.word(),
id: any.integer(),
type: any.fromList(['branch'])
}))
this.removedDeploymentBranchPolicyIds = {}

this.server.use(
Expand Down Expand Up @@ -373,7 +377,12 @@ Given('an environment is defined in the config with a protected branches deploym

Given('an environment is defined in the config with a custom branches deployment branch policy', async function () {
this.environmentName = any.word()
this.customBranchNames = any.listOf(any.word)
this.customBranches = any.listOf(() => ({
name: any.word(),
id: any.integer(),
type: any.fromList(['branch'])
}))
this.customBranchNames = this.customBranches.map(branch => branch.name)
this.createdDeploymentBranchPolicyNames = {}

this.server.use(
Expand All @@ -388,7 +397,7 @@ Given('an environment is defined in the config with a custom branches deployment
environments: [
{
name: this.environmentName,
deployment_branch_policy: { protected_branches: false, custom_branches: this.customBranchNames }
deployment_branch_policy: { protected_branches: false, custom_branches: this.customBranches }
}
]
})
Expand Down
Loading

0 comments on commit 7c25368

Please sign in to comment.