@zgeoff/chex 0.8.7 → 0.8.9
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/README.md +4 -4
- package/lefthook.yml +22 -5
- package/package.json +1 -1
- package/src/ci-plan/build-ci-plan-output.ts +1 -37
- package/src/ci-plan/collect-shared-checks.ts +1 -41
- package/src/ci-plan/find-chex-args.ts +40 -0
- package/src/ci-plan/parse-package-scripts.ts +36 -0
- package/src/cli/check-names.ts +1 -0
- package/src/cli.ts +2 -0
- package/src/typecheck/pick-typecheck-plan.ts +172 -0
- package/src/typecheck/run-typecheck-check.ts +245 -0
- package/src/workflows/collect-allowances.ts +101 -0
- package/src/workflows/collect-concurrency-findings.ts +1 -1
- package/src/workflows/collect-hook-findings.ts +13 -0
- package/src/workflows/collect-moved-repo-findings.ts +34 -0
- package/src/workflows/collect-run-commands.ts +18 -0
- package/src/workflows/collect-runner-labels.ts +8 -0
- package/src/workflows/collect-steps.ts +9 -0
- package/src/workflows/constants.ts +4 -0
- package/src/workflows/find-heavy-runner.ts +20 -0
- package/src/workflows/is-list.ts +4 -0
- package/src/workflows/parse-repo-slug.ts +7 -0
- package/src/workflows/pick-checked-paths.ts +33 -0
- package/src/workflows/pick-repo-move.ts +28 -0
- package/src/workflows/read-repo-slug.ts +12 -0
- package/src/workflows/read-repo-state.ts +57 -0
- package/src/workflows/rules/build-rule-finding.ts +10 -0
- package/src/workflows/rules/collect-condition-terms.ts +116 -0
- package/src/workflows/rules/collect-hook-check-findings.ts +84 -0
- package/src/workflows/rules/collect-main-only-job-findings.ts +123 -0
- package/src/workflows/rules/collect-main-run-concurrency-findings.ts +77 -0
- package/src/workflows/rules/collect-main-run-shape-findings.ts +161 -0
- package/src/workflows/rules/collect-needs.ts +13 -0
- package/src/workflows/rules/collect-path-filter-findings.ts +56 -0
- package/src/workflows/rules/collect-pr-gate-findings.ts +72 -0
- package/src/workflows/rules/collect-publish-findings.ts +83 -0
- package/src/workflows/rules/collect-release-jobs.ts +55 -0
- package/src/workflows/rules/collect-release-shape-findings.ts +67 -0
- package/src/workflows/rules/find-file-filter.ts +91 -0
- package/src/workflows/rules/find-image-build.ts +59 -0
- package/src/workflows/rules/find-job-environment.ts +9 -0
- package/src/workflows/rules/find-matrix-runner.ts +38 -0
- package/src/workflows/rules/find-shipped-build.ts +36 -0
- package/src/workflows/rules/is-arm-label.ts +5 -0
- package/src/workflows/rules/is-release-condition.ts +19 -0
- package/src/workflows/rules/is-shared-check-script.ts +22 -0
- package/src/workflows/rules/is-shared-release-workflow.ts +14 -0
- package/src/workflows/rules/is-workflow-call.ts +12 -0
- package/src/workflows/rules/types.ts +31 -0
- package/src/workflows/run-workflows-check.ts +60 -55
- package/src/workflows/to-unwrapped-command.ts +28 -0
- package/src/workflows/to-workflow-findings.ts +24 -0
- package/src/workflows/types.ts +68 -0
- package/src/workflows/workflow-context.ts +0 -24
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { isRecord, readOptionalText } from '@zgeoff/std';
|
|
4
|
+
import { match } from 'ts-pattern';
|
|
5
|
+
import { readBranchFiles } from '../branch/read-branch-files.ts';
|
|
6
|
+
import type { RunProgram } from '../program/run-program.ts';
|
|
7
|
+
import { runProgram } from '../program/run-program.ts';
|
|
8
|
+
import type { BranchFiles, TypecheckScope, WorkspacePackage } from './pick-typecheck-plan.ts';
|
|
9
|
+
import { pickTypecheckPlan } from './pick-typecheck-plan.ts';
|
|
10
|
+
|
|
11
|
+
export interface TypecheckCheckOptions {
|
|
12
|
+
readonly cwd: string;
|
|
13
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
14
|
+
readonly runProgram: RunProgram;
|
|
15
|
+
|
|
16
|
+
// Returns undefined for a file that does not exist.
|
|
17
|
+
readonly readText: (path: string) => Promise<string | undefined>;
|
|
18
|
+
|
|
19
|
+
readonly probeFile: (path: string) => boolean;
|
|
20
|
+
|
|
21
|
+
// Runs a command with this process's output streams, and returns its exit code.
|
|
22
|
+
readonly runScript: (
|
|
23
|
+
argv: readonly string[],
|
|
24
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
25
|
+
) => Promise<number>;
|
|
26
|
+
|
|
27
|
+
readonly writeOutput: (text: string) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Returns the exit code of the typecheck it runs, and 0 when it skips.
|
|
31
|
+
export async function runTypecheckCheck(options: Readonly<TypecheckCheckOptions>): Promise<number> {
|
|
32
|
+
const scripts = await readPackageScripts(options);
|
|
33
|
+
|
|
34
|
+
const script = scripts['typecheck'] ?? null;
|
|
35
|
+
|
|
36
|
+
const globalDependencies = await readGlobalDependencies(options);
|
|
37
|
+
|
|
38
|
+
const plan = pickTypecheckPlan({
|
|
39
|
+
branchFiles: readTypecheckBranchFiles(options),
|
|
40
|
+
script,
|
|
41
|
+
scope: readTypecheckScope(options, scripts, globalDependencies),
|
|
42
|
+
globalDependencies,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return match(plan)
|
|
46
|
+
.with({ kind: 'skip' }, (skip) => {
|
|
47
|
+
options.writeOutput(`typecheck: skipped, because ${skip.reason}\n`);
|
|
48
|
+
|
|
49
|
+
return Promise.resolve(0);
|
|
50
|
+
})
|
|
51
|
+
.with({ kind: 'whole' }, (whole) => {
|
|
52
|
+
options.writeOutput(`typecheck: the whole repo, because ${whole.reason}\n`);
|
|
53
|
+
|
|
54
|
+
return options.runScript(['bun', 'run', 'typecheck'], options.env);
|
|
55
|
+
})
|
|
56
|
+
.with({ kind: 'scoped' }, (scoped) => {
|
|
57
|
+
options.writeOutput(
|
|
58
|
+
`typecheck: ${scoped.packageNames.join(', ')} and the packages that depend on them\n`,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// bun run puts the repo's node_modules/.bin on the PATH of a script, so this run does too.
|
|
62
|
+
const searchPath = [join(options.cwd, 'node_modules/.bin'), options.env['PATH']]
|
|
63
|
+
.filter((entry) => entry !== undefined && entry !== '')
|
|
64
|
+
.join(':');
|
|
65
|
+
|
|
66
|
+
return options.runScript(['sh', '-c', scoped.command], {
|
|
67
|
+
...options.env,
|
|
68
|
+
PATH: searchPath,
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.exhaustive();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function readPackageScripts(
|
|
75
|
+
options: Readonly<TypecheckCheckOptions>,
|
|
76
|
+
): Promise<Readonly<Record<string, string>>> {
|
|
77
|
+
const text = await options.readText(join(options.cwd, 'package.json'));
|
|
78
|
+
|
|
79
|
+
if (text === undefined) {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const manifest: unknown = JSON.parse(text);
|
|
84
|
+
const scripts = isRecord(manifest) ? manifest['scripts'] : undefined;
|
|
85
|
+
|
|
86
|
+
return isRecord(scripts)
|
|
87
|
+
? Object.fromEntries(
|
|
88
|
+
Object.entries(scripts).flatMap(([name, body]) =>
|
|
89
|
+
typeof body === 'string' ? [[name, body]] : [],
|
|
90
|
+
),
|
|
91
|
+
)
|
|
92
|
+
: {};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// turbo reads turbo.json, or turbo.jsonc when the repo has no turbo.json. Returns null for a
|
|
96
|
+
// config chex cannot read.
|
|
97
|
+
async function readGlobalDependencies(
|
|
98
|
+
options: Readonly<TypecheckCheckOptions>,
|
|
99
|
+
): Promise<string[] | null> {
|
|
100
|
+
const text = await readTurboConfigText(options);
|
|
101
|
+
|
|
102
|
+
if (text === null) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (text === undefined) {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
const config: unknown = Bun.JSONC.parse(text);
|
|
112
|
+
const entries = isRecord(config) ? (config['globalDependencies'] ?? []) : null;
|
|
113
|
+
|
|
114
|
+
return Array.isArray(entries) && entries.every((entry) => typeof entry === 'string')
|
|
115
|
+
? entries
|
|
116
|
+
: null;
|
|
117
|
+
} catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// A config chex cannot read, such as a folder named turbo.jsonc, returns null.
|
|
123
|
+
async function readTurboConfigText(
|
|
124
|
+
options: Readonly<TypecheckCheckOptions>,
|
|
125
|
+
): Promise<string | undefined | null> {
|
|
126
|
+
try {
|
|
127
|
+
const json = await options.readText(join(options.cwd, 'turbo.json'));
|
|
128
|
+
|
|
129
|
+
if (json !== undefined) {
|
|
130
|
+
return json;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return await options.readText(join(options.cwd, 'turbo.jsonc'));
|
|
134
|
+
} catch {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// A deleted file can break the files that import it, so the list keeps deletions.
|
|
140
|
+
function readTypecheckBranchFiles(options: Readonly<TypecheckCheckOptions>): BranchFiles {
|
|
141
|
+
try {
|
|
142
|
+
return {
|
|
143
|
+
kind: 'read',
|
|
144
|
+
files: readBranchFiles({ ...options, withDeleted: true }),
|
|
145
|
+
};
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return {
|
|
148
|
+
kind: 'unread',
|
|
149
|
+
reason: `chex cannot read the branch's files: ${error instanceof Error ? error.message : String(error)}`,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function readTypecheckScope(
|
|
155
|
+
options: Readonly<TypecheckCheckOptions>,
|
|
156
|
+
scripts: Readonly<Record<string, string>>,
|
|
157
|
+
globalDependencies: readonly string[] | null,
|
|
158
|
+
): TypecheckScope {
|
|
159
|
+
if (!(scripts['typecheck'] ?? '').includes('turbo')) {
|
|
160
|
+
return { kind: 'none', reason: 'the typecheck script does not run turbo' };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// bun run typecheck runs these around the script, and a narrowed run calls the script itself.
|
|
164
|
+
if (scripts['pretypecheck'] !== undefined || scripts['posttypecheck'] !== undefined) {
|
|
165
|
+
return { kind: 'none', reason: 'the repo has a pretypecheck or posttypecheck script' };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const turboPath = join(options.cwd, 'node_modules/.bin/turbo');
|
|
169
|
+
|
|
170
|
+
if (!options.probeFile(turboPath)) {
|
|
171
|
+
return { kind: 'none', reason: 'turbo is not installed' };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (globalDependencies === null) {
|
|
175
|
+
return { kind: 'none', reason: "chex cannot read the turbo config's globalDependencies" };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const packages = readTurboPackages(options, turboPath);
|
|
179
|
+
|
|
180
|
+
if (packages === null) {
|
|
181
|
+
return { kind: 'none', reason: 'turbo ls cannot list the workspace packages' };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (packages.length < 2) {
|
|
185
|
+
return { kind: 'none', reason: 'the repo has one package' };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return { kind: 'turbo', packages };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function readTurboPackages(
|
|
192
|
+
options: Readonly<TypecheckCheckOptions>,
|
|
193
|
+
turboPath: string,
|
|
194
|
+
): WorkspacePackage[] | null {
|
|
195
|
+
const result = options.runProgram([turboPath, 'ls', '--output=json'], {
|
|
196
|
+
cwd: options.cwd,
|
|
197
|
+
env: options.env,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (result.exitCode !== 0) {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const listing: unknown = JSON.parse(result.stdout);
|
|
206
|
+
const packages = isRecord(listing) ? listing['packages'] : undefined;
|
|
207
|
+
const items = isRecord(packages) ? packages['items'] : undefined;
|
|
208
|
+
|
|
209
|
+
if (!Array.isArray(items)) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const parsed = items.flatMap((item: unknown) =>
|
|
214
|
+
isRecord(item) && typeof item['name'] === 'string' && typeof item['path'] === 'string'
|
|
215
|
+
? [{ name: item['name'], path: item['path'].replace(/\/$/u, '') }]
|
|
216
|
+
: [],
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
return parsed.length === items.length ? parsed : null;
|
|
220
|
+
} catch {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Returns the exit code: the typecheck's own, 0 when it skips, and 2 when it cannot run.
|
|
226
|
+
export async function runTypecheckCommand(): Promise<number> {
|
|
227
|
+
try {
|
|
228
|
+
return await runTypecheckCheck({
|
|
229
|
+
cwd: process.cwd(),
|
|
230
|
+
env: process.env,
|
|
231
|
+
runProgram,
|
|
232
|
+
readText: readOptionalText,
|
|
233
|
+
probeFile: existsSync,
|
|
234
|
+
runScript: (argv, env) =>
|
|
235
|
+
Bun.spawn([...argv], { env: { ...env }, stdio: ['inherit', 'inherit', 'inherit'] }).exited,
|
|
236
|
+
writeOutput: (text) => {
|
|
237
|
+
process.stdout.write(text);
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
} catch (error) {
|
|
241
|
+
process.stderr.write(`typecheck: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
242
|
+
|
|
243
|
+
return 2;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { LineCounter, Parser } from 'yaml';
|
|
2
|
+
import { ruleNames } from './rules/types.ts';
|
|
3
|
+
import type { Allowances } from './types.ts';
|
|
4
|
+
|
|
5
|
+
const commentPattern = /^#\s*chex-allow\b(?<rest>.*)$/u;
|
|
6
|
+
const entryPattern = /^\s+(?<name>[\w-]+)\s*:\s*(?:\S.*)$/u;
|
|
7
|
+
const nameOnlyPattern = /^\s+(?<name>[\w-]+)\s*:?\s*$/u;
|
|
8
|
+
|
|
9
|
+
// A comment covers the key on its own line, or the line below when the comment stands alone. Only
|
|
10
|
+
// a YAML comment counts: the same text inside a value, a quoted key or a block scalar is data.
|
|
11
|
+
export function collectAllowances(lines: readonly string[]): Allowances {
|
|
12
|
+
const covered = new Set<string>();
|
|
13
|
+
|
|
14
|
+
const faults: { line: number; message: string }[] = [];
|
|
15
|
+
|
|
16
|
+
for (const comment of collectComments(lines)) {
|
|
17
|
+
const line = comment.line;
|
|
18
|
+
const rest = commentPattern.exec(comment.source)?.groups?.['rest'];
|
|
19
|
+
|
|
20
|
+
if (rest === undefined) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const entry = entryPattern.exec(rest);
|
|
25
|
+
const name = (entry ?? nameOnlyPattern.exec(rest))?.groups?.['name'];
|
|
26
|
+
|
|
27
|
+
if (name === undefined) {
|
|
28
|
+
faults.push({
|
|
29
|
+
line,
|
|
30
|
+
message: `chex-allow: the comment names no rule; write # chex-allow <rule>: <reason>`,
|
|
31
|
+
});
|
|
32
|
+
} else if (!ruleNames.some((rule) => rule === name)) {
|
|
33
|
+
faults.push({
|
|
34
|
+
line,
|
|
35
|
+
message: `chex-allow: "${name}" is not a rule; use one of ${ruleNames.join(', ')}`,
|
|
36
|
+
});
|
|
37
|
+
} else if (entry === null) {
|
|
38
|
+
faults.push({
|
|
39
|
+
line,
|
|
40
|
+
message: `chex-allow: the comment for ${name} gives no reason; write # chex-allow ${name}: <reason>`,
|
|
41
|
+
});
|
|
42
|
+
} else {
|
|
43
|
+
covered.add(`${name}:${comment.isAlone ? line + 1 : line}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { covered, faults };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface YAMLComment {
|
|
51
|
+
readonly line: number;
|
|
52
|
+
readonly source: string;
|
|
53
|
+
|
|
54
|
+
// Whether nothing but spaces comes before the comment on its line.
|
|
55
|
+
readonly isAlone: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function collectComments(lines: readonly string[]): YAMLComment[] {
|
|
59
|
+
const comments: YAMLComment[] = [];
|
|
60
|
+
|
|
61
|
+
const lineCounter = new LineCounter();
|
|
62
|
+
|
|
63
|
+
const text = lines.join('\n');
|
|
64
|
+
|
|
65
|
+
const visit = (node: unknown): void => {
|
|
66
|
+
if (Array.isArray(node)) {
|
|
67
|
+
for (const member of node) {
|
|
68
|
+
visit(member);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (typeof node !== 'object' || node === null) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const type: unknown = Reflect.get(node, 'type');
|
|
79
|
+
const offset: unknown = Reflect.get(node, 'offset');
|
|
80
|
+
const source: unknown = Reflect.get(node, 'source');
|
|
81
|
+
|
|
82
|
+
if (type === 'comment' && typeof offset === 'number' && typeof source === 'string') {
|
|
83
|
+
const position = lineCounter.linePos(offset);
|
|
84
|
+
const before = (lines[position.line - 1] ?? '').slice(0, position.col - 1);
|
|
85
|
+
|
|
86
|
+
comments.push({ line: position.line, source, isAlone: before.trim() === '' });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
for (const member of Object.values(node)) {
|
|
90
|
+
visit(member);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
for (const token of new Parser((offset) => {
|
|
95
|
+
lineCounter.addNewLine(offset);
|
|
96
|
+
}).parse(text)) {
|
|
97
|
+
visit(token);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return comments;
|
|
101
|
+
}
|
|
@@ -2,7 +2,7 @@ import { collectExpressionReferences } from './collect-expression-references.ts'
|
|
|
2
2
|
import { findMember } from './find-member.ts';
|
|
3
3
|
import { getTopLevelLine } from './get-top-level-line.ts';
|
|
4
4
|
import { pullRequestTriggers } from './pull-request-triggers.ts';
|
|
5
|
-
import type { Job, WorkflowContext, WorkflowFinding } from './
|
|
5
|
+
import type { Job, WorkflowContext, WorkflowFinding } from './types.ts';
|
|
6
6
|
|
|
7
7
|
export function collectConcurrencyFindings(context: WorkflowContext): WorkflowFinding[] {
|
|
8
8
|
const deployJobs = context.jobs.filter((job) => isDeployJob(job));
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { collectAllowances } from './collect-allowances.ts';
|
|
2
|
+
import { collectHookCheckFindings } from './rules/collect-hook-check-findings.ts';
|
|
3
|
+
import { toWorkflowFindings } from './to-workflow-findings.ts';
|
|
4
|
+
import type { HookFile, MovedRepo, WorkflowFinding } from './types.ts';
|
|
5
|
+
|
|
6
|
+
// The pre-push hook's findings, in the lefthook file that holds the hook.
|
|
7
|
+
export function collectHookFindings(hook: HookFile, repo: MovedRepo): WorkflowFinding[] {
|
|
8
|
+
return toWorkflowFindings(
|
|
9
|
+
collectHookCheckFindings(hook, repo),
|
|
10
|
+
hook.path,
|
|
11
|
+
collectAllowances(hook.lines),
|
|
12
|
+
);
|
|
13
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { collectAllowances } from './collect-allowances.ts';
|
|
2
|
+
import { collectMainOnlyJobFindings } from './rules/collect-main-only-job-findings.ts';
|
|
3
|
+
import { collectMainRunConcurrencyFindings } from './rules/collect-main-run-concurrency-findings.ts';
|
|
4
|
+
import { collectMainRunShapeFindings } from './rules/collect-main-run-shape-findings.ts';
|
|
5
|
+
import { collectPathFilterFindings } from './rules/collect-path-filter-findings.ts';
|
|
6
|
+
import { collectPrGateFindings } from './rules/collect-pr-gate-findings.ts';
|
|
7
|
+
import { collectPublishFindings } from './rules/collect-publish-findings.ts';
|
|
8
|
+
import { collectReleaseShapeFindings } from './rules/collect-release-shape-findings.ts';
|
|
9
|
+
import type { WorkflowRule } from './rules/types.ts';
|
|
10
|
+
import { toWorkflowFindings } from './to-workflow-findings.ts';
|
|
11
|
+
import type { MovedRepo, WorkflowContext, WorkflowFinding } from './types.ts';
|
|
12
|
+
|
|
13
|
+
const workflowRules: readonly WorkflowRule[] = [
|
|
14
|
+
collectMainOnlyJobFindings,
|
|
15
|
+
collectPrGateFindings,
|
|
16
|
+
collectMainRunConcurrencyFindings,
|
|
17
|
+
collectMainRunShapeFindings,
|
|
18
|
+
collectReleaseShapeFindings,
|
|
19
|
+
collectPathFilterFindings,
|
|
20
|
+
collectPublishFindings,
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
// The rules of a moved repo that read one workflow file under .github/workflows/, less the findings
|
|
24
|
+
// that a chex-allow comment turns off, plus the faults of the comments themselves.
|
|
25
|
+
export function collectMovedRepoFindings(
|
|
26
|
+
workflow: WorkflowContext,
|
|
27
|
+
repo: MovedRepo,
|
|
28
|
+
): WorkflowFinding[] {
|
|
29
|
+
return toWorkflowFindings(
|
|
30
|
+
workflowRules.flatMap((rule) => rule(workflow, repo)),
|
|
31
|
+
workflow.path,
|
|
32
|
+
collectAllowances(workflow.lines),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { parseShellWords } from '../shell/parse-shell-words.ts';
|
|
2
|
+
import type { ShellWord } from '../shell/parse-shell-words.ts';
|
|
3
|
+
|
|
4
|
+
// Each simple command of a run, as its words after any NAME=value assignments. A new line and each
|
|
5
|
+
// shell operator end a command.
|
|
6
|
+
export function collectRunCommands(run: string): ShellWord[][] {
|
|
7
|
+
const commands: ShellWord[][] = [[]];
|
|
8
|
+
|
|
9
|
+
for (const word of parseShellWords(run)) {
|
|
10
|
+
if (word.isOperator) {
|
|
11
|
+
commands.push([]);
|
|
12
|
+
} else if (!word.isAssignment || commands.at(-1)?.length !== 0) {
|
|
13
|
+
commands.at(-1)?.push(word);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return commands.filter((command) => command.length > 0);
|
|
18
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { findMember } from './find-member.ts';
|
|
2
|
+
|
|
3
|
+
// The labels of a runs-on value, whether it is one label, a list or a map with labels.
|
|
4
|
+
export function collectRunnerLabels(runsOn: unknown): string[] {
|
|
5
|
+
return [runsOn, findMember(runsOn, 'labels')]
|
|
6
|
+
.flat()
|
|
7
|
+
.filter((label): label is string => typeof label === 'string');
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { findMember } from './find-member.ts';
|
|
2
|
+
import { isList } from './is-list.ts';
|
|
3
|
+
import type { Job } from './types.ts';
|
|
4
|
+
|
|
5
|
+
export function collectSteps(job: Job): readonly unknown[] {
|
|
6
|
+
const steps = findMember(job.definition, 'steps');
|
|
7
|
+
|
|
8
|
+
return isList(steps) ? steps : [];
|
|
9
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { collectRunnerLabels } from './collect-runner-labels.ts';
|
|
2
|
+
import { expressionOpening } from './constants.ts';
|
|
3
|
+
import { findMember } from './find-member.ts';
|
|
4
|
+
|
|
5
|
+
// Any runner but a GitHub-hosted Ubuntu one counts as heavy: macOS, Windows, self-hosted, or a
|
|
6
|
+
// runner group. A runner chosen by an expression is left alone, because GitHub resolves it only
|
|
7
|
+
// when the job runs.
|
|
8
|
+
export function findHeavyRunner(runsOn: unknown): string | undefined {
|
|
9
|
+
if (findMember(runsOn, 'group') !== undefined) {
|
|
10
|
+
return 'a runner group';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const labels = collectRunnerLabels(runsOn);
|
|
14
|
+
|
|
15
|
+
if (labels.some((label) => label.includes(expressionOpening))) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return labels.find((label) => !label.startsWith('ubuntu-'));
|
|
20
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const remotePattern =
|
|
2
|
+
/^(?:https?:\/\/(?:[^@/]+@)?|ssh:\/\/git@|git@)github\.com[:/](?<slug>[^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/u;
|
|
3
|
+
|
|
4
|
+
// The `owner/name` of a GitHub remote URL, or undefined for any other URL.
|
|
5
|
+
export function parseRepoSlug(remoteURL: string): string | undefined {
|
|
6
|
+
return remotePattern.exec(remoteURL.trim())?.groups?.['slug'];
|
|
7
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { hookFilePaths } from './constants.ts';
|
|
2
|
+
import type { HookFile, RepoState } from './types.ts';
|
|
3
|
+
|
|
4
|
+
interface CheckedPaths {
|
|
5
|
+
readonly workflows: readonly string[];
|
|
6
|
+
readonly hooks: readonly HookFile[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const settingsPath = '.github/ci.yml';
|
|
10
|
+
|
|
11
|
+
// No path asks for every file. In a moved repo the rules read across files, so a path that is a
|
|
12
|
+
// workflow, .github/ci.yml, package.json or a lefthook file asks for every workflow and hook: its
|
|
13
|
+
// change can break the main run or the hook that the change did not touch.
|
|
14
|
+
export function pickCheckedPaths(
|
|
15
|
+
asked: readonly string[],
|
|
16
|
+
workflowPaths: readonly string[],
|
|
17
|
+
state: RepoState,
|
|
18
|
+
): CheckedPaths {
|
|
19
|
+
const isAll =
|
|
20
|
+
asked.length === 0 ||
|
|
21
|
+
(state.repo !== undefined &&
|
|
22
|
+
asked.some(
|
|
23
|
+
(path) =>
|
|
24
|
+
path === settingsPath ||
|
|
25
|
+
path === 'package.json' ||
|
|
26
|
+
path.startsWith('.github/workflows/') ||
|
|
27
|
+
hookFilePaths.includes(path),
|
|
28
|
+
));
|
|
29
|
+
|
|
30
|
+
return isAll
|
|
31
|
+
? { workflows: workflowPaths, hooks: state.hooks }
|
|
32
|
+
: { workflows: workflowPaths.filter((path) => asked.includes(path)), hooks: [] };
|
|
33
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { findMember } from './find-member.ts';
|
|
2
|
+
import type { RepoMove } from './types.ts';
|
|
3
|
+
|
|
4
|
+
const sharedChecksPattern = /^zgeoff\/tools\/\.github\/workflows\/checks\.yml(?:@.*)?$/u;
|
|
5
|
+
const localChecksPath = './.github/workflows/checks.yml';
|
|
6
|
+
|
|
7
|
+
// A repo that calls the shared checks.yml gets the two-workflow rules; one that doesn't keeps the
|
|
8
|
+
// old rules until every repo has moved. A local call counts only in zgeoff/tools itself.
|
|
9
|
+
export function pickRepoMove(
|
|
10
|
+
workflows: readonly unknown[],
|
|
11
|
+
repoSlug: string | undefined,
|
|
12
|
+
): RepoMove {
|
|
13
|
+
const calls = workflows.flatMap((workflow) => {
|
|
14
|
+
const jobs = findMember(workflow, 'jobs');
|
|
15
|
+
|
|
16
|
+
return typeof jobs === 'object' && jobs !== null
|
|
17
|
+
? Object.values(jobs).map((job: unknown) => findMember(job, 'uses'))
|
|
18
|
+
: [];
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
if (repoSlug === 'zgeoff/tools' && calls.includes(localChecksPath)) {
|
|
22
|
+
return 'tools';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return calls.some((uses) => typeof uses === 'string' && sharedChecksPattern.test(uses))
|
|
26
|
+
? 'shared'
|
|
27
|
+
: 'none';
|
|
28
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProgramOptions, RunProgram } from '../program/run-program.ts';
|
|
2
|
+
import { parseRepoSlug } from './parse-repo-slug.ts';
|
|
3
|
+
|
|
4
|
+
// The `owner/name` of the origin remote, or undefined when the repo has no origin on GitHub.
|
|
5
|
+
export function readRepoSlug(
|
|
6
|
+
runProgram: RunProgram,
|
|
7
|
+
options: Readonly<ProgramOptions>,
|
|
8
|
+
): string | undefined {
|
|
9
|
+
const result = runProgram(['git', 'remote', 'get-url', 'origin'], options);
|
|
10
|
+
|
|
11
|
+
return result.exitCode === 0 ? parseRepoSlug(result.stdout) : undefined;
|
|
12
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { parseCIPlan } from '../ci-plan/parse-ci-plan.ts';
|
|
2
|
+
import { parsePackageScripts } from '../ci-plan/parse-package-scripts.ts';
|
|
3
|
+
import { hookFilePaths } from './constants.ts';
|
|
4
|
+
import type { HookFile, RepoMove, RepoState } from './types.ts';
|
|
5
|
+
|
|
6
|
+
interface RepoFiles {
|
|
7
|
+
// Every tracked path.
|
|
8
|
+
readonly tracked: ReadonlySet<string>;
|
|
9
|
+
|
|
10
|
+
// The text of a tracked file at HEAD.
|
|
11
|
+
readonly readText: (path: string) => string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const settingsPath = '.github/ci.yml';
|
|
15
|
+
|
|
16
|
+
export function readRepoState(move: RepoMove, files: Readonly<RepoFiles>): RepoState {
|
|
17
|
+
if (move === 'none') {
|
|
18
|
+
return { move, repo: undefined, hooks: [] };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const settingsText = files.tracked.has(settingsPath) ? files.readText(settingsPath) : undefined;
|
|
22
|
+
|
|
23
|
+
const packageText = files.tracked.has('package.json')
|
|
24
|
+
? files.readText('package.json')
|
|
25
|
+
: undefined;
|
|
26
|
+
|
|
27
|
+
const packageScripts = parsePackageScripts(packageText);
|
|
28
|
+
const scripts = packageScripts.kind === 'read' ? packageScripts.scripts : undefined;
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
move,
|
|
32
|
+
repo: {
|
|
33
|
+
move,
|
|
34
|
+
plan:
|
|
35
|
+
packageScripts.kind === 'invalid'
|
|
36
|
+
? { kind: 'invalid', errors: [packageScripts.message] }
|
|
37
|
+
: parseCIPlan({ settingsText, packageScripts: scripts }),
|
|
38
|
+
packageScripts: scripts,
|
|
39
|
+
planLines: settingsText?.split('\n') ?? [],
|
|
40
|
+
},
|
|
41
|
+
hooks: hookFilePaths
|
|
42
|
+
.filter((path) => files.tracked.has(path))
|
|
43
|
+
.map((path) => readHook(path, files.readText(path))),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readHook(path: string, text: string): HookFile {
|
|
48
|
+
let document: unknown;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
document = Bun.YAML.parse(text);
|
|
52
|
+
} catch {
|
|
53
|
+
document = undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return { path, lines: text.split('\n'), document };
|
|
57
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RuleFinding, RuleName } from './types.ts';
|
|
2
|
+
|
|
3
|
+
export function buildRuleFinding(
|
|
4
|
+
rule: RuleName,
|
|
5
|
+
line: number,
|
|
6
|
+
text: string,
|
|
7
|
+
allowAt: readonly number[],
|
|
8
|
+
): RuleFinding {
|
|
9
|
+
return { rule, path: undefined, line, message: `${rule}: ${text}`, allowAt };
|
|
10
|
+
}
|