Skip to content

Commit 6fbdc66

Browse files
committed
update the pr branch directly
1 parent 8e7a576 commit 6fbdc66

File tree

2 files changed

+218
-3
lines changed

2 files changed

+218
-3
lines changed

__tests__/main.test.js

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,113 @@ test('successfully runs the action and sets labels when one PR has no CI defined
11011101
number: 100,
11021102
html_url: 'https://github.com/test-owner/test-repo/pull/100'
11031103
}
1104+
}),
1105+
updateBranch: jest.fn().mockImplementation(() => {
1106+
throw new Error('updateBranch error')
1107+
})
1108+
}
1109+
}
1110+
}
1111+
})
1112+
1113+
process.env.INPUT_REVIEW_REQUIRED = 'true'
1114+
process.env.INPUT_LABELS = 'label1,label2, label3'
1115+
expect(await run()).toBe('success')
1116+
1117+
expect(infoMock).toHaveBeenCalledWith('Merged branch dependabot-1')
1118+
expect(warningMock).toHaveBeenCalledWith(
1119+
'Failed to merge branch dependabot-2'
1120+
)
1121+
expect(warningMock).toHaveBeenCalledWith(
1122+
'Failed to update combined pr branch with the base branch'
1123+
)
1124+
expect(setOutputMock).toHaveBeenCalledWith('pr_number', 100)
1125+
expect(setOutputMock).toHaveBeenCalledWith(
1126+
'pr_url',
1127+
'https://github.com/test-owner/test-repo/pull/100'
1128+
)
1129+
})
1130+
1131+
test('successfully runs the action and sets labels when one PR has no CI defined and the update_branch logic fails due to a non 202 status code', async () => {
1132+
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
1133+
return {
1134+
paginate: jest.fn().mockImplementation(() => {
1135+
return [
1136+
buildPR(1, 'dependabot-1', ['question']),
1137+
buildPR(2, 'dependabot-2'),
1138+
buildPR(3, 'dependabot-3', ['nocombine']),
1139+
buildPR(4, 'dependabot-4'),
1140+
buildPR(5, 'dependabot-5'),
1141+
buildPR(6, 'dependabot-6'),
1142+
buildPR(7, 'fix-package')
1143+
]
1144+
}),
1145+
graphql: jest.fn().mockImplementation((_query, params) => {
1146+
switch (params.pull_number) {
1147+
case 1:
1148+
case 2:
1149+
case 3:
1150+
return buildStatusResponse('APPROVED', 'SUCCESS')
1151+
case 4:
1152+
return buildStatusResponse('APPROVED', 'FAILURE')
1153+
case 5:
1154+
return buildStatusResponse(null, 'SUCCESS')
1155+
case 6:
1156+
return {
1157+
repository: {
1158+
pullRequest: {
1159+
reviewDecision: null,
1160+
commits: {
1161+
nodes: [
1162+
{
1163+
commit: {
1164+
statusCheckRollup: null
1165+
}
1166+
}
1167+
]
1168+
}
1169+
}
1170+
}
1171+
}
1172+
default:
1173+
throw new Error(
1174+
`params.pull_number of ${params.pull_number} is not configured.`
1175+
)
1176+
}
1177+
}),
1178+
rest: {
1179+
issues: {
1180+
addLabels: jest.fn().mockReturnValueOnce({
1181+
data: {}
1182+
})
1183+
},
1184+
git: {
1185+
createRef: jest.fn().mockReturnValueOnce({
1186+
data: {}
1187+
})
1188+
},
1189+
repos: {
1190+
// mock the first value of merge to be a success and the second to be an exception
1191+
merge: jest
1192+
.fn()
1193+
.mockReturnValueOnce({
1194+
data: {
1195+
merged: true
1196+
}
1197+
})
1198+
.mockImplementation(() => {
1199+
throw new Error('merge error')
1200+
})
1201+
},
1202+
pulls: {
1203+
create: jest.fn().mockReturnValueOnce({
1204+
data: {
1205+
number: 100,
1206+
html_url: 'https://github.com/test-owner/test-repo/pull/100'
1207+
}
1208+
}),
1209+
updateBranch: jest.fn().mockReturnValueOnce({
1210+
status: 500
11041211
})
11051212
}
11061213
}
@@ -1125,6 +1232,107 @@ test('successfully runs the action and sets labels when one PR has no CI defined
11251232
)
11261233
})
11271234

1235+
test('successfully runs the action and updates the pull request branch', async () => {
1236+
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
1237+
return {
1238+
paginate: jest.fn().mockImplementation(() => {
1239+
return [
1240+
buildPR(1, 'dependabot-1', ['question']),
1241+
buildPR(2, 'dependabot-2'),
1242+
buildPR(3, 'dependabot-3', ['nocombine']),
1243+
buildPR(4, 'dependabot-4'),
1244+
buildPR(5, 'dependabot-5'),
1245+
buildPR(6, 'dependabot-6'),
1246+
buildPR(7, 'fix-package')
1247+
]
1248+
}),
1249+
graphql: jest.fn().mockImplementation((_query, params) => {
1250+
switch (params.pull_number) {
1251+
case 1:
1252+
case 2:
1253+
case 3:
1254+
return buildStatusResponse('APPROVED', 'SUCCESS')
1255+
case 4:
1256+
return buildStatusResponse('APPROVED', 'FAILURE')
1257+
case 5:
1258+
return buildStatusResponse(null, 'SUCCESS')
1259+
case 6:
1260+
return {
1261+
repository: {
1262+
pullRequest: {
1263+
reviewDecision: null,
1264+
commits: {
1265+
nodes: [
1266+
{
1267+
commit: {
1268+
statusCheckRollup: null
1269+
}
1270+
}
1271+
]
1272+
}
1273+
}
1274+
}
1275+
}
1276+
default:
1277+
throw new Error(
1278+
`params.pull_number of ${params.pull_number} is not configured.`
1279+
)
1280+
}
1281+
}),
1282+
rest: {
1283+
issues: {
1284+
addLabels: jest.fn().mockReturnValueOnce({
1285+
data: {}
1286+
})
1287+
},
1288+
git: {
1289+
createRef: jest.fn().mockReturnValueOnce({
1290+
data: {}
1291+
})
1292+
},
1293+
repos: {
1294+
// mock the first value of merge to be a success and the second to be an exception
1295+
merge: jest
1296+
.fn()
1297+
.mockReturnValueOnce({
1298+
data: {
1299+
merged: true
1300+
}
1301+
})
1302+
.mockImplementation(() => {
1303+
throw new Error('merge error')
1304+
})
1305+
},
1306+
pulls: {
1307+
create: jest.fn().mockReturnValueOnce({
1308+
data: {
1309+
number: 100,
1310+
html_url: 'https://github.com/test-owner/test-repo/pull/100'
1311+
}
1312+
}),
1313+
updateBranch: jest.fn().mockReturnValueOnce({
1314+
status: 202
1315+
})
1316+
}
1317+
}
1318+
}
1319+
})
1320+
1321+
process.env.INPUT_REVIEW_REQUIRED = 'true'
1322+
process.env.INPUT_LABELS = 'label1,label2, label3'
1323+
expect(await run()).toBe('success')
1324+
1325+
expect(infoMock).toHaveBeenCalledWith('Merged branch dependabot-1')
1326+
expect(warningMock).toHaveBeenCalledWith(
1327+
'Failed to merge branch dependabot-2'
1328+
)
1329+
expect(setOutputMock).toHaveBeenCalledWith('pr_number', 100)
1330+
expect(setOutputMock).toHaveBeenCalledWith(
1331+
'pr_url',
1332+
'https://github.com/test-owner/test-repo/pull/100'
1333+
)
1334+
})
1335+
11281336
function buildStatusResponse(reviewDecision, ciStatus) {
11291337
return {
11301338
repository: {

src/main.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,19 @@ export async function run() {
238238
if (updateBranch === true) {
239239
core.info('Attempting to update branch')
240240
try {
241-
await octokit.rest.repos.merge({
241+
const result = await octokit.rest.pulls.updateBranch({
242242
owner: context.repo.owner,
243243
repo: context.repo.repo,
244-
base: baseBranch,
245-
head: combineBranchName
244+
pull_number: pullRequest.data.number
246245
})
246+
247+
// If the result is not a 202, return an error message and exit
248+
if (result.status !== 202) {
249+
throw new Error(
250+
`Failed to update combined pr branch with the base branch - ${result}`
251+
)
252+
}
253+
247254
core.info('Branch updated')
248255
} catch (error) {
249256
core.warning('Failed to update combined pr branch with the base branch')

0 commit comments

Comments
 (0)