Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pm-stack/aggregator/scripts/collect-github-billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand Down Expand Up @@ -109,7 +120,7 @@ async function main(): Promise<void> {
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
Loading