Skip to content

Commit

Permalink
update the pr branch directly
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Nov 22, 2023
1 parent 8e7a576 commit 6fbdc66
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 3 deletions.
208 changes: 208 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,113 @@ test('successfully runs the action and sets labels when one PR has no CI defined
number: 100,
html_url: 'https://github.com/test-owner/test-repo/pull/100'
}
}),
updateBranch: jest.fn().mockImplementation(() => {
throw new Error('updateBranch error')
})
}
}
}
})

process.env.INPUT_REVIEW_REQUIRED = 'true'
process.env.INPUT_LABELS = 'label1,label2, label3'
expect(await run()).toBe('success')

expect(infoMock).toHaveBeenCalledWith('Merged branch dependabot-1')
expect(warningMock).toHaveBeenCalledWith(
'Failed to merge branch dependabot-2'
)
expect(warningMock).toHaveBeenCalledWith(
'Failed to update combined pr branch with the base branch'
)
expect(setOutputMock).toHaveBeenCalledWith('pr_number', 100)
expect(setOutputMock).toHaveBeenCalledWith(
'pr_url',
'https://github.com/test-owner/test-repo/pull/100'
)
})

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 () => {
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
paginate: jest.fn().mockImplementation(() => {
return [
buildPR(1, 'dependabot-1', ['question']),
buildPR(2, 'dependabot-2'),
buildPR(3, 'dependabot-3', ['nocombine']),
buildPR(4, 'dependabot-4'),
buildPR(5, 'dependabot-5'),
buildPR(6, 'dependabot-6'),
buildPR(7, 'fix-package')
]
}),
graphql: jest.fn().mockImplementation((_query, params) => {
switch (params.pull_number) {
case 1:
case 2:
case 3:
return buildStatusResponse('APPROVED', 'SUCCESS')
case 4:
return buildStatusResponse('APPROVED', 'FAILURE')
case 5:
return buildStatusResponse(null, 'SUCCESS')
case 6:
return {
repository: {
pullRequest: {
reviewDecision: null,
commits: {
nodes: [
{
commit: {
statusCheckRollup: null
}
}
]
}
}
}
}
default:
throw new Error(
`params.pull_number of ${params.pull_number} is not configured.`
)
}
}),
rest: {
issues: {
addLabels: jest.fn().mockReturnValueOnce({
data: {}
})
},
git: {
createRef: jest.fn().mockReturnValueOnce({
data: {}
})
},
repos: {
// mock the first value of merge to be a success and the second to be an exception
merge: jest
.fn()
.mockReturnValueOnce({
data: {
merged: true
}
})
.mockImplementation(() => {
throw new Error('merge error')
})
},
pulls: {
create: jest.fn().mockReturnValueOnce({
data: {
number: 100,
html_url: 'https://github.com/test-owner/test-repo/pull/100'
}
}),
updateBranch: jest.fn().mockReturnValueOnce({
status: 500
})
}
}
Expand All @@ -1125,6 +1232,107 @@ test('successfully runs the action and sets labels when one PR has no CI defined
)
})

test('successfully runs the action and updates the pull request branch', async () => {
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
paginate: jest.fn().mockImplementation(() => {
return [
buildPR(1, 'dependabot-1', ['question']),
buildPR(2, 'dependabot-2'),
buildPR(3, 'dependabot-3', ['nocombine']),
buildPR(4, 'dependabot-4'),
buildPR(5, 'dependabot-5'),
buildPR(6, 'dependabot-6'),
buildPR(7, 'fix-package')
]
}),
graphql: jest.fn().mockImplementation((_query, params) => {
switch (params.pull_number) {
case 1:
case 2:
case 3:
return buildStatusResponse('APPROVED', 'SUCCESS')
case 4:
return buildStatusResponse('APPROVED', 'FAILURE')
case 5:
return buildStatusResponse(null, 'SUCCESS')
case 6:
return {
repository: {
pullRequest: {
reviewDecision: null,
commits: {
nodes: [
{
commit: {
statusCheckRollup: null
}
}
]
}
}
}
}
default:
throw new Error(
`params.pull_number of ${params.pull_number} is not configured.`
)
}
}),
rest: {
issues: {
addLabels: jest.fn().mockReturnValueOnce({
data: {}
})
},
git: {
createRef: jest.fn().mockReturnValueOnce({
data: {}
})
},
repos: {
// mock the first value of merge to be a success and the second to be an exception
merge: jest
.fn()
.mockReturnValueOnce({
data: {
merged: true
}
})
.mockImplementation(() => {
throw new Error('merge error')
})
},
pulls: {
create: jest.fn().mockReturnValueOnce({
data: {
number: 100,
html_url: 'https://github.com/test-owner/test-repo/pull/100'
}
}),
updateBranch: jest.fn().mockReturnValueOnce({
status: 202
})
}
}
}
})

process.env.INPUT_REVIEW_REQUIRED = 'true'
process.env.INPUT_LABELS = 'label1,label2, label3'
expect(await run()).toBe('success')

expect(infoMock).toHaveBeenCalledWith('Merged branch dependabot-1')
expect(warningMock).toHaveBeenCalledWith(
'Failed to merge branch dependabot-2'
)
expect(setOutputMock).toHaveBeenCalledWith('pr_number', 100)
expect(setOutputMock).toHaveBeenCalledWith(
'pr_url',
'https://github.com/test-owner/test-repo/pull/100'
)
})

function buildStatusResponse(reviewDecision, ciStatus) {
return {
repository: {
Expand Down
13 changes: 10 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,19 @@ export async function run() {
if (updateBranch === true) {
core.info('Attempting to update branch')
try {
await octokit.rest.repos.merge({
const result = await octokit.rest.pulls.updateBranch({
owner: context.repo.owner,
repo: context.repo.repo,
base: baseBranch,
head: combineBranchName
pull_number: pullRequest.data.number
})

// If the result is not a 202, return an error message and exit
if (result.status !== 202) {
throw new Error(
`Failed to update combined pr branch with the base branch - ${result}`
)
}

core.info('Branch updated')
} catch (error) {
core.warning('Failed to update combined pr branch with the base branch')
Expand Down

0 comments on commit 6fbdc66

Please sign in to comment.