mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-29 03:15:05 +01:00
Open pull requests with three plain lines and land each one as a single commit (GST-2737) (#4438)
* WIP plain PR bodies and squash messages Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Fold PR bodies to three plain lines and land every PR as one plainly titled commit Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Expect the plain commit body in the train land tests Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Count the new policy keys in the floor tests and record the ship size change Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Let older, generated and Dependabot PRs pass the body check, and keep comments posting The opening check now follows the same date and branch exemption as the rest of the body check, Dependabot branches are exempt, the exemption only counts for branches in this repository, a merge that cannot read the PR waits instead of refusing, and a broken policy file no longer stops comments. Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p1/specialist-batched#2 reader=testing Review-Finding: p1/red-team#1 reader=red-team Review-Finding: p1/red-team#2 reader=red-team Review-Finding: p1/red-team#4 reader=red-team Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Stop re-posted PR bodies from duplicating sections, and let file names through the word check Posting an already-folded body now replaces each section once instead of keeping the old copy beside it, and listed words inside file paths or file names, such as a test file in the Tests section, no longer count as prose. Review-Finding: v2/outside-voice#3 reader=outside-voice Review-Finding: v2/adversarial-native#2 reader=adversarial-native Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> * Describe the folded three-line PR body in the skills guide and the prose lint help Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --------- Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
aeb74eb00b
commit
c53b15c48d
47 files changed
+1182
-208
No files matched your search
+58
-16
@@ -13,7 +13,10 @@
|
||||
* its first `## ` section replaces the text between the receipt and the
|
||||
* first heading while the opening text above the receipt is kept;
|
||||
* the Agent notes section (lib/agent-notes.ts) is one section that always
|
||||
* stays last; `--replace` overwrites instead.
|
||||
* stays last; `--replace` overwrites instead. Folds in the current body merge by
|
||||
* their summary, and every body is folded before it is sent (lib/pr-body-shape.ts:
|
||||
* three visible lines, every `## ` section collapsed), then checked; `fold`
|
||||
* prints the folded body and posts nothing.
|
||||
* `pr-title` and `pr-create` first check the title against the repository's
|
||||
* plain-title policy (lib/plain-title.ts); a refused title posts nothing.
|
||||
* The scan runs on the merged bytes, which are the bytes sent.
|
||||
@@ -22,7 +25,8 @@
|
||||
* 2 MEDIUM findings need confirmation, printed as `RULE:` lines plus a
|
||||
* `TOKEN:` that binds the bytes, destination, operation and findings;
|
||||
* 3 `gh`/`glab` failed; 4 the title breaks the plain-title policy, printed as
|
||||
* `PLAIN_TITLE:` lines; 64 usage or invalid input. Nothing is posted on any
|
||||
* `PLAIN_TITLE:` lines, or the body breaks the shape, printed as `PLAIN_BODY:`
|
||||
* lines (title and body are checked in one pass); 64 usage or invalid input. Nothing is posted on any
|
||||
* non-zero exit. bin/gstack-post is the CLI; tests drive `runPost` with a fake
|
||||
* runner so the logic is covered on every platform.
|
||||
*/
|
||||
@@ -30,14 +34,15 @@ import { createHash } from "crypto";
|
||||
import { scan, type Finding, type RepoVisibility } from "./redact-engine";
|
||||
import { CI_RUN_WORD_BEFORE } from "./redact-patterns";
|
||||
import { agentNotesLines, splitAgentNotes } from "./agent-notes";
|
||||
import { checkTitle, loadPlainTitlePolicy, PLAIN_TITLE_POLICY_PATH } from "./plain-title";
|
||||
import { checkTitle, loadPlainTitlePolicy, PLAIN_TITLE_POLICY_PATH, type PlainTitlePolicy } from "./plain-title";
|
||||
import { bodyFindingLine, checkBodyShape, foldSections, unfoldSections } from "./pr-body-shape";
|
||||
import { RECEIPT_FENCE, RUNS_FENCE, extractReceipt, placeReceipt, renderReceiptBlock, validateReceipt } from "./ship-receipt";
|
||||
import type { ShipReceipt } from "./headless-artifacts";
|
||||
import { canonicalRemote } from "./remote-identity";
|
||||
import { parseChangelog } from "./changelog-check";
|
||||
import { versionWidth } from "./version-source";
|
||||
|
||||
export const OPS = ["pr-comment", "issue-comment", "reply", "pr-title", "pr-body", "pr-create", "issue-create"] as const;
|
||||
export const OPS = ["pr-comment", "issue-comment", "reply", "pr-title", "pr-body", "pr-create", "issue-create", "fold"] as const;
|
||||
export type Op = (typeof OPS)[number];
|
||||
export type HostKind = "github" | "gitlab";
|
||||
|
||||
@@ -61,6 +66,7 @@ export const USAGE = `usage: gstack-post <op> [<target>] [options]
|
||||
gstack-post pr-body <pr> --body-file F [--replace]
|
||||
gstack-post pr-create --base <branch> [--head <branch>] [--draft] --title-file F --body-file F
|
||||
gstack-post issue-create --title-file F --body-file F
|
||||
gstack-post fold --body-file F (prints the folded body; posts nothing)
|
||||
|
||||
<pr>/<issue> is a number or a URL of this repository on the same host.
|
||||
Options: --host github|gitlab (default: detected from the origin remote)
|
||||
@@ -79,8 +85,11 @@ export const USAGE = `usage: gstack-post <op> [<target>] [options]
|
||||
|
||||
Exit: 0 posted · 1 HIGH finding refused · 2 MEDIUM findings need
|
||||
confirmation (RULE:/TOKEN: lines on stdout) · 3 gh/glab failed ·
|
||||
4 the title is not plain words (PLAIN_TITLE: lines on stdout; rewrite the
|
||||
title file and rerun) or the plain-title policy is invalid; nothing posted · 64 usage
|
||||
4 the title is not plain words (PLAIN_TITLE: lines; rewrite the title file)
|
||||
or the body breaks the shape (PLAIN_BODY: lines; rewrite the body file), or
|
||||
the plain-title policy is invalid; nothing posted · 64 usage
|
||||
Every PR body is folded before it is sent: the lines above the first ## stay
|
||||
visible (at most 3, docs/writing-prs.md), each ## section becomes a fold.
|
||||
`;
|
||||
|
||||
interface Request {
|
||||
@@ -156,10 +165,10 @@ function parseArgs(argv: string[]): Request {
|
||||
if (!req.attribute || /[\r\n\u0000-\u001f]/.test(req.attribute)) throw new UsageError("--attribute needs one line of text, like \"<model> on behalf of <login>\"");
|
||||
if (req.attribute.length > 200) throw new UsageError("--attribute must be 200 characters or fewer");
|
||||
}
|
||||
const creates = req.op === "pr-create" || req.op === "issue-create";
|
||||
const creates = req.op === "pr-create" || req.op === "issue-create" || req.op === "fold";
|
||||
if (positional.length > (creates ? 0 : 1)) throw new UsageError(`unexpected argument "${positional[creates ? 0 : 1]}"`);
|
||||
if (!creates && !req.target) throw new UsageError(`${req.op} needs a target (a number or a URL)`);
|
||||
const needsTitle = req.op === "pr-title" || creates;
|
||||
const needsTitle = req.op === "pr-title" || (creates && req.op !== "fold");
|
||||
const needsBody = req.op !== "pr-title";
|
||||
if (needsTitle !== !!req.titleFile) throw new UsageError(needsTitle ? `${req.op} needs --title-file` : `${req.op} takes no --title-file`);
|
||||
if (needsBody !== !!req.bodyFile) throw new UsageError(needsBody ? `${req.op} needs --body-file` : `${req.op} takes no --body-file (use --title-file)`);
|
||||
@@ -663,15 +672,21 @@ function send(env: PostEnv, kind: HostKind, req: Request, n: string | undefined,
|
||||
* title passes or the repository has no policy. A finding prints one `PLAIN_TITLE:` line each, in gstack-plain-title's
|
||||
* format, then `PLAIN_TITLE: refused=<n>`; an invalid policy refuses too, so nothing is posted until it is fixed.
|
||||
*/
|
||||
function plainTitle(env: PostEnv, title: string): boolean {
|
||||
function plainPolicy(env: PostEnv, comment = false): PlainTitlePolicy | null | "invalid" {
|
||||
const top = env.run("git", ["rev-parse", "--show-toplevel"]);
|
||||
const root = top.status === 0 ? top.stdout.trim() : "";
|
||||
if (!root) return true;
|
||||
let findings;
|
||||
try { findings = checkTitle(title, loadPlainTitlePolicy(root)); } catch (e) {
|
||||
env.err(`gstack-post: ${e instanceof Error ? e.message : String(e)} (PLAIN_POLICY_INVALID); nothing was posted. Fix the policy (gstack-plain-title validate), then rerun.\n`);
|
||||
return false;
|
||||
if (!root) return null;
|
||||
try { return loadPlainTitlePolicy(root); } catch (e) {
|
||||
const why = `gstack-post: ${e instanceof Error ? e.message : String(e)} (PLAIN_POLICY_INVALID)`;
|
||||
if (comment) { env.err(`${why}; the comment is posted unchecked. Fix the policy (gstack-plain-title validate).\n`); return null; }
|
||||
env.err(`${why}; nothing was posted. Fix the policy (gstack-plain-title validate), then rerun.\n`);
|
||||
return "invalid";
|
||||
}
|
||||
}
|
||||
|
||||
function plainTitle(env: PostEnv, title: string, policy: PlainTitlePolicy | null | "invalid"): boolean {
|
||||
if (policy === "invalid") return false;
|
||||
const findings = checkTitle(title, policy);
|
||||
if (!findings.length) return true;
|
||||
const clean = (s: string) => s.replace(/[\u0000-\u001f\u007f"]/g, " ").slice(0, 80);
|
||||
for (const f of findings) env.out(`PLAIN_TITLE: ${f.code} ${f.kind} "${clean(f.match)}" — ${f.hint}\n`);
|
||||
@@ -680,6 +695,23 @@ function plainTitle(env: PostEnv, title: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The body shape check (lib/pr-body-shape.ts) on the folded bytes: one `PLAIN_BODY:` line per finding, then
|
||||
* `PLAIN_BODY: refused=<n>` or `warned=<n>`. Findings refuse only with a policy file and for a PR created on or after
|
||||
* its body_since (an existing PR's date is read only when a finding needs it); anything else, comments included, warns.
|
||||
*/
|
||||
function plainBody(env: PostEnv, body: string, policy: PlainTitlePolicy | null | "invalid", createdAt: () => string | undefined, comment = false): boolean {
|
||||
if (policy === "invalid") return false;
|
||||
const findings = comment ? (policy ? checkBodyShape(`<details>\n\n${body}\n\n</details>`, policy.terms).filter(f => f.kind === "term") : []) : checkBodyShape(body, policy?.terms ?? []);
|
||||
if (!findings.length) return true;
|
||||
const when = comment || !policy ? undefined : createdAt();
|
||||
const refuse = !!policy && !comment && (when === undefined || Date.parse(when) >= Date.parse(policy.body_since ?? policy.since));
|
||||
for (const f of findings) env.out(bodyFindingLine(f, !refuse) + "\n");
|
||||
env.out(`PLAIN_BODY: ${refuse ? "refused" : "warned"}=${findings.length}\n`);
|
||||
if (refuse) env.err("gstack-post: the body does not have the plain shape; nothing was posted. Rewrite the body file the way each PLAIN_BODY: line says (docs/writing-prs.md), then rerun.\n");
|
||||
return !refuse;
|
||||
}
|
||||
|
||||
export function runPost(argv: string[], env: PostEnv, configBin = "gstack-config"): number {
|
||||
if (argv.length === 0 || ["help", "--help", "-h"].includes(argv[0]!)) {
|
||||
(argv.length === 0 ? env.err : env.out)(USAGE);
|
||||
@@ -687,11 +719,17 @@ export function runPost(argv: string[], env: PostEnv, configBin = "gstack-config
|
||||
}
|
||||
try {
|
||||
const req = parseArgs(argv);
|
||||
if (req.op === "fold") {
|
||||
const folded = foldSections(readText(env, req.bodyFile!, "body"));
|
||||
env.out(folded.endsWith("\n") ? folded : folded + "\n");
|
||||
return plainBody({ ...env, out: env.err }, folded, plainPolicy(env), () => undefined) ? EXIT.posted : EXIT.plainTitle;
|
||||
}
|
||||
const remote = resolveRemote(env, req.host);
|
||||
if (req.op === "reply" && remote.kind !== "github") throw new UsageError("reply is supported on GitHub only");
|
||||
const n = req.target === undefined ? undefined : targetNumber(req.target, req.op, remote);
|
||||
const title = req.titleFile ? readText(env, req.titleFile, "title") : undefined;
|
||||
if (title !== undefined && (req.op === "pr-title" || req.op === "pr-create") && !plainTitle(env, title)) return EXIT.plainTitle;
|
||||
const policy = req.op === "issue-create" ? null : plainPolicy(env, COMMENT_OPS.has(req.op));
|
||||
const titleOk = title === undefined || !(req.op === "pr-title" || req.op === "pr-create") || plainTitle(env, title, policy);
|
||||
let body = req.bodyFile ? readText(env, req.bodyFile, "body") : undefined;
|
||||
if (req.op === "pr-body" && !req.replace) {
|
||||
const current = currentBody(env, remote.kind, n!);
|
||||
@@ -699,11 +737,15 @@ export function runPost(argv: string[], env: PostEnv, configBin = "gstack-config
|
||||
env.err(`gstack-post: cannot read PR ${n}'s current body to merge into (${current.error}); nothing was posted. Rerun with --replace to overwrite the whole body.\n`);
|
||||
return EXIT.cliFailed;
|
||||
}
|
||||
const m = mergeBodySections(current.body, body!);
|
||||
const m = mergeBodySections(unfoldSections(current.body), unfoldSections(body!));
|
||||
body = m.body;
|
||||
const list = (names: string[]) => (names.length ? names.join(", ") : "none");
|
||||
env.err(`gstack-post: pr-body merged by section: replaced ${list(m.replaced)}; kept ${list(m.kept)}; added ${list(m.added)}${m.dropped.length ? `; dropped ${m.dropped.join(", ")}` : ""}\n`);
|
||||
}
|
||||
if (body !== undefined && (req.op === "pr-body" || req.op === "pr-create")) body = foldSections(body);
|
||||
const createdAt = () => { const r = env.run("gh", ["pr", "view", n!, "--json", "createdAt", "--jq", ".createdAt"]); return r.status === 0 && r.stdout.trim() ? r.stdout.trim() : undefined; };
|
||||
const bodyOk = body === undefined || req.op === "issue-create" || plainBody(env, body, policy, req.op === "pr-create" ? () => undefined : createdAt, COMMENT_OPS.has(req.op));
|
||||
if (!titleOk || !bodyOk) return EXIT.plainTitle;
|
||||
if (body !== undefined && req.attribute) body = attributionCallout(req.attribute) + body;
|
||||
const parts: Array<[Part, string]> = [];
|
||||
if (title !== undefined) parts.push(["title", title]);
|
||||
|
||||
Reference in new issue
Block a user