PR Spec Preview Comment #8
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 Spec Preview Comment | |
| on: | |
| workflow_run: | |
| workflows: ["Check Specs"] | |
| types: [completed] | |
| permissions: | |
| actions: read | |
| pull-requests: write | |
| jobs: | |
| comment: | |
| name: Post spec preview | |
| runs-on: ubuntu-latest | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'success' | |
| steps: | |
| - name: Download PR metadata | |
| uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| run_id: ${{ github.event.workflow_run.id }} | |
| name: pr-metadata | |
| - name: Read PR number | |
| id: pr | |
| run: echo "number=$(cat pr-number.txt)" >> "$GITHUB_OUTPUT" | |
| - name: Download spec artifacts | |
| uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| run_id: ${{ github.event.workflow_run.id }} | |
| name_is_regexp: true | |
| name: pr-${{ steps.pr.outputs.number }}-.* | |
| path: /tmp/pr-artifacts | |
| continue-on-error: true | |
| - name: Comment on PR | |
| uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const prNumber = parseInt('${{ steps.pr.outputs.number }}'); | |
| const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{ github.event.workflow_run.id }}`; | |
| // Find spec artifacts | |
| const specsDir = fs.readdirSync('/tmp/pr-artifacts').find(d => d.includes('specs')); | |
| const diffsDir = fs.readdirSync('/tmp/pr-artifacts').find(d => d.includes('diffs')); | |
| let table = '| Spec | Changed |\n|------|--------|\n'; | |
| if (specsDir) { | |
| const specsPath = path.join('/tmp/pr-artifacts', specsDir); | |
| const htmlFiles = fs.readdirSync(specsPath) | |
| .filter(f => f.startsWith('draft-') && f.endsWith('.html')) | |
| .sort(); | |
| for (const file of htmlFiles) { | |
| const name = file.replace('.html', ''); | |
| let changed = '-'; | |
| if (diffsDir) { | |
| const diffFile = path.join('/tmp/pr-artifacts', diffsDir, `${name}-diff.txt`); | |
| if (fs.existsSync(diffFile)) { | |
| const content = fs.readFileSync(diffFile, 'utf8'); | |
| if (content.trim() && !content.includes('New spec')) { | |
| changed = 'Yes'; | |
| } | |
| } | |
| } | |
| table += `| \`${name}\` | ${changed} |\n`; | |
| } | |
| } | |
| const body = `<!-- ietf-spec-preview --> | |
| ## Spec Preview | |
| ${table} | |
| **[Download spec artifacts](${runUrl}#artifacts)** (HTML, TXT, XML, PDF) | |
| `; | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const existingComment = comments.find(c => c.body.includes('<!-- ietf-spec-preview -->')); | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body: body | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: body | |
| }); | |
| } |