|
| 1 | +name: TypeScript Error PR Comment |
| 2 | + |
| 3 | +permissions: |
| 4 | + actions: read |
| 5 | + contents: read |
| 6 | + issues: write |
| 7 | + pull-requests: write |
| 8 | + |
| 9 | +on: |
| 10 | + workflow_run: |
| 11 | + workflows: ['Lint Check'] |
| 12 | + types: [completed] |
| 13 | + |
| 14 | +jobs: |
| 15 | + comment: |
| 16 | + if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }} |
| 17 | + runs-on: ubuntu-latest |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Download TypeScript error report artifact |
| 21 | + env: |
| 22 | + GH_TOKEN: ${{ github.token }} |
| 23 | + REPO: ${{ github.repository }} |
| 24 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 25 | + run: | |
| 26 | + set -euo pipefail |
| 27 | + ARTIFACT_ID=$(gh api "repos/${REPO}/actions/runs/${RUN_ID}/artifacts" --jq '.artifacts[] | select(.name=="ts-error-report") | .id' | head -n 1) |
| 28 | + if [ -z "${ARTIFACT_ID}" ]; then |
| 29 | + echo "No ts-error-report artifact found for workflow run ${RUN_ID}." |
| 30 | + exit 0 |
| 31 | + fi |
| 32 | +
|
| 33 | + gh api -H "Accept: application/vnd.github+json" "repos/${REPO}/actions/artifacts/${ARTIFACT_ID}/zip" > ts-error-report.zip |
| 34 | + mkdir -p artifact |
| 35 | + unzip -o ts-error-report.zip -d artifact |
| 36 | +
|
| 37 | + - name: Post TypeScript error report comment |
| 38 | + uses: actions/github-script@v7 |
| 39 | + with: |
| 40 | + script: | |
| 41 | + const fs = require('node:fs'); |
| 42 | + const path = 'artifact/ts-error-report.json'; |
| 43 | + if (!fs.existsSync(path)) { |
| 44 | + core.info('No ts-error-report.json found, skipping PR comment.'); |
| 45 | + return; |
| 46 | + } |
| 47 | +
|
| 48 | + const pullRequests = context.payload.workflow_run?.pull_requests || []; |
| 49 | + const prNumber = pullRequests[0]?.number; |
| 50 | + if (!prNumber) { |
| 51 | + core.info('No pull request found on workflow_run payload, skipping PR comment.'); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + const report = JSON.parse(fs.readFileSync(path, 'utf8')); |
| 56 | + const toNumber = (value) => Number.isFinite(Number(value)) ? Number(value) : 0; |
| 57 | + const expectedHeadSha = context.payload.workflow_run?.head_sha || ''; |
| 58 | + const reportedHeadSha = String(report.workflow_run_head_sha || ''); |
| 59 | + if (!reportedHeadSha || reportedHeadSha !== expectedHeadSha) { |
| 60 | + core.warning( |
| 61 | + `Head SHA mismatch between workflow_run (${expectedHeadSha}) and artifact (${reportedHeadSha}). Skipping PR comment.` |
| 62 | + ); |
| 63 | + return; |
| 64 | + } |
| 65 | +
|
| 66 | + const prErrors = toNumber(report.ts_errors_pr); |
| 67 | + const mainErrors = toNumber(report.ts_errors_main); |
| 68 | + const diff = prErrors - mainErrors; |
| 69 | + const sign = diff > 0 ? `+${diff}` : `${diff}`; |
| 70 | + const emoji = diff > 0 ? '⚠️' : diff < 0 ? '🎉' : 'ℹ️'; |
| 71 | + const prSvelteErrors = toNumber(report.svelte_errors_pr); |
| 72 | + const mainSvelteErrors = toNumber(report.svelte_errors_main); |
| 73 | + const svelteDiff = prSvelteErrors - mainSvelteErrors; |
| 74 | + const svelteSign = svelteDiff > 0 ? `+${svelteDiff}` : `${svelteDiff}`; |
| 75 | + const svelteEmoji = svelteDiff > 0 ? '⚠️' : svelteDiff < 0 ? '🎉' : 'ℹ️'; |
| 76 | + const marker = '<!-- ts-error-report -->'; |
| 77 | + const body = [ |
| 78 | + marker, |
| 79 | + '**TypeScript error report:**', |
| 80 | + `- This branch: ${prErrors}`, |
| 81 | + `- main branch: ${mainErrors}`, |
| 82 | + `- Difference (PR - main): ${sign} ${emoji}`, |
| 83 | + '', |
| 84 | + '**Svelte TypeScript error report:**', |
| 85 | + `- This branch: ${prSvelteErrors}`, |
| 86 | + `- main branch: ${mainSvelteErrors}`, |
| 87 | + `- Difference (PR - main): ${svelteSign} ${svelteEmoji}` |
| 88 | + ].join('\n'); |
| 89 | +
|
| 90 | + const { owner, repo } = context.repo; |
| 91 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 92 | + owner, |
| 93 | + repo, |
| 94 | + issue_number: prNumber, |
| 95 | + per_page: 100 |
| 96 | + }); |
| 97 | + const existingComment = comments.find((comment) => comment.body?.includes(marker)); |
| 98 | +
|
| 99 | + if (existingComment) { |
| 100 | + await github.rest.issues.updateComment({ |
| 101 | + owner, |
| 102 | + repo, |
| 103 | + comment_id: existingComment.id, |
| 104 | + body |
| 105 | + }); |
| 106 | + } else { |
| 107 | + await github.rest.issues.createComment({ |
| 108 | + owner, |
| 109 | + repo, |
| 110 | + issue_number: prNumber, |
| 111 | + body |
| 112 | + }); |
| 113 | + } |
0 commit comments