name: CEM API comment # Posts the report of the "CEM API" workflow to the pull request, also for pull # requests from forks and Dependabot, which only get a read-only token there. # This workflow has a write token, so it never checks out or runs the code of # the pull request. It only reads the report artifact, as untrusted text. A # fork can shape the text it posts, but only on its own pull request, which the # head SHA check below enforces. on: workflow_run: workflows: [CEM API] types: [completed] permissions: {} jobs: comment: name: Post API report if: github.event.workflow_run.event == 'pull_request' && (github.event.workflow_run.conclusion == 'success' || github.event.workflow_run.conclusion == 'failure') runs-on: ubuntu-26.04 timeout-minutes: 5 permissions: actions: read pull-requests: write steps: - name: Download report continue-on-error: true uses: actions/download-artifact@v8 with: name: cem-api-report path: report run-id: ${{ github.event.workflow_run.id }} github-token: ${{ github.token }} - name: Post or update comment uses: actions/github-script@v9 env: HEAD_SHA: ${{ github.event.workflow_run.head_sha }} CONCLUSION: ${{ github.event.workflow_run.conclusion }} RUN_URL: ${{ github.event.workflow_run.html_url }} with: script: | const fs = require('node:fs'); const marker = ''; const { owner, repo } = context.repo; const { HEAD_SHA: headSha, CONCLUSION: conclusion, RUN_URL: runUrl } = process.env; if (!fs.existsSync('report/pr-number')) { core.notice('The CEM API run uploaded no report, skipping'); return; } const prNumber = Number(fs.readFileSync('report/pr-number', 'utf-8').trim()); if (!Number.isInteger(prNumber) || prNumber <= 0) { core.setFailed('The report has no valid pull request number'); return; } // The artifact can be written by a fork, so only post to the pull // request whose head the report was made for const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber }); if (pr.head.sha !== headSha) { core.notice(`#${prNumber} has moved on from ${headSha}, skipping the outdated report`); return; } const comments = await github.paginate(github.rest.issues.listComments, { owner, repo, issue_number: prNumber, per_page: 100, }); const existing = comments.find( (comment) => comment.user.login === 'github-actions[bot]' && comment.body.startsWith(marker), ); // A fork can write any artifact, so the failure text only uses // fields of the workflow run if (conclusion === 'failure') { if (existing) { const body = [ marker, '### Custom elements API', '', `The API report for ${headSha} failed, see ${runUrl}. This comment is outdated.`, '', ].join('\n'); await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body }); } return; } const body = fs.readFileSync('report/comment.md', 'utf-8'); const { changes } = JSON.parse(fs.readFileSync('report/status.json', 'utf-8')); if (!body.startsWith(marker) || body.length > 65536) { core.setFailed('The report is not a valid comment'); return; } if (existing) { await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body }); } else if (changes === true) { await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body }); }