@maniumek/suwgate 0.15.0 → 0.17.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/AGENTS.shared.md +13 -2
- package/README.md +95 -9
- package/dist/cli.js +208 -12
- package/dist/eslint/index.d.ts +1 -0
- package/dist/eslint/index.js +20 -1
- package/dist/gate/breakdown.d.ts +38 -0
- package/dist/gate/breakdown.js +107 -0
- package/dist/gate/check.d.ts +3 -0
- package/dist/gate/check.js +37 -1
- package/dist/gate/config.d.ts +5 -1
- package/dist/gate/config.js +56 -2
- package/dist/gate/context.d.ts +15 -0
- package/dist/gate/context.js +97 -0
- package/dist/gate/contract.d.ts +7 -0
- package/dist/gate/contract.js +1 -0
- package/dist/gate/dashboard.d.ts +107 -0
- package/dist/gate/dashboard.js +126 -0
- package/dist/gate/dashboardFile.d.ts +31 -0
- package/dist/gate/dashboardFile.js +154 -0
- package/dist/gate/dashboardJson.d.ts +2 -0
- package/dist/gate/dashboardJson.js +25 -0
- package/dist/gate/dashboardPage.d.ts +3 -0
- package/dist/gate/dashboardPage.js +294 -0
- package/dist/gate/dashboardServer.d.ts +11 -0
- package/dist/gate/dashboardServer.js +70 -0
- package/dist/gate/dashboardTesting.d.ts +12 -0
- package/dist/gate/dashboardTesting.js +98 -0
- package/dist/gate/guardsStep.d.ts +6 -0
- package/dist/gate/guardsStep.js +8 -1
- package/dist/gate/history.d.ts +28 -0
- package/dist/gate/history.js +72 -0
- package/dist/gate/plan.d.ts +3 -1
- package/dist/gate/plan.js +6 -3
- package/dist/gate/repository.d.ts +1 -0
- package/dist/gate/repository.js +1 -1
- package/dist/gate/runner.d.ts +3 -0
- package/dist/gate/runner.js +4 -1
- package/dist/gate/stats.d.ts +27 -0
- package/dist/gate/stats.js +84 -0
- package/dist/guards/comments/eslint.js +1 -0
- package/dist/guards/comments/scan.js +1 -0
- package/dist/guards/comments/switches.js +1 -0
- package/dist/guards/database/connectionHost.js +1 -0
- package/dist/guards/dotnet/appsettingsBound.js +1 -0
- package/dist/guards/dotnet/bannedNugetPackages.js +1 -0
- package/dist/guards/dotnet/openApiVersions.js +1 -0
- package/dist/guards/dotnet/suwbackkitWiring.js +1 -0
- package/dist/guards/dotnet/unusedNugetVersions.js +1 -0
- package/dist/guards/repository/bannedNpmPackages.js +1 -0
- package/dist/guards/repository/composePorts.js +1 -0
- package/dist/guards/repository/dependencyDirection.js +1 -0
- package/dist/guards/repository/forbiddenNames.js +1 -0
- package/dist/guards/repository/humanDocs.js +1 -0
- package/dist/guards/repository/imagePins.js +1 -0
- package/dist/guards/repository/justModules.js +1 -0
- package/dist/guards/repository/noRawColors.js +1 -0
- package/dist/guards/repository/repositoryLayout.js +1 -0
- package/dist/guards/repository/scriptsCalled.js +1 -0
- package/dist/guards/repository/scriptsHelp.js +1 -0
- package/dist/guards/repository/suwfrontkitWiring.js +1 -0
- package/dist/guards/repository/vsCodeSettings.js +1 -0
- package/dist/profiles/holdout.d.ts +2 -0
- package/dist/profiles/holdout.js +38 -0
- package/dist/profiles/index.d.ts +1 -0
- package/dist/profiles/index.js +1 -0
- package/dist/profiles/node.d.ts +1 -0
- package/dist/profiles/node.js +12 -4
- package/dist/profiles/reports.d.ts +6 -0
- package/dist/profiles/reports.js +14 -0
- package/dist/profiles/verdict.js +45 -3
- package/dist/session/doctor.d.ts +26 -0
- package/dist/session/doctor.js +265 -0
- package/dist/session/gardenRecord.d.ts +12 -0
- package/dist/session/gardenRecord.js +58 -0
- package/dist/session/guardPr.js +2 -2
- package/dist/session/prePush.js +1 -1
- package/dist/session/suwagentInfo.d.ts +28 -0
- package/dist/session/suwagentInfo.js +103 -0
- package/package.json +3 -1
- package/schema/suwgate.schema.json +26 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createServer } from "node:http";
|
|
2
|
+
const HOST = "127.0.0.1";
|
|
3
|
+
const LOCAL_NAMES = ["127.0.0.1", "localhost"];
|
|
4
|
+
function reply(response, status, type, body) {
|
|
5
|
+
response.writeHead(status, {
|
|
6
|
+
"content-type": `${type}; charset=utf-8`,
|
|
7
|
+
"cache-control": "no-store",
|
|
8
|
+
...(status === 405 ? { allow: "GET" } : {}),
|
|
9
|
+
});
|
|
10
|
+
response.end(body);
|
|
11
|
+
}
|
|
12
|
+
function fromThisMachine(request) {
|
|
13
|
+
const address = request.socket.remoteAddress ?? "";
|
|
14
|
+
const host = (request.headers.host ?? "").replace(/:\d+$/, "");
|
|
15
|
+
return (address === "127.0.0.1" || address === "::1" || address === "::ffff:127.0.0.1") && LOCAL_NAMES.includes(host);
|
|
16
|
+
}
|
|
17
|
+
function handler(options) {
|
|
18
|
+
return (request, response) => {
|
|
19
|
+
const pathname = new URL(request.url ?? "/", `http://${HOST}`).pathname;
|
|
20
|
+
if (!fromThisMachine(request)) {
|
|
21
|
+
reply(response, 403, "text/plain", "this page answers only requests from this machine");
|
|
22
|
+
}
|
|
23
|
+
else if (request.method !== "GET") {
|
|
24
|
+
reply(response, 405, "text/plain", "only GET");
|
|
25
|
+
}
|
|
26
|
+
else if (pathname === "/version") {
|
|
27
|
+
reply(response, 200, "text/plain", options.fingerprint());
|
|
28
|
+
}
|
|
29
|
+
else if (pathname !== "/") {
|
|
30
|
+
reply(response, 404, "text/plain", "not found");
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
serveRoot(options, response);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function serveRoot(options, response) {
|
|
38
|
+
const version = options.fingerprint();
|
|
39
|
+
options
|
|
40
|
+
.page()
|
|
41
|
+
.then((html) => reply(response, 200, "text/html", html.replace('data-version=""', `data-version="${version}"`)))
|
|
42
|
+
.catch((error) => reply(response, 500, "text/plain", error instanceof Error ? error.message : String(error)));
|
|
43
|
+
}
|
|
44
|
+
function listen(server, port) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const failed = (error) => (error.code === "EADDRINUSE" ? resolve("in use") : reject(error));
|
|
47
|
+
server.once("error", failed);
|
|
48
|
+
server.listen(port, HOST, () => {
|
|
49
|
+
server.off("error", failed);
|
|
50
|
+
resolve("listening");
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
export async function startDashboardServer(options) {
|
|
55
|
+
const server = createServer(handler(options));
|
|
56
|
+
const attempts = options.port === 0 ? 1 : options.fallbacks + 1;
|
|
57
|
+
for (let offset = 0; offset < attempts; offset += 1) {
|
|
58
|
+
if ((await listen(server, options.port + offset)) === "listening") {
|
|
59
|
+
const address = server.address();
|
|
60
|
+
return {
|
|
61
|
+
port: typeof address === "object" && address !== null ? address.port : options.port + offset,
|
|
62
|
+
close: () => new Promise((resolve) => {
|
|
63
|
+
server.close(() => resolve());
|
|
64
|
+
server.closeAllConnections();
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`suwgate dashboard: the ports ${options.port} to ${options.port + options.fallbacks} are all in use: stop what holds them or pass --port <n>`);
|
|
70
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Catalog } from "./config.ts";
|
|
2
|
+
import type { DashboardInput } from "./dashboard.ts";
|
|
3
|
+
import type { RunRecord } from "./history.ts";
|
|
4
|
+
export declare const NOW: Date;
|
|
5
|
+
export declare function run(overrides: Partial<RunRecord>): RunRecord;
|
|
6
|
+
export declare const history: RunRecord[];
|
|
7
|
+
export declare const fakeCatalog: Catalog;
|
|
8
|
+
export declare function collected(overrides?: Partial<DashboardInput>): import("./dashboard.ts").DashboardData;
|
|
9
|
+
export declare const wiredHooks: {
|
|
10
|
+
read: (file: string) => string | null;
|
|
11
|
+
hooksPath: string;
|
|
12
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { collectDashboard } from "./dashboard.js";
|
|
2
|
+
export const NOW = new Date("2026-12-02T10:40:00.000Z");
|
|
3
|
+
export function run(overrides) {
|
|
4
|
+
return {
|
|
5
|
+
at: "2026-12-01T10:00:00.000Z",
|
|
6
|
+
mode: "quick",
|
|
7
|
+
source: "cli",
|
|
8
|
+
branch: "feature/x",
|
|
9
|
+
verdict: "green",
|
|
10
|
+
seconds: 5,
|
|
11
|
+
overBudget: false,
|
|
12
|
+
steps: [],
|
|
13
|
+
ran: ["core-guard", "quiet-guard"],
|
|
14
|
+
guards: {},
|
|
15
|
+
excepted: {},
|
|
16
|
+
ci: false,
|
|
17
|
+
...overrides,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export const history = [
|
|
21
|
+
run({ at: "2026-10-20T10:00:00.000Z", seconds: 4, branch: "master" }),
|
|
22
|
+
run({ at: "2026-11-30T10:00:00.000Z", seconds: 6, branch: "master" }),
|
|
23
|
+
run({
|
|
24
|
+
at: "2026-12-01T10:00:00.000Z",
|
|
25
|
+
verdict: "red",
|
|
26
|
+
seconds: 7,
|
|
27
|
+
guards: { "repository-layout": 2 },
|
|
28
|
+
excepted: { "core-guard": 3 },
|
|
29
|
+
ran: ["core-guard", "repository-layout", "quiet-guard"],
|
|
30
|
+
}),
|
|
31
|
+
run({
|
|
32
|
+
at: "2026-12-02T09:00:00.000Z",
|
|
33
|
+
verdict: "red",
|
|
34
|
+
seconds: 25,
|
|
35
|
+
overBudget: true,
|
|
36
|
+
branch: "feature/y",
|
|
37
|
+
steps: [
|
|
38
|
+
{ id: "lint", verdict: "pass", seconds: 3 },
|
|
39
|
+
{ id: "unit-tests", verdict: "fail", seconds: 12 },
|
|
40
|
+
],
|
|
41
|
+
}),
|
|
42
|
+
];
|
|
43
|
+
const config = {
|
|
44
|
+
profiles: ["library"],
|
|
45
|
+
directories: {},
|
|
46
|
+
guardSets: [],
|
|
47
|
+
add: [],
|
|
48
|
+
skip: [],
|
|
49
|
+
configure: {},
|
|
50
|
+
environment: {},
|
|
51
|
+
skippedGuards: [{ guard: "skipped-guard", reason: "not needed here" }],
|
|
52
|
+
exceptions: [{ guard: "core-guard", target: "docs/a.md", reason: "the doc names files of another repository" }],
|
|
53
|
+
settings: {},
|
|
54
|
+
};
|
|
55
|
+
const check = () => [];
|
|
56
|
+
export const fakeCatalog = {
|
|
57
|
+
guards: [
|
|
58
|
+
{ id: "core-guard", description: "d", profiles: "every", check },
|
|
59
|
+
{ id: "quiet-guard", description: "d", profiles: "every", check },
|
|
60
|
+
{ id: "repository-layout", description: "d", profiles: "every", set: "stack", check },
|
|
61
|
+
{ id: "skipped-guard", description: "d", profiles: "every", check },
|
|
62
|
+
{ id: "dotnet-only", description: "d", profiles: ["dotnet"], check },
|
|
63
|
+
],
|
|
64
|
+
dotnetGuards: [],
|
|
65
|
+
stepsFor: () => [{ id: "lint", title: "lint (eslint)", run: "true" }],
|
|
66
|
+
};
|
|
67
|
+
function dashboardInput(overrides = {}) {
|
|
68
|
+
return {
|
|
69
|
+
name: "owner/repo",
|
|
70
|
+
days: 30,
|
|
71
|
+
now: NOW,
|
|
72
|
+
runs: history,
|
|
73
|
+
config,
|
|
74
|
+
catalog: fakeCatalog,
|
|
75
|
+
wall: [
|
|
76
|
+
{ name: "pull request required before merging", state: "holds", detail: "" },
|
|
77
|
+
{ name: "force pushes blocked", state: "broken", detail: 'force pushes are not blocked: enable "Block force pushes"' },
|
|
78
|
+
],
|
|
79
|
+
wallCheckedAt: NOW,
|
|
80
|
+
live: false,
|
|
81
|
+
files: [],
|
|
82
|
+
read: () => null,
|
|
83
|
+
hooksPath: "",
|
|
84
|
+
criterionIds: () => [],
|
|
85
|
+
plugin: null,
|
|
86
|
+
garden: null,
|
|
87
|
+
...overrides,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
export function collected(overrides = {}) {
|
|
91
|
+
return collectDashboard(dashboardInput(overrides));
|
|
92
|
+
}
|
|
93
|
+
const WIRED = {
|
|
94
|
+
".claude/settings.json": "suwgate hook guard-edit, suwgate hook guard-pr, suwgate hook stop",
|
|
95
|
+
".githooks/pre-commit": "suwgate hook pre-commit",
|
|
96
|
+
".githooks/pre-push": "suwgate hook pre-push",
|
|
97
|
+
};
|
|
98
|
+
export const wiredHooks = { read: (file) => WIRED[file] ?? null, hooksPath: ".githooks" };
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { type Config } from "./config.ts";
|
|
2
2
|
import type { Guard, GuardException } from "./contract.ts";
|
|
3
|
+
export interface GuardTally {
|
|
4
|
+
ran: string[];
|
|
5
|
+
failed: Record<string, number>;
|
|
6
|
+
excepted: Record<string, number>;
|
|
7
|
+
}
|
|
3
8
|
interface GuardsSummary {
|
|
4
9
|
passed: boolean;
|
|
5
10
|
log: string;
|
|
11
|
+
tally: GuardTally;
|
|
6
12
|
}
|
|
7
13
|
interface GuardsInput {
|
|
8
14
|
root: string;
|
package/dist/gate/guardsStep.js
CHANGED
|
@@ -35,6 +35,10 @@ export async function runGuards(input) {
|
|
|
35
35
|
const lines = [];
|
|
36
36
|
let excepted = 0;
|
|
37
37
|
let failed = false;
|
|
38
|
+
const tally = { ran: input.guards.map((guard) => guard.id), failed: {}, excepted: {} };
|
|
39
|
+
const count = (counts, id) => {
|
|
40
|
+
counts[id] = (counts[id] ?? 0) + 1;
|
|
41
|
+
};
|
|
38
42
|
for (const guard of input.guards) {
|
|
39
43
|
const own = input.config.exceptions.filter((exception) => exception.guard === guard.id);
|
|
40
44
|
const merged = own.filter((exception) => (input.merged ?? []).some((done) => sameException(done, exception)));
|
|
@@ -45,15 +49,18 @@ export async function runGuards(input) {
|
|
|
45
49
|
if (exception === undefined) {
|
|
46
50
|
lines.push(shown(guard, finding));
|
|
47
51
|
failed = true;
|
|
52
|
+
count(tally.failed, guard.id);
|
|
48
53
|
}
|
|
49
54
|
else {
|
|
50
55
|
used.add(exception);
|
|
56
|
+
count(tally.excepted, guard.id);
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
excepted += used.size;
|
|
54
60
|
for (const stale of exceptions.filter((exception) => !used.has(exception))) {
|
|
55
61
|
lines.push(`${guard.id}: ${stale.target}: the exception in ${CONFIG_FILE}${stale.message === undefined ? "" : ` (message: ${stale.message})`} matches nothing any more: delete it`);
|
|
56
62
|
failed = true;
|
|
63
|
+
count(tally.failed, guard.id);
|
|
57
64
|
}
|
|
58
65
|
for (const done of merged) {
|
|
59
66
|
lines.push(`${guard.id}: ${done.target}: the one-time exception is on master now and excepts nothing: delete it from ${CONFIG_FILE}`);
|
|
@@ -63,5 +70,5 @@ export async function runGuards(input) {
|
|
|
63
70
|
lines.push(`${done.guard}: ${done.target}: the one-time exception is on master now and excepts nothing: delete it from ${CONFIG_FILE}`);
|
|
64
71
|
}
|
|
65
72
|
const header = `${input.guards.length} guard${input.guards.length === 1 ? "" : "s"}, ${excepted} findings excepted in ${CONFIG_FILE}`;
|
|
66
|
-
return { passed: !failed, log: [header, ...lines].join("\n") };
|
|
73
|
+
return { passed: !failed, log: [header, ...lines].join("\n"), tally };
|
|
67
74
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare const HISTORY_FILE = "suwgate-runs.jsonl";
|
|
2
|
+
export type RunMode = "quick" | "full" | "ship";
|
|
3
|
+
export type RunSource = "cli" | "stop" | "pre-commit";
|
|
4
|
+
interface StepRecord {
|
|
5
|
+
id: string;
|
|
6
|
+
verdict: "pass" | "fail" | "skip";
|
|
7
|
+
seconds: number;
|
|
8
|
+
}
|
|
9
|
+
export interface RunRecord {
|
|
10
|
+
at: string;
|
|
11
|
+
mode: RunMode;
|
|
12
|
+
source: RunSource;
|
|
13
|
+
branch: string;
|
|
14
|
+
verdict: "green" | "red";
|
|
15
|
+
seconds: number;
|
|
16
|
+
overBudget: boolean;
|
|
17
|
+
steps: StepRecord[];
|
|
18
|
+
ran: string[];
|
|
19
|
+
guards: Record<string, number>;
|
|
20
|
+
excepted: Record<string, number>;
|
|
21
|
+
ci: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare function inCommonDir(root: string, file: string): string | null;
|
|
24
|
+
export declare function historyFile(root: string): string | null;
|
|
25
|
+
export declare function branchOf(root: string): string;
|
|
26
|
+
export declare function recordRun(root: string, record: RunRecord, maxBytes?: number): void;
|
|
27
|
+
export declare function readRuns(root: string): RunRecord[];
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { appendFileSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const HISTORY_FILE = "suwgate-runs.jsonl";
|
|
5
|
+
const MAX_HISTORY_BYTES = 5 * 1024 * 1024;
|
|
6
|
+
function git(root, args) {
|
|
7
|
+
try {
|
|
8
|
+
return execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return "";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function inCommonDir(root, file) {
|
|
15
|
+
const common = git(root, ["rev-parse", "--git-common-dir"]);
|
|
16
|
+
return common === "" ? null : path.join(path.resolve(root, common), file);
|
|
17
|
+
}
|
|
18
|
+
export function historyFile(root) {
|
|
19
|
+
return inCommonDir(root, HISTORY_FILE);
|
|
20
|
+
}
|
|
21
|
+
export function branchOf(root) {
|
|
22
|
+
return git(root, ["symbolic-ref", "-q", "--short", "HEAD"]) || "HEAD";
|
|
23
|
+
}
|
|
24
|
+
function dropOldestHalf(file) {
|
|
25
|
+
const lines = readFileSync(file, "utf8")
|
|
26
|
+
.split("\n")
|
|
27
|
+
.filter((line) => line !== "");
|
|
28
|
+
writeFileSync(file, lines
|
|
29
|
+
.slice(Math.ceil(lines.length / 2))
|
|
30
|
+
.map((line) => `${line}\n`)
|
|
31
|
+
.join(""));
|
|
32
|
+
}
|
|
33
|
+
export function recordRun(root, record, maxBytes = MAX_HISTORY_BYTES) {
|
|
34
|
+
try {
|
|
35
|
+
const file = historyFile(root);
|
|
36
|
+
if (file === null)
|
|
37
|
+
return;
|
|
38
|
+
try {
|
|
39
|
+
if (statSync(file).size > maxBytes)
|
|
40
|
+
dropOldestHalf(file);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error.code !== "ENOENT")
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
appendFileSync(file, `${JSON.stringify(record)}\n`);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export function readRuns(root) {
|
|
53
|
+
const file = historyFile(root);
|
|
54
|
+
if (file === null)
|
|
55
|
+
return [];
|
|
56
|
+
let text;
|
|
57
|
+
try {
|
|
58
|
+
text = readFileSync(file, "utf8");
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
return text.split("\n").flatMap((line) => {
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(line);
|
|
66
|
+
return typeof parsed === "object" && parsed !== null && "at" in parsed ? [parsed] : [];
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
package/dist/gate/plan.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Catalog, type Config, type Environment } from "./config.ts";
|
|
2
2
|
import type { GuardException, Step } from "./contract.ts";
|
|
3
|
+
import { type GuardTally } from "./guardsStep.ts";
|
|
3
4
|
export declare const GUARDS_STEP = "guards";
|
|
4
5
|
export interface Outcome {
|
|
5
6
|
passed: boolean;
|
|
6
7
|
log: string;
|
|
8
|
+
guards?: GuardTally;
|
|
7
9
|
}
|
|
8
10
|
export interface PlannedStep {
|
|
9
11
|
id: string;
|
package/dist/gate/plan.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { accessSync, constants } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
+
import { takesGuard } from "./config.js";
|
|
3
4
|
import { runGuards } from "./guardsStep.js";
|
|
4
5
|
const DEFAULT_LIMIT_SECONDS = 300;
|
|
5
6
|
export const GUARDS_STEP = "guards";
|
|
@@ -25,7 +26,8 @@ function addedSteps(config) {
|
|
|
25
26
|
}));
|
|
26
27
|
}
|
|
27
28
|
export function allSteps(config, catalog) {
|
|
28
|
-
|
|
29
|
+
const holdout = config.holdout === undefined ? [] : [catalog.holdoutStep?.(config.holdout)].filter((found) => found !== undefined);
|
|
30
|
+
return [...catalog.stepsFor(config.profiles), ...holdout, ...addedSteps(config)];
|
|
29
31
|
}
|
|
30
32
|
function chosenRun(step, quick) {
|
|
31
33
|
if (!quick)
|
|
@@ -36,7 +38,8 @@ function chosenRun(step, quick) {
|
|
|
36
38
|
}
|
|
37
39
|
function activeGuards(input) {
|
|
38
40
|
const skipped = input.config.skippedGuards.map((entry) => entry.guard);
|
|
39
|
-
return input.catalog.guards.filter((guard) => (
|
|
41
|
+
return input.catalog.guards.filter((guard) => takesGuard(input.config, guard) &&
|
|
42
|
+
(guard.profiles === "every" || guard.profiles.some((profile) => input.config.profiles.includes(profile))) &&
|
|
40
43
|
(!input.quick || guard.quick !== false) &&
|
|
41
44
|
!skipped.includes(guard.id));
|
|
42
45
|
}
|
|
@@ -61,7 +64,7 @@ function guardsStep(input) {
|
|
|
61
64
|
config: input.config,
|
|
62
65
|
merged: input.merged ?? [],
|
|
63
66
|
});
|
|
64
|
-
return { passed: summary.passed, log: summary.log };
|
|
67
|
+
return { passed: summary.passed, log: summary.log, guards: summary.tally };
|
|
65
68
|
},
|
|
66
69
|
};
|
|
67
70
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare function git(root: string, args: readonly string[]): string | null;
|
|
1
2
|
export declare function repositoryRoot(cwd: string): string | null;
|
|
2
3
|
export declare function repositoryFiles(root: string): string[];
|
|
3
4
|
export declare function changedFiles(root: string): string[];
|
package/dist/gate/repository.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
function git(root, args) {
|
|
4
|
+
export function git(root, args) {
|
|
5
5
|
try {
|
|
6
6
|
return execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] });
|
|
7
7
|
}
|
package/dist/gate/runner.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { GuardTally } from "./guardsStep.ts";
|
|
1
2
|
import type { PlannedStep } from "./plan.ts";
|
|
2
3
|
type Status = "pass" | "fail" | "skip";
|
|
3
4
|
export interface StepResult {
|
|
@@ -5,6 +6,8 @@ export interface StepResult {
|
|
|
5
6
|
status: Status;
|
|
6
7
|
seconds: number;
|
|
7
8
|
log: string;
|
|
9
|
+
elapsed?: number;
|
|
10
|
+
guards?: GuardTally;
|
|
8
11
|
}
|
|
9
12
|
export declare function runSteps(steps: readonly PlannedStep[], env: NodeJS.ProcessEnv, stepLimit: number | null): Promise<StepResult[]>;
|
|
10
13
|
export {};
|
package/dist/gate/runner.js
CHANGED
|
@@ -76,11 +76,14 @@ export async function runSteps(steps, env, stepLimit) {
|
|
|
76
76
|
const outcome = step.command !== undefined
|
|
77
77
|
? await runCommand(step.command, limit, stepEnv(env, step.environment))
|
|
78
78
|
: await (step.inline ?? (() => Promise.resolve({ passed: true, log: "" })))();
|
|
79
|
+
const elapsed = (performance.now() - began) / 1000;
|
|
79
80
|
return {
|
|
80
81
|
step,
|
|
81
82
|
status: outcome.passed ? "pass" : "fail",
|
|
82
|
-
seconds: Math.round(
|
|
83
|
+
seconds: Math.round(elapsed),
|
|
84
|
+
elapsed,
|
|
83
85
|
log: outcome.log,
|
|
86
|
+
...(outcome.guards === undefined ? {} : { guards: outcome.guards }),
|
|
84
87
|
};
|
|
85
88
|
})();
|
|
86
89
|
running.set(step.id, result);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { RunRecord } from "./history.ts";
|
|
2
|
+
export interface Stats {
|
|
3
|
+
days: number;
|
|
4
|
+
runs: number;
|
|
5
|
+
modes: Record<string, {
|
|
6
|
+
runs: number;
|
|
7
|
+
red: number;
|
|
8
|
+
}>;
|
|
9
|
+
quick: {
|
|
10
|
+
runs: number;
|
|
11
|
+
medianSeconds: number;
|
|
12
|
+
slowestSeconds: number;
|
|
13
|
+
overBudget: number;
|
|
14
|
+
} | null;
|
|
15
|
+
slowestSteps: {
|
|
16
|
+
id: string;
|
|
17
|
+
medianSeconds: number;
|
|
18
|
+
}[];
|
|
19
|
+
guards: {
|
|
20
|
+
id: string;
|
|
21
|
+
findings: number;
|
|
22
|
+
excepted: number;
|
|
23
|
+
}[];
|
|
24
|
+
quiet: string[];
|
|
25
|
+
}
|
|
26
|
+
export declare function summarize(history: readonly RunRecord[], days: number, now: Date): Stats;
|
|
27
|
+
export declare function formatStats(stats: Stats): string;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const SLOWEST_STEPS = 5;
|
|
2
|
+
const MODES = ["quick", "full", "ship"];
|
|
3
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
4
|
+
function median(values) {
|
|
5
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
6
|
+
const middle = Math.floor(sorted.length / 2);
|
|
7
|
+
const value = sorted.length % 2 === 1 ? (sorted[middle] ?? 0) : ((sorted[middle - 1] ?? 0) + (sorted[middle] ?? 0)) / 2;
|
|
8
|
+
return Math.round(value * 10) / 10;
|
|
9
|
+
}
|
|
10
|
+
function sumInto(total, counts) {
|
|
11
|
+
for (const [id, count] of Object.entries(counts))
|
|
12
|
+
total.set(id, (total.get(id) ?? 0) + count);
|
|
13
|
+
}
|
|
14
|
+
export function summarize(history, days, now) {
|
|
15
|
+
const since = now.getTime() - days * DAY_MS;
|
|
16
|
+
const runs = history.filter((run) => Date.parse(run.at) >= since);
|
|
17
|
+
const modes = Object.fromEntries(MODES.flatMap((mode) => {
|
|
18
|
+
const own = runs.filter((run) => run.mode === mode);
|
|
19
|
+
return own.length === 0 ? [] : [[mode, { runs: own.length, red: own.filter((run) => run.verdict === "red").length }]];
|
|
20
|
+
}));
|
|
21
|
+
const quickRuns = runs.filter((run) => run.mode === "quick");
|
|
22
|
+
const stepSeconds = new Map();
|
|
23
|
+
for (const step of runs.flatMap((run) => run.steps).filter((step) => step.verdict !== "skip")) {
|
|
24
|
+
stepSeconds.set(step.id, [...(stepSeconds.get(step.id) ?? []), step.seconds]);
|
|
25
|
+
}
|
|
26
|
+
const findings = new Map();
|
|
27
|
+
const excepted = new Map();
|
|
28
|
+
for (const run of runs) {
|
|
29
|
+
sumInto(findings, run.guards);
|
|
30
|
+
sumInto(excepted, run.excepted);
|
|
31
|
+
}
|
|
32
|
+
const ran = new Set(runs.flatMap((run) => run.ran));
|
|
33
|
+
return {
|
|
34
|
+
days,
|
|
35
|
+
runs: runs.length,
|
|
36
|
+
modes,
|
|
37
|
+
quick: quickRuns.length === 0
|
|
38
|
+
? null
|
|
39
|
+
: {
|
|
40
|
+
runs: quickRuns.length,
|
|
41
|
+
medianSeconds: median(quickRuns.map((run) => run.seconds)),
|
|
42
|
+
slowestSeconds: Math.max(...quickRuns.map((run) => run.seconds)),
|
|
43
|
+
overBudget: quickRuns.filter((run) => run.overBudget).length,
|
|
44
|
+
},
|
|
45
|
+
slowestSteps: [...stepSeconds]
|
|
46
|
+
.map(([id, seconds]) => ({ id, medianSeconds: median(seconds) }))
|
|
47
|
+
.sort((a, b) => b.medianSeconds - a.medianSeconds || a.id.localeCompare(b.id))
|
|
48
|
+
.slice(0, SLOWEST_STEPS),
|
|
49
|
+
guards: [...new Set([...findings.keys(), ...excepted.keys()])]
|
|
50
|
+
.map((id) => ({ id, findings: findings.get(id) ?? 0, excepted: excepted.get(id) ?? 0 }))
|
|
51
|
+
.sort((a, b) => b.findings - a.findings || b.excepted - a.excepted || a.id.localeCompare(b.id)),
|
|
52
|
+
quiet: [...ran].filter((id) => !findings.has(id) && !excepted.has(id)).sort(),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function percent(part, whole) {
|
|
56
|
+
return `${Math.round((part / whole) * 100)}%`;
|
|
57
|
+
}
|
|
58
|
+
export function formatStats(stats) {
|
|
59
|
+
if (stats.runs === 0) {
|
|
60
|
+
return `suwgate stats: no gate run recorded in the last ${stats.days} days: run suwgate check --quick, or widen the period with --days <n>`;
|
|
61
|
+
}
|
|
62
|
+
const lines = [`The gate in the last ${stats.days} days: ${stats.runs} run${stats.runs === 1 ? "" : "s"}`, ""];
|
|
63
|
+
for (const [mode, own] of Object.entries(stats.modes)) {
|
|
64
|
+
lines.push(` ${mode.padEnd(6)} ${String(own.runs).padStart(4)} run${own.runs === 1 ? " " : "s"}, ${own.red} red (${percent(own.red, own.runs)})`);
|
|
65
|
+
}
|
|
66
|
+
if (stats.quick !== null) {
|
|
67
|
+
lines.push("", `Quick gate: median ${stats.quick.medianSeconds}s, slowest ${stats.quick.slowestSeconds}s, ${stats.quick.overBudget} over budget`);
|
|
68
|
+
}
|
|
69
|
+
if (stats.slowestSteps.length > 0) {
|
|
70
|
+
lines.push("", "Slowest steps (median seconds):");
|
|
71
|
+
for (const step of stats.slowestSteps)
|
|
72
|
+
lines.push(` ${String(step.medianSeconds).padStart(6)}s ${step.id}`);
|
|
73
|
+
}
|
|
74
|
+
if (stats.guards.length > 0) {
|
|
75
|
+
lines.push("", "Guards by findings (failed, excepted):");
|
|
76
|
+
for (const guard of stats.guards)
|
|
77
|
+
lines.push(` ${String(guard.findings).padStart(5)} ${String(guard.excepted).padStart(5)} ${guard.id}`);
|
|
78
|
+
}
|
|
79
|
+
if (stats.quiet.length > 0) {
|
|
80
|
+
lines.push("", `Guards with no finding in ${stats.days} days:`, ...stats.quiet.map((id) => ` ${id}`));
|
|
81
|
+
lines.push("Candidates for review: a quiet guard may still be worth keeping when it guards something rare.");
|
|
82
|
+
}
|
|
83
|
+
return lines.join("\n");
|
|
84
|
+
}
|
|
@@ -79,5 +79,6 @@ export const commentsEslint = {
|
|
|
79
79
|
id: "comments-eslint",
|
|
80
80
|
description: `ESLint lints every TypeScript and JavaScript file but the web app's generated API types and refuses its comments with ${RULE} as an error`,
|
|
81
81
|
profiles: LINTED_PROFILES,
|
|
82
|
+
set: "stack",
|
|
82
83
|
check: async (context) => (await Promise.all(lintedDirectories(context).map((directory) => findingsIn(context, directory)))).flat(),
|
|
83
84
|
};
|
|
@@ -77,6 +77,7 @@ export function commentGuard(guard) {
|
|
|
77
77
|
id: guard.id,
|
|
78
78
|
description: guard.description,
|
|
79
79
|
profiles: guard.profiles,
|
|
80
|
+
set: "stack",
|
|
80
81
|
check: (context) => context.files
|
|
81
82
|
.filter((file) => isScanned(file) && guard.scans(file))
|
|
82
83
|
.flatMap((file) => guard.commentsIn(file, context.read(file)).map((comment) => commentFinding(file, comment))),
|
|
@@ -26,5 +26,6 @@ export const noInPlaceSwitches = {
|
|
|
26
26
|
id: "no-in-place-switches",
|
|
27
27
|
description: "No source file switches the type checker, a lint rule or an analyzer off in place (@ts-ignore, @ts-nocheck, eslint-disable, an eslint config comment, #pragma warning disable)",
|
|
28
28
|
profiles: ["web", "library", "dotnet"],
|
|
29
|
+
set: "stack",
|
|
29
30
|
check: (context) => context.files.filter(isScanned).flatMap((file) => switchesIn(file, context.read(file))),
|
|
30
31
|
};
|
|
@@ -9,6 +9,7 @@ export const connectionHost = {
|
|
|
9
9
|
id: "connection-host",
|
|
10
10
|
description: "No connection string names the host by its name instead of its address (write Host=127.0.0.1)",
|
|
11
11
|
profiles: ["database"],
|
|
12
|
+
set: "stack",
|
|
12
13
|
check(context) {
|
|
13
14
|
const findings = [];
|
|
14
15
|
for (const file of context.files.filter(scanned)) {
|
|
@@ -46,6 +46,7 @@ export const appsettingsBound = {
|
|
|
46
46
|
id: "appsettings-bound",
|
|
47
47
|
description: "Every section of appsettings*.json is read by the code (an options class bound to it, by a string or a const string), so no setting is dead",
|
|
48
48
|
profiles: ["dotnet"],
|
|
49
|
+
set: "stack",
|
|
49
50
|
check(context) {
|
|
50
51
|
const read = sectionsRead(dotnetFiles(context, CSHARP).map((file) => context.read(file)));
|
|
51
52
|
const problems = [];
|
|
@@ -21,6 +21,7 @@ export const bannedNugetPackages = {
|
|
|
21
21
|
id: "banned-nuget-packages",
|
|
22
22
|
description: "No project references or versions a NuGet package that hides calls from grep, mocks the real thing or duplicates the platform",
|
|
23
23
|
profiles: ["dotnet"],
|
|
24
|
+
set: "stack",
|
|
24
25
|
settingsSchema: {
|
|
25
26
|
type: "object",
|
|
26
27
|
additionalProperties: false,
|
|
@@ -14,6 +14,7 @@ export const openApiVersions = {
|
|
|
14
14
|
id: "openapi-versions",
|
|
15
15
|
description: `${TOOL}, which writes the OpenAPI contract at build, is pinned in Directory.Packages.props to the same version as ${LIBRARY}, whose OpenAPI library it loads`,
|
|
16
16
|
profiles: ["dotnet"],
|
|
17
|
+
set: "stack",
|
|
17
18
|
check(context) {
|
|
18
19
|
const file = centralPackagesFile(context);
|
|
19
20
|
if (!context.files.includes(file))
|
|
@@ -22,6 +22,7 @@ export const suwbackkitWiring = {
|
|
|
22
22
|
id: "suwbackkit-wiring",
|
|
23
23
|
description: "A web server that references Suwbackkit.Server calls AddServerDefaults and MapServerDefaults, so the kit is wired, not only installed",
|
|
24
24
|
profiles: ["dotnet"],
|
|
25
|
+
set: "stack",
|
|
25
26
|
check: (context) => dotnetFiles(context, PROJECT)
|
|
26
27
|
.filter((project) => {
|
|
27
28
|
const text = context.read(project);
|
|
@@ -3,6 +3,7 @@ export const unusedNugetVersions = {
|
|
|
3
3
|
id: "unused-nuget-versions",
|
|
4
4
|
description: "Every package version of Directory.Packages.props is referenced by a project",
|
|
5
5
|
profiles: ["dotnet"],
|
|
6
|
+
set: "stack",
|
|
6
7
|
check(context) {
|
|
7
8
|
const central = centralPackagesFile(context);
|
|
8
9
|
if (!context.files.includes(central)) {
|
|
@@ -21,6 +21,7 @@ export const bannedNpmPackages = {
|
|
|
21
21
|
id: "banned-npm-packages",
|
|
22
22
|
description: "No package.json depends on a banned package (component kits, CSS-in-JS, lodash, moment, jQuery, classnames, and what settings add)",
|
|
23
23
|
profiles: ["web", "library"],
|
|
24
|
+
set: "stack",
|
|
24
25
|
settingsSchema: {
|
|
25
26
|
type: "object",
|
|
26
27
|
additionalProperties: false,
|
|
@@ -43,6 +43,7 @@ export const composePorts = {
|
|
|
43
43
|
id: "compose-ports",
|
|
44
44
|
description: "Every compose file publishes its ports on the loopback (127.0.0.1) only, never on every network interface",
|
|
45
45
|
profiles: "every",
|
|
46
|
+
set: "stack",
|
|
46
47
|
check(context) {
|
|
47
48
|
return context.files
|
|
48
49
|
.filter((file) => COMPOSE_FILE.test(baseName(file)))
|
|
@@ -68,6 +68,7 @@ export const dependencyDirection = {
|
|
|
68
68
|
id: "dependency-direction",
|
|
69
69
|
description: "Dependencies point one way: a library never depends at runtime on an application (named in settings, or taken from a local path), and no repository depends on a package its settings forbid",
|
|
70
70
|
profiles: "every",
|
|
71
|
+
set: "stack",
|
|
71
72
|
settingsSchema: {
|
|
72
73
|
type: "object",
|
|
73
74
|
additionalProperties: false,
|