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 assignees Feature #59

Merged
merged 1 commit into from
Oct 10, 2024
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 @@ -33,6 +33,7 @@ As mentioned above is this README, a core reason why this Action exists is to "c
| `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 separated list of labels to add to the combined PR - Example: `dependencies,combined-pr,etc` | `""` | `false` |
| `assignees` | A comma separated list of assignees the combined PR is assigned to - Example: `octocat` | `""` | `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` |
| `create_from_scratch` | Whether or not to start from a clean base branch when (re)creating the combined PR | `"false"` | `false` |
Expand Down
83 changes: 83 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ beforeEach(() => {
process.env.GITHUB_REPOSITORY = 'test-owner/test-repo'
process.env.INPUT_MIN_COMBINE_NUMBER = '2'
process.env.INPUT_LABELS = ''
process.env.INPUT_ASSIGNEES = ''
process.env.INPUT_AUTOCLOSE = 'true'
process.env.INPUT_UPDATE_BRANCH = 'true'
process.env.INPUT_CREATE_FROM_SCRATCH = 'false'
Expand Down Expand Up @@ -1773,6 +1774,88 @@ test('runs the action and fails to create a working branch', async () => {
expect(setFailedMock).toHaveBeenCalledWith('Failed to create working branch')
})

test('successfully runs the action and sets assignees', async () => {
jest.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
paginate: jest.fn().mockImplementation(() => {
return [
buildPR(1, 'dependabot-1', ['question']),
buildPR(2, 'dependabot-2')
]
}),
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: {
addAssignees: jest.fn().mockReturnValueOnce({
data: {}
}),
addLabels: jest.fn().mockReturnValueOnce({
data: {}
})
},
git: {
createRef: jest.fn().mockReturnValueOnce({
data: {}
}),
updateRef: jest.fn().mockReturnValueOnce({
data: {}
}),
getRef: jest.fn().mockReturnValueOnce({
data: {
object: {
sha: randomSha1()
}
}
})
},
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
}
})
},
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_ASSIGNEES = 'octocat ,another-user, kolossal'
expect(await run()).toBe('success')

expect(infoMock).toHaveBeenCalledWith(
`Adding assignees to combined PR: octocat,another-user,kolossal`
)

expect(setOutputMock).toHaveBeenCalledWith('pr_number', 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 @@ -53,6 +53,10 @@ inputs:
description: A comma seperated list of labels to add to the combined PR
required: false
default: ""
assignees:
description: A comma seperated list of assignees to add to the combined PR
required: false
default: ""
autoclose:
description: Whether or not to close combined PRs if the combined PR is merged
required: false
Expand Down
17 changes: 17 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.

17 changes: 17 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function run() {
const ignoreLabel = core.getInput('ignore_label')
const selectLabel = core.getInput('select_label')
const labels = core.getInput('labels').trim()
const assignees = core.getInput('assignees').trim()
const token = core.getInput('github_token', {required: true})
const prTitle = core.getInput('pr_title', {required: true})
const prBodyHeader = core.getInput('pr_body_header', {required: true})
Expand Down Expand Up @@ -295,6 +296,22 @@ export async function run() {
}
}

if (assignees !== '') {
// split and trim assignees
const assigneesArray = assignees.split(',').map(label => label.trim())

// add assignees to the combined PR if specified
if (assigneesArray.length > 0) {
core.info(`Adding assignees to combined PR: ${assigneesArray}`)
await octokit.rest.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.data.number,
assignees: assigneesArray
})
}
}

// lastly, if the pull request's branch can be updated cleanly, update it
if (updateBranch === true) {
core.info('Attempting to update branch')
Expand Down