From 11bd3f1907272e47b544edc268bf1858f571a2f8 Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Sat, 7 Nov 2026 10:59:16 -0600 Subject: [PATCH] fix(aggregator): validate collect-github-billing --by value before any gh call (#6922) --- .../scripts/collect-github-billing.ts | 23 +++++++++---- .../collect-github-billing-args-3419.test.ts | 34 ++++++++++++++++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/pm-stack/aggregator/scripts/collect-github-billing.ts b/pm-stack/aggregator/scripts/collect-github-billing.ts index d7af16d8f9..01bf369b00 100644 --- a/pm-stack/aggregator/scripts/collect-github-billing.ts +++ b/pm-stack/aggregator/scripts/collect-github-billing.ts @@ -36,11 +36,6 @@ const outboxPath = process.env.USAGE_SNAPSHOT_OUTBOX_PATH ?? join(HOME, "workspace/goals/keep-the-four-repos-healthy/hidden_files/usage-snapshot-outbox.jsonl"); -function argValue(flag: string): string | undefined { - const i = process.argv.indexOf(flag); - return i >= 0 ? process.argv[i + 1] : undefined; -} - // Validate explicit --year/--month/--user values BEFORE any gh call // (#3419). Malformed values used to flow straight into the billing API // URL (month=NaN, month=13, users/--print) and the failure was then @@ -60,6 +55,22 @@ function explicitArg(flag: string): string { return value; } +// Resolve the --by value BEFORE any gh call (#6922). It is the last +// unguarded flag value in this script: an empty/whitespace value used +// to flow into every derived record, fail isSnapshotRecord for all of +// them, and throw AFTER the gh call — discarding the whole month's +// collection with a data-validation error for a usage mistake; a flag +// token (e.g. --by --print) was recorded verbatim as the observer (and +// consumed a second time as the flag itself); a trailing --by silently +// fell back to the default. Same guard as append-usage-snapshots' +// resolveObservedBy (#3295), built on this file's explicitArg (#3419). +function resolveObservedBy(): string { + if (!process.argv.includes("--by")) return "collect-github-billing"; + const raw = explicitArg("--by"); + if (raw.trim() === "") usageError(`--by requires a non-empty name (got ${JSON.stringify(raw)})`); + return raw; +} + function resolveYear(now: Date): number { if (!process.argv.includes("--year")) return now.getUTCFullYear(); const raw = explicitArg("--year"); @@ -109,7 +120,7 @@ async function main(): Promise { const year = resolveYear(now); const month = resolveMonth(now); const user = resolveUser(); - const observedBy = argValue("--by") ?? "collect-github-billing"; + const observedBy = resolveObservedBy(); const items = fetchUsageItems(user, year, month); const totals = summarizeBillingDays(aggregateBillingUsage(items)); console.log( diff --git a/pm-stack/aggregator/test/collect-github-billing-args-3419.test.ts b/pm-stack/aggregator/test/collect-github-billing-args-3419.test.ts index 30d844f8e9..40e1f4dfc9 100644 --- a/pm-stack/aggregator/test/collect-github-billing-args-3419.test.ts +++ b/pm-stack/aggregator/test/collect-github-billing-args-3419.test.ts @@ -1,4 +1,4 @@ -// Pinning test for #3419: collect-github-billing must validate explicit +// Pinning test for #3419 (and #6922 for --by): collect-github-billing must validate explicit // --year/--month/--user values BEFORE any gh call. Malformed values used // to flow straight into the billing API URL (month=NaN, month=13, // users/--print) and the failure was then misreported as a gh @@ -69,6 +69,10 @@ describe("collect-github-billing argument validation (#3419)", () => { ["--month"], ["--year", "1999"], ["--user", " "], + ["--by"], + ["--by", ""], + ["--by", " "], + ["--by", "--print"], ]; for (const args of badArgSets) { const { code, stderr } = await runScript(dir, logPath, args); @@ -103,6 +107,34 @@ describe("collect-github-billing argument validation (#3419)", () => { } }, 60000); + test("a valid --by is recorded verbatim as observedBy (#6922)", async () => { + const dir = await mkdtemp(join(tmpdir(), "billing-args-6922-by-")); + try { + const binDir = join(dir, "bin"); + const { mkdir } = await import("node:fs/promises"); + await mkdir(binDir, { recursive: true }); + const stub = join(binDir, "gh"); + await writeFile( + stub, + "#!/bin/sh\nprintf '{\"usageItems\":[{\"date\":\"2026-10-15\",\"product\":\"actions\",\"unitType\":\"minutes\",\"quantity\":10,\"grossAmount\":1,\"netAmount\":1}]}\n'", + ); + await chmod(stub, 0o755); + const env = { ...process.env }; + delete env.GH_BILLING_USER; + const proc = Bun.spawn([process.execPath, script, "--by", "stewardship-cycle", "--print"], { + env: { ...env, PATH: `${binDir}:${process.env.PATH}` }, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]); + expect(await proc.exited).toBe(0); + expect(stdout).toContain('\"observedBy\":\"stewardship-cycle\"'); + expect(stdout).not.toContain('\"observedBy\":\"collect-github-billing\"'); + } finally { + await rm(dir, { recursive: true, force: true }); + } + }, 60000); + test("omitted flags keep their defaults", async () => { const dir = await mkdtemp(join(tmpdir(), "billing-args-3419-defaults-")); try {