|
| 1 | +name: PR Triage |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: read |
| 9 | + pull-requests: write |
| 10 | + issues: write |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: pr-triage-${{ github.event.pull_request.number }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +jobs: |
| 17 | + triage: |
| 18 | + name: Quality-signal triage |
| 19 | + runs-on: ubuntu-latest |
| 20 | + timeout-minutes: 10 |
| 21 | + steps: |
| 22 | + - name: Label and guide |
| 23 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 24 | + with: |
| 25 | + script: | |
| 26 | + const pr = context.payload.pull_request; |
| 27 | + const { owner, repo } = context.repo; |
| 28 | + const prNumber = pr.number; |
| 29 | +
|
| 30 | + // Trusted authors bypass triage entirely. |
| 31 | + const trusted = ["OWNER", "MEMBER", "COLLABORATOR"]; |
| 32 | + if (trusted.includes(pr.author_association)) { |
| 33 | + core.info(`Author association ${pr.author_association} is trusted; skipping triage.`); |
| 34 | + return; |
| 35 | + } |
| 36 | +
|
| 37 | + // Labels this workflow owns. Anything here may be added or removed each run. |
| 38 | + const MANAGED = ["needs-issue", "needs-description", "needs-tests", "workflow-change"]; |
| 39 | + const want = new Set(); |
| 40 | +
|
| 41 | + const body = pr.body || ""; |
| 42 | +
|
| 43 | + // Linked issue: look for "closes/fixes/resolves #N" or any bare #N reference. |
| 44 | + const hasLinkedIssue = |
| 45 | + /\b(close[sd]?|fix(e[sd])?|resolve[sd]?)\b[^\n]*#\d+/i.test(body) || |
| 46 | + /#\d+/.test(body); |
| 47 | + if (!hasLinkedIssue) want.add("needs-issue"); |
| 48 | +
|
| 49 | + // Description substance: strip HTML comments and markdown headings, then measure. |
| 50 | + const stripped = body |
| 51 | + .replace(/<!--[\s\S]*?-->/g, "") |
| 52 | + .replace(/^#+.*$/gm, "") |
| 53 | + .trim(); |
| 54 | + if (stripped.length < 50) want.add("needs-description"); |
| 55 | +
|
| 56 | + // File-based signals. |
| 57 | + const files = await github.paginate(github.rest.pulls.listFiles, { |
| 58 | + owner, repo, pull_number: prNumber, per_page: 100, |
| 59 | + }); |
| 60 | + const names = files.map(f => f.filename); |
| 61 | +
|
| 62 | + const isFork = pr.head.repo && pr.head.repo.full_name !== pr.base.repo.full_name; |
| 63 | + const touchesWorkflows = names.some(n => n.startsWith(".github/workflows/")); |
| 64 | + if (isFork && touchesWorkflows) want.add("workflow-change"); |
| 65 | +
|
| 66 | + const changedRust = names.some(n => n.startsWith("crates/") && n.endsWith(".rs")); |
| 67 | + // A test can live in a tests/ dir, a *_test.rs file, or — the common Rust |
| 68 | + // pattern — an inline #[test] / #[cfg(test)] block added inside the source file. |
| 69 | + const addsInlineTest = files.some(f => |
| 70 | + f.filename.endsWith(".rs") && f.patch && |
| 71 | + /^\+.*#\[(test\]|cfg\(test\))/m.test(f.patch)); |
| 72 | + const touchedTests = |
| 73 | + names.some(n => n.includes("/tests/") || n.endsWith("_test.rs")) || addsInlineTest; |
| 74 | + if (changedRust && !touchedTests) want.add("needs-tests"); |
| 75 | +
|
| 76 | + // Reconcile labels: add wanted managed labels, remove managed labels no longer wanted. |
| 77 | + const current = (await github.rest.issues.listLabelsOnIssue({ |
| 78 | + owner, repo, issue_number: prNumber, |
| 79 | + })).data.map(l => l.name); |
| 80 | +
|
| 81 | + const toAdd = [...want].filter(l => !current.includes(l)); |
| 82 | + const toRemove = MANAGED.filter(l => current.includes(l) && !want.has(l)); |
| 83 | +
|
| 84 | + if (toAdd.length) { |
| 85 | + await github.rest.issues.addLabels({ owner, repo, issue_number: prNumber, labels: toAdd }); |
| 86 | + } |
| 87 | + for (const name of toRemove) { |
| 88 | + await github.rest.issues.removeLabel({ owner, repo, issue_number: prNumber, name }) |
| 89 | + .catch(() => {}); // ignore if already gone |
| 90 | + } |
| 91 | +
|
| 92 | + // Guidance comment: only for advisory signals, and only once. |
| 93 | + const advisory = ["needs-issue", "needs-description", "needs-tests"].filter(l => want.has(l)); |
| 94 | + if (!advisory.length) return; |
| 95 | +
|
| 96 | + const MARKER = "<!-- gitlawb-triage -->"; |
| 97 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 98 | + owner, repo, issue_number: prNumber, per_page: 100, |
| 99 | + }); |
| 100 | + if (comments.some(c => c.body && c.body.includes(MARKER))) { |
| 101 | + core.info("Guidance comment already present; not reposting."); |
| 102 | + return; |
| 103 | + } |
| 104 | +
|
| 105 | + const reasons = { |
| 106 | + "needs-issue": "Link the issue this addresses (`Closes #123`). For protocol changes, open an issue first.", |
| 107 | + "needs-description": "Add a short summary of what changes and why — the template's Summary and Motivation sections.", |
| 108 | + "needs-tests": "This changes Rust source but no tests changed. Tests are required for fixes and strongly encouraged for features.", |
| 109 | + }; |
| 110 | + const lines = advisory.map(l => `- ${reasons[l]}`).join("\n"); |
| 111 | + const comment = [ |
| 112 | + MARKER, |
| 113 | + "Thanks for the contribution. A couple of things will help us review this faster:", |
| 114 | + "", |
| 115 | + lines, |
| 116 | + "", |
| 117 | + "See [CONTRIBUTING.md](https://github.com/Gitlawb/node/blob/main/CONTRIBUTING.md). Update the PR and these notes will clear automatically.", |
| 118 | + ].join("\n"); |
| 119 | +
|
| 120 | + await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: comment }); |
0 commit comments