Skip to content

Commit da2ff7f

Browse files
committed
add the non_default_target_branch_used output and leave a warning if non default target branches are used
1 parent e2c5b01 commit da2ff7f

File tree

7 files changed

+95
-12
lines changed

7 files changed

+95
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ As seen above, we have two steps. One for a noop deploy, and one for a regular d
343343
| `needs_to_be_deployed` | A comma separated list of environments that need successful and active deployments before the current environment (that was requested) can be deployed. This output is tied to the `enforced_deployment_order` input option - See the [enforced deployment order docs](./docs/enforced-deployment-order.md) for more details |
344344
| `commit_verified` | The string `"true"` if the commit is verified, otherwise `"false"` |
345345
| `total_seconds` | The total number of seconds that the deployment took to complete (Integer) |
346+
| `non_default_target_branch_used` | The string `"true"` if the pull request is targeting a branch other than the default branch (aka stable branch) for the merge, otherwise unset |
346347

347348
## Custom Deployment Messages ✏️
348349

__tests__/functions/prechecks.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ beforeEach(() => {
6060
permissions: ['admin', 'write'],
6161
commit_verification: false,
6262
ignored_checks: [],
63+
use_security_warnings: true,
6364
allow_non_default_target_branch_deployments: false
6465
}
6566
}
@@ -1034,6 +1035,11 @@ test('runs prechecks and finds that the IssueOps command is valid for a branch d
10341035
sha: 'abcde12345',
10351036
isFork: true
10361037
})
1038+
1039+
expect(setOutputMock).not.toHaveBeenCalledWith(
1040+
'non_default_target_branch_used',
1041+
'true'
1042+
)
10371043
})
10381044

10391045
test('runs prechecks and finds that the PR from a fork is targeting a non-default branch and rejects the deployment', async () => {
@@ -1082,6 +1088,11 @@ test('runs prechecks and finds that the PR from a fork is targeting a non-defaul
10821088
message: `### ⚠️ Cannot proceed with deployment\n\nThis pull request is attempting to merge into the \`some-other-branch\` branch which is not the default branch of this repository (\`${data.inputs.stable_branch}\`). This deployment has been rejected since it could be dangerous to proceed.`,
10831089
status: false
10841090
})
1091+
1092+
expect(setOutputMock).toHaveBeenCalledWith(
1093+
'non_default_target_branch_used',
1094+
'true'
1095+
)
10851096
})
10861097

10871098
test('runs prechecks and finds that the PR from a fork is targeting a non-default branch and allows it based on the action config', async () => {
@@ -1137,6 +1148,11 @@ test('runs prechecks and finds that the PR from a fork is targeting a non-defaul
11371148
sha: 'abcde12345',
11381149
isFork: true
11391150
})
1151+
1152+
expect(setOutputMock).toHaveBeenCalledWith(
1153+
'non_default_target_branch_used',
1154+
'true'
1155+
)
11401156
})
11411157

11421158
test('runs prechecks and finds that the PR is targeting a non-default branch and rejects the deployment', async () => {
@@ -1185,9 +1201,14 @@ test('runs prechecks and finds that the PR is targeting a non-default branch and
11851201
message: `### ⚠️ Cannot proceed with deployment\n\nThis pull request is attempting to merge into the \`not-main\` branch which is not the default branch of this repository (\`${data.inputs.stable_branch}\`). This deployment has been rejected since it could be dangerous to proceed.`,
11861202
status: false
11871203
})
1204+
1205+
expect(setOutputMock).toHaveBeenCalledWith(
1206+
'non_default_target_branch_used',
1207+
'true'
1208+
)
11881209
})
11891210

