Skip to content

Commit

Permalink
Merge pull request #6 from ipdxco/fix-pr-matching
Browse files Browse the repository at this point in the history
Added pull_number inference from workflow_run event context
  • Loading branch information
galargh authored Jun 20, 2024
2 parents 8385e5a + a3eb7c2 commit c65eca8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
1 change: 0 additions & 1 deletion .github/workflows/comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ on:
pull_number:
description: 'The pull request number'
required: false
default: '${{ github.event.workflow_run.pull_requests[0].number }}'
type: string
template:
description: 'The template name or a lodash style template to use for the comment'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ permissions:
pull-requests: write

concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.pull_requests[0].number }}
group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
cancel-in-progress: true

jobs:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Added
- Added `pull_number` inference from the `workflow_run` event context

### Changed
- Removed the default value for the `pull_number` input

## [1.0.2] - 2024-04-09
### Added
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ permissions:
pull-requests: write

concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.pull_requests[0].number }}
group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
cancel-in-progress: true

jobs:
Expand Down Expand Up @@ -76,7 +76,7 @@ with:
# The repository to comment on
repository: '${{ github.repository }}'
# The pull request number
pull_number: '${{ github.event.workflow_run.pull_requests[0].number }}'
pull_number: ''
# The template name or a lodash style template to use for the comment
template: 'sorted_by_result'
# The identifier to use for the sticky comment
Expand Down
3 changes: 1 addition & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ inputs:
default: '${{ github.repository }}'
pull_number:
description: 'The pull request number'
required: true
default: '${{ github.event.workflow_run.pull_requests[0].number }}'
required: false
template:
description: 'The template name or a lodash style template to use for the comment'
required: false
Expand Down
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function run() {
core.info('Retrieving the inputs...')
const repository = core.getInput('repository')
const [owner, repo] = repository.split('/').slice(-2)
const pull_number = core.getInput('pull_number')
const pull_number_option = core.getInput('pull_number')
const template_name = core.getInput('template')
const identifier = core.getInput('identifier')
const token = core.getInput('token')
Expand Down Expand Up @@ -68,6 +68,29 @@ async function run() {
core.info('Setting up the GitHub client...')
const octokit = new github.getOctokit(token)

core.info('Inferring the pull request number...')
let pull_number
if (pull_number_option === '') {
if (github.context.eventName !== 'workflow_run' || github.context.payload.event !== 'pull_request') {
throw new Error('The pull request number must be provided when not running in a workflow run event (triggered by pull_request event) context.')
}
core.info('Finding matching pull requests...')
const pull_requests = await octokit.paginate(octokit.rest.issues.search, {
q: `is:pr repo:${owner}/${repo} head:${github.context.payload.head_branch} base:${github.context.payload.base_branch} ${github.context.payload.commit.id}`
})
core.debug(`Pull requests: ${JSON.stringify(pull_requests, null, 2)}`)
if (pull_requests.length === 0) {
throw new Error('No pull request found for the commit.')
}
if (pull_requests.length > 1) {
throw new Error('Multiple pull requests found for the commit.')
}
pull_number = pull_requests[0].number
} else {
pull_number = parseInt(pull_number_option)
}
core.info(`Pull request number: ${pull_number}`)

core.info('Retrieving the pull request...')
const pull = await octokit.rest.pulls.get({
owner,
Expand Down

0 comments on commit c65eca8

Please sign in to comment.