-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add an action that can be used to fetch a PR number (#32)
* feat: Add an action that can be used to fetch a PR number * test: Add test for PR from a fork * docs: Add documentation on the new action
- Loading branch information
Showing
9 changed files
with
186 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,25 +23,32 @@ jobs: | |
else | ||
echo "BREAKING_CHANGES=true" >> "$GITHUB_ENV" | ||
fi | ||
- name: Fetch PR number | ||
id: pr | ||
uses: 8BitJonny/gh-get-current-pr@08e737c57a3a4eb24cec6487664b243b77eb5e36 | ||
- if: ${{ endsWith(github.repository, '/python-package-ci-cd') }} # Run the local action when this is run in the python-package-ci-cd repository | ||
id: fetch-pr-number-local | ||
uses: ./actions/fetch_pr_number | ||
with: | ||
sha: ${{ github.event.workflow_run.head_sha }} | ||
github-repository: ${{ github.repository }} | ||
- if: ${{ !endsWith(github.repository, '/python-package-ci-cd') }} # Run the public action when this is run outside the python-package-ci-cd repository | ||
id: fetch-pr-number | ||
uses: tektronix/python-package-ci-cd/actions/[email protected] | ||
with: | ||
sha: ${{ github.event.workflow_run.head_sha }} | ||
github-repository: ${{ github.repository }} | ||
- name: Publish API Breaking Changes Check Results | ||
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 | ||
if: ${{ env.BREAKING_CHANGES == 'true' }} | ||
with: | ||
header: breaking-api-changes | ||
number: ${{ steps.pr.outputs.number }} | ||
number: ${{ steps.fetch-pr-number.outputs.number || steps.fetch-pr-number-local.outputs.number }} | ||
recreate: true | ||
path: artifacts/breaking_changes.md | ||
- name: Add workflow link to comment | ||
if: ${{ env.BREAKING_CHANGES == 'true' }} | ||
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 | ||
with: | ||
header: breaking-api-changes | ||
number: ${{ steps.pr.outputs.number }} | ||
number: ${{ steps.fetch-pr-number.outputs.number || steps.fetch-pr-number-local.outputs.number }} | ||
append: true | ||
message: |- | ||
<p><a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}">Link to workflow run</a></p> | ||
|
@@ -50,5 +57,5 @@ jobs: | |
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 | ||
with: | ||
header: breaking-api-changes | ||
number: ${{ steps.pr.outputs.number }} | ||
number: ${{ steps.fetch-pr-number.outputs.number || steps.fetch-pr-number-local.outputs.number }} | ||
delete: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,23 +27,30 @@ jobs: | |
run_id: ${{ github.event.workflow_run.id }} | ||
name: artifact_${{ matrix.os-name }}_tests | ||
path: artifacts | ||
- name: Fetch PR number | ||
id: pr | ||
uses: 8BitJonny/gh-get-current-pr@08e737c57a3a4eb24cec6487664b243b77eb5e36 | ||
- if: ${{ endsWith(github.repository, '/python-package-ci-cd') }} # Run the local action when this is run in the python-package-ci-cd repository | ||
id: fetch-pr-number-local | ||
uses: ./actions/fetch_pr_number | ||
with: | ||
sha: ${{ github.event.workflow_run.head_sha }} | ||
github-repository: ${{ github.repository }} | ||
- if: ${{ !endsWith(github.repository, '/python-package-ci-cd') }} # Run the public action when this is run outside the python-package-ci-cd repository | ||
id: fetch-pr-number | ||
uses: tektronix/python-package-ci-cd/actions/[email protected] | ||
with: | ||
sha: ${{ github.event.workflow_run.head_sha }} | ||
github-repository: ${{ github.repository }} | ||
- name: Publish Test Results | ||
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 | ||
with: | ||
header: test-results-${{ matrix.os-name }} | ||
number: ${{ steps.pr.outputs.number }} | ||
number: ${{ steps.fetch-pr-number.outputs.number || steps.fetch-pr-number-local.outputs.number }} | ||
recreate: true | ||
path: artifacts/.results_tests/github_report.md | ||
- name: Add workflow link to comment | ||
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 | ||
with: | ||
header: test-results-${{ matrix.os-name }} | ||
number: ${{ steps.pr.outputs.number }} | ||
number: ${{ steps.fetch-pr-number.outputs.number || steps.fetch-pr-number-local.outputs.number }} | ||
append: true | ||
message: |- | ||
<p><a href="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}">Link to workflow run</a></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
name: Fetch PR Number | ||
description: Find the PR number for the provided SHA. This action also works for PRs | ||
that are from forks. | ||
inputs: | ||
sha: | ||
description: The SHA of the commit to find the PR number for. | ||
required: true | ||
github-repository: | ||
description: The GitHub repository to search for the PR in. | ||
required: true | ||
retry-delay: | ||
description: The delay in seconds between retries. | ||
default: '120' | ||
required: false | ||
max-attempts: | ||
description: The maximum number of attempts to find the PR number. | ||
default: '5' | ||
required: false | ||
outputs: | ||
number: | ||
description: The PR number. | ||
value: ${{ steps.fetch-pr.outputs.result }} | ||
runs: | ||
using: composite | ||
steps: | ||
- name: Fetch PR number | ||
id: fetch-pr | ||
uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 | ||
with: | ||
script: |- | ||
const maxAttempts = ${{ inputs.max-attempts }}; | ||
let attempt = 0; | ||
let pullRequestNumber; | ||
while (attempt < maxAttempts) { | ||
try { | ||
const response = await github.rest.search.issuesAndPullRequests({ | ||
q: 'repo:${{ inputs.github-repository }} is:pr sha:${{ inputs.sha }}', | ||
per_page: 1, | ||
}); | ||
const items = response.data.items; | ||
if (items.length < 1) { | ||
throw new Error('No PRs found'); | ||
} | ||
pullRequestNumber = items[0].number; | ||
console.info("Pull request number is", pullRequestNumber); | ||
break; // Exit loop on success | ||
} catch (error) { | ||
console.error(`Attempt ${attempt + 1} failed:`, error.message); | ||
if (attempt < maxAttempts - 1) { // Check if not last attempt | ||
console.log(`Waiting for ${{ inputs.retry-delay }} seconds before retrying...`); | ||
await new Promise(resolve => setTimeout(resolve, ${{ inputs.retry-delay }} * 1000)); // Wait for 2 minutes | ||
} | ||
} | ||
attempt++; | ||
} | ||
if (!pullRequestNumber) { | ||
core.setFailed(`Failed to fetch PR number after ${maxAttempts} attempts`); | ||
} | ||
return pullRequestNumber; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# fetch_pr_number | ||
|
||
This action fetches the Pull Request number for the provided SHA in the provided GitHub repository. | ||
See the [example](#example) below for usage details. | ||
|
||
## Inputs | ||
|
||
| Input variable | Necessity | Description | Default | | ||
| ------------------- | --------- | ----------------------------------------------------- | ------- | | ||
| `sha` | required | The SHA of the commit to find the PR number for. | | | ||
| `github-repository` | required | The GitHub repository to search for the PR in. | | | ||
| `retry-delay` | optional | The delay in seconds between retries. | 120 | | ||
| `max-attempts` | optional | The maximum number of attempts to find the PR number. | 5 | | ||
|
||
## Outputs | ||
|
||
| Output variable | Description | | ||
| --------------- | -------------- | | ||
| `number` | The PR number. | | ||
|
||
## Example | ||
|
||
```yaml | ||
name: Publish Results | ||
on: | ||
workflow_call: | ||
jobs: | ||
publish-results: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: tektronix/python-package-ci-cd/actions/[email protected] | ||
id: fetch-pr-number | ||
with: | ||
sha: ${{ github.event.workflow_run.head_sha }} # required | ||
github-repository: ${{ github.repository }} # required | ||
retry-delay: 120 # optional | ||
max-attempts: 5 # optional | ||
- name: Echo PR Number | ||
run: | | ||
echo "PR Number: ${{ steps.fetch-pr-number.outputs.number }}" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters