Skip to content

Commit e2c5b01

Browse files
committed
add allow_non_default_target_branch_deployments controls into prechecks()
1 parent 0336ed7 commit e2c5b01

File tree

4 files changed

+166
-6
lines changed

4 files changed

+166
-6
lines changed

__tests__/functions/prechecks.test.js

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ beforeEach(() => {
5959
checks: 'all',
6060
permissions: ['admin', 'write'],
6161
commit_verification: false,
62-
ignored_checks: []
62+
ignored_checks: [],
63+
allow_non_default_target_branch_deployments: false
6364
}
6465
}
6566

@@ -1083,6 +1084,163 @@ test('runs prechecks and finds that the PR from a fork is targeting a non-defaul
10831084
})
10841085
})
10851086

1087+
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 () => {
1088+
octokit.graphql = jest.fn().mockReturnValue({
1089+
repository: {
1090+
pullRequest: {
1091+
reviewDecision: 'APPROVED',
1092+
reviews: {
1093+
totalCount: 1
1094+
},
1095+
commits: {
1096+
nodes: [
1097+
{
1098+
commit: {
1099+
oid: 'abcde12345',
1100+
checkSuites: {
1101+
totalCount: 8
1102+
},
1103+
statusCheckRollup: {
1104+
state: 'SUCCESS'
1105+
}
1106+
}
1107+
}
1108+
]
1109+
}
1110+
}
1111+
}
1112+
})
1113+
octokit.rest.pulls.get = jest.fn().mockReturnValue({
1114+
data: {
1115+
head: {
1116+
sha: 'abcde12345',
1117+
ref: 'test-ref',
1118+
label: 'test-repo:test-ref',
1119+
repo: {
1120+
fork: true
1121+
}
1122+
},
1123+
base: {
1124+
ref: 'some-other-branch'
1125+
}
1126+
},
1127+
status: 200
1128+
})
1129+
1130+
data.inputs.allow_non_default_target_branch_deployments = true
1131+
1132+
expect(await prechecks(context, octokit, data)).toStrictEqual({
1133+
message: `✅ PR is approved and all CI checks passed`,
1134+
status: true,
1135+
noopMode: false,
1136+
ref: 'abcde12345',
1137+
sha: 'abcde12345',
1138+
isFork: true
1139+
})
1140+
})
1141+
1142+
test('runs prechecks and finds that the PR is targeting a non-default branch and rejects the deployment', async () => {
1143+
octokit.graphql = jest.fn().mockReturnValue({
1144+
repository: {
1145+
pullRequest: {
1146+
reviewDecision: 'APPROVED',
1147+
reviews: {
1148+
totalCount: 1
1149+
},
1150+
commits: {
1151+
nodes: [
1152+
{
1153+
commit: {
1154+
oid: 'abcde12345',
1155+
checkSuites: {
1156+
totalCount: 8
1157+
},
1158+
statusCheckRollup: {
1159+
state: 'SUCCESS'
1160+
}
1161+
}
1162+
}
1163+
]
1164+
}
1165+
}
1166+
}
1167+
})
1168+
octokit.rest.pulls.get = jest.fn().mockReturnValue({
1169+
data: {
1170+
head: {
1171+
ref: 'test-ref',
1172+
sha: 'abc123'
1173+
},
1174+
repo: {
1175+
fork: false
1176+
},
1177+
base: {
1178+
ref: 'not-main'
1179+
}
1180+
},
1181+
status: 200
1182+
})
1183+
1184+
expect(await prechecks(context, octokit, data)).toStrictEqual({
1185+
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.`,
1186+
status: false
1187+
})
1188+
})
1189+
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 () => {
1191+
octokit.graphql = jest.fn().mockReturnValue({
1192+
repository: {
1193+
pullRequest: {
1194+
reviewDecision: 'APPROVED',
1195+
reviews: {
1196+
totalCount: 1
1197+
},
1198+
commits: {
1199+
nodes: [
1200+
{
1201+
commit: {
1202+
oid: 'abcde12345',
1203+
checkSuites: {
1204+
totalCount: 8
1205+
},
1206+
statusCheckRollup: {
1207+
state: 'SUCCESS'
1208+
}
1209+
}
1210+
}
1211+
]
1212+
}
1213+
}
1214+
}
1215+
})
1216+
octokit.rest.pulls.get = jest.fn().mockReturnValue({
1217+
data: {
1218+
head: {
1219+
ref: 'test-ref',
1220+
sha: 'abcde12345'
1221+
},
1222+
repo: {
1223+
fork: false
1224+
},
1225+
base: {
1226+
ref: 'not-main'
1227+
}
1228+
},
1229+
status: 200
1230+
})
1231+
1232+
data.inputs.allow_non_default_target_branch_deployments = true
1233+
1234+
expect(await prechecks(context, octokit, data)).toStrictEqual({
1235+
message: `✅ PR is approved and all CI checks passed`,
1236+
status: true,
1237+
noopMode: false,
1238+
ref: 'test-ref',
1239+
sha: 'abcde12345',
1240+
isFork: false
1241+
})
1242+
})
1243+
10861244
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 () => {
10871245
octokit.graphql = jest.fn().mockReturnValue({
10881246
repository: {

dist/index.js

Lines changed: 3 additions & 2 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ export async function prechecks(context, octokit, data) {
8989
)
9090
}
9191

92-
// If the PR is targeting a branch other than the default branch (and it is not a stable branch deploy) reject the deployment
92+
// 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
9393
if (
9494
data.environmentObj.stable_branch_used === false &&
95-
data.inputs.stable_branch !== baseRef
95+
data.inputs.stable_branch !== baseRef &&
96+
data.inputs.allow_non_default_target_branch_deployments === false
9697
) {
9798
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.`
9899
return {message: message, status: false}

0 commit comments

Comments
 (0)