|
| 1 | +name: pr-summary |
| 2 | + |
| 3 | +# Posts (and keeps updating) a single sticky comment summarizing what a PR |
| 4 | +# changes in the list: entries added/removed, their section, and a live link |
| 5 | +# check. Informational only — the awesome-lint and link-check jobs remain the |
| 6 | +# authoritative gate. |
| 7 | +# |
| 8 | +# Uses pull_request_target so it has permission to comment on PRs from forks. |
| 9 | +# It NEVER checks out or executes the PR's code: it only reads the diff and the |
| 10 | +# head README.md through the API and performs outbound HTTP checks on the URLs. |
| 11 | + |
| 12 | +on: |
| 13 | + pull_request_target: |
| 14 | + types: [opened, synchronize, reopened] |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + pull-requests: write |
| 19 | + |
| 20 | +jobs: |
| 21 | + summary: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - uses: actions/github-script@v7 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const MARKER = '<!-- awesome-gno-pr-bot -->'; |
| 28 | + try { |
| 29 | + const { owner, repo } = context.repo; |
| 30 | + const pr = context.payload.pull_request; |
| 31 | + const number = pr.number; |
| 32 | +
|
| 33 | + // Head README (data only — no code execution). |
| 34 | + let headLines = []; |
| 35 | + try { |
| 36 | + const { data } = await github.rest.repos.getContent({ |
| 37 | + owner: pr.head.repo.owner.login, |
| 38 | + repo: pr.head.repo.name, |
| 39 | + path: 'README.md', |
| 40 | + ref: pr.head.sha, |
| 41 | + }); |
| 42 | + headLines = Buffer.from(data.content, 'base64').toString('utf8').split('\n'); |
| 43 | + } catch (e) { /* README may not exist on head; ignore */ } |
| 44 | +
|
| 45 | + // Added / removed list entries from the README diff. |
| 46 | + const files = await github.paginate(github.rest.pulls.listFiles, { |
| 47 | + owner, repo, pull_number: number, per_page: 100, |
| 48 | + }); |
| 49 | + const readme = files.find(f => f.filename === 'README.md'); |
| 50 | + const added = [], removed = []; |
| 51 | + if (readme && readme.patch) { |
| 52 | + for (const line of readme.patch.split('\n')) { |
| 53 | + if (/^\+- \[/.test(line)) added.push(line.slice(1)); |
| 54 | + else if (/^-- \[/.test(line)) removed.push(line.slice(1)); |
| 55 | + } |
| 56 | + } |
| 57 | +
|
| 58 | + const nameOf = l => ((l.match(/\[([^\]]+)\]/) || [])[1] || '?'); |
| 59 | + const urlOf = l => { const m = l.match(/\]\((https?:\/\/[^)\s]+)\)/); return m ? m[1] : null; }; |
| 60 | + const sectionOf = entry => { |
| 61 | + const i = headLines.indexOf(entry); |
| 62 | + if (i < 0) return null; |
| 63 | + for (let j = i; j >= 0; j--) if (headLines[j].startsWith('## ')) return headLines[j].slice(3).trim(); |
| 64 | + return null; |
| 65 | + }; |
| 66 | + const check = async u => { |
| 67 | + try { |
| 68 | + const r = await fetch(u, { redirect: 'follow', headers: { 'User-Agent': 'awesome-gno-pr-bot' } }); |
| 69 | + return r.status; |
| 70 | + } catch (e) { return 0; } |
| 71 | + }; |
| 72 | +
|
| 73 | + let body = `${MARKER}\n### 📋 awesome-gno PR summary\n\n`; |
| 74 | + if (added.length === 0 && removed.length === 0) { |
| 75 | + body += `_No list entries added or removed in this PR._\n`; |
| 76 | + } else { |
| 77 | + if (added.length) { |
| 78 | + const rows = []; |
| 79 | + for (const l of added) { |
| 80 | + const u = urlOf(l); |
| 81 | + const s = sectionOf(l) || '—'; |
| 82 | + const st = u ? await check(u) : null; |
| 83 | + const ok = st && (st < 400 || st === 403 || st === 429); |
| 84 | + const status = st == null ? '—' : (ok ? `✅ ${st}` : `❌ ${st || 'conn'}`); |
| 85 | + rows.push(`| ${nameOf(l)} | ${s} | ${u ? `[link](${u})` : '—'} | ${status} |`); |
| 86 | + } |
| 87 | + body += `**Added ${added.length} ${added.length === 1 ? 'entry' : 'entries'}:**\n\n`; |
| 88 | + body += `| Entry | Section | Link | Status |\n|---|---|---|---|\n${rows.join('\n')}\n\n`; |
| 89 | + } |
| 90 | + if (removed.length) { |
| 91 | + body += `**Removed ${removed.length}:**\n${removed.map(l => `- ${nameOf(l)}`).join('\n')}\n\n`; |
| 92 | + } |
| 93 | + } |
| 94 | + body += `<sub>Live link check — 403/429 are shown but tolerated (some hosts block CI runners). `; |
| 95 | + body += `The **awesome-lint** and **link-check** jobs are the authoritative gate.</sub>`; |
| 96 | +
|
| 97 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 98 | + owner, repo, issue_number: number, per_page: 100, |
| 99 | + }); |
| 100 | + const existing = comments.find(c => c.body && c.body.includes(MARKER)); |
| 101 | + if (existing) { |
| 102 | + await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body }); |
| 103 | + } else { |
| 104 | + await github.rest.issues.createComment({ owner, repo, issue_number: number, body }); |
| 105 | + } |
| 106 | + } catch (err) { |
| 107 | + core.warning(`pr-summary bot failed (non-blocking): ${err.message}`); |
| 108 | + } |
0 commit comments