Skip to content

Commit

Permalink
Allow specifying review input for workflow_dispatch event.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Mar 18, 2023
1 parent f482a8c commit e0cf02b
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ on:
pull-request:
description: Pull Request Number
required: false
review:
description: Review ID
required: false

jobs:
automerge:
Expand All @@ -43,3 +46,4 @@ jobs:
do-not-merge-labels: never-merge
required-labels: automerge
pull-request: ${{ github.event.inputs.pull-request }}
review: ${{ github.event.inputs.review }}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Ensure the following is set up in your repository settings before enabling this
| `required-labels` | no | Comma-separated list of labels that are required to be applied to a pull request for it to be merged automatically. |
| `pull-request` | no | Try merging the specified pull request automatically. For example, you can pass an input from a `workflow_dispatch` event. |
| `pull-request-author-associations` | no | Comma-separated list of required [author associations](https://docs.github.com/en/graphql/reference/enums#commentauthorassociation) for the pull request author. (By default, pull requests by any author are allowed.) |
| `review` | no | Try merging the pull request associated with the specified review ID automatically. For example, you can pass an input from a `workflow_dispatch` event. The `pull-request` input is also required if this is specified. |
| `review-author-associations` | no | Comma-separated list of required [author associations](https://docs.github.com/en/graphql/reference/enums#commentauthorassociation) for the review author. (By default, pull requests reviewd by `OWNER`s, `MEMBER`s and `COLLABORATOR`s are allowed.) |
| `dry-run` | no | If set to `true`, will not actually merge pull requests but still perform all other checks. |

Expand Down Expand Up @@ -70,12 +71,16 @@ on:
- unlabeled
- ready_for_review

# Try enabling auto-merge for the specified pull request or all open pull requests if none is specified.
# Try enabling auto-merge for the specified pull request, review or all open pull requests if
# none is specified.
workflow_dispatch:
inputs:
pull-request:
description: Pull Request Number
required: false
review:
description: Review ID
required: false

jobs:
automerge:
Expand All @@ -88,6 +93,7 @@ jobs:
do-not-merge-labels: never-merge
required-labels: automerge
pull-request: ${{ github.event.inputs.pull-request }}
review: ${{ github.event.inputs.review }}
dry-run: true
```
Expand Down
14 changes: 12 additions & 2 deletions __tests__/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const testEnvVars = {
'INPUT_REQUIRED-LABELS': 'automerge',
'INPUT_PULL-REQUEST': '',
'INPUT_PULL-REQUEST-AUTHOR-ASSOCIATION': '',
INPUT_REVIEW: '',
'INPUT_REVIEW-AUTHOR-ASSOCIATION': '',
'INPUT_DRY-RUN': '',
}

Expand All @@ -24,8 +26,10 @@ describe('input', () => {
expect(input.mergeMethod).toBe(undefined)
expect(input.doNotMergeLabels).toStrictEqual(['never-merge', 'blocked'])
expect(input.requiredLabels).toStrictEqual(['automerge'])
expect(input.pullRequest).toBe(null)
expect(input.pullRequest).toBe(undefined)
expect(input.pullRequestAuthorAssociations).toStrictEqual([])
expect(input.review).toBe(undefined)
expect(input.reviewAuthorAssociations).toStrictEqual([])
expect(input.dryRun).toBe(false)
})

Expand Down Expand Up @@ -74,7 +78,7 @@ describe('input', () => {
expect(input.pullRequest).toBe(1234)
})

it('fails if `pull-request` input si not a number', () => {
it('fails if `pull-request` input is not a number', () => {
process.env['INPUT_PULL-REQUEST'] = 'abc'

expect(() => new Input()).toThrow()
Expand All @@ -88,6 +92,12 @@ describe('input', () => {
expect(input.pullRequestAuthorAssociations).toStrictEqual(['COLLABORATOR', 'MEMBER', 'OWNER'])
})

it('fails if `review` input is not a number', () => {
process.env['INPUT_REVIEW'] = 'abc'

expect(() => new Input()).toThrow()
})

it('accepts an optional `dry-run` input', () => {
process.env['INPUT_DRY-RUN'] = 'true'

Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ inputs:
description: >
Comma-separated list of required author associations for the pull request author.
(By default, pull requests by any author are allowed.)
review:
required: false
description: >
Try merging the pull request associated with the specified review ID automatically. For example, you can pass an input from
a `workflow_dispatch` event. The `pull-request` input is also required if this is specified.
review-author-associations:
required: false
default: OWNER,MEMBER,COLLABORATOR
Expand Down
31 changes: 28 additions & 3 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.

34 changes: 34 additions & 0 deletions src/automerge-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,40 @@ export class AutomergeAction {
await this.autoMergePullRequest(pullRequest.number, review)
}

async handleWorkflowDispatch(): Promise<void> {
core.debug('handleWorkflowDispatch()')

const pullRequestNumber = this.input.pullRequest
const reviewId = this.input.review

if (reviewId !== undefined) {
if (!pullRequestNumber) {
core.setFailed(
'The `pull-request` input is required for `workflow_dispatch` event when `review` input is specified.'
)
return
}

const review = (
await this.octokit.rest.pulls.getReview({
...github.context.repo,
pull_number: pullRequestNumber,
review_id: reviewId,
})
).data

await this.autoMergePullRequest(pullRequestNumber, review)
return
}

if (pullRequestNumber !== undefined) {
await this.autoMergePullRequest(pullRequestNumber)
return
}

await this.handleSchedule()
}

async handleSchedule(): Promise<void> {
core.debug('handleSchedule()')

Expand Down
6 changes: 4 additions & 2 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ export class Input {
squashCommitMessage: string | undefined
doNotMergeLabels: string[]
requiredLabels: string[]
pullRequest: number | null
pullRequest: number | undefined
pullRequestAuthorAssociations: string[]
review: number | undefined
reviewAuthorAssociations: string[]
dryRun: boolean

Expand Down Expand Up @@ -70,9 +71,10 @@ export class Input {
}
}

this.pullRequest = getNumber('pull-request')
this.pullRequest = getNumber('pull-request') || undefined
this.pullRequestAuthorAssociations = getArray('pull-request-author-associations')

this.review = getNumber('review') || undefined
this.reviewAuthorAssociations = getArray('review-author-associations')

this.dryRun = core.getInput('dry-run') === 'true'
Expand Down
7 changes: 5 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ async function run(): Promise<void> {
break
}
case 'push':
case 'schedule':
case 'workflow_dispatch': {
case 'schedule': {
await action.handleSchedule()
break
}
case 'workflow_dispatch': {
await action.handleWorkflowDispatch()
break
}
default: {
core.warning(`This action does not support the '${eventName}' event.`)
break
Expand Down

0 comments on commit e0cf02b

Please sign in to comment.