PR Code Coverage Comment #65
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: PR Code Coverage Comment | |
| on: | |
| workflow_run: | |
| workflows: ["Build repo"] | |
| types: | |
| - completed | |
| permissions: | |
| pull-requests: write | |
| contents: read | |
| jobs: | |
| coverage-comment: | |
| runs-on: ubuntu-latest | |
| if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' | |
| steps: | |
| - name: Download Coverage Results | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| with: | |
| script: | | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: ${{ github.event.workflow_run.id }}, | |
| }); | |
| const matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
| return artifact.name == "coverage-results" | |
| })[0]; | |
| if (!matchArtifact) { | |
| console.log('No coverage-results artifact found'); | |
| return; | |
| } | |
| const download = await github.rest.actions.downloadArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: matchArtifact.id, | |
| archive_format: 'zip', | |
| }); | |
| const fs = require('fs'); | |
| fs.writeFileSync('coverage-results.zip', Buffer.from(download.data)); | |
| - name: Extract Coverage Results | |
| run: | | |
| if [ -f coverage-results.zip ]; then | |
| unzip coverage-results.zip | |
| echo "Coverage results extracted" | |
| ls -la | |
| else | |
| echo "No coverage results found, skipping comment" | |
| exit 0 | |
| fi | |
| - name: Get PR Number | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 | |
| id: get-pr | |
| with: | |
| script: | | |
| const { data: pullRequests } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| commit_sha: '${{ github.event.workflow_run.head_sha }}', | |
| }); | |
| if (pullRequests.length === 0) { | |
| console.log('No pull request found for this commit'); | |
| return; | |
| } | |
| const pullRequest = pullRequests[0]; | |
| console.log(`Found PR #${pullRequest.number}`); | |
| core.setOutput('pr-number', pullRequest.number); | |
| - name: Add Coverage PR Comment | |
| uses: marocchino/sticky-pull-request-comment@70d2764d1a7d5d9560b100cbea0077fc8f633987 # v3.0.2 | |
| if: steps.get-pr.outputs.pr-number | |
| with: | |
| number: ${{ steps.get-pr.outputs.pr-number }} | |
| recreate: true | |
| path: code-coverage-results.md |