Leave a comment #1286
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
| name: Leave a comment | |
| on: | |
| workflow_run: | |
| workflows: ['Generate and Compare Docs'] | |
| types: [completed] | |
| permissions: | |
| contents: read | |
| actions: read | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.workflow_run.id }} | |
| cancel-in-progress: true | |
| jobs: | |
| aggregate: | |
| name: Aggregate Comparison Results | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| steps: | |
| - name: Harden Runner | |
| uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0 | |
| with: | |
| egress-policy: block | |
| allowed-endpoints: > | |
| api.github.com:443 | |
| - name: Download all comparison artifacts | |
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | |
| with: | |
| run-id: ${{ github.event.workflow_run.id }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| path: results | |
| - name: Combine results | |
| id: combine | |
| # Even if the cat fails (no files found), we don't want to fail the workflow | |
| continue-on-error: true | |
| run: | | |
| { | |
| echo "combined<<EOF" | |
| cat results/*/comparison.txt | |
| echo "EOF" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Resolve Pull Request Number | |
| id: pr | |
| if: steps.combine.outputs.combined | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| with: | |
| script: | | |
| const run = context.payload.workflow_run; | |
| // 1. For same-repo Pull Requests the run is already linked to its PR(s). | |
| if (run.pull_requests && run.pull_requests.length) { | |
| core.setOutput('number', run.pull_requests[0].number); | |
| return; | |
| } | |
| // 2. For forks that list is empty, so find the open Pull Request who has the | |
| // correct branch information | |
| const match = await github.rest.pulls.list({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| state: 'open', | |
| head: `${run.head_repository.owner.login}:${run.head_branch}`, | |
| sort: 'updated', | |
| direction: 'desc', | |
| per_page: 1, | |
| }).then(r => r.data[0]); | |
| if (!match) { | |
| core.info(`No open pull request found for HEAD ${run.head_sha}`); | |
| return; | |
| } | |
| core.setOutput('number', match.number); | |
| - name: Add Comment to PR | |
| if: steps.combine.outputs.combined && steps.pr.outputs.number != '' | |
| uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 | |
| with: | |
| comment-tag: compared | |
| message: ${{ steps.combine.outputs.combined }} | |
| pr-number: ${{ steps.pr.outputs.number }} |