Skip to content

Commit

Permalink
Merge pull request #9 from github/pr-create-fixes
Browse files Browse the repository at this point in the history
if the combined pr already exists, just update it
  • Loading branch information
GrantBirki committed Mar 15, 2023
2 parents d96a909 + 276be77 commit 1defd8b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 27 deletions.
23 changes: 15 additions & 8 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ test('successfully runs the action', async () => {
'PR body: # Combined PRs ➡️📦⬅️\n\n✅ The following pull requests have been successfully combined on this PR:\n- #1 Update dependency 1\n- #5 Update dependency 5\n\n⚠️ The following PRs were left out due to merge conflicts:\n- #2 Update dependency 2\n\n> This PR was created by the [`github/combine-prs`](https://github.com/github/combine-prs) action'
)
expect(infoMock).toHaveBeenCalledWith(
'Combined PR created: https://github.com/test-owner/test-repo/pull/100'
'Combined PR url: https://github.com/test-owner/test-repo/pull/100'
)
expect(infoMock).toHaveBeenCalledWith('Combined PR number: 100')
expect(setOutputMock).toHaveBeenCalledWith('pr_number', 100)
Expand Down Expand Up @@ -413,7 +413,7 @@ test('successfully runs the action with the branch_regex option', async () => {
'PR body: # Combined PRs ➡️📦⬅️\n\n✅ The following pull requests have been successfully combined on this PR:\n- #1 Update dependency 1\n- #5 Update dependency 5\n\n⚠️ The following PRs were left out due to merge conflicts:\n- #2 Update dependency 2\n\n> This PR was created by the [`github/combine-prs`](https://github.com/github/combine-prs) action'
)
expect(infoMock).toHaveBeenCalledWith(
'Combined PR created: https://github.com/test-owner/test-repo/pull/100'
'Combined PR url: https://github.com/test-owner/test-repo/pull/100'
)
expect(infoMock).toHaveBeenCalledWith('Combined PR number: 100')
expect(setOutputMock).toHaveBeenCalledWith('pr_number', 100)
Expand Down Expand Up @@ -502,7 +502,7 @@ test('runs the action and fails to create the combine branch', async () => {
expect(setFailedMock).toHaveBeenCalledWith('Failed to create combined branch')
})

test('runs the action and finds the combine branch already exists', async () => {
test('runs the action and finds the combine branch already exists and the PR also exists', async () => {
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
paginate: jest.fn().mockImplementation(() => {
Expand Down Expand Up @@ -573,11 +573,18 @@ test('runs the action and finds the combine branch already exists', async () =>
})
},
pulls: {
create: jest.fn().mockReturnValueOnce({
data: {
html_url: 'https://github.com/test-owner/test-repo/pull/100',
number: 100
}
create: jest
.fn()
.mockRejectedValueOnce(new AlreadyExistsError('PR already exists')),
list: jest.fn().mockReturnValueOnce({
data: [
{
number: 100
}
]
}),
update: jest.fn().mockReturnValueOnce({
data: {}
})
}
}
Expand Down
42 changes: 33 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

42 changes: 33 additions & 9 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,41 @@ export async function run() {

core.debug('PR body: ' + body)

const pullRequest = await octokit.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: prTitle,
head: combineBranchName,
base: baseBranch,
body: body
})
let pullRequest
try {
pullRequest = await octokit.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: prTitle,
head: combineBranchName,
base: baseBranch,
body: body
})
} catch (error) {
if (error.status == 422) {
core.warning('Combined PR already exists')
// update the PR body
const prs = await octokit.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: context.repo.owner + ':' + combineBranchName,
base: baseBranch,
state: 'open'
})
const pr = prs.data[0]
core.info('Updating PR body')
await octokit.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
body: body
})
pullRequest = {data: pr}
}
}

// output pull request url
core.info('Combined PR created: ' + pullRequest.data.html_url)
core.info('Combined PR url: ' + pullRequest.data.html_url)
core.setOutput('pr_url', pullRequest.data.html_url)

// output pull request number
Expand Down

0 comments on commit 1defd8b

Please sign in to comment.