forgedock 1.61.1 → 1.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/config-schema.mjs +10 -1
- package/bin/engine/outcome-codes.mjs +4 -0
- package/bin/engine-cli.mjs +160 -18
- package/bin/env/env.mjs +6 -4
- package/bin/forge-home-check.mjs +91 -0
- package/bin/forgedock.mjs +50 -7
- package/bin/gc.mjs +2 -0
- package/bin/hooks/pre-tool-use.mjs +4 -0
- package/bin/journey.mjs +55 -5
- package/bin/lean/admission.mjs +16 -5
- package/bin/lean/batch-health.mjs +55 -16
- package/bin/lean/batch-health.test.mjs +67 -1
- package/bin/lean/batch.mjs +8 -0
- package/bin/lean/batch.test.mjs +5 -0
- package/bin/lean/ceiling.mjs +20 -0
- package/bin/lean/ceiling.test.mjs +33 -2
- package/bin/lean/ci-gate.mjs +240 -0
- package/bin/lean/ci-gate.test.mjs +227 -0
- package/bin/lean/ci-precheck.mjs +178 -0
- package/bin/lean/ci-precheck.test.mjs +149 -0
- package/bin/lean/conformance.mjs +118 -20
- package/bin/lean/conformance.test.mjs +193 -3
- package/bin/lean/context.test.mjs +7 -6
- package/bin/lean/dag.mjs +128 -0
- package/bin/lean/dag.test.mjs +131 -0
- package/bin/lean/decisions.mjs +5 -4
- package/bin/lean/decisions.test.mjs +11 -0
- package/bin/lean/delguard.mjs +84 -0
- package/bin/lean/delguard.test.mjs +104 -0
- package/bin/lean/depcache.mjs +125 -17
- package/bin/lean/depcache.test.mjs +111 -0
- package/bin/lean/design-gate.mjs +5 -5
- package/bin/lean/design-gate.test.mjs +14 -0
- package/bin/lean/effects.mjs +81 -7
- package/bin/lean/execution.mjs +81 -15
- package/bin/lean/execution.test.mjs +156 -3
- package/bin/lean/forgedock-lane-workflow.test.mjs +35 -0
- package/bin/lean/guardgate.mjs +30 -7
- package/bin/lean/guardgate.test.mjs +44 -1
- package/bin/lean/guards.test.mjs +1 -1
- package/bin/lean/heavy.test.mjs +87 -0
- package/bin/lean/hotarea.mjs +73 -0
- package/bin/lean/hotarea.test.mjs +73 -0
- package/bin/lean/issuewatch-reconcile.mjs +20 -1
- package/bin/lean/issuewatch-reconcile.test.mjs +30 -0
- package/bin/lean/lanecheck.mjs +27 -3
- package/bin/lean/lanecheck.test.mjs +47 -5
- package/bin/lean/laneconfine.mjs +83 -0
- package/bin/lean/laneconfine.test.mjs +113 -0
- package/bin/lean/lean.mjs +398 -91
- package/bin/lean/lean.test.mjs +792 -26
- package/bin/lean/live-reconcile.mjs +169 -0
- package/bin/lean/live-reconcile.test.mjs +176 -0
- package/bin/lean/orchestrate-envhold.test.mjs +245 -0
- package/bin/lean/orchestrate-hostfallback.test.mjs +39 -0
- package/bin/lean/orchestrate.mjs +207 -17
- package/bin/lean/outcomeevidence.mjs +104 -0
- package/bin/lean/outcomeevidence.test.mjs +78 -0
- package/bin/lean/page-capture.mjs +68 -0
- package/bin/lean/page-capture.test.mjs +44 -0
- package/bin/lean/pagegate.mjs +46 -7
- package/bin/lean/pagegate.test.mjs +66 -1
- package/bin/lean/postdeploy-check.mjs +18 -4
- package/bin/lean/postdeploy-check.test.mjs +32 -1
- package/bin/lean/prefilter.mjs +36 -4
- package/bin/lean/prefilter.test.mjs +31 -1
- package/bin/lean/preflight.mjs +22 -0
- package/bin/lean/preflight.test.mjs +19 -1
- package/bin/lean/probe.mjs +18 -0
- package/bin/lean/probe.test.mjs +40 -0
- package/bin/lean/prompts.mjs +41 -4
- package/bin/lean/recovery.test.mjs +20 -0
- package/bin/lean/recurrence.mjs +133 -0
- package/bin/lean/recurrence.test.mjs +99 -0
- package/bin/lean/release-config.mjs +2 -1
- package/bin/lean/release-config.test.mjs +18 -0
- package/bin/lean/release-control.mjs +4 -2
- package/bin/lean/release-control.test.mjs +15 -0
- package/bin/lean/release-merge-hold.mjs +65 -0
- package/bin/lean/release-merge-hold.test.mjs +69 -0
- package/bin/lean/release-review.mjs +26 -9
- package/bin/lean/release-review.test.mjs +53 -0
- package/bin/lean/release-status.mjs +3 -1
- package/bin/lean/release-status.test.mjs +11 -0
- package/bin/lean/release.test.mjs +26 -1
- package/bin/lean/render.mjs +8 -7
- package/bin/lean/repro-output.test.mjs +45 -0
- package/bin/lean/routing.mjs +49 -0
- package/bin/lean/routing.test.mjs +54 -1
- package/bin/lean/run-issue-placement.test.mjs +30 -1
- package/bin/lean/sandbox-actions.mjs +121 -0
- package/bin/lean/sandbox-actions.test.mjs +94 -0
- package/bin/lean/sandbox-provider.contract.test.mjs +22 -1
- package/bin/lean/sandbox-remote.mjs +3 -1
- package/bin/lean/schedd-health.mjs +14 -2
- package/bin/lean/schedd-health.test.mjs +13 -0
- package/bin/lean/schedd.mjs +43 -17
- package/bin/lean/schedd.test.mjs +61 -1
- package/bin/lean/toolchain.mjs +16 -6
- package/bin/lean/toolplace.mjs +12 -0
- package/bin/lean/weakguard.mjs +105 -0
- package/bin/lean/weakguard.test.mjs +59 -0
- package/bin/lean/winspawn.mjs +61 -0
- package/bin/lean/winspawn.test.mjs +45 -0
- package/bin/lean/worktree.mjs +3 -1
- package/bin/lean/worktree.test.mjs +11 -0
- package/bin/pool/agent.mjs +215 -15
- package/bin/pool/cache-sweep.mjs +74 -0
- package/bin/pool/cli.mjs +11 -3
- package/bin/pool/docker.mjs +33 -2
- package/bin/pool/health.mjs +38 -0
- package/bin/pool/lane-tools.mjs +23 -4
- package/bin/pool/lane-tools.test.mjs +12 -1
- package/bin/pool/router.mjs +21 -3
- package/bin/runner.mjs +37 -0
- package/bin/splash-scene.mjs +356 -0
- package/bin/win-hide-children.mjs +114 -0
- package/commands/orchestrate.md +18 -27
- package/commands/review-pr-agents/infra.md +1 -0
- package/commands/review-pr-staging.md +2 -2
- package/docs/CONFIG.md +60 -3
- package/docs/OUTCOME-CODES.md +3 -0
- package/package.json +1 -1
- package/plugins/forgedock-splash/.claude-plugin/plugin.json +1 -1
- package/plugins/forgedock-splash/README.md +1 -1
- package/plugins/forgedock-splash/hooks/scene.ts +142 -8
- package/pool/README.md +6 -0
- package/pool/image/entrypoint.sh +1 -0
- package/scripts/acceptance-red-green.sh +8 -3
- package/scripts/acceptance-red-green.test.sh +8 -0
- package/scripts/check-spec-size.sh +3 -1
- package/scripts/gen-splash-scene.mjs +24 -0
- package/scripts/review-staging-pre.sh +8 -3
- package/scripts/review-staging-pre.test.sh +2 -0
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
3
|
import { describe, it } from "node:test";
|
|
4
4
|
import assert from "node:assert/strict";
|
|
5
|
-
import { parseCeilingBySize, ceilingFor, predictRestart, handoffFacts, firstEditTurn, HANDOFF_FILES_MAX } from "./ceiling.mjs";
|
|
6
|
-
import { contextRestartPrompt } from "./prompts.mjs";
|
|
5
|
+
import { parseCeilingBySize, ceilingFor, predictRestart, handoffFacts, firstEditTurn, HANDOFF_FILES_MAX, routeHandoff, buildReadsOf, ROUTE_DIGEST_MAX_BYTES } from "./ceiling.mjs";
|
|
6
|
+
import { contextRestartPrompt, routeDigestPrompt } from "./prompts.mjs";
|
|
7
7
|
import { toolStats } from "./toolstats.mjs";
|
|
8
8
|
|
|
9
9
|
describe("context ceiling helpers (#5346)", () => {
|
|
@@ -52,3 +52,34 @@ describe("context ceiling helpers (#5346)", () => {
|
|
|
52
52
|
assert.match(p, /Last validate result/);
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe("route hand-off (#5812)", () => {
|
|
57
|
+
it("routeHandoff digests a planning session above the bound, capped at half the ceiling; unknown or 0 resumes", () => {
|
|
58
|
+
assert.equal(routeHandoff({ planTokens: 96000, bound: 40000, ceiling: 120000 }).digest, true);
|
|
59
|
+
assert.equal(routeHandoff({ planTokens: 30000, bound: 40000, ceiling: 120000 }).digest, false);
|
|
60
|
+
assert.deepEqual([routeHandoff({ planTokens: 35000, bound: 40000, ceiling: 60000 }).digest, routeHandoff({ planTokens: 35000, bound: 40000, ceiling: 60000 }).bound], [true, 30000]);
|
|
61
|
+
assert.equal(routeHandoff({ planTokens: null, bound: 40000, ceiling: 120000 }).digest, false);
|
|
62
|
+
assert.equal(routeHandoff({ planTokens: 500000, bound: 0, ceiling: 120000 }).digest, false);
|
|
63
|
+
assert.equal(routeHandoff({ planTokens: 96000, bound: 40000 }).digestBytes, ROUTE_DIGEST_MAX_BYTES);
|
|
64
|
+
assert.equal(routeHandoff({ planTokens: 96000, bound: 10000 }).digestBytes, 10000);
|
|
65
|
+
});
|
|
66
|
+
it("buildReadsOf takes the plan reply's last Build reads list", () => {
|
|
67
|
+
assert.equal(buildReadsOf("x\nBuild reads: a.js:1-9\n- grep foo"), "a.js:1-9\n- grep foo");
|
|
68
|
+
assert.equal(buildReadsOf("no list"), "");
|
|
69
|
+
assert.equal(buildReadsOf(null), "");
|
|
70
|
+
});
|
|
71
|
+
it("routeDigestPrompt carries deliverables, decisions, open rows and reads, and stays within maxBytes", () => {
|
|
72
|
+
const contract = { requirements: [{ id: "R1", quote: "do it", disposition: "deliver" }, { id: "R2", quote: "later", disposition: "defer" }],
|
|
73
|
+
deliverables: [{ id: "D1", path: "src/a.js", change: "fix" }], decisions: [{ id: "K1", choice: "reuse b", justification: "b.js:3" }] };
|
|
74
|
+
const p = routeDigestPrompt({ contract, read: ["src/b.js", "src/b.js"], buildReads: "src/a.js:1-5", planTokens: 96000 });
|
|
75
|
+
assert.match(p, /Deliverables:\n- src\/a\.js: fix/);
|
|
76
|
+
assert.match(p, /Decisions:\n- K1 reuse b \(b\.js:3\)/);
|
|
77
|
+
assert.match(p, /Open \(not delivered in this lane; do not implement\):\n- R2 defer: later/);
|
|
78
|
+
assert.doesNotMatch(p, /R1 deliver/);
|
|
79
|
+
assert.equal(p.match(/- src\/b\.js/g).length, 1, "reads deduped");
|
|
80
|
+
assert.doesNotMatch(p, /Branch state/, "no branch section without changes");
|
|
81
|
+
const huge = routeDigestPrompt({ contract, read: Array.from({ length: 500 }, (_, i) => `src/${"x".repeat(80)}${i}.js`), stat: "a | 1", diff: "d".repeat(200000), maxBytes: 4096 });
|
|
82
|
+
assert.ok(Buffer.byteLength(huge) <= 4096, `digest is ${Buffer.byteLength(huge)} bytes`);
|
|
83
|
+
assert.match(huge, /<\/route_digest>$/);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
|
+
/**
|
|
4
|
+
* #5760: CI budget gate. A lane that adds a workflow file, a job, or a CI step running one test file must fold it into the
|
|
5
|
+
* forge.yaml `ci.budget.shared_regression_job` or carry a `# FORGE:CI_BUDGET trigger=<scope> minutes=<N>/month why=<text>`
|
|
6
|
+
* comment in the workflow YAML; a new workflow or job needs concurrency, timeout-minutes, path scoping and (issue/comment
|
|
7
|
+
* events) a job-level `if:`; `max_workflows`/`max_ci_jobs` cap the counts unless the same diff removes as many. Deterministic,
|
|
8
|
+
* 0 LLM calls, no YAML dependency (an indentation scan like bin/forge-utils.mjs). The one-level forge.yaml parser flattens
|
|
9
|
+
* `ci.budget.*` into `ci`, so `ci.budget.monthly_minutes` is the same key as #5278's `ci.monthly_minutes`.
|
|
10
|
+
*/
|
|
11
|
+
import { execFileSync } from "node:child_process";
|
|
12
|
+
import { readFileSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
import { OUTCOME_CODES } from "../engine/outcome-codes.mjs";
|
|
16
|
+
import { parseForgeYaml } from "../forge-utils.mjs";
|
|
17
|
+
|
|
18
|
+
const C = OUTCOME_CODES.CONFORMANCE_UNIMPLEMENTED;
|
|
19
|
+
export const WORKFLOW_RE = /^\.github\/workflows\/[^/]+\.ya?ml$/;
|
|
20
|
+
const COMMENT_EVENTS = ["issues", "issue_comment", "pull_request_review_comment", "discussion_comment"];
|
|
21
|
+
const PATH_EVENTS = ["push", "pull_request", "pull_request_target"];
|
|
22
|
+
// A runner at command position: segment start, after env assignments and a launcher, never inside a path or extension (#5787).
|
|
23
|
+
const RUNNER_RE = /^(?:\w+=\S*\s+)*(?:(?:npx|bunx|pnpm(?:\s+exec)?|yarn|uv\s+run|poetry\s+run|python3?\s+-m)\s+)?(?:[^\s;&|]*\/)?(?:node\s+(?:[^\s]+\s+)*--test|vitest|jest|pytest|go\s+test|playwright\s+test|mocha|bash|sh)(?=\s|$)/;
|
|
24
|
+
const YAML_KEY_RE = /^["']?[A-Za-z_][\w-]*["']?\s*:(?:\s|$)/;
|
|
25
|
+
const TEST_FILE_RE = /^(?:[^\s'"]*\/)?(?:[^\s/'"]+\.(?:test|spec)\.[a-z0-9]+|[^\s/'"]+_test\.[a-z0-9]+|test_[^\s/'"]+\.py)$/i;
|
|
26
|
+
// `# FORGE:CI_PATHS_EXEMPT why=<text>`: opt-out of path scoping for a push that must run on every merge (#5792).
|
|
27
|
+
const PATHS_EXEMPT_RE = /#\s*FORGE:CI_PATHS_EXEMPT\b.*\bwhy=\S/;
|
|
28
|
+
const ANNOT_RE = /#\s*FORGE:CI_BUDGET\b(.*)$/;
|
|
29
|
+
|
|
30
|
+
const posInt = (v) => { const n = Number(String(v ?? "").trim()); return String(v ?? "").trim() !== "" && Number.isInteger(n) && n >= 0 ? n : null; };
|
|
31
|
+
const str = (v) => (typeof v === "string" && v.trim() ? v.trim().replace(/^(["'])(.*)\1$/, "$2") : null);
|
|
32
|
+
const indentOf = (l) => l.match(/^ */)[0].length;
|
|
33
|
+
const blank = (l) => /^\s*(#.*)?$/.test(l);
|
|
34
|
+
const keyOf = (l) => l.match(/^\s*["']?([A-Za-z_][\w-]*)["']?\s*:(?:\s+(.*))?$/);
|
|
35
|
+
|
|
36
|
+
/** forge.yaml `ci` section (flat) -> {maxWorkflows, maxCiJobs, monthlyMinutes, sharedJob}; junk is null. */
|
|
37
|
+
export function parseCiBudget(sec) {
|
|
38
|
+
const s = sec && typeof sec === "object" ? sec : {};
|
|
39
|
+
return { maxWorkflows: posInt(s.max_workflows), maxCiJobs: posInt(s.max_ci_jobs), monthlyMinutes: posInt(s.monthly_minutes), sharedJob: str(s.shared_regression_job) };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `# FORGE:CI_BUDGET trigger=<scope> minutes=<N>[/month] why=<text>` -> {trigger, minutes, why, missing:[...]}, or null when no marker. */
|
|
43
|
+
export function parseAnnotation(line) {
|
|
44
|
+
const m = String(line ?? "").match(ANNOT_RE);
|
|
45
|
+
if (!m) return null;
|
|
46
|
+
const body = m[1];
|
|
47
|
+
const why = body.match(/\bwhy=(.*)$/)?.[1]?.trim() || null;
|
|
48
|
+
const head = body.replace(/\bwhy=.*$/, "");
|
|
49
|
+
const trigger = head.match(/\btrigger=(\S+)/)?.[1] ?? null;
|
|
50
|
+
const mm = head.match(/\bminutes=(\d+(?:\.\d+)?)(?:\/month)?(?=\s|$)/);
|
|
51
|
+
const minutes = mm ? Number(mm[1]) : null;
|
|
52
|
+
const missing = [...(trigger ? [] : ["trigger"]), ...(minutes == null ? ["minutes"] : []), ...(why ? [] : ["why"])];
|
|
53
|
+
return { trigger, minutes, why, missing };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Children (key lines) of the block whose header is at line `at`, at the first deeper indent, until a line at or above `ind`. */
|
|
57
|
+
function children(lines, at, ind, end = lines.length) {
|
|
58
|
+
const out = [];
|
|
59
|
+
let child = -1;
|
|
60
|
+
for (let i = at + 1; i < end; i++) {
|
|
61
|
+
const l = lines[i];
|
|
62
|
+
if (blank(l)) continue;
|
|
63
|
+
const n = indentOf(l);
|
|
64
|
+
if (n <= ind) break;
|
|
65
|
+
if (child < 0) child = n;
|
|
66
|
+
if (n !== child) continue;
|
|
67
|
+
const k = keyOf(l);
|
|
68
|
+
if (k) out.push({ key: k[1], value: (k[2] ?? "").replace(/\s+#.*$/, "").trim(), line: i });
|
|
69
|
+
else { const it = l.match(/^\s*-\s+["']?([\w-]+)["']?\s*$/); if (it) out.push({ key: it[1], value: "", line: i }); }
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
const blockEnd = (lines, at, ind) => { let i = at + 1; for (; i < lines.length; i++) if (!blank(lines[i]) && indentOf(lines[i]) <= ind) break; return i; };
|
|
74
|
+
|
|
75
|
+
/** Indentation scan of one workflow file: triggers, concurrency, jobs (with their line ranges) and FORGE:CI_BUDGET annotations. */
|
|
76
|
+
export function scanWorkflow(text) {
|
|
77
|
+
const lines = String(text ?? "").split(/\r?\n/);
|
|
78
|
+
const top = children(lines, -1, -1);
|
|
79
|
+
const triggers = {};
|
|
80
|
+
const on = top.find((t) => t.key === "on" || t.key === "true");
|
|
81
|
+
if (on) {
|
|
82
|
+
const flow = on.value.replace(/^\[|\]$/g, "");
|
|
83
|
+
if (on.value) for (const t of flow.split(",").map((x) => x.trim().replace(/^["']|["']$/g, "")).filter(Boolean)) triggers[t] = { paths: false };
|
|
84
|
+
else for (const t of children(lines, on.line, 0)) {
|
|
85
|
+
const e = blockEnd(lines, t.line, indentOf(lines[t.line]));
|
|
86
|
+
const body = lines.slice(t.line + 1, e);
|
|
87
|
+
const has = (re) => body.some((l) => re.test(l));
|
|
88
|
+
triggers[t.key] = { paths: has(/^\s*(paths|paths-ignore)\s*:/), tagOnly: has(/^\s*tags(-ignore)?\s*:/) && !has(/^\s*branches(-ignore)?\s*:/) };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const jobs = [];
|
|
92
|
+
const jb = top.find((t) => t.key === "jobs");
|
|
93
|
+
if (jb) {
|
|
94
|
+
const end = blockEnd(lines, jb.line, 0);
|
|
95
|
+
const ids = children(lines, jb.line, 0, end).filter((c) => keyOf(lines[c.line]));
|
|
96
|
+
ids.forEach((c, i) => {
|
|
97
|
+
const stop = i + 1 < ids.length ? ids[i + 1].line : end;
|
|
98
|
+
let start = c.line;
|
|
99
|
+
while (start > 0 && /^\s*#/.test(lines[start - 1])) start--;
|
|
100
|
+
const kids = children(lines, c.line, indentOf(lines[c.line]), stop).map((k) => k.key);
|
|
101
|
+
jobs.push({ id: c.key, line: c.line, start, end: stop - 1, timeout: kids.includes("timeout-minutes"), concurrency: kids.includes("concurrency"), if: kids.includes("if") });
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const annotations = [];
|
|
105
|
+
lines.forEach((l, i) => { const a = parseAnnotation(l); if (a) annotations.push({ line: i, ...a }); });
|
|
106
|
+
const pathsExempt = lines.some((l) => PATHS_EXEMPT_RE.test(l));
|
|
107
|
+
return { lines, triggers, pathsExempt, concurrency: top.some((t) => t.key === "concurrency"), jobs, annotations };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Lines of `head` not matched by a line of `base` (multiset on trimmed text), as indexes into head. */
|
|
111
|
+
function addedLineIdx(base, head) {
|
|
112
|
+
const left = new Map();
|
|
113
|
+
for (const l of String(base ?? "").split(/\r?\n/)) left.set(l.trim(), (left.get(l.trim()) ?? 0) + 1);
|
|
114
|
+
const out = [];
|
|
115
|
+
String(head ?? "").split(/\r?\n/).forEach((l, i) => { const k = l.trim(); if (left.get(k) > 0) left.set(k, left.get(k) - 1); else out.push(i); });
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The single test file a run line invokes, or null (a test runner with exactly one test-file argument). */
|
|
120
|
+
export function singleTestFile(line) {
|
|
121
|
+
const cmd = String(line ?? "").replace(/^\s*-?\s*(?:run\s*:\s*[|>]?)?/, "").trim();
|
|
122
|
+
if (!cmd || cmd.startsWith("#") || YAML_KEY_RE.test(cmd)) return null; // name:, if:, with: keys are not commands
|
|
123
|
+
if (!cmd.split(/&&|\|\||[;|]|\$\(/).some((seg) => RUNNER_RE.test(seg.trim()))) return null;
|
|
124
|
+
const files = cmd.split(/\s+/).map((t) => t.replace(/^["']|["'];?$/g, "")).filter((t) => TEST_FILE_RE.test(t));
|
|
125
|
+
return files.length === 1 ? files[0] : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Single-test-file steps among `addedLines` (strings): [{line, file}]. */
|
|
129
|
+
export function singleTestFileSteps(addedLines) {
|
|
130
|
+
return (addedLines ?? []).map((l, i) => ({ line: i, file: singleTestFile(l) })).filter((x) => x.file);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const fail = (kind, detail) => ({ code: C, kind, detail });
|
|
134
|
+
const FIX = (shared) => `${shared ? `fold it into the shared job \`${shared}\` (forge.yaml ci.budget.shared_regression_job) or ` : ""}add \`# FORGE:CI_BUDGET trigger=<scope> minutes=<N>/month why=<why a shared job cannot hold it>\` in the workflow YAML`;
|
|
135
|
+
|
|
136
|
+
/** Annotation verdict over the scan's annotations inside [from, to]: "ok" | {invalid: missing[]} | null (none). */
|
|
137
|
+
function annotated(scan, from, to) {
|
|
138
|
+
const inScope = scan.annotations.filter((a) => a.line >= from && a.line <= to);
|
|
139
|
+
if (inScope.some((a) => !a.missing.length)) return "ok";
|
|
140
|
+
return inScope.length ? { invalid: inScope[0].missing } : null;
|
|
141
|
+
}
|
|
142
|
+
function annotationFailure(where, verdict, shared) {
|
|
143
|
+
if (verdict === "ok") return [];
|
|
144
|
+
if (verdict?.invalid) return [fail("ci_budget_annotation_invalid", `${where}: the FORGE:CI_BUDGET annotation is missing ${verdict.invalid.join(", ")} (needs trigger=, minutes= and why=)`)];
|
|
145
|
+
return [fail("ci_budget_unannotated", `${where} adds CI with no cost statement: ${FIX(shared)}`)];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function hygiene(file, scan, job, { wholeFile }) {
|
|
149
|
+
const gaps = [];
|
|
150
|
+
if (!scan.concurrency && !job.concurrency) gaps.push("concurrency");
|
|
151
|
+
if (!job.timeout) gaps.push("timeout-minutes");
|
|
152
|
+
const pathTriggers = PATH_EVENTS.filter((e) => scan.triggers[e] && !scan.triggers[e].paths && !(e === "push" && scan.triggers[e].tagOnly) && !scan.pathsExempt);
|
|
153
|
+
if (pathTriggers.length && (wholeFile || !job.if)) gaps.push(`path scoping (paths/paths-ignore on ${pathTriggers.join(", ")}; tag-only push is exempt, or add \`# FORGE:CI_PATHS_EXEMPT why=<text>\`)`);
|
|
154
|
+
if (COMMENT_EVENTS.some((e) => scan.triggers[e]) && !job.if) gaps.push("a job-level if: filter for issue/comment events");
|
|
155
|
+
return gaps.length ? [fail("ci_hygiene", `${file} job \`${job.id}\` is new but has no ${gaps.join(", no ")}`)] : [];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const countJobs = (scans) => Object.values(scans).reduce((n, s) => n + s.jobs.length, 0);
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The gate over base and head workflow maps ({path: text}, only `.github/workflows/*.yml|yaml` are read).
|
|
162
|
+
* @returns {{code, kind, detail}[]} kinds: ci_budget_unannotated, ci_budget_annotation_invalid, ci_budget_exceeded, ci_hygiene
|
|
163
|
+
*/
|
|
164
|
+
export function ciGateFindings({ cfg = parseCiBudget(null), base = {}, head = {} } = {}) {
|
|
165
|
+
const wf = (m) => Object.fromEntries(Object.entries(m ?? {}).filter(([p]) => WORKFLOW_RE.test(p)).map(([p, t]) => [p, scanWorkflow(t)]));
|
|
166
|
+
const b = wf(base), h = wf(head), out = [];
|
|
167
|
+
const shared = cfg?.sharedJob ?? null;
|
|
168
|
+
const sharedDir = shared && shared.includes("/") ? shared.replace(/\/+$/, "") + "/" : null;
|
|
169
|
+
for (const [file, scan] of Object.entries(h)) {
|
|
170
|
+
if (!b[file]) {
|
|
171
|
+
out.push(...annotationFailure(`new workflow ${file}`, annotated(scan, 0, scan.lines.length), shared));
|
|
172
|
+
for (const j of scan.jobs) out.push(...hygiene(file, scan, j, { wholeFile: true }));
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const baseIds = new Set(b[file].jobs.map((j) => j.id));
|
|
176
|
+
const added = new Set(addedLineIdx(base[file], head[file]));
|
|
177
|
+
for (const j of scan.jobs) {
|
|
178
|
+
if (baseIds.has(j.id)) continue;
|
|
179
|
+
out.push(...annotationFailure(`${file} new job \`${j.id}\``, annotated(scan, j.start, j.end), shared));
|
|
180
|
+
out.push(...hygiene(file, scan, j, { wholeFile: false }));
|
|
181
|
+
}
|
|
182
|
+
for (const i of added) {
|
|
183
|
+
const test = singleTestFile(scan.lines[i]);
|
|
184
|
+
if (!test) continue;
|
|
185
|
+
const j = scan.jobs.find((x) => i >= x.start && i <= x.end);
|
|
186
|
+
if (!j || !baseIds.has(j.id)) continue; // outside a job, or inside a new job (judged above)
|
|
187
|
+
if (shared && (j.id === shared || (sharedDir && test.replace(/^\.\//, "").startsWith(sharedDir)))) continue;
|
|
188
|
+
out.push(...annotationFailure(`${file} job \`${j.id}\` step running only ${test}`, annotated(scan, j.start, j.end), shared));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const [wb, wh, jbn, jhn] = [Object.keys(b).length, Object.keys(h).length, countJobs(b), countJobs(h)];
|
|
192
|
+
if (cfg?.maxWorkflows != null && wh > cfg.maxWorkflows && wh > wb) out.push(fail("ci_budget_exceeded", `${wh} workflow files exceed ci.budget.max_workflows ${cfg.maxWorkflows} (+${wh - wb} in this diff): remove or fold as many workflows as you add`));
|
|
193
|
+
if (cfg?.maxCiJobs != null && jhn > cfg.maxCiJobs && jhn > jbn) out.push(fail("ci_budget_exceeded", `${jhn} CI jobs exceed ci.budget.max_ci_jobs ${cfg.maxCiJobs} (+${jhn - jbn} in this diff): fold the new job(s) into an existing job or remove as many`));
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Net CI change between two workflow maps: {workflows, jobs, minutes, line}. */
|
|
198
|
+
export function netCiChange({ base = {}, head = {}, cfg = null } = {}) {
|
|
199
|
+
const wf = (m) => Object.entries(m ?? {}).filter(([p]) => WORKFLOW_RE.test(p)).map(([, t]) => scanWorkflow(t));
|
|
200
|
+
const [b, h] = [wf(base), wf(head)];
|
|
201
|
+
const mins = (s) => s.reduce((n, x) => n + x.annotations.reduce((k, a) => k + (a.minutes ?? 0), 0), 0);
|
|
202
|
+
const jobs = (s) => s.reduce((n, x) => n + x.jobs.length, 0);
|
|
203
|
+
const r = { workflows: h.length - b.length, jobs: jobs(h) - jobs(b), minutes: mins(h) - mins(b) };
|
|
204
|
+
const sg = (n) => (n >= 0 ? `+${n}` : String(n));
|
|
205
|
+
return { ...r, line: `CI_NET: workflows ${sg(r.workflows)}, jobs ${sg(r.jobs)}, est. minutes/month ${sg(r.minutes)}${cfg?.monthlyMinutes ? ` of ${cfg.monthlyMinutes} budget` : ""}` };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Workflow map {path: text} of a git ref through `git(args) -> stdout`. */
|
|
209
|
+
async function workflowsAt(git, ref) {
|
|
210
|
+
const names = String(await git(["ls-tree", "-r", "--name-only", ref, "--", ".github/workflows"])).split("\n").map((x) => x.trim()).filter((p) => WORKFLOW_RE.test(p));
|
|
211
|
+
const out = {};
|
|
212
|
+
for (const p of names) out[p] = String(await git(["show", `${ref}:${p}`]));
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Lane wiring: [] after one `git diff` when the diff touches no workflow file; else the gate over merge-base vs HEAD. */
|
|
217
|
+
export async function ciBudgetFailures({ cfg = null, base, git }) {
|
|
218
|
+
const changed = String(await git(["diff", "--name-only", `${base}...HEAD`, "--", ".github/workflows"])).split("\n").map((x) => x.trim()).filter((p) => WORKFLOW_RE.test(p));
|
|
219
|
+
if (!changed.length) return [];
|
|
220
|
+
const mb = String(await git(["merge-base", base, "HEAD"])).trim() || base;
|
|
221
|
+
return ciGateFindings({ cfg: cfg ?? parseCiBudget(null), base: await workflowsAt(git, mb), head: await workflowsAt(git, "HEAD") });
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** `node bin/lean/ci-gate.mjs net --base REF --head REF [--cwd DIR]`: prints the CI_NET line; exit 0, `CI_NET: unknown (<why>)` on failure. */
|
|
225
|
+
export async function ciGateCli(argv, { print = (s) => process.stdout.write(s + "\n"), cwd: cwdIn = process.cwd() } = {}) {
|
|
226
|
+
const arg = (k) => { const i = argv.indexOf(k); return i >= 0 ? argv[i + 1] : null; };
|
|
227
|
+
if (argv[0] !== "net" || !arg("--base") || !arg("--head")) { print("usage: ci-gate.mjs net --base REF --head REF [--cwd DIR]"); return 2; }
|
|
228
|
+
const cwd = arg("--cwd") ?? cwdIn;
|
|
229
|
+
try {
|
|
230
|
+
const git = async (a) => execFileSync("git", a, { cwd, encoding: "utf8", maxBuffer: 64 << 20, stdio: ["ignore", "pipe", "pipe"] });
|
|
231
|
+
let cfg = null;
|
|
232
|
+
try { cfg = parseCiBudget(parseForgeYaml(readFileSync(join(cwd, "forge.yaml"), "utf8"))?.ci); } catch { /* no forge.yaml */ }
|
|
233
|
+
print(netCiChange({ base: await workflowsAt(git, arg("--base")), head: await workflowsAt(git, arg("--head")), cfg }).line);
|
|
234
|
+
} catch (e) {
|
|
235
|
+
print(`CI_NET: unknown (${String(e?.stderr || e?.message || e).split("\n")[0].trim()})`);
|
|
236
|
+
}
|
|
237
|
+
return 0;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) process.exitCode = await ciGateCli(process.argv.slice(2));
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
|
+
// #5760: CI budget gate fixtures (in-memory base/head workflow maps).
|
|
4
|
+
import { describe, it } from "node:test";
|
|
5
|
+
import assert from "node:assert/strict";
|
|
6
|
+
import { tmpDir } from "../tests/_tmp.mjs";
|
|
7
|
+
import { parseCiBudget, parseAnnotation, scanWorkflow, singleTestFile, singleTestFileSteps, ciGateFindings, netCiChange, ciBudgetFailures, ciGateCli } from "./ci-gate.mjs";
|
|
8
|
+
import { parseForgeYaml } from "../forge-utils.mjs";
|
|
9
|
+
|
|
10
|
+
const kinds = (f) => f.map((x) => x.kind).sort();
|
|
11
|
+
const CI = ".github/workflows/ci.yml";
|
|
12
|
+
const BASE_CI = [
|
|
13
|
+
"name: CI",
|
|
14
|
+
"on:",
|
|
15
|
+
" pull_request:",
|
|
16
|
+
" paths: ['src/**']",
|
|
17
|
+
"concurrency:",
|
|
18
|
+
" group: ci-${{ github.ref }}",
|
|
19
|
+
" cancel-in-progress: true",
|
|
20
|
+
"jobs:",
|
|
21
|
+
" lint:",
|
|
22
|
+
" runs-on: ubuntu-latest",
|
|
23
|
+
" timeout-minutes: 10",
|
|
24
|
+
" steps:",
|
|
25
|
+
" - run: npm run lint",
|
|
26
|
+
" regression:",
|
|
27
|
+
" runs-on: ubuntu-latest",
|
|
28
|
+
" timeout-minutes: 20",
|
|
29
|
+
" steps:",
|
|
30
|
+
" - run: node --test tests/a.test.mjs tests/b.test.mjs",
|
|
31
|
+
"",
|
|
32
|
+
].join("\n");
|
|
33
|
+
const cfg = parseCiBudget({ shared_regression_job: "regression" });
|
|
34
|
+
const BARE = "name: one\non: push\njobs:\n check:\n runs-on: ubuntu-latest\n steps:\n - run: node --test tests/x.test.mjs\n";
|
|
35
|
+
const GOOD = [
|
|
36
|
+
"# FORGE:CI_BUDGET trigger=pull_request:src/** minutes=120/month why=needs a GPU runner the shared job lacks",
|
|
37
|
+
"name: gpu",
|
|
38
|
+
"on:",
|
|
39
|
+
" pull_request:",
|
|
40
|
+
" paths:",
|
|
41
|
+
" - 'gpu/**'",
|
|
42
|
+
"concurrency:",
|
|
43
|
+
" group: gpu-${{ github.ref }}",
|
|
44
|
+
"jobs:",
|
|
45
|
+
" gpu:",
|
|
46
|
+
" runs-on: gpu",
|
|
47
|
+
" timeout-minutes: 30",
|
|
48
|
+
" steps:",
|
|
49
|
+
" - run: make gpu-test",
|
|
50
|
+
"",
|
|
51
|
+
].join("\n");
|
|
52
|
+
|
|
53
|
+
describe("lane check (#5760 R10)", () => {
|
|
54
|
+
it("a lane adding a bare workflow fails (unannotated + hygiene)", () => {
|
|
55
|
+
const f = ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: BASE_CI, ".github/workflows/one.yml": BARE } });
|
|
56
|
+
assert.deepEqual(kinds(f), ["ci_budget_unannotated", "ci_hygiene"]);
|
|
57
|
+
assert.match(f.find((x) => x.kind === "ci_hygiene").detail, /concurrency.*timeout-minutes.*path scoping/);
|
|
58
|
+
assert.equal(f[0].code, "conformance.unimplemented");
|
|
59
|
+
assert.match(f.find((x) => x.kind === "ci_budget_unannotated").detail, /shared job `regression`/);
|
|
60
|
+
});
|
|
61
|
+
it("a lane folding a one-file check into the shared job passes", () => {
|
|
62
|
+
const head = BASE_CI.replace(" - run: node --test tests/a.test.mjs tests/b.test.mjs", " - run: node --test tests/a.test.mjs tests/b.test.mjs\n - run: node --test tests/new.test.mjs");
|
|
63
|
+
assert.deepEqual(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: head } }), []);
|
|
64
|
+
});
|
|
65
|
+
it("an annotated, scoped workflow passes", () => {
|
|
66
|
+
assert.deepEqual(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: BASE_CI, ".github/workflows/gpu.yaml": GOOD } }), []);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("annotation and single-file steps", () => {
|
|
71
|
+
it("an annotation missing minutes is invalid", () => {
|
|
72
|
+
const f = ciGateFindings({ cfg, base: {}, head: { ".github/workflows/gpu.yml": GOOD.replace("minutes=120/month ", "") } });
|
|
73
|
+
assert.deepEqual(kinds(f), ["ci_budget_annotation_invalid"]);
|
|
74
|
+
assert.match(f[0].detail, /missing minutes/);
|
|
75
|
+
});
|
|
76
|
+
it("parseAnnotation reads the three fields", () => {
|
|
77
|
+
assert.deepEqual(parseAnnotation(" # FORGE:CI_BUDGET trigger=push:main minutes=30 why=deploy smoke, not a test"), { trigger: "push:main", minutes: 30, why: "deploy smoke, not a test", missing: [] });
|
|
78
|
+
assert.deepEqual(parseAnnotation("# FORGE:CI_BUDGET why=x").missing, ["trigger", "minutes"]);
|
|
79
|
+
assert.equal(parseAnnotation("# a comment"), null);
|
|
80
|
+
});
|
|
81
|
+
it("a single-file step added to another job fails unless annotated", () => {
|
|
82
|
+
const head = BASE_CI.replace(" - run: npm run lint", " - run: npm run lint\n - run: npx vitest run src/a.spec.ts");
|
|
83
|
+
assert.deepEqual(kinds(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: head } })), ["ci_budget_unannotated"]);
|
|
84
|
+
const ann = head.replace(" lint:\n", " # FORGE:CI_BUDGET trigger=pull_request minutes=5 why=lint-coupled spec\n lint:\n");
|
|
85
|
+
assert.deepEqual(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: ann } }), []);
|
|
86
|
+
});
|
|
87
|
+
it("a shared dir folds a step whose file lives under it", () => {
|
|
88
|
+
const head = BASE_CI.replace(" - run: npm run lint", " - run: npm run lint\n - run: pytest tests/regression/test_x.py");
|
|
89
|
+
assert.deepEqual(ciGateFindings({ cfg: parseCiBudget({ shared_regression_job: "tests/regression" }), base: { [CI]: BASE_CI }, head: { [CI]: head } }), []);
|
|
90
|
+
});
|
|
91
|
+
it("singleTestFile needs a runner and exactly one test file", () => {
|
|
92
|
+
assert.equal(singleTestFile(" - run: node --test bin/x.test.mjs"), "bin/x.test.mjs");
|
|
93
|
+
assert.equal(singleTestFile("run: bash scripts/foo.test.sh"), "scripts/foo.test.sh");
|
|
94
|
+
assert.equal(singleTestFile("run: go test ./pkg/foo_test.go"), "./pkg/foo_test.go");
|
|
95
|
+
assert.equal(singleTestFile("run: node --test a.test.mjs b.test.mjs"), null);
|
|
96
|
+
assert.equal(singleTestFile("run: npm test"), null);
|
|
97
|
+
assert.equal(singleTestFile("run: cat x.test.mjs"), null);
|
|
98
|
+
assert.deepEqual(singleTestFileSteps(["npm ci", "pytest tests/test_a.py"]), [{ line: 1, file: "tests/test_a.py" }]);
|
|
99
|
+
});
|
|
100
|
+
it("singleTestFile ignores runner words inside paths and non-command lines (#5787)", () => {
|
|
101
|
+
for (const l of [" - run: echo scripts/foo.test.sh", " - name: check scripts/foo.test.sh", " - scripts/foo.test.sh", "run: cat x.test.sh", " - name: Run vitest on a.test.ts", "run: cat lib/bash/x.test.mjs"]) assert.equal(singleTestFile(l), null, l);
|
|
102
|
+
assert.equal(singleTestFile("run: npm ci && bash scripts/foo.test.sh"), "scripts/foo.test.sh");
|
|
103
|
+
assert.equal(singleTestFile(" npx vitest run src/a.test.ts"), "src/a.test.ts");
|
|
104
|
+
const head = BASE_CI.replace(" - run: npm run lint", " - run: npm run lint\n - name: check scripts/foo.test.sh\n - run: echo scripts/foo.test.sh");
|
|
105
|
+
assert.deepEqual(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: head } }), []);
|
|
106
|
+
});
|
|
107
|
+
it("singleTestFile still flags real runner steps (#5787 over-narrowing guard)", () => {
|
|
108
|
+
assert.equal(singleTestFile("run: cd x && pytest tests/test_a.py"), "tests/test_a.py");
|
|
109
|
+
assert.equal(singleTestFile("run: sh scripts/foo.test.sh"), "scripts/foo.test.sh");
|
|
110
|
+
const head = BASE_CI.replace(" - run: npm run lint", " - run: npm run lint\n - run: bash scripts/foo.test.sh");
|
|
111
|
+
assert.deepEqual(kinds(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: head } })), ["ci_budget_unannotated"]);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("push path scoping exemptions (#5792)", () => {
|
|
116
|
+
const rel = (on, pre = "") => `${pre}# FORGE:CI_BUDGET trigger=push minutes=5 why=release\nname: rel\non:\n${on}\nconcurrency: rel\njobs:\n rel:\n runs-on: ubuntu-latest\n timeout-minutes: 10\n steps:\n - run: make release\n`;
|
|
117
|
+
const run = (text) => ciGateFindings({ cfg, base: {}, head: { ".github/workflows/rel.yml": text } });
|
|
118
|
+
it("a tag-only push trigger needs no paths", () => {
|
|
119
|
+
assert.deepEqual(run(rel(" push:\n tags: ['v*']")), []);
|
|
120
|
+
});
|
|
121
|
+
it("a push with branches (or tags and branches) still needs paths", () => {
|
|
122
|
+
assert.match(run(rel(" push:\n branches: [main]"))[0].detail, /path scoping/);
|
|
123
|
+
assert.match(run(rel(" push:\n tags: ['v*']\n branches: [main]"))[0].detail, /path scoping/);
|
|
124
|
+
});
|
|
125
|
+
it("a FORGE:CI_PATHS_EXEMPT annotation with why exempts a deploy-on-main push; without why it does not", () => {
|
|
126
|
+
const on = " push:\n branches: [main]";
|
|
127
|
+
assert.deepEqual(run(rel(on, "# FORGE:CI_PATHS_EXEMPT why=deploys on every merge\n")), []);
|
|
128
|
+
assert.match(run(rel(on, "# FORGE:CI_PATHS_EXEMPT\n"))[0].detail, /path scoping/);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("new jobs and hygiene", () => {
|
|
133
|
+
const withJob = (job) => BASE_CI + job;
|
|
134
|
+
it("a job added to an existing workflow needs an annotation above or inside it", () => {
|
|
135
|
+
const job = " e2e:\n runs-on: ubuntu-latest\n timeout-minutes: 15\n steps:\n - run: npm run e2e\n";
|
|
136
|
+
assert.deepEqual(kinds(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: withJob(job) } })), ["ci_budget_unannotated"]);
|
|
137
|
+
const ann = " # FORGE:CI_BUDGET trigger=pull_request:src/** minutes=200 why=browser stack\n" + job;
|
|
138
|
+
assert.deepEqual(ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: withJob(ann) } }), []);
|
|
139
|
+
});
|
|
140
|
+
it("a new job without timeout-minutes is a hygiene failure", () => {
|
|
141
|
+
const job = " # FORGE:CI_BUDGET trigger=pr minutes=5 why=x\n e2e:\n runs-on: ubuntu-latest\n steps:\n - run: npm run e2e\n";
|
|
142
|
+
const f = ciGateFindings({ cfg, base: { [CI]: BASE_CI }, head: { [CI]: withJob(job) } });
|
|
143
|
+
assert.deepEqual(kinds(f), ["ci_hygiene"]);
|
|
144
|
+
assert.match(f[0].detail, /`e2e`.*timeout-minutes/);
|
|
145
|
+
});
|
|
146
|
+
it("an issue_comment job without a job-level if: fails ci_hygiene", () => {
|
|
147
|
+
const wf = "# FORGE:CI_BUDGET trigger=issue_comment minutes=10 why=bot command\non:\n issue_comment:\n types: [created]\nconcurrency: bot\njobs:\n bot:\n timeout-minutes: 5\n runs-on: ubuntu-latest\n steps:\n - run: echo hi\n";
|
|
148
|
+
const f = ciGateFindings({ cfg, base: {}, head: { ".github/workflows/bot.yml": wf } });
|
|
149
|
+
assert.deepEqual(kinds(f), ["ci_hygiene"]);
|
|
150
|
+
assert.match(f[0].detail, /job-level if:/);
|
|
151
|
+
const ok = wf.replace(" bot:\n", " bot:\n if: startsWith(github.event.comment.body, '/bot')\n");
|
|
152
|
+
assert.deepEqual(ciGateFindings({ cfg, base: {}, head: { ".github/workflows/bot.yml": ok } }), []);
|
|
153
|
+
});
|
|
154
|
+
it("a schedule-only workflow needs no path scoping", () => {
|
|
155
|
+
const wf = "# FORGE:CI_BUDGET trigger=nightly minutes=300 why=full suite\non:\n schedule:\n - cron: '0 3 * * *'\n workflow_dispatch:\nconcurrency: nightly\njobs:\n full:\n timeout-minutes: 60\n runs-on: ubuntu-latest\n steps:\n - run: npm test\n";
|
|
156
|
+
assert.deepEqual(ciGateFindings({ cfg, base: {}, head: { ".github/workflows/nightly.yml": wf } }), []);
|
|
157
|
+
});
|
|
158
|
+
it("scanWorkflow reads triggers, concurrency and job children", () => {
|
|
159
|
+
const s = scanWorkflow(BASE_CI);
|
|
160
|
+
assert.deepEqual(s.triggers, { pull_request: { paths: true, tagOnly: false } });
|
|
161
|
+
assert.equal(s.concurrency, true);
|
|
162
|
+
assert.deepEqual(s.jobs.map((j) => [j.id, j.timeout, j.if]), [["lint", true, false], ["regression", true, false]]);
|
|
163
|
+
assert.deepEqual(Object.keys(scanWorkflow("on: [push, pull_request]\njobs: {}\n").triggers), ["push", "pull_request"]);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("max_workflows / max_ci_jobs (#5760 R8)", () => {
|
|
168
|
+
const two = { [CI]: BASE_CI, ".github/workflows/old.yml": GOOD };
|
|
169
|
+
it("exceeding max_workflows fails, and passes when the same diff removes one", () => {
|
|
170
|
+
const c = parseCiBudget({ max_workflows: "2" });
|
|
171
|
+
const head = { ...two, ".github/workflows/gpu.yml": GOOD };
|
|
172
|
+
assert.deepEqual(kinds(ciGateFindings({ cfg: c, base: two, head })), ["ci_budget_exceeded"]);
|
|
173
|
+
const swapped = { [CI]: BASE_CI, ".github/workflows/gpu.yml": GOOD };
|
|
174
|
+
assert.deepEqual(ciGateFindings({ cfg: c, base: two, head: swapped }), []);
|
|
175
|
+
});
|
|
176
|
+
it("exceeding max_ci_jobs fails unless a job is folded away", () => {
|
|
177
|
+
const c = parseCiBudget({ max_ci_jobs: "2" });
|
|
178
|
+
const job = " # FORGE:CI_BUDGET trigger=pr minutes=5 why=x\n e2e:\n runs-on: ubuntu-latest\n timeout-minutes: 5\n steps:\n - run: npm run e2e\n";
|
|
179
|
+
assert.deepEqual(kinds(ciGateFindings({ cfg: c, base: { [CI]: BASE_CI }, head: { [CI]: BASE_CI + job } })), ["ci_budget_exceeded"]);
|
|
180
|
+
const folded = BASE_CI.replace(/ lint:\n(?: .*\n)*?(?= regression:)/, "") + job;
|
|
181
|
+
// the fold removes exactly the lint job (regression guard for the unambiguous, non-backtracking pattern)
|
|
182
|
+
assert.ok(!folded.includes("lint:") && !folded.includes("npm run lint"));
|
|
183
|
+
assert.ok(folded.includes(" regression:\n") && folded.endsWith(job));
|
|
184
|
+
assert.deepEqual(ciGateFindings({ cfg: c, base: { [CI]: BASE_CI }, head: { [CI]: folded } }), []);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("config, net change and wiring", () => {
|
|
189
|
+
it("parseCiBudget reads the flattened ci.budget block and shares monthly_minutes with ci.monthly_minutes", () => {
|
|
190
|
+
const y = parseForgeYaml("ci:\n mode: auto\n budget:\n max_workflows: 12\n max_ci_jobs: 40\n monthly_minutes: 50000\n shared_regression_job: regression\n");
|
|
191
|
+
assert.deepEqual(parseCiBudget(y.ci), { maxWorkflows: 12, maxCiJobs: 40, monthlyMinutes: 50000, sharedJob: "regression" });
|
|
192
|
+
assert.equal(y.ci.monthly_minutes, "50000");
|
|
193
|
+
assert.deepEqual(parseCiBudget(null), { maxWorkflows: null, maxCiJobs: null, monthlyMinutes: null, sharedJob: null });
|
|
194
|
+
assert.equal(parseCiBudget({ max_workflows: "many" }).maxWorkflows, null);
|
|
195
|
+
});
|
|
196
|
+
it("netCiChange reports workflows, jobs and annotated minutes", () => {
|
|
197
|
+
const head = { [CI]: BASE_CI, ".github/workflows/gpu.yml": GOOD, ".github/workflows/one.yml": BARE };
|
|
198
|
+
const r = netCiChange({ base: { [CI]: BASE_CI }, head, cfg: parseCiBudget({ monthly_minutes: 3000 }) });
|
|
199
|
+
assert.deepEqual([r.workflows, r.jobs, r.minutes], [2, 2, 120]);
|
|
200
|
+
assert.equal(r.line, "CI_NET: workflows +2, jobs +2, est. minutes/month +120 of 3000 budget");
|
|
201
|
+
assert.match(netCiChange({ base: head, head: { [CI]: BASE_CI } }).line, /workflows -2, jobs -2, est\. minutes\/month -120$/);
|
|
202
|
+
});
|
|
203
|
+
it("ciBudgetFailures makes one git call when no workflow changed", async () => {
|
|
204
|
+
const calls = [];
|
|
205
|
+
const git = async (a) => { calls.push(a); return "src/a.js\n"; };
|
|
206
|
+
assert.deepEqual(await ciBudgetFailures({ cfg, base: "origin/staging", git }), []);
|
|
207
|
+
assert.equal(calls.length, 1);
|
|
208
|
+
});
|
|
209
|
+
it("ciBudgetFailures reads merge-base and HEAD workflows when a workflow changed", async () => {
|
|
210
|
+
const files = { mb: { [CI]: BASE_CI }, HEAD: { [CI]: BASE_CI, ".github/workflows/one.yml": BARE } };
|
|
211
|
+
const git = async (a) => {
|
|
212
|
+
if (a[0] === "diff") return ".github/workflows/one.yml\n";
|
|
213
|
+
if (a[0] === "merge-base") return "mb\n";
|
|
214
|
+
if (a[0] === "ls-tree") return Object.keys(files[a[3]]).join("\n");
|
|
215
|
+
const [ref, p] = a[1].split(/:(.*)/s);
|
|
216
|
+
return files[ref][p];
|
|
217
|
+
};
|
|
218
|
+
assert.deepEqual(kinds(await ciBudgetFailures({ cfg, base: "origin/staging", git })), ["ci_budget_unannotated", "ci_hygiene"]);
|
|
219
|
+
});
|
|
220
|
+
it("the CLI prints CI_NET unknown on a bad ref and usage on bad args", async () => {
|
|
221
|
+
const out = [];
|
|
222
|
+
const cwd = tmpDir("ci-gate-");
|
|
223
|
+
assert.equal(await ciGateCli(["net", "--base", "nope", "--head", "nope2"], { print: (s) => out.push(s), cwd }), 0);
|
|
224
|
+
assert.match(out[0], /^CI_NET: unknown \(/);
|
|
225
|
+
assert.equal(await ciGateCli(["net"], { print: (s) => out.push(s) }), 2);
|
|
226
|
+
});
|
|
227
|
+
});
|