@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
|
@@ -4,15 +4,25 @@ import { runProgram } from '../program/run-program.ts';
|
|
|
4
4
|
import { parseShellWords } from '../shell/parse-shell-words.ts';
|
|
5
5
|
import { collectCommandNames } from './collect-command-names.ts';
|
|
6
6
|
import { collectConcurrencyFindings } from './collect-concurrency-findings.ts';
|
|
7
|
+
import { collectHookFindings } from './collect-hook-findings.ts';
|
|
8
|
+
import { collectMovedRepoFindings } from './collect-moved-repo-findings.ts';
|
|
9
|
+
import { collectSteps } from './collect-steps.ts';
|
|
10
|
+
import { expressionOpening } from './constants.ts';
|
|
11
|
+
import { findHeavyRunner } from './find-heavy-runner.ts';
|
|
7
12
|
import { findInstallCachePath } from './find-install-cache-path.ts';
|
|
8
13
|
import { findMember } from './find-member.ts';
|
|
9
14
|
import { getTopLevelLine } from './get-top-level-line.ts';
|
|
15
|
+
import { isList } from './is-list.ts';
|
|
16
|
+
import { pickCheckedPaths } from './pick-checked-paths.ts';
|
|
17
|
+
import { pickRepoMove } from './pick-repo-move.ts';
|
|
10
18
|
import { pullRequestTriggers } from './pull-request-triggers.ts';
|
|
11
19
|
import type { ChecksProfile } from './read-checks-profile.ts';
|
|
12
20
|
import { readChecksProfile } from './read-checks-profile.ts';
|
|
13
|
-
import
|
|
21
|
+
import { readRepoSlug } from './read-repo-slug.ts';
|
|
22
|
+
import { readRepoState } from './read-repo-state.ts';
|
|
23
|
+
import type { Job, RepoState, WorkflowContext, WorkflowFinding } from './types.ts';
|
|
14
24
|
|
|
15
|
-
export type { WorkflowFinding } from './
|
|
25
|
+
export type { WorkflowFinding } from './types.ts';
|
|
16
26
|
|
|
17
27
|
export interface WorkflowsCheckOptions {
|
|
18
28
|
readonly cwd: string;
|
|
@@ -32,17 +42,28 @@ const workflowPathPattern = /^(?:\.github\/workflows|sync\/src\/workflow-callers
|
|
|
32
42
|
|
|
33
43
|
// The check reads the files as HEAD holds them, so a push checks what it sends.
|
|
34
44
|
export function runWorkflowsCheck(options: Readonly<WorkflowsCheckOptions>): WorkflowFinding[] {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
45
|
+
const trackedPaths = runGit(options, ['ls-tree', '-r', '-z', '--name-only', 'HEAD']).split('\0');
|
|
46
|
+
const readText = (path: string): string => runGit(options, ['show', `HEAD:${path}`]);
|
|
47
|
+
const workflowPaths = trackedPaths.filter((path) => workflowPathPattern.test(path));
|
|
48
|
+
|
|
49
|
+
const move = pickRepoMove(
|
|
50
|
+
workflowPaths
|
|
51
|
+
.filter((path) => path.startsWith('.github/workflows/'))
|
|
52
|
+
.map((path) => parseWorkflow(readText(path))),
|
|
53
|
+
readRepoSlug(options.runProgram, options),
|
|
54
|
+
);
|
|
40
55
|
|
|
41
|
-
const
|
|
56
|
+
const state = readRepoState(move, { tracked: new Set(trackedPaths), readText });
|
|
57
|
+
const checked = pickCheckedPaths(options.paths, workflowPaths, state);
|
|
42
58
|
|
|
43
|
-
return
|
|
44
|
-
|
|
45
|
-
|
|
59
|
+
return [
|
|
60
|
+
...checked.workflows.flatMap((path) =>
|
|
61
|
+
collectWorkflowFindings(path, readText(path), options.profile, state),
|
|
62
|
+
),
|
|
63
|
+
...checked.hooks.flatMap((hook) =>
|
|
64
|
+
state.repo === undefined ? [] : collectHookFindings(hook, state.repo),
|
|
65
|
+
),
|
|
66
|
+
];
|
|
46
67
|
}
|
|
47
68
|
|
|
48
69
|
function runGit(options: Readonly<WorkflowsCheckOptions>, args: readonly string[]): string {
|
|
@@ -55,10 +76,19 @@ function runGit(options: Readonly<WorkflowsCheckOptions>, args: readonly string[
|
|
|
55
76
|
return result.stdout;
|
|
56
77
|
}
|
|
57
78
|
|
|
79
|
+
function parseWorkflow(text: string): unknown {
|
|
80
|
+
try {
|
|
81
|
+
return Bun.YAML.parse(text);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
58
87
|
function collectWorkflowFindings(
|
|
59
88
|
path: string,
|
|
60
89
|
text: string,
|
|
61
90
|
profile: ChecksProfile | undefined,
|
|
91
|
+
state: RepoState,
|
|
62
92
|
): WorkflowFinding[] {
|
|
63
93
|
const workflow = parseWorkflow(text);
|
|
64
94
|
|
|
@@ -71,33 +101,33 @@ function collectWorkflowFindings(
|
|
|
71
101
|
const context: WorkflowContext = {
|
|
72
102
|
path,
|
|
73
103
|
profile,
|
|
104
|
+
repoMove: state.move,
|
|
74
105
|
workflow,
|
|
75
106
|
lines,
|
|
76
107
|
jobs: collectJobs(findMember(workflow, 'jobs'), lines),
|
|
77
108
|
triggers: collectTriggers(findMember(workflow, 'on')),
|
|
78
109
|
};
|
|
79
110
|
|
|
111
|
+
// The main run of a moved repo queues its pushes in one group that never cancels, which
|
|
112
|
+
// main-run-concurrency checks in place of the job-level groups that the rule below asks for.
|
|
113
|
+
const isMovedMainRun = state.move !== 'none' && path === '.github/workflows/main.yml';
|
|
114
|
+
|
|
80
115
|
return [
|
|
81
116
|
...context.jobs.flatMap((job) => jobRules.flatMap((rule) => rule(context, job))),
|
|
82
|
-
...collectConcurrencyFindings(context),
|
|
117
|
+
...(isMovedMainRun ? [] : collectConcurrencyFindings(context)),
|
|
83
118
|
...collectWorkflowRunFindings(context),
|
|
119
|
+
...(state.repo !== undefined && path.startsWith('.github/workflows/')
|
|
120
|
+
? collectMovedRepoFindings(context, state.repo)
|
|
121
|
+
: []),
|
|
84
122
|
];
|
|
85
123
|
}
|
|
86
124
|
|
|
87
|
-
function parseWorkflow(text: string): unknown {
|
|
88
|
-
try {
|
|
89
|
-
return Bun.YAML.parse(text);
|
|
90
|
-
} catch (error) {
|
|
91
|
-
return error instanceof Error ? error : new Error(String(error));
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
125
|
function collectJobs(jobs: unknown, lines: readonly string[]): Job[] {
|
|
96
126
|
if (typeof jobs !== 'object' || jobs === null) {
|
|
97
127
|
return [];
|
|
98
128
|
}
|
|
99
129
|
|
|
100
|
-
const jobsLine = lines.findIndex((line) =>
|
|
130
|
+
const jobsLine = lines.findIndex((line) => /^['"]?jobs['"]?\s*:/u.test(line));
|
|
101
131
|
|
|
102
132
|
return Object.entries(jobs).map(([id, definition]: readonly [string, unknown]) => ({
|
|
103
133
|
id,
|
|
@@ -107,7 +137,10 @@ function collectJobs(jobs: unknown, lines: readonly string[]): Job[] {
|
|
|
107
137
|
}
|
|
108
138
|
|
|
109
139
|
function getJobLine(lines: readonly string[], jobsLine: number, id: string): number {
|
|
110
|
-
const keyPattern = new RegExp(
|
|
140
|
+
const keyPattern = new RegExp(
|
|
141
|
+
`^\\s+(?:${RegExp.escape(id)}|'${RegExp.escape(id)}'|"${RegExp.escape(id)}")\\s*:(?:\\s|$)`,
|
|
142
|
+
'u',
|
|
143
|
+
);
|
|
111
144
|
|
|
112
145
|
const offset = lines.slice(jobsLine + 1).findIndex((line) => keyPattern.test(line));
|
|
113
146
|
|
|
@@ -126,11 +159,6 @@ function collectTriggers(on: unknown): Set<string> {
|
|
|
126
159
|
return typeof on === 'object' && on !== null ? new Set(Object.keys(on)) : new Set();
|
|
127
160
|
}
|
|
128
161
|
|
|
129
|
-
// Array.isArray narrows to any[], which turns the type check off for each value.
|
|
130
|
-
function isList(value: unknown): value is readonly unknown[] {
|
|
131
|
-
return Array.isArray(value);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
162
|
const jobRules: readonly ((context: WorkflowContext, job: Job) => WorkflowFinding[])[] = [
|
|
135
163
|
collectRunFindings,
|
|
136
164
|
collectTimeoutFindings,
|
|
@@ -156,14 +184,6 @@ function collectRunFindings(context: WorkflowContext, job: Job): WorkflowFinding
|
|
|
156
184
|
});
|
|
157
185
|
}
|
|
158
186
|
|
|
159
|
-
function collectSteps(job: Job): readonly unknown[] {
|
|
160
|
-
const steps = findMember(job.definition, 'steps');
|
|
161
|
-
|
|
162
|
-
return isList(steps) ? steps : [];
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const expressionOpening = `\${{`;
|
|
166
|
-
|
|
167
187
|
const shellKeywords = new Set(['if', 'for', 'while', 'case']);
|
|
168
188
|
|
|
169
189
|
function findRunFault(run: string): string | undefined {
|
|
@@ -213,7 +233,11 @@ function collectHeavyFindings(context: WorkflowContext, job: Job): WorkflowFindi
|
|
|
213
233
|
const trigger = pullRequestTriggers.find((name) => context.triggers.has(name));
|
|
214
234
|
const reason = findHeavyReason(job);
|
|
215
235
|
|
|
216
|
-
|
|
236
|
+
// A moved repo gets main-only-job-in-pr in place of this rule, whatever its profile.
|
|
237
|
+
return context.repoMove !== 'none' ||
|
|
238
|
+
context.profile === 'public' ||
|
|
239
|
+
trigger === undefined ||
|
|
240
|
+
reason === undefined
|
|
217
241
|
? []
|
|
218
242
|
: [
|
|
219
243
|
{
|
|
@@ -244,25 +268,6 @@ function findHeavyReason(job: Job): string | undefined {
|
|
|
244
268
|
return command === undefined ? undefined : `enables KVM through ${command}`;
|
|
245
269
|
}
|
|
246
270
|
|
|
247
|
-
// Any runner but a GitHub-hosted Ubuntu one counts as heavy: macOS, Windows, self-hosted, or a
|
|
248
|
-
// runner group. A runner chosen by an expression is left alone, because GitHub resolves it only
|
|
249
|
-
// when the job runs.
|
|
250
|
-
function findHeavyRunner(runsOn: unknown): string | undefined {
|
|
251
|
-
if (findMember(runsOn, 'group') !== undefined) {
|
|
252
|
-
return 'a runner group';
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const labels = [runsOn, findMember(runsOn, 'labels')]
|
|
256
|
-
.flat()
|
|
257
|
-
.filter((label): label is string => typeof label === 'string');
|
|
258
|
-
|
|
259
|
-
if (labels.some((label) => label.includes(expressionOpening))) {
|
|
260
|
-
return undefined;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return labels.find((label) => !label.startsWith('ubuntu-'));
|
|
264
|
-
}
|
|
265
|
-
|
|
266
271
|
const setupBunPattern = /^oven-sh\/setup-bun(?:@|$)/u;
|
|
267
272
|
const cachePattern = /^actions\/cache(?:\/(?:restore|save))?(?:@|$)/u;
|
|
268
273
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Wrappers that run the command after them, with the options that take a value.
|
|
2
|
+
const wrappers: Readonly<Record<string, ReadonlySet<string>>> = {
|
|
3
|
+
sudo: new Set(['-u', '-g', '-h', '-p', '-C', '-r', '-t', '-U', '-D', '-R', '-T']),
|
|
4
|
+
env: new Set(['-u', '-C', '-S']),
|
|
5
|
+
nohup: new Set(),
|
|
6
|
+
time: new Set(),
|
|
7
|
+
nice: new Set(['-n']),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// The words of a command without the wrappers that sudo, env, nohup, time or nice put before it, so
|
|
11
|
+
// `sudo -E systemctl start x` reads as `systemctl start x`.
|
|
12
|
+
export function toUnwrappedCommand(words: readonly string[]): string[] {
|
|
13
|
+
let index = 0;
|
|
14
|
+
|
|
15
|
+
for (;;) {
|
|
16
|
+
const valueOptions = wrappers[words[index] ?? ''];
|
|
17
|
+
|
|
18
|
+
if (valueOptions === undefined) {
|
|
19
|
+
return words.slice(index);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
index += 1;
|
|
23
|
+
|
|
24
|
+
while (index < words.length && (words[index] ?? '').startsWith('-')) {
|
|
25
|
+
index += valueOptions.has(words[index] ?? '') ? 2 : 1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RuleFinding } from './rules/types.ts';
|
|
2
|
+
import type { Allowances, WorkflowFinding } from './types.ts';
|
|
3
|
+
|
|
4
|
+
// The findings of a file less those that a chex-allow comment turns off, plus the faults of the
|
|
5
|
+
// file's comments.
|
|
6
|
+
export function toWorkflowFindings(
|
|
7
|
+
findings: readonly RuleFinding[],
|
|
8
|
+
path: string,
|
|
9
|
+
allowances: Allowances,
|
|
10
|
+
): WorkflowFinding[] {
|
|
11
|
+
return [
|
|
12
|
+
...allowances.faults.map((fault) => ({ path, ...fault })),
|
|
13
|
+
...findings
|
|
14
|
+
.filter(
|
|
15
|
+
(finding) =>
|
|
16
|
+
!finding.allowAt.some((line) => allowances.covered.has(`${finding.rule}:${line}`)),
|
|
17
|
+
)
|
|
18
|
+
.map((finding) => ({
|
|
19
|
+
path: finding.path ?? path,
|
|
20
|
+
line: finding.line,
|
|
21
|
+
message: finding.message,
|
|
22
|
+
})),
|
|
23
|
+
];
|
|
24
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { CIPlanOutcome } from '../ci-plan/parse-ci-plan.ts';
|
|
2
|
+
import type { ChecksProfile } from './read-checks-profile.ts';
|
|
3
|
+
|
|
4
|
+
// A repo that calls the shared checks.yml of zgeoff/tools has moved to the two-workflow CI. tools
|
|
5
|
+
// itself has moved once it calls its own copy by local path.
|
|
6
|
+
export type RepoMove = 'none' | 'shared' | 'tools';
|
|
7
|
+
|
|
8
|
+
export interface Allowances {
|
|
9
|
+
// The rule and the 1-based line of the key that the comment covers, as `rule:line`.
|
|
10
|
+
readonly covered: ReadonlySet<string>;
|
|
11
|
+
|
|
12
|
+
// The lines of comments that name an unknown rule or give no reason, each with its message.
|
|
13
|
+
readonly faults: readonly Omit<WorkflowFinding, 'path'>[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface HookFile {
|
|
17
|
+
readonly path: string;
|
|
18
|
+
readonly lines: readonly string[];
|
|
19
|
+
readonly document: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface MovedRepo {
|
|
23
|
+
readonly move: Exclude<RepoMove, 'none'>;
|
|
24
|
+
|
|
25
|
+
// What `.github/ci.yml` and package.json make of the repo's plan.
|
|
26
|
+
readonly plan: CIPlanOutcome;
|
|
27
|
+
|
|
28
|
+
readonly packageScripts: Readonly<Record<string, string>> | undefined;
|
|
29
|
+
|
|
30
|
+
// The lines of `.github/ci.yml`, empty when the repo has none.
|
|
31
|
+
readonly planLines: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface RepoState {
|
|
35
|
+
readonly move: RepoMove;
|
|
36
|
+
|
|
37
|
+
// Set when the repo has moved.
|
|
38
|
+
readonly repo: MovedRepo | undefined;
|
|
39
|
+
|
|
40
|
+
readonly hooks: readonly HookFile[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface WorkflowContext {
|
|
44
|
+
readonly path: string;
|
|
45
|
+
readonly profile: ChecksProfile | undefined;
|
|
46
|
+
|
|
47
|
+
// Whether the repo has moved to the two-workflow CI, which swaps the profile rule for the lint
|
|
48
|
+
// rules of a moved repo.
|
|
49
|
+
readonly repoMove: RepoMove;
|
|
50
|
+
readonly workflow: unknown;
|
|
51
|
+
readonly lines: readonly string[];
|
|
52
|
+
readonly jobs: readonly Job[];
|
|
53
|
+
readonly triggers: ReadonlySet<string>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface Job {
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly definition: unknown;
|
|
59
|
+
readonly line: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface WorkflowFinding {
|
|
63
|
+
// Relative to the repo root.
|
|
64
|
+
readonly path: string;
|
|
65
|
+
|
|
66
|
+
readonly line: number;
|
|
67
|
+
readonly message: string;
|
|
68
|
+
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { ChecksProfile } from './read-checks-profile.ts';
|
|
2
|
-
|
|
3
|
-
export interface WorkflowContext {
|
|
4
|
-
readonly path: string;
|
|
5
|
-
readonly profile: ChecksProfile | undefined;
|
|
6
|
-
readonly workflow: unknown;
|
|
7
|
-
readonly lines: readonly string[];
|
|
8
|
-
readonly jobs: readonly Job[];
|
|
9
|
-
readonly triggers: ReadonlySet<string>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface Job {
|
|
13
|
-
readonly id: string;
|
|
14
|
-
readonly definition: unknown;
|
|
15
|
-
readonly line: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface WorkflowFinding {
|
|
19
|
-
// Relative to the repo root.
|
|
20
|
-
readonly path: string;
|
|
21
|
-
|
|
22
|
-
readonly line: number;
|
|
23
|
-
readonly message: string;
|
|
24
|
-
}
|