| name: pr-comment |
| |
| on: |
| # Runs once when the 'pr' workflow is requested, and again when it's completed |
| workflow_run: |
| workflows: |
| - pr |
| types: |
| - requested |
| - completed |
| |
| jobs: |
| build: |
| runs-on: ubuntu-latest |
| timeout-minutes: 15 |
| permissions: |
| pull-requests: write |
| steps: |
| - # https://github.com/orgs/community/discussions/25220#discussioncomment-11316244 |
| name: 'Get PR context' |
| id: pr-context |
| env: |
| GH_TOKEN: ${{ github.token }} |
| PR_TARGET_REPO: ${{ github.repository }} |
| PR_BRANCH: |- |
| ${{ |
| (github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login) |
| && format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch) |
| || github.event.workflow_run.head_branch |
| }} |
| run: | |
| gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \ |
| --json 'number' --jq '"number=\(.number)"' \ |
| >> "${GITHUB_OUTPUT}" |
| - name: Find Comment |
| uses: peter-evans/find-comment@v4 |
| id: find-comment |
| with: |
| issue-number: ${{ steps.pr-context.outputs.number }} |
| comment-author: 'github-actions[bot]' |
| body-includes: "<!-- pr-comment -->" |
| |
| # If 'pr' workflow run was 'requested'... |
| - if: github.event.action == 'requested' && steps.find-comment.outputs.comment-id != '' |
| name: PR workflow started - Update Comment |
| uses: peter-evans/create-or-update-comment@v5 |
| with: |
| issue-number: ${{ steps.pr-context.outputs.number }} |
| comment-id: ${{ steps.find-comment.outputs.comment-id }} |
| edit-mode: replace |
| body: | |
| <!-- pr-comment --> |
| Waiting for [build job](https://github.com/${{github.repository}}/actions/runs/${{github.event.workflow_run.id}}) (at ${{ github.event.workflow_run.head_sha }})... |
| |
| # If 'pr' workflow run was 'completed' (but not 'cancelled', that usually means a new job is |
| # running, and we don't want to race with it)... |
| - if: github.event.action == 'completed' && github.event.workflow_run.conclusion != 'cancelled' |
| name: PR workflow completed - Download pr-comment-body.txt |
| uses: actions/download-artifact@v4 |
| continue-on-error: true |
| with: |
| github-token: ${{ github.token }} |
| repository: ${{ github.event.workflow_run.repository.full_name }} |
| run-id: ${{ github.event.workflow_run.id }} |
| name: pr-comment-body.txt |
| - if: github.event.action == 'completed' && github.event.workflow_run.conclusion != 'cancelled' |
| name: PR workflow completed - Create pr-comment.txt |
| env: |
| REPO: ${{github.repository}} |
| RUN_ID: ${{github.event.workflow_run.id}} |
| RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| run: | |
| set -eu |
| |
| ( |
| cat << EOF |
| <!-- pr-comment --> |
| Results for [build job](https://github.com/${REPO}/actions/runs/${RUN_ID}) (at ${RUN_HEAD_SHA}): |
| |
| EOF |
| |
| if [ -f pr-comment-body.txt ]; then |
| cat pr-comment-body.txt |
| else |
| echo '(No pr-comment-body.txt artifact found from this job.)' |
| fi |
| ) > pr-comment.txt |
| - if: github.event.action == 'completed' && github.event.workflow_run.conclusion != 'cancelled' |
| name: PR workflow completed - Post or Update Comment |
| uses: peter-evans/create-or-update-comment@v5 |
| with: |
| issue-number: ${{ steps.pr-context.outputs.number }} |
| comment-id: ${{ steps.find-comment.outputs.comment-id }} |
| edit-mode: replace |
| body-path: pr-comment.txt |