1190-
test('runs prechecks and finds that the PR is targeting a non-default branch and allows the deployment based on the action config', async () => {
1211+
test('runs prechecks and finds that the PR is targeting a non-default branch and allows the deployment based on the action config and logs a warning', async () => {
11911212
octokit.graphql = jest.fn().mockReturnValue({
11921213
repository: {
11931214
pullRequest: {
@@ -1239,6 +1260,15 @@ test('runs prechecks and finds that the PR is targeting a non-default branch and
12391260
sha: 'abcde12345',
12401261
isFork: false
12411262
})
1263+
1264+
expect(setOutputMock).toHaveBeenCalledWith(
1265+
'non_default_target_branch_used',
1266+
'true'
1267+
)
1268+
1269+
expect(warningMock).toHaveBeenCalledWith(
1270+
`🚨 this pull request is attempting to merge into the \`not-main\` branch which is not the default branch of this repository (\`${data.inputs.stable_branch}\`) - this action is potentially dangerous`
1271+
)
12421272
})
12431273

12441274
test('runs prechecks and finds that the IssueOps command is valid for a branch deployment and is from a forked repository and the PR is approved but CI is failing and it is a noop', async () => {

__tests__/schemas/action.schema.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,7 @@ outputs:
651651
description:
652652
type: string
653653
required: true
654+
non_default_target_branch_used:
655+
description:
656+
type: string
657+
required: true

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ outputs:
268268
description: 'The string "true" if the commit has a verified signature, otherwise "false"'
269269
total_seconds:
270270
description: 'The total number of seconds that the deployment took to complete (Integer)'
271+
non_default_target_branch_used:
272+
description: 'The string "true" if the pull request is targeting a branch other than the default branch (aka stable branch) for the merge, otherwise unset'
271273
runs:
272274
using: "node20"
273275
main: "dist/index.js"

dist/index.js

Lines changed: 28 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/prechecks.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,37 @@ export async function prechecks(context, octokit, data) {
8989
)
9090
}
9191

92+
const nonDefaultTargetBranchUsed = data.inputs.stable_branch !== baseRef
93+
const isNotStableBranchDeploy = !data.environmentObj.stable_branch_used
94+
const nonDefaultDeploysAllowed =
95+
data.inputs.allow_non_default_target_branch_deployments
96+
const securityWarningsEnabled = data.inputs.use_security_warnings
97+
98+
if (nonDefaultTargetBranchUsed) {
99+
core.setOutput('non_default_target_branch_used', 'true')
100+
}
101+
92102
// If the PR is targeting a branch other than the default branch (and it is not a stable branch deploy) reject the deployment, unless the Action is explicitly configured to allow it
93103
if (
94-
data.environmentObj.stable_branch_used === false &&
95-
data.inputs.stable_branch !== baseRef &&
96-
data.inputs.allow_non_default_target_branch_deployments === false
104+
isNotStableBranchDeploy &&
105+
nonDefaultTargetBranchUsed &&
106+
!nonDefaultDeploysAllowed
97107
) {
98-
message = `### ⚠️ Cannot proceed with deployment\n\nThis pull request is attempting to merge into the \`${baseRef}\` branch which is not the default branch of this repository (\`${data.inputs.stable_branch}\`). This deployment has been rejected since it could be dangerous to proceed.`
99-
return {message: message, status: false}
108+
return {
109+
message: `### ⚠️ Cannot proceed with deployment\n\nThis pull request is attempting to merge into the \`${baseRef}\` branch which is not the default branch of this repository (\`${data.inputs.stable_branch}\`). This deployment has been rejected since it could be dangerous to proceed.`,
110+
status: false
111+
}
112+
}
113+
114+
if (
115+
isNotStableBranchDeploy &&
116+
nonDefaultTargetBranchUsed &&
117+
nonDefaultDeploysAllowed &&
118+
securityWarningsEnabled
119+
) {
120+
core.warning(
121+
`🚨 this pull request is attempting to merge into the \`${baseRef}\` branch which is not the default branch of this repository (\`${data.inputs.stable_branch}\`) - this action is potentially dangerous`
122+
)
100123
}
101124

102125
// Determine whether to use the ref or sha depending on if the PR is from a fork or not

0 commit comments

Comments
 (0)