PR Artifact Comment #34
This file contains hidden or 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
| # Credit goes to polarathene for a work around to the rate-limiting API | |
| # https://github.com/orgs/community/discussions/25220 | |
| name: PR Artifact Comment | |
| on: | |
| workflow_run: | |
| workflows: | |
| - Maven CI | |
| types: | |
| - completed | |
| permissions: | |
| actions: read | |
| contents: read | |
| pull-requests: write | |
| issues: write | |
| jobs: | |
| comment: | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Get PR number for commit | |
| id: pr-context | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| PR_TARGET_REPO: ${{ github.repository }} | |
| PR_SHA: ${{ github.event.workflow_run.head_sha }} | |
| run: | | |
| gh search prs "$PR_SHA" \ | |
| --repo "$PR_TARGET_REPO" \ | |
| --state open \ | |
| --json number \ | |
| --jq '"pr_number=\(.[0].number)"' >> "$GITHUB_OUTPUT" | |
| - name: Get artifact link | |
| id: artifact | |
| if: steps.pr-context.outputs.pr_number != '' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const runId = context.payload.workflow_run.id; | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: runId | |
| }); | |
| const expectedName = `quickshop-pr-${{ steps.pr-context.outputs.pr_number }}`; | |
| const artifact = artifacts.data.artifacts.find( | |
| a => a.name === expectedName | |
| ); | |
| if (!artifact) { | |
| core.setOutput('url', context.payload.workflow_run.html_url); | |
| core.setOutput('name', expectedName); | |
| return; | |
| } | |
| const url = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/artifacts/${artifact.id}`; | |
| core.setOutput('url', url); | |
| core.setOutput('name', artifact.name); | |
| - name: Comment artifact link on PR | |
| if: steps.pr-context.outputs.pr_number != '' | |
| uses: marocchino/sticky-pull-request-comment@v2 | |
| with: | |
| number: ${{ steps.pr-context.outputs.pr_number }} | |
| header: quickshop-pr-artifacts | |
| recreate: true | |
| message: | | |
| Latest build artifacts are available for this PR. | |
| Commit: ${{ github.event.workflow_run.head_sha }} | |
| Workflow run: ${{ github.event.workflow_run.html_url }} | |
| Download the artifact named: | |
| [${{ steps.artifact.outputs.name }}](${{ steps.artifact.outputs.url }}) |