Skip to content

Commit 0db86e8

Browse files
committed
feat: Add support for tags in deployment branch policies
1 parent d22c065 commit 0db86e8

File tree

3 files changed

+104
-29
lines changed

3 files changed

+104
-29
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: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ 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+
policy.names.forEach(name => {
22+
policies.push({ name: name, type: policy.type })
23+
})
24+
})
25+
environment.deployment_branch_policy.custom_branch_policies = policies
26+
}
1727
})
1828
}
1929
}
@@ -52,7 +62,8 @@ module.exports = class Environments extends Diffable {
5262
repo: this.repo.repo,
5363
environment_name: environment.name
5464
})).data.branch_policies.map(policy => ({
55-
name: policy.name
65+
name: policy.name,
66+
type: policy.type
5667
}))
5768
},
5869
variables: (await this.github.request('GET /repos/:org/:repo/environments/:environment_name/variables', {
@@ -93,11 +104,11 @@ module.exports = class Environments extends Diffable {
93104

94105
let existingCustomBranchPolicies = existing.deployment_branch_policy === null ? null : existing.deployment_branch_policy.custom_branch_policies
95106
if (typeof (existingCustomBranchPolicies) === 'object' && existingCustomBranchPolicies !== null) {
96-
existingCustomBranchPolicies = existingCustomBranchPolicies.sort()
107+
existingCustomBranchPolicies = existingCustomBranchPolicies.sort((x1, x2) => x1.name.localeCompare(x2.name))
97108
}
98109
let attrsCustomBranchPolicies = attrs.deployment_branch_policy === null ? null : attrs.deployment_branch_policy.custom_branch_policies
99110
if (typeof (attrsCustomBranchPolicies) === 'object' && attrsCustomBranchPolicies !== null) {
100-
attrsCustomBranchPolicies = attrsCustomBranchPolicies.sort()
111+
attrsCustomBranchPolicies = attrsCustomBranchPolicies.sort((x1, x2) => x1.name.localeCompare(x2.name))
101112
}
102113
let deploymentBranchPolicy
103114
if (existing.deployment_branch_policy === attrs.deployment_branch_policy) {
@@ -163,7 +174,8 @@ module.exports = class Environments extends Diffable {
163174
await this.nopifyRequest(
164175
'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', {
165176
...baseRequestOptions,
166-
name: policy
177+
name: policy.name,
178+
type: policy.type
167179
}, 'Create deployment branch policy')
168180
}
169181
}
@@ -256,7 +268,8 @@ module.exports = class Environments extends Diffable {
256268
await this.nopifyRequest(
257269
'POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', {
258270
...baseRequestOptions,
259-
name: policy.name
271+
name: policy.name,
272+
type: policy.type
260273
}, 'Create deployment branch policy'
261274
)
262275
}

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

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,14 @@ describe('Environments Plugin test suite', () => {
302302
deployment_branch_policy: {
303303
protected_branches: false,
304304
custom_branch_policies: [
305-
'master',
306-
'dev'
305+
{
306+
names: ['main','dev'],
307+
type: 'branch'
308+
},
309+
{
310+
names: ['v*.*.*'],
311+
type: 'tag'
312+
}
307313
]
308314
}
309315
}
@@ -342,13 +348,22 @@ describe('Environments Plugin test suite', () => {
342348
org,
343349
repo,
344350
environment_name: environmentName,
345-
name: 'master'
351+
name: 'main',
352+
type: 'branch'
353+
}))
354+
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', expect.objectContaining({
355+
org,
356+
repo,
357+
environment_name: environmentName,
358+
name: 'dev',
359+
type: 'branch'
346360
}))
347361
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', expect.objectContaining({
348362
org,
349363
repo,
350364
environment_name: environmentName,
351-
name: 'dev'
365+
name: 'v*.*.*',
366+
type: 'tag'
352367
}))
353368
})
354369
})
@@ -753,8 +768,14 @@ describe('Environments Plugin test suite', () => {
753768
deployment_branch_policy: {
754769
protected_branches: false,
755770
custom_branch_policies: [
756-
'master',
757-
'dev'
771+
{
772+
names: ['main','dev'],
773+
type: 'branch'
774+
},
775+
{
776+
names: ['v*.*.*'],
777+
type: 'tag'
778+
}
758779
]
759780
}
760781
},
@@ -890,14 +911,24 @@ describe('Environments Plugin test suite', () => {
890911
org,
891912
repo,
892913
environment_name: 'deployment-branch-policy-custom_environment',
893-
name: 'master'
914+
name: 'main',
915+
type: 'branch'
894916
}))
895917

896918
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', expect.objectContaining({
897919
org,
898920
repo,
899921
environment_name: 'deployment-branch-policy-custom_environment',
900-
name: 'dev'
922+
name: 'dev',
923+
type: 'branch'
924+
}))
925+
926+
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', expect.objectContaining({
927+
org,
928+
repo,
929+
environment_name: 'deployment-branch-policy-custom_environment',
930+
name: 'v*.*.*',
931+
type: 'tag'
901932
}))
902933

903934
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/variables', expect.objectContaining({
@@ -957,8 +988,14 @@ describe('Environments Plugin test suite', () => {
957988
deployment_branch_policy: {
958989
protected_branches: false,
959990
custom_branch_policies: [
960-
'master',
961-
'dev'
991+
{
992+
names: ['main','dev'],
993+
type: 'branch'
994+
},
995+
{
996+
names: ['v*.*.*'],
997+
type: 'tag'
998+
}
962999
]
9631000
}
9641001
},
@@ -1012,8 +1049,14 @@ describe('Environments Plugin test suite', () => {
10121049
deployment_branch_policy: {
10131050
protected_branches: false,
10141051
custom_branch_policies: [
1015-
'master',
1016-
'dev'
1052+
{
1053+
names: ['main','dev'],
1054+
type: 'branch'
1055+
},
1056+
{
1057+
names: ['v*.*.*'],
1058+
type: 'tag'
1059+
}
10171060
]
10181061
}
10191062
},
@@ -1149,14 +1192,24 @@ describe('Environments Plugin test suite', () => {
11491192
org,
11501193
repo,
11511194
environment_name: 'deployment-branch-policy-custom_environment',
1152-
name: 'master'
1195+
name: 'main',
1196+
type: 'branch'
1197+
}))
1198+
1199+
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', expect.objectContaining({
1200+
org,
1201+
repo,
1202+
environment_name: 'deployment-branch-policy-custom_environment',
1203+
name: 'dev',
1204+
type: 'branch'
11531205
}))
11541206

11551207
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/deployment-branch-policies', expect.objectContaining({
11561208
org,
11571209
repo,
11581210
environment_name: 'deployment-branch-policy-custom_environment',
1159-
name: 'dev'
1211+
name: 'v*.*.*',
1212+
type: 'tag'
11601213
}))
11611214

11621215
expect(github.request).toHaveBeenCalledWith('POST /repos/:org/:repo/environments/:environment_name/variables', expect.objectContaining({

0 commit comments

Comments
 (0)