Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Labels Feature #20

Merged
merged 8 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This Action is customizable so you can use it for your own purposes and it doesn
| `review_required` | Whether or not reviews should be passing to combine the PR - can be `"true"` or `"false"` | `"false"` | `false` |
| `ignore_label` | The label to ignore when combining PRs | `"nocombine"` | `true` |
| `select_label` | The label which marks PRs that should be combined. Leave empty to consider all PRs. | `""` | `false` |
| `labels` | A comma seperated list of labels to add to the combined PR - Example: `dependencies,combined-pr,etc` | `""` | `false` |

## Outputs 📤

Expand Down
138 changes: 138 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ beforeEach(() => {
process.env.INPUT_SELECT_LABEL = ''
process.env.GITHUB_REPOSITORY = 'test-owner/test-repo'
process.env.INPUT_MIN_COMBINE_NUMBER = '2'
process.env.INPUT_LABELS = ''

jest.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
Expand Down Expand Up @@ -660,6 +661,143 @@ test('ignore_label and select_label can both be empty', async () => {
expect(await run()).toBe('success')
})

test('successfully runs the action and sets labels', 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 buildStatusResponse('REVIEW_REQUIRED', 'SUCCESS')
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
}
})
.mockImplementationOnce(() => {
throw new Error('merge error')
})
},
pulls: {
create: jest.fn().mockReturnValueOnce({
data: {
number: 100,
html_url: 'https://github.com/test-owner/test-repo/pull/100'
}
})
}
}
}
})

process.env.INPUT_REVIEW_REQUIRED = 'true'
process.env.INPUT_LABELS = 'label1,label2, label3'
expect(await run()).toBe('success')
expect(infoMock).toHaveBeenCalledWith('Pull for branch: dependabot-1')
expect(infoMock).toHaveBeenCalledWith('Branch matched prefix: dependabot-1')
expect(infoMock).toHaveBeenCalledWith('Checking green status: dependabot-1')
expect(infoMock).toHaveBeenCalledWith('Validating status: SUCCESS')
expect(infoMock).toHaveBeenCalledWith('Validating review decision: APPROVED')
expect(infoMock).toHaveBeenCalledWith('Branch dependabot-1 is approved')
expect(infoMock).toHaveBeenCalledWith('Pull for branch: dependabot-2')
expect(infoMock).toHaveBeenCalledWith('Branch matched prefix: dependabot-2')
expect(infoMock).toHaveBeenCalledWith('Checking green status: dependabot-2')
expect(infoMock).toHaveBeenCalledWith('Validating status: SUCCESS')
expect(infoMock).toHaveBeenCalledWith('Validating review decision: APPROVED')
expect(infoMock).toHaveBeenCalledWith('Branch dependabot-2 is approved')
expect(infoMock).toHaveBeenCalledWith('Pull for branch: dependabot-3')
expect(infoMock).toHaveBeenCalledWith('Branch matched prefix: dependabot-3')
expect(infoMock).toHaveBeenCalledWith('Pull for branch: dependabot-4')
expect(infoMock).toHaveBeenCalledWith('Branch matched prefix: dependabot-4')
expect(infoMock).toHaveBeenCalledWith('Checking green status: dependabot-4')
expect(infoMock).toHaveBeenCalledWith('Validating status: FAILURE')
expect(infoMock).toHaveBeenCalledWith(
'Discarding dependabot-4 with status FAILURE'
)
expect(infoMock).toHaveBeenCalledWith('Branch matched prefix: dependabot-5')
expect(infoMock).toHaveBeenCalledWith('Checking green status: dependabot-5')
expect(infoMock).toHaveBeenCalledWith('Validating status: SUCCESS')
expect(infoMock).toHaveBeenCalledWith('Validating review decision: null')
expect(infoMock).toHaveBeenCalledWith(
'Branch dependabot-5 has no required reviewers - OK'
)
expect(infoMock).toHaveBeenCalledWith('Checking labels: dependabot-1')
expect(infoMock).toHaveBeenCalledWith('Checking ignore_label for: question')
expect(infoMock).toHaveBeenCalledWith('Adding branch to array: dependabot-1')
expect(infoMock).toHaveBeenCalledWith('Checking labels: dependabot-2')
expect(infoMock).toHaveBeenCalledWith('Adding branch to array: dependabot-2')
expect(infoMock).toHaveBeenCalledWith('Checking labels: dependabot-3')
expect(infoMock).toHaveBeenCalledWith('Checking ignore_label for: nocombine')
expect(infoMock).toHaveBeenCalledWith(
'Discarding dependabot-3 with label nocombine because it matches ignore_label'
)
expect(infoMock).toHaveBeenCalledWith('Checking labels: dependabot-4')
expect(infoMock).toHaveBeenCalledWith('Checking labels: dependabot-5')
expect(infoMock).toHaveBeenCalledWith('Checking labels: dependabot-6')
expect(infoMock).toHaveBeenCalledWith('Merged branch dependabot-1')
expect(warningMock).toHaveBeenCalledWith(
'Failed to merge branch dependabot-2'
)
expect(infoMock).toHaveBeenCalledWith('Merged branch dependabot-5')
expect(infoMock).toHaveBeenCalledWith('Creating combined PR')
expect(debugMock).toHaveBeenCalledWith(
'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(
`Adding labels to combined PR: label1,label2,label3`
)

expect(infoMock).toHaveBeenCalledWith(
'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)
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 @@ -49,6 +49,10 @@ inputs:
description: The label marking PRs that should be combined
required: false
default: ""
labels:
description: A comma seperated list of labels to add to the combined PR
required: false
default: ""
outputs:
pr_url:
description: The pull request URL if a PR was created
Expand Down
Loading