mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-27 18:34:05 +01:00
fix(post): a full pr-body post keeps the opening text and badge byte for byte; run ids in runs= lists and actions/runs/ paths aren't phone numbers (GST-1227)
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
2f9d11a59d
commit
637b60bc2f
6 files changed
+176
-52
No files matched your search
+38
-21
@@ -10,7 +10,8 @@
|
||||
* the same runner): a section in the file replaces the same-titled one, the
|
||||
* ship-receipt block is a section of its own, sections the file does not
|
||||
* rewrite are kept, new ones are appended, and a file with a receipt above
|
||||
* its first `## ` section replaces the text above the first heading;
|
||||
* its first `## ` section replaces the text between the receipt and the
|
||||
* first heading while the opening text above the receipt is kept;
|
||||
* `--replace` overwrites instead.
|
||||
* The scan runs on the merged bytes, which are the bytes sent.
|
||||
*
|
||||
@@ -159,7 +160,7 @@ function parseArgs(argv: string[]): Request {
|
||||
return req;
|
||||
}
|
||||
|
||||
/** One piece of a PR body: the leading text, a `## ` section, the ship-receipt block, or text after a receipt block. */
|
||||
/** One piece of a PR body: the leading text, a `## ` section, the ship-receipt block, a marker block, or text after a receipt or marker block. */
|
||||
interface Section { key: string; label: string; text: string }
|
||||
|
||||
/**
|
||||
@@ -174,25 +175,38 @@ interface Section { key: string; label: string; text: string }
|
||||
* nothing; a receipt fence that never closes makes the body from that line
|
||||
* on one section the merge never matches (`unclosed-receipt`), so it is kept
|
||||
* byte for byte instead of being read as a receipt or as replaceable text.
|
||||
* After the first receipt or heading, a `<!-- <name>:start -->` line outside
|
||||
* a fence opens a marker block (a bot's badge, such as Capy's
|
||||
* `<!-- capy-badge:start -->…<!-- capy-badge:end -->`) that runs through its
|
||||
* `<!-- <name>:end -->` line, or up to the next heading when it never
|
||||
* closes, so a badge at the end of the body never leaves with the section
|
||||
* above it (GST-1227). A marker block in the leading text stays part of it.
|
||||
*/
|
||||
function splitSections(body: string): Section[] {
|
||||
const out: Section[] = [];
|
||||
let fence: string | null = null;
|
||||
let block: string | null = null;
|
||||
let plain = 0;
|
||||
const open = (key: string, label: string) => out.push({ key, label, text: "" });
|
||||
const openText = () => out.length === 0 ? open("text:lead", "leading text") : open(`text:${plain++}`, "trailing text");
|
||||
const closes = (run: string) => new RegExp(`^ {0,3}${run[0] === "`" ? "`" : "~"}{${run.length},}\\s*$`).test(bare);
|
||||
let bare = "";
|
||||
const endsBlock = () => { if (block !== null && bare.includes(`<!-- ${block}:end -->`)) { block = null; openText(); } };
|
||||
for (const line of body.split(/(?<=\n)/)) {
|
||||
bare = line.replace(/\r?\n$/, "");
|
||||
if (block !== null && /^ {0,3}## /.test(bare)) block = null;
|
||||
if (block !== null) { out.at(-1)!.text += line; endsBlock(); continue; }
|
||||
const fenceOpen: RegExpExecArray | null = fence === null ? /^ {0,3}(`{3,}|~{3,})/.exec(bare) : null;
|
||||
const marker = fence === null ? /^ {0,3}<!-- ([\w.-]+):start -->/.exec(bare) : null;
|
||||
if (fenceOpen && bare.trim() === "```" + RECEIPT_FENCE) { open("receipt", "ship receipt"); fence = "receipt"; }
|
||||
else if (fence === "receipt") { out.at(-1)!.text += line; if (closes("```")) { fence = null; openText(); } continue; }
|
||||
else if (fenceOpen) fence = fenceOpen[1]!;
|
||||
else if (fence && closes(fence)) fence = null;
|
||||
else if (fence === null && /^ {0,3}## /.test(bare)) { const title = bare.replace(/^ {0,3}## /, "").trim(); open(`h2:${title.toLowerCase()}`, title); }
|
||||
else if (marker && out.some(s => s.key === "receipt" || s.key.startsWith("h2:"))) { open(`block:${marker[1]}`, `${marker[1]} block`); block = marker[1]!; }
|
||||
if (out.length === 0) openText();
|
||||
out.at(-1)!.text += line;
|
||||
endsBlock();
|
||||
}
|
||||
if (fence === "receipt") Object.assign(out.at(-1)!, { key: "unclosed-receipt", label: "unclosed receipt fence" });
|
||||
return out.filter(s => s.text !== "");
|
||||
@@ -202,8 +216,10 @@ function splitSections(body: string): Section[] {
|
||||
* The body to send for `pr-body` without `--replace`: each section of the
|
||||
* file replaces the same-keyed section of the current body in place, current
|
||||
* sections the file does not rewrite are kept byte for byte, new `## `
|
||||
* sections are appended after them, and a new receipt or leading text goes
|
||||
* first (where lib/ship-receipt.ts puts the receipt). Text outside sections
|
||||
* sections are appended after them (above a marker block that ends the
|
||||
* body), new leading text goes first, and a new receipt goes right after the
|
||||
* current leading text, or first when there is none (where
|
||||
* lib/ship-receipt.ts puts the receipt). Text outside sections
|
||||
* matches by role (the file's leading text replaces the leading text; text
|
||||
* after a receipt block never replaces an intro), and whitespace-only text in
|
||||
* the file is dropped, so reposting the body /ship composes (receipt, blank
|
||||
@@ -214,15 +230,17 @@ function splitSections(body: string): Section[] {
|
||||
* description pasted under the new one) matches no single section, so the
|
||||
* merge refuses instead of replacing one copy and keeping the stale other.
|
||||
* A full post (a file with a receipt block above its first `## ` section, as
|
||||
* /ship composes it) manages the preamble (GST-412): every
|
||||
* non-blank text section before the current body's first heading (the leading
|
||||
* text, text after a receipt block) is dropped, the file's own text before its
|
||||
* first heading takes its place (leading text first, text after the file's
|
||||
* receipt right after the receipt), a dropped piece the file brings no text for
|
||||
* /ship composes it) manages the text under the receipt (GST-412): every
|
||||
* non-blank text section after a receipt block and before the current body's
|
||||
* first heading is dropped, the file's own text after its receipt takes its
|
||||
* place, right after the receipt, and a dropped piece the file brings no text for
|
||||
* is listed in `dropped` with its first line (withheld when the redaction scan
|
||||
* flags it, since the merged bytes no longer hold it), and the receipt keeps the rules
|
||||
* above. Any other file (`## ` sections with no receipt above them, or no
|
||||
* `## ` section) merges as described above, so the current intro is kept.
|
||||
* flags it, since the merged bytes no longer hold it). The current leading text
|
||||
* (the opening paragraph and a bot's badge above the first receipt or heading)
|
||||
* is kept byte for byte unless the file brings leading text of its own, which
|
||||
* replaces it as in any merge (GST-1227). Any other file (`## ` sections with
|
||||
* no receipt above them, or no `## ` section) merges as described above, so
|
||||
* the text under the receipt is kept too.
|
||||
*/
|
||||
export function mergeBodySections(current: string, incoming: string): { body: string; replaced: string[]; kept: string[]; added: string[]; dropped: string[] } {
|
||||
const now = splitSections(current);
|
||||
@@ -231,18 +249,13 @@ export function mergeBodySections(current: string, incoming: string): { body: st
|
||||
if (ambiguous.length) throw new UsageError(`the PR's current body holds ${[...new Set(ambiguous)].join(", ")} more than once, so the file's copy matches no single section; nothing was posted. Rerun with --replace and the whole body`);
|
||||
const preamble = (list: Section[]) => {
|
||||
const end = list.findIndex(s => s.key.startsWith("h2:"));
|
||||
return list.slice(0, end < 0 ? list.length : end).filter(s => s.key.startsWith("text:") && s.text.trim() !== "");
|
||||
return list.slice(0, end < 0 ? list.length : end).filter(s => s.key.startsWith("text:") && s.key !== "text:lead" && s.text.trim() !== "");
|
||||
};
|
||||
const firstHeading = next.findIndex(n => n.key.startsWith("h2:"));
|
||||
const managed = firstHeading > 0 && next.slice(0, firstHeading).some(n => n.key === "receipt");
|
||||
const stale = managed ? preamble(now) : [];
|
||||
const ours = managed ? preamble(next) : [];
|
||||
const role = (s: Section) => (s.key === "text:lead" ? "lead" : "trailing");
|
||||
const paired = new Set<Section>();
|
||||
for (const r of ["lead", "trailing"]) {
|
||||
const theirs = ours.filter(n => role(n) === r);
|
||||
stale.filter(s => role(s) === r).slice(0, theirs.length).forEach((s, i) => { paired.add(s); paired.add(theirs[i]!); });
|
||||
}
|
||||
const paired = new Set<Section>([...stale.slice(0, ours.length), ...ours.slice(0, stale.length)]);
|
||||
const used = new Set<Section>(ours);
|
||||
const replaced: string[] = [], kept: string[] = [], added: string[] = [], dropped: string[] = [];
|
||||
const merged: Section[] = [];
|
||||
@@ -262,8 +275,12 @@ export function mergeBodySections(current: string, incoming: string): { body: st
|
||||
for (const n of next) if (fresh.includes(n) || (ours.includes(n) && !paired.has(n))) added.push(n.label);
|
||||
const first = fresh.filter(n => !n.key.startsWith("h2:"));
|
||||
if (now.length === 0) return { body: incoming, replaced, kept, added, dropped };
|
||||
const all = [...ours.filter(n => role(n) === "lead"), ...first, ...merged, ...fresh.filter(n => n.key.startsWith("h2:"))];
|
||||
for (const t of ours.filter(n => role(n) === "trailing").reverse()) all.splice(all.indexOf(next[next.indexOf(t) - 1]!) + 1, 0, t);
|
||||
const lead = merged[0]?.key === "text:lead" && merged[0].text.trim() ? 1 : 0;
|
||||
let tail = merged.length;
|
||||
while (tail > lead && (merged[tail - 1]!.key.startsWith("block:") || !merged[tail - 1]!.text.trim())) tail--;
|
||||
if (!merged.slice(tail).some(s => s.key.startsWith("block:"))) tail = merged.length;
|
||||
const all = [...merged.slice(0, lead), ...first, ...merged.slice(lead, tail), ...fresh.filter(n => n.key.startsWith("h2:")), ...merged.slice(tail)];
|
||||
for (const t of [...ours].reverse()) all.splice(all.indexOf(next[next.indexOf(t) - 1]!) + 1, 0, t);
|
||||
const fromFile = (s: Section) => next.includes(s);
|
||||
const body = all.map((s, i) => {
|
||||
const after = all[i + 1];
|
||||
|
||||
+13
-4
@@ -356,8 +356,12 @@ export function isNumericMetadataValue(span: string, match: RegExpExecArray): bo
|
||||
* (`bun run eval:pass-rates --run 37235771700`), and redacting it breaks that
|
||||
* command. Only the position decides: a run/job flag (`--run 37235771700`,
|
||||
* `--job=111534615007`), a `gh run view|watch|rerun|download|cancel`
|
||||
* argument, an `actions/runs/<id>` or `/job/<id>` URL segment, or a
|
||||
* `run_id` / `job_id` key. An 11-digit id (every GitHub Actions run id since
|
||||
* argument, an `actions/runs/<id>` or `/job/<id>` URL segment (with or
|
||||
* without a host or a leading `/`), a `run_id` / `job_id` key, or any id
|
||||
* of a `runs=<id>/<attempt>[,<id>/<attempt>…]` list (`run=` too), the
|
||||
* receipt a CI carry or run clear prints (lib/run-evidence.ts
|
||||
* runReceiptFields); the `/<attempt>` of one to three digits after every id
|
||||
* is what tells that list from a number (GST-1227). An 11-digit id (every GitHub Actions run id since
|
||||
* 2025) also counts right after a run word on the same line, the way a ship
|
||||
* receipt, a READY block or an approval record names one (`screen 40477638211
|
||||
* GREEN`, `gate=40323989742`, `rerun passed 40287239206`, `run #19368712345`);
|
||||
@@ -369,7 +373,8 @@ export function isNumericMetadataValue(span: string, match: RegExpExecArray): bo
|
||||
* else still report.
|
||||
*/
|
||||
const CI_ID_POSITION_BEFORE =
|
||||
/(?:--(?:run|job)(?:-id)?(?:[ \t]+|[ \t]*=[ \t]*)|\bgh[ \t]+run[ \t]+(?:view|watch|rerun|download|cancel)[ \t]+|\/actions\/runs\/|\/jobs?\/|(?:run|job)_id["']?[ \t]*[:=][ \t]*["']?)$/i;
|
||||
/(?:--(?:run|job)(?:-id)?(?:[ \t]+|[ \t]*=[ \t]*)|\bgh[ \t]+run[ \t]+(?:view|watch|rerun|download|cancel)[ \t]+|\bactions\/runs\/|\/jobs?\/|(?:run|job)_id["']?[ \t]*[:=][ \t]*["']?)$/i;
|
||||
const CI_RUN_LIST = /(?<![\w-])runs?=\d+\/\d{1,3}(?:,\d+\/\d{1,3})*(?![\w\/])/gi;
|
||||
/** A run word with an optional ':' '#' '=' right before an 11-digit token; shared with gstack-post's chained-id allowlist. */
|
||||
export const CI_RUN_WORD_BEFORE = /\b(?:runs?|screens?|gates?|backstop|bisect|rerun|dispatch(?:ed)?|failed|passed)\s*[:#=]?\s*$/i;
|
||||
export function isCiRunIdentifier(span: string, match: RegExpExecArray): boolean {
|
||||
@@ -378,7 +383,11 @@ export function isCiRunIdentifier(span: string, match: RegExpExecArray): boolean
|
||||
const { start } = spanBounds(match);
|
||||
const lineStart = input.lastIndexOf("\n", start - 1) + 1;
|
||||
const before = input.slice(Math.max(lineStart, start - 80), start);
|
||||
return CI_ID_POSITION_BEFORE.test(before) || (span.length === 11 && CI_RUN_WORD_BEFORE.test(before));
|
||||
if (CI_ID_POSITION_BEFORE.test(before) || (span.length === 11 && CI_RUN_WORD_BEFORE.test(before))) return true;
|
||||
const lineEnd = input.indexOf("\n", start);
|
||||
const line = input.slice(lineStart, lineEnd < 0 ? input.length : lineEnd);
|
||||
const at = start - lineStart;
|
||||
return [...line.matchAll(CI_RUN_LIST)].some(m => m.index! < at && at + span.length <= m.index! + m[0].length);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user