-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecks.ts
More file actions
42 lines (38 loc) · 1.63 KB
/
Copy pathchecks.ts
File metadata and controls
42 lines (38 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { exec } from "child_process";
import { TIER_ORDER } from "./tiers.js";
import type { Finding, Config } from "./types.js";
export interface CommandResult {
command: string;
success: boolean;
code: number;
stdout: string;
stderr: string;
timedOut: boolean;
durationMs: number;
}
export function runCommand(command: string, cwd: string, timeoutMs = 10 * 60 * 1000): Promise<CommandResult> {
const startedAt = Date.now();
return new Promise((resolve) => {
exec(command, { cwd, timeout: timeoutMs, maxBuffer: 32 * 1024 * 1024 }, (error, stdout, stderr) => {
resolve({
command,
success: !error,
code: error ? ((error as NodeJS.ErrnoException).code ? Number((error as NodeJS.ErrnoException).code) : 1) : 0,
stdout: stdout || "",
stderr: stderr || "",
timedOut: !!(error && (error as NodeJS.ErrnoException & { killed?: boolean }).killed),
durationMs: Date.now() - startedAt,
});
});
});
}
export function shouldGate(findings: Finding[], config: Config): boolean {
const threshold = TIER_ORDER[config?.gate?.failOn || "orange"] ?? 2;
return findings.some((f) => f.blocking || (TIER_ORDER[f.tier] ?? 0) >= threshold);
}
export async function runGate({ cwd, config, findings }: { cwd: string; config: Config; findings: Finding[] }): Promise<{ ran: boolean; reason?: string } & Partial<CommandResult>> {
if (!config.testCommand) return { ran: false, reason: "no-test-command" };
if (!shouldGate(findings, config)) return { ran: false, reason: "below-threshold" };
const result = await runCommand(config.testCommand, cwd);
return { ran: true, ...result };
}