Skip to content

Commit cabd7c2

Browse files
JakubBieldecyjphr
andauthored
feat: Add support for tags in deployment branch policies (#796)
* feat: Add support for tags in deployment branch policies * feat: Add support for branch policies as string array --------- Co-authored-by: Yadhav Jayaraman <[email protected]>
1 parent 28b3dfd commit cabd7c2

File tree

3 files changed

+265
-32
lines changed

3 files changed

+265
-32
lines changed

docs/github-settings/6. deployment-environments.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ environments:
2727
- type: User
2828
id: 139262123
2929
deployment_branch_policy:
30-
protected_branches: true
31-
custom_branch_policies: false
30+
protected_branches: false
31+
custom_branch_policies:
32+
- names: ['main','dev']
33+
type: branch
34+
- names: ['v*.*.*']
35+
type: tag
3236
deployment_protection_rules:
33-
- app_id: 25112
37+
- app_id: 25112
3438
variables:
3539
- name: MY_AWESOME_VAR
3640
value: '845705'
@@ -43,7 +47,8 @@ environments:
4347
>[!TIP]
4448
>GitHub's API documentation defines these inputs and types:
4549
>1. [Create or update an environment](https://docs.github.com/en/rest/deployments/environments?apiVersion=2022-11-28#create-or-update-an-environment)
46-
>2. [Create an environment variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#create-an-environment-variable)
50+
>2. [Create a deployment branch policy](https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28#create-a-deployment-branch-policy)
51+
>3. [Create an environment variable](https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#create-an-environment-variable)
4752
4853
<table>
4954
<tr><td>
@@ -126,11 +131,11 @@ environments:
126131

127132
<details><summary>Properties of <code>deployment_branch_policy</code></summary>
128133
<br>
129-
<p>&emsp;<code>protected_branches</code><span style="color:gray;">&emsp;<i>string</i>&emsp;</span><span style="color:orange;">${\text{\color{orange}Required}}$</span></p>
130-
<p>&emsp;&emsp;Whether only branches with branch protection rules can deploy<br>&emsp;&emsp;to this environment. If <code>protected_branches</code> is <code>true</code>,<br>&emsp;&emsp;<code>custom_branch_policies</code> must be <code>false</code>; if <code>protected_branches</code><br>&emsp;&emsp;is <code>false</code>, <code>custom_branch_policies</code> must be <code>true</code>.</p>
134+
<p>&emsp;<code>protected_branches</code><span style="color:gray;">&emsp;<i>boolean</i>&emsp;</span><span style="color:orange;">${\text{\color{orange}Required}}$</span></p>
135+
<p>&emsp;&emsp;Whether only branches with branch protection rules can deploy<br>&emsp;&emsp;to this environment. If <code>protected_branches</code> is <code>true</code>,<br>&emsp;&emsp;<code>custom_branch_policies</code> must be <code>false</code>; if <code>protected_branches</code><br>&emsp;&emsp;is <code>false</code>, <code>custom_branch_policies</code> must be an object.</p>
131136

132-
<p>&emsp;<code>id</code><span style="color:gray;">&emsp;<i>integer</i>&emsp;</span></p>
133-
<p>&emsp;&emsp;Whether only branches that match the specified name patterns<br>&emsp;&emsp;can deploy to this environment. If <code>custom_branch_policies</code><br>&emsp;&emsp;is <code>true</code>, <code>protected_branches</code> must be <code>false</code>; if<br>&emsp;&emsp;<code>custom_branch_policies</code> is <code>false</code>, <code>protected_branches</code><br>&emsp;&emsp;must be <code>true</code>.</p>
137+
<p>&emsp;<code>custom_branch_policies</code><span style="color:gray;">&emsp;<i>boolean or object</i>&emsp;</span></p>
138+
<p>&emsp;&emsp;Whether only branches that match the specified name patterns<br>&emsp;&emsp;can deploy to this environment. If <code>custom_branch_policies</code><br>&emsp;&emsp;is <code>false</code>, <code>protected_branches</code> must be <code>true</code>; if<br>&emsp;&emsp;<code>custom_branch_policies</code> is an object, <code>protected_branches</code><br>&emsp;&emsp;must be <code>false</code>.</p>
134139

135140
</details>
136141

@@ -142,8 +147,12 @@ environments:
142147
- name: production
143148
...
144149
deployment_branch_policy:
145-
protected_branches: true
146-
custom_branch_policies: false
150+
protected_branches: false
151+
custom_branch_policies:
152+
- names: ['main','dev']
153+
type: branch
154+
- names: ['v*.*.*']
155+
type: tag
147156
...
148157
```
149158

lib/plugins/environments.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ module.exports = class Environments extends Diffable {
1414
variable.name = variable.name.toLowerCase()
1515
})
1616
}
17+
// Expand custom_branch_policies to match GitHub schema.
18+
if (environment.deployment_branch_policy && environment.deployment_branch_policy.custom_branch_policies) {
19+
const policies = []
20+
environment.deployment_branch_policy.custom_branch_policies.forEach(policy => {
21+
if (typeof policy === 'string') {
22+
// Convert string policy to object with type 'branch'
23+
policies.push({ name: policy, type: 'branch' })
24+
} else if (typeof policy === 'object' && Array.isArray(policy.names)) {
25+
policy.names.forEach(name => {
26+
policies.push({ name: name, type: policy.type })
27+
})
28+
}
29+
})
30+
environment.deployment_branch_policy.custom_branch_policies = policies
31+
}
1732
})
1833
}
1934
}
@@ -52,7 +67,8 @@ module.exports = class Environments extends Diffable {
5267
repo: this.repo.repo,
5368
environment_name: environment.name
5469
})).data.branch_policies.map(policy => ({
55-
name: policy.name
70+
name: policy.name,
71+
type: policy.type
5672
}))
5773
},
5874
variables: (await this.github.request('GET /repos/:org/:repo/environments/:environment_name/variables', {
@@ -93,11 +109,11 @@ module.exports = class Environments extends Diffable {
93109

94110
let existingCustomBranchPolicies = existing.deployment_branch_policy === null ? null : existing.deployment_branch_policy.custom_branch_policies
95111
if (typeof (existingCustomBranchPolicies) === 'object' && existingCustomBranchPolicies !== null) {
96-
existingCustomBranchPolicies = existingCustomBranchPolicies.sort()
112+
existingCustomBranchPolicies = existingCustomBranchPolicies.sort((x1, x2) => x1.name.localeCompare(x2.name))
97113
}
98114
let attrsCustomBranchPolicies = attrs.deployment_branch_policy === null ? null : attrs.deployment_branch_policy.custom_branch_policies
99115
if (typeof (attrsCustomBranchPolicies) === 'object' && attrsCustomBranchPolicies !== null) {
100-
attrsCustomBranchPolicies = attrsCustomBranchPolicies.sort()
116+
attrsCustomBranchPolicies = attrsCustomBranchPolicies.sort((x1, x2) => x1.name.localeCompare(x2.name))
101117
}
102118
let deploymentBranchPolicy
103119
if (existing.deployment_branch_policy === attrs.deployment_branch_policy) {
@@ -163,7 +179,8 @@ module.exports = class Environments extends Diffable {
163179
await this.nopifyRequest(
164180
'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', {
165181
...baseRequestOptions,
166-
name: policy
182+
name: policy.name,
183+
type: policy.type
167184
}, 'Create deployment branch policy')
168185
}
169186
}
@@ -256,7 +273,8 @@ module.exports = class Environments extends Diffable {
256273
await this.nopifyRequest(
257274
'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', {
258275
...baseRequestOptions,
259-
name: policy.name
276+
name: policy.name,
277+
type: policy.type
260278
}, 'Create deployment branch policy'
261279
)
262280
}

0 commit comments

Comments
 (0)