|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require("node:path"); |
| 4 | +const { |
| 5 | + clean, |
| 6 | + isPlaceholderOnlyValue, |
| 7 | + hasSubstantialStructuredContent, |
| 8 | +} = require(path.join(__dirname, "issue-quality.cjs")); |
| 9 | + |
| 10 | +const ANCESTRY_BEHIND_THRESHOLD = 20; |
| 11 | +/** Cap on ahead_by vs main so stale `dev` forks (many commits ahead of main) are not flagged. */ |
| 12 | +const ANCESTRY_AHEAD_MAIN_MAX = 5; |
| 13 | +const MIN_SECTION_LEN = 40; |
| 14 | +const MIN_RICH_SECTIONS = 2; |
| 15 | +const UNSTRUCTURED_MIN_LEN = 120; |
| 16 | +const UNSTRUCTURED_MIN_BLOCKS = 2; |
| 17 | + |
| 18 | +/** |
| 19 | + * Exact instruction / checklist lines from `.github/PULL_REQUEST_TEMPLATE.md`. |
| 20 | + * Untouched templates must not count as substance. |
| 21 | + */ |
| 22 | +const PR_TEMPLATE_BOILERPLATE_LINES = new Set([ |
| 23 | + "explain the user-visible or maintainer-facing change.", |
| 24 | + "list the commands or checks you ran.", |
| 25 | + "scope stays focused and avoids unrelated cleanup.", |
| 26 | + "docs or release notes were updated when needed.", |
| 27 | + "security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.", |
| 28 | +]); |
| 29 | + |
| 30 | +function isWrongAncestry({ |
| 31 | + behindMain, |
| 32 | + behindBase, |
| 33 | + aheadMain = 0, |
| 34 | + threshold = ANCESTRY_BEHIND_THRESHOLD, |
| 35 | + aheadMainMax = ANCESTRY_AHEAD_MAIN_MAX, |
| 36 | +}) { |
| 37 | + return ( |
| 38 | + behindMain === 0 && |
| 39 | + behindBase >= threshold && |
| 40 | + aheadMain <= aheadMainMax |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +function authorHasPushPermission(permission) { |
| 45 | + return permission === "admin" || permission === "maintain" || permission === "write"; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * True when the body uses literal backslash-n as the dominant line break |
| 50 | + * (agent bug seen on #644) rather than real newlines. |
| 51 | + */ |
| 52 | +function hasEscapedNewlines(text) { |
| 53 | + const escaped = (text.match(/\\n/g) || []).length; |
| 54 | + if (escaped < 2) return false; |
| 55 | + const real = (text.match(/\n/g) || []).length; |
| 56 | + return escaped > real; |
| 57 | +} |
| 58 | + |
| 59 | +function countContentBlocks(text) { |
| 60 | + const blocks = text |
| 61 | + .split(/\n\s*\n/) |
| 62 | + .map((b) => b.trim()) |
| 63 | + .filter(Boolean); |
| 64 | + if (blocks.length >= 2) return blocks.length; |
| 65 | + const bullets = text |
| 66 | + .split("\n") |
| 67 | + .map((l) => l.trim()) |
| 68 | + .filter((l) => /^[-*+]\s+\S/.test(l)); |
| 69 | + return Math.max(blocks.length, bullets.length); |
| 70 | +} |
| 71 | + |
| 72 | +function normalizeTemplateLine(line) { |
| 73 | + return line |
| 74 | + .replace(/^\s*[-*+]\s+/, "") |
| 75 | + .replace(/^\s*\[[ xX]\]\s+/, "") |
| 76 | + .replace(/^\s*#{1,6}\s+/, "") |
| 77 | + .trim() |
| 78 | + .toLowerCase(); |
| 79 | +} |
| 80 | + |
| 81 | +/** Drop stock PR template headings, instructions, and checklist lines. */ |
| 82 | +function stripPrTemplateBoilerplate(text) { |
| 83 | + return text |
| 84 | + .split("\n") |
| 85 | + .filter((line) => { |
| 86 | + const normalized = normalizeTemplateLine(line); |
| 87 | + if (!normalized) return true; |
| 88 | + if (PR_TEMPLATE_BOILERPLATE_LINES.has(normalized)) return false; |
| 89 | + if (/^(summary|verification|checklist)$/.test(normalized)) return false; |
| 90 | + return true; |
| 91 | + }) |
| 92 | + .join("\n"); |
| 93 | +} |
| 94 | + |
| 95 | +function assessPrDescription(body) { |
| 96 | + if (typeof body !== "string" || !body.trim()) { |
| 97 | + return { ok: false, reason: "empty" }; |
| 98 | + } |
| 99 | + if (hasEscapedNewlines(body)) { |
| 100 | + return { ok: false, reason: "escaped_newlines" }; |
| 101 | + } |
| 102 | + const withoutTemplate = stripPrTemplateBoilerplate(body); |
| 103 | + const cleaned = clean(withoutTemplate); |
| 104 | + if (!cleaned) { |
| 105 | + const strippedComments = withoutTemplate.replace(/<!--[\s\S]*?-->/g, "").trim(); |
| 106 | + if (!strippedComments) return { ok: false, reason: "empty" }; |
| 107 | + if (isPlaceholderOnlyValue(strippedComments)) { |
| 108 | + return { ok: false, reason: "placeholder" }; |
| 109 | + } |
| 110 | + return { ok: false, reason: "empty" }; |
| 111 | + } |
| 112 | + if (isPlaceholderOnlyValue(cleaned)) { |
| 113 | + return { ok: false, reason: "placeholder" }; |
| 114 | + } |
| 115 | + if (hasSubstantialStructuredContent(cleaned, MIN_SECTION_LEN, MIN_RICH_SECTIONS)) { |
| 116 | + return { ok: true }; |
| 117 | + } |
| 118 | + if ( |
| 119 | + cleaned.length >= UNSTRUCTURED_MIN_LEN && |
| 120 | + countContentBlocks(cleaned) >= UNSTRUCTURED_MIN_BLOCKS |
| 121 | + ) { |
| 122 | + return { ok: true }; |
| 123 | + } |
| 124 | + return { ok: false, reason: "thin" }; |
| 125 | +} |
| 126 | + |
| 127 | +function collectPrQualityFailures({ |
| 128 | + baseRef, |
| 129 | + allowedBases, |
| 130 | + body, |
| 131 | + behindMain, |
| 132 | + behindBase, |
| 133 | + aheadMain = 0, |
| 134 | + authorPermission, |
| 135 | + permissionLookupFailed = false, |
| 136 | + ancestryLookupFailed = false, |
| 137 | +}) { |
| 138 | + const failures = []; |
| 139 | + const wrongBase = !allowedBases.includes(baseRef); |
| 140 | + if (wrongBase) { |
| 141 | + failures.push({ code: "wrong_base" }); |
| 142 | + } else { |
| 143 | + // Permission lookup fails closed (still evaluate ancestry). Compare API |
| 144 | + // failures skip ancestry — zeros would falsely pass the #644 heuristic. |
| 145 | + const skipAncestry = |
| 146 | + ancestryLookupFailed || |
| 147 | + (!permissionLookupFailed && authorHasPushPermission(authorPermission)); |
| 148 | + if ( |
| 149 | + !skipAncestry && |
| 150 | + isWrongAncestry({ behindMain, behindBase, aheadMain }) |
| 151 | + ) { |
| 152 | + failures.push({ code: "wrong_ancestry" }); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + const desc = assessPrDescription(body); |
| 157 | + if (!desc.ok) { |
| 158 | + failures.push({ code: "bad_description", reason: desc.reason }); |
| 159 | + } |
| 160 | + return failures; |
| 161 | +} |
| 162 | + |
| 163 | +module.exports = { |
| 164 | + ANCESTRY_BEHIND_THRESHOLD, |
| 165 | + ANCESTRY_AHEAD_MAIN_MAX, |
| 166 | + isWrongAncestry, |
| 167 | + authorHasPushPermission, |
| 168 | + assessPrDescription, |
| 169 | + collectPrQualityFailures, |
| 170 | + hasEscapedNewlines, |
| 171 | + stripPrTemplateBoilerplate, |
| 172 | +}; |
0 commit comments