@zgeoff/chex 0.8.6 → 0.8.8
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 +3 -2
- package/lefthook.yml +13 -0
- package/package.json +5 -2
- package/src/ci-plan/build-ci-plan-output.ts +36 -0
- package/src/ci-plan/collect-shared-checks.ts +62 -0
- package/src/ci-plan/find-chex-args.ts +40 -0
- package/src/ci-plan/parse-ci-plan.ts +299 -0
- package/src/ci-plan/parse-docs-checks.ts +1 -0
- package/src/ci-plan/parse-package-scripts.ts +36 -0
- package/src/ci-plan/write-ci-plan-json.ts +21 -0
- package/src/cli/parse-chex-args.ts +7 -2
- package/src/cli/run-chex.ts +2 -0
- package/src/cli.ts +9 -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,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
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// The terms of an `if` that GitHub runs only when every term holds, lower-cased and without the
|
|
2
|
+
// whitespace outside quoted values, or undefined when the condition is no plain conjunction: an
|
|
3
|
+
// `||` outside brackets lets a run through without its other terms.
|
|
4
|
+
export function collectConditionTerms(condition: unknown): string[] | undefined {
|
|
5
|
+
if (condition === undefined) {
|
|
6
|
+
return [];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (typeof condition === 'boolean') {
|
|
10
|
+
return [String(condition)];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (typeof condition !== 'string') {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const text = condition.trim();
|
|
18
|
+
const body = text.startsWith('${{') && text.endsWith('}}') ? text.slice(3, -2) : text;
|
|
19
|
+
|
|
20
|
+
return body.includes('${{') ? undefined : collectTerms(body);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function collectTerms(expression: string): string[] | undefined {
|
|
24
|
+
const text = toUnbracketed(expression.trim());
|
|
25
|
+
|
|
26
|
+
if (splitOutsideBrackets(text, '||').length > 1) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const parts = splitOutsideBrackets(text, '&&');
|
|
31
|
+
|
|
32
|
+
if (parts.length === 1) {
|
|
33
|
+
return [toNormalizedTerm(text)];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const terms = parts.map((part) => collectTerms(part));
|
|
37
|
+
|
|
38
|
+
return terms.every((term) => term !== undefined) ? terms.flat() : undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toUnbracketed(text: string): string {
|
|
42
|
+
return text.startsWith('(') && isClosedAtEnd(text)
|
|
43
|
+
? toUnbracketed(text.slice(1, -1).trim())
|
|
44
|
+
: text;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Whether the bracket that opens the text closes at its last character.
|
|
48
|
+
function isClosedAtEnd(text: string): boolean {
|
|
49
|
+
let depth = 0;
|
|
50
|
+
let isInString = false;
|
|
51
|
+
|
|
52
|
+
// The brackets and quotes are ASCII, so a loop over UTF-16 code units finds them.
|
|
53
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
54
|
+
const character = text[index];
|
|
55
|
+
|
|
56
|
+
if (character === "'") {
|
|
57
|
+
isInString = !isInString;
|
|
58
|
+
} else if (!isInString && (character === '(' || character === '[')) {
|
|
59
|
+
depth += 1;
|
|
60
|
+
} else if (!isInString && (character === ')' || character === ']')) {
|
|
61
|
+
depth -= 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (depth === 0 && index < text.length - 1) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return depth === 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function splitOutsideBrackets(text: string, separator: string): string[] {
|
|
73
|
+
const parts: string[] = [];
|
|
74
|
+
let depth = 0;
|
|
75
|
+
let isInString = false;
|
|
76
|
+
let start = 0;
|
|
77
|
+
|
|
78
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
79
|
+
const character = text.charAt(index);
|
|
80
|
+
|
|
81
|
+
if (character === "'") {
|
|
82
|
+
isInString = !isInString;
|
|
83
|
+
} else if (!isInString && (character === '(' || character === '[')) {
|
|
84
|
+
depth += 1;
|
|
85
|
+
} else if (!isInString && (character === ')' || character === ']')) {
|
|
86
|
+
depth -= 1;
|
|
87
|
+
} else if (!isInString && depth === 0 && text.startsWith(separator, index)) {
|
|
88
|
+
parts.push(text.slice(start, index));
|
|
89
|
+
|
|
90
|
+
start = index + separator.length;
|
|
91
|
+
index += separator.length - 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
parts.push(text.slice(start));
|
|
96
|
+
|
|
97
|
+
return parts;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// GitHub compares strings without regard to case, and a space inside a quoted value is part of it.
|
|
101
|
+
function toNormalizedTerm(text: string): string {
|
|
102
|
+
let isInString = false;
|
|
103
|
+
let term = '';
|
|
104
|
+
|
|
105
|
+
for (const character of text.toLowerCase()) {
|
|
106
|
+
if (character === "'") {
|
|
107
|
+
isInString = !isInString;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (isInString || character === "'" || !/\s/u.test(character)) {
|
|
111
|
+
term += character;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return term;
|
|
116
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { findChexArgs } from '../../ci-plan/find-chex-args.ts';
|
|
2
|
+
import type { ShellWord } from '../../shell/parse-shell-words.ts';
|
|
3
|
+
import { collectCommandNames } from '../collect-command-names.ts';
|
|
4
|
+
import { collectRunCommands } from '../collect-run-commands.ts';
|
|
5
|
+
import { findMember } from '../find-member.ts';
|
|
6
|
+
import { isList } from '../is-list.ts';
|
|
7
|
+
import type { HookFile, MovedRepo } from '../types.ts';
|
|
8
|
+
import { buildRuleFinding } from './build-rule-finding.ts';
|
|
9
|
+
import { isSharedCheckScript } from './is-shared-check-script.ts';
|
|
10
|
+
import type { RuleFinding } from './types.ts';
|
|
11
|
+
|
|
12
|
+
// audit and check:docs are shared checks that every run makes.
|
|
13
|
+
const sharedScripts = new Set(['audit', 'check:docs']);
|
|
14
|
+
|
|
15
|
+
export function collectHookCheckFindings(hook: HookFile, repo: MovedRepo): RuleFinding[] {
|
|
16
|
+
if (repo.plan.kind !== 'planned') {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const listed = new Set(repo.plan.plan.scripts);
|
|
21
|
+
|
|
22
|
+
return collectPrePushRuns(findMember(hook.document, 'pre-push')).flatMap((run) => {
|
|
23
|
+
const line = Math.max(
|
|
24
|
+
hook.lines.findIndex((text) => text.includes(run.split('\n')[0] ?? run)) + 1,
|
|
25
|
+
1,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return collectRunCommands(run).flatMap((words) => {
|
|
29
|
+
const script = findHookScript(words, repo.packageScripts);
|
|
30
|
+
|
|
31
|
+
if (
|
|
32
|
+
script === undefined ||
|
|
33
|
+
listed.has(script) ||
|
|
34
|
+
sharedScripts.has(script) ||
|
|
35
|
+
isSharedCheckScript(repo.packageScripts?.[script])
|
|
36
|
+
) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return [
|
|
41
|
+
{
|
|
42
|
+
...buildRuleFinding(
|
|
43
|
+
'hook-check-missing-in-ci',
|
|
44
|
+
line,
|
|
45
|
+
`the pre-push hook runs the script ${script}, which .github/ci.yml does not list and no shared check runs; add ${script} to scripts in .github/ci.yml, or remove it from the hook`,
|
|
46
|
+
[line],
|
|
47
|
+
),
|
|
48
|
+
path: hook.path,
|
|
49
|
+
},
|
|
50
|
+
];
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function collectPrePushRuns(node: unknown): string[] {
|
|
56
|
+
if (isList(node)) {
|
|
57
|
+
return node.flatMap((member) => collectPrePushRuns(member));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (typeof node !== 'object' || node === null) {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const run = findMember(node, 'run');
|
|
65
|
+
const nested = Object.values(node).flatMap((member: unknown) => collectPrePushRuns(member));
|
|
66
|
+
|
|
67
|
+
return typeof run === 'string' ? [run, ...nested] : nested;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The package script of a `bun run <script>` or `bun <script>` command. A command that runs chex is
|
|
71
|
+
// one of the shared checks, and a file that bun runs is no package script.
|
|
72
|
+
function findHookScript(
|
|
73
|
+
words: readonly ShellWord[],
|
|
74
|
+
packageScripts: Readonly<Record<string, string>> | undefined,
|
|
75
|
+
): string | undefined {
|
|
76
|
+
if (findChexArgs(words.map((word) => word.text)) !== undefined) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const names = collectCommandNames(words);
|
|
81
|
+
const script = names?.length === 2 && names[0] === 'bun' ? names[1] : undefined;
|
|
82
|
+
|
|
83
|
+
return script !== undefined && packageScripts?.[script] !== undefined ? script : undefined;
|
|
84
|
+
}
|