From a3eb7c2c448abb92e444aea3a61970635bfa1d81 Mon Sep 17 00:00:00 2001 From: galargh Date: Thu, 20 Jun 2024 15:30:16 +0100 Subject: [PATCH] Added pull_number inference from workflow_run event context --- .github/workflows/comment.yml | 1 - .github/workflows/test.yml | 2 +- CHANGELOG.md | 5 +++++ README.md | 4 ++-- action.yml | 3 +-- index.js | 25 ++++++++++++++++++++++++- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.github/workflows/comment.yml b/.github/workflows/comment.yml index 9468bf0..835ac3a 100644 --- a/.github/workflows/comment.yml +++ b/.github/workflows/comment.yml @@ -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' diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 77f4c8f..e0ecf2a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index c8c16b1..c1a18db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 12e850e..ddd766f 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 diff --git a/action.yml b/action.yml index 60af1a2..56eb7ec 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/index.js b/index.js index 7f1a3f0..ba7f982 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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,