Skip to content

Commit

Permalink
Merge pull request #45 from github/update-branch
Browse files Browse the repository at this point in the history
Update Branch Logic
  • Loading branch information
GrantBirki authored Nov 22, 2023
2 parents 16a1198 + 494a358 commit df3bb99
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ As mentioned above is this README, a core reason why this Action exists is to "c
| `select_label` | The label which marks PRs that should be combined. Leave empty to consider all PRs. | `""` | `false` |
| `labels` | A comma separated list of labels to add to the combined PR - Example: `dependencies,combined-pr,etc` | `""` | `false` |
| `autoclose` | Whether or not to close combined PRs if the combined PR is merged - can be `"true"` or `"false"` | `"true"` | `false` |
| `update_branch` | Whether or not to update the combined branch with the latest changes from the base branch after creating the combined pull request | `"true"` | `false` |

## Outputs 📤

Expand Down
310 changes: 310 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ beforeEach(() => {
process.env.INPUT_MIN_COMBINE_NUMBER = '2'
process.env.INPUT_LABELS = ''
process.env.INPUT_AUTOCLOSE = 'true'
process.env.INPUT_UPDATE_BRANCH = 'true'

jest.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
Expand Down Expand Up @@ -1023,6 +1024,315 @@ test('successfully runs the action and sets labels when one PR has no CI defined
)
})

test('successfully runs the action and sets labels when one PR has no CI defined and the update_branch logic fails', 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().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
})
}
}
}
})

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 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
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ inputs:
description: Whether or not to close combined PRs if the combined PR is merged
required: false
default: "true"
update_branch:
description: Whether or not to update the combined branch with the latest changes from the base branch after creating the combined pull request
default: "true"
required: false
outputs:
pr_url:
description: The pull request URL if a PR was created
Expand Down
24 changes: 24 additions & 0 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.

Loading

0 comments on commit df3bb99

Please sign in to comment.