@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,67 @@
|
|
|
1
|
+
import { findMember } from '../find-member.ts';
|
|
2
|
+
import type { MovedRepo, WorkflowContext } from '../types.ts';
|
|
3
|
+
import { buildRuleFinding } from './build-rule-finding.ts';
|
|
4
|
+
import { collectNeeds } from './collect-needs.ts';
|
|
5
|
+
import { collectReleaseJobs } from './collect-release-jobs.ts';
|
|
6
|
+
import { findJobEnvironment } from './find-job-environment.ts';
|
|
7
|
+
import { isReleaseCondition } from './is-release-condition.ts';
|
|
8
|
+
import { isSharedReleaseWorkflow } from './is-shared-release-workflow.ts';
|
|
9
|
+
import type { RuleFinding } from './types.ts';
|
|
10
|
+
|
|
11
|
+
export function collectReleaseShapeFindings(
|
|
12
|
+
workflow: WorkflowContext,
|
|
13
|
+
repo: MovedRepo,
|
|
14
|
+
): RuleFinding[] {
|
|
15
|
+
const releaseEnvironmentJobs = workflow.jobs.filter(
|
|
16
|
+
(job) => findJobEnvironment(job) === 'release',
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
if (!workflow.path.endsWith('/main.yml')) {
|
|
20
|
+
return isSharedReleaseWorkflow(workflow, repo.move)
|
|
21
|
+
? []
|
|
22
|
+
: releaseEnvironmentJobs.map((job) =>
|
|
23
|
+
buildRuleFinding(
|
|
24
|
+
'release-shape',
|
|
25
|
+
job.line,
|
|
26
|
+
`job ${job.id} uses the release environment outside main.yml; move the job into main.yml, since only the release job of the main run may use it`,
|
|
27
|
+
[job.line],
|
|
28
|
+
),
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const findings = workflow.jobs
|
|
33
|
+
.filter((job) => findJobEnvironment(job) === 'release' || job.id === 'release')
|
|
34
|
+
.filter((job) => !isReleaseCondition(findMember(job.definition, 'if')))
|
|
35
|
+
.map((job) =>
|
|
36
|
+
buildRuleFinding(
|
|
37
|
+
'release-shape',
|
|
38
|
+
job.line,
|
|
39
|
+
`job ${job.id} releases without the condition of a release; set \`if: \${{ !cancelled() && github.event_name == 'push' && !contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled') }}\`, so a schedule or a dispatch never ships and a failed test job stops it`,
|
|
40
|
+
[job.line],
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const release = workflow.jobs.find((job) => job.id === 'release');
|
|
45
|
+
|
|
46
|
+
if (release !== undefined) {
|
|
47
|
+
const releaseJobs = collectReleaseJobs(workflow.jobs, true);
|
|
48
|
+
const needs = collectNeeds(release);
|
|
49
|
+
|
|
50
|
+
const missing = workflow.jobs.filter(
|
|
51
|
+
(job) => !releaseJobs.has(job.id) && !needs.includes(job.id),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
if (missing.length > 0) {
|
|
55
|
+
findings.push(
|
|
56
|
+
buildRuleFinding(
|
|
57
|
+
'release-shape',
|
|
58
|
+
release.line,
|
|
59
|
+
`job release does not need ${missing.map((job) => job.id).join(', ')}; list every test job in its needs, so it ships only a commit they all passed`,
|
|
60
|
+
[release.line],
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return findings;
|
|
67
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { parseShellWords } from '../../shell/parse-shell-words.ts';
|
|
2
|
+
import { collectSteps } from '../collect-steps.ts';
|
|
3
|
+
import { findMember } from '../find-member.ts';
|
|
4
|
+
import type { Job, RepoMove } from '../types.ts';
|
|
5
|
+
import { isWorkflowCall } from './is-workflow-call.ts';
|
|
6
|
+
|
|
7
|
+
const filterActionPattern =
|
|
8
|
+
/^(?:dorny\/paths-filter|tj-actions\/changed-files|step-security\/changed-files|trilom\/file-changes-action|jitterbit\/get-changed-files)(?:@|$)/u;
|
|
9
|
+
|
|
10
|
+
const scriptActionPattern = /^actions\/github-script(?:@|$)/u;
|
|
11
|
+
|
|
12
|
+
// A github-script step that lists the files of the pull request through the API, by an Octokit
|
|
13
|
+
// method or by the REST route of the pull request's files or of a comparison.
|
|
14
|
+
const fileListPattern =
|
|
15
|
+
/\b(?:listFiles|compareCommits(?:WithBasehead)?)\b|\/pulls\/[^/\s'"`]+\/files\b|\/compare\/[^\s'"`]+/u;
|
|
16
|
+
|
|
17
|
+
// What the job uses to read the files a pull request changes, other than paths-changed.yml, so that
|
|
18
|
+
// it can skip on them. A step that only prints the files, such as a bare `git diff --name-only`,
|
|
19
|
+
// skips nothing.
|
|
20
|
+
export function findFileFilter(job: Job, move: RepoMove): string | undefined {
|
|
21
|
+
if (isWorkflowCall(findMember(job.definition, 'uses'), 'paths-changed.yml', move)) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const step of collectSteps(job)) {
|
|
26
|
+
const action = findMember(step, 'uses');
|
|
27
|
+
const run = findMember(step, 'run');
|
|
28
|
+
const script = findMember(findMember(step, 'with'), 'script');
|
|
29
|
+
|
|
30
|
+
if (typeof action === 'string' && filterActionPattern.test(action)) {
|
|
31
|
+
return action.split('@')[0];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (
|
|
35
|
+
typeof action === 'string' &&
|
|
36
|
+
scriptActionPattern.test(action) &&
|
|
37
|
+
typeof script === 'string' &&
|
|
38
|
+
fileListPattern.test(script)
|
|
39
|
+
) {
|
|
40
|
+
return 'actions/github-script';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (typeof run === 'string' && isDiffOutputUsed(run)) {
|
|
44
|
+
return 'git diff --name-only';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const diffSubstitutionPattern = /(?:\$\(|`)\s*git\s+diff\b[^)`]*--name-/u;
|
|
52
|
+
|
|
53
|
+
// Whether a git diff that lists file names pipes or redirects its output, or sits in a command
|
|
54
|
+
// substitution, which is how a run keeps the list.
|
|
55
|
+
function isDiffOutputUsed(run: string): boolean {
|
|
56
|
+
if (diffSubstitutionPattern.test(run)) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const words = parseShellWords(run);
|
|
61
|
+
let start = 0;
|
|
62
|
+
|
|
63
|
+
for (const [index, word] of [...words, undefined].entries()) {
|
|
64
|
+
if (word !== undefined && !word.isOperator) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const command = words.slice(start, index);
|
|
69
|
+
|
|
70
|
+
if (isNameListDiff(command.map((part) => part.text)) && isConsumed(command, word?.text)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
start = index + 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isNameListDiff(command: readonly string[]): boolean {
|
|
81
|
+
const [name, subcommand, ...rest] = command;
|
|
82
|
+
|
|
83
|
+
return name === 'git' && subcommand === 'diff' && rest.some((word) => word.startsWith('--name-'));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isConsumed(
|
|
87
|
+
command: readonly { readonly text: string }[],
|
|
88
|
+
operator: string | undefined,
|
|
89
|
+
): boolean {
|
|
90
|
+
return operator === '|' || command.some((word) => word.text.startsWith('>'));
|
|
91
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { collectRunCommands } from '../collect-run-commands.ts';
|
|
2
|
+
import { collectSteps } from '../collect-steps.ts';
|
|
3
|
+
import { findMember } from '../find-member.ts';
|
|
4
|
+
import { toUnwrappedCommand } from '../to-unwrapped-command.ts';
|
|
5
|
+
import type { Job } from '../types.ts';
|
|
6
|
+
|
|
7
|
+
const buildPushActionPattern = /^docker\/build-push-action(?:@|$)/u;
|
|
8
|
+
|
|
9
|
+
// What the job runs to build an image or a binary that ships, in a step of the workflow itself.
|
|
10
|
+
// A build inside a test script is out of sight, which is where a PR job may keep one.
|
|
11
|
+
export function findImageBuild(job: Job): string | undefined {
|
|
12
|
+
for (const step of collectSteps(job)) {
|
|
13
|
+
const action = findMember(step, 'uses');
|
|
14
|
+
const run = findMember(step, 'run');
|
|
15
|
+
|
|
16
|
+
if (typeof action === 'string' && buildPushActionPattern.test(action)) {
|
|
17
|
+
return 'docker/build-push-action';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const command =
|
|
21
|
+
typeof run === 'string'
|
|
22
|
+
? collectRunCommands(run)
|
|
23
|
+
.map((words) =>
|
|
24
|
+
findBuildCommandName(toUnwrappedCommand(words.map((word) => word.text))),
|
|
25
|
+
)
|
|
26
|
+
.find((name) => name !== undefined)
|
|
27
|
+
: undefined;
|
|
28
|
+
|
|
29
|
+
if (command !== undefined) {
|
|
30
|
+
return command;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function findBuildCommandName(words: readonly string[]): string | undefined {
|
|
38
|
+
const [first, second, third] = words;
|
|
39
|
+
|
|
40
|
+
if (first === 'docker' && second === 'build') {
|
|
41
|
+
return 'docker build';
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (first === 'docker' && second === 'buildx' && third === 'build') {
|
|
45
|
+
return 'docker buildx build';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (first === 'docker' && second === 'compose' && third === 'build') {
|
|
49
|
+
return 'docker compose build';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (first === 'docker-compose' && second === 'build') {
|
|
53
|
+
return 'docker-compose build';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return first === 'bun' && second === 'build' && words.includes('--compile')
|
|
57
|
+
? 'bun build --compile'
|
|
58
|
+
: undefined;
|
|
59
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { findMember } from '../find-member.ts';
|
|
2
|
+
import type { Job } from '../types.ts';
|
|
3
|
+
|
|
4
|
+
export function findJobEnvironment(job: Job): string | undefined {
|
|
5
|
+
const environment = findMember(job.definition, 'environment');
|
|
6
|
+
const name = typeof environment === 'string' ? environment : findMember(environment, 'name');
|
|
7
|
+
|
|
8
|
+
return typeof name === 'string' ? name : undefined;
|
|
9
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { collectExpressionReferences } from '../collect-expression-references.ts';
|
|
2
|
+
import { collectRunnerLabels } from '../collect-runner-labels.ts';
|
|
3
|
+
import { findMember } from '../find-member.ts';
|
|
4
|
+
import { isList } from '../is-list.ts';
|
|
5
|
+
import type { Job } from '../types.ts';
|
|
6
|
+
import { isARMLabel } from './is-arm-label.ts';
|
|
7
|
+
|
|
8
|
+
// The first runner a matrix gives `runs-on` that is not an ubuntu x64 one, when `runs-on` reads a
|
|
9
|
+
// matrix key such as `${{ matrix.os }}`. The labels come from the key's list and from each include
|
|
10
|
+
// entry, which GitHub merges into the matrix.
|
|
11
|
+
export function findMatrixRunner(job: Job): string | undefined {
|
|
12
|
+
const matrix = findMember(findMember(job.definition, 'strategy'), 'matrix');
|
|
13
|
+
|
|
14
|
+
const references = collectRunnerLabels(findMember(job.definition, 'runs-on')).flatMap((label) =>
|
|
15
|
+
collectExpressionReferences(label),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
const keys = references.flatMap((reference) =>
|
|
19
|
+
reference.startsWith('matrix.') ? [reference.slice('matrix.'.length)] : [],
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const include = findMember(matrix, 'include');
|
|
23
|
+
const entries = isList(include) ? include : [];
|
|
24
|
+
const labels: unknown[] = [];
|
|
25
|
+
|
|
26
|
+
for (const key of keys) {
|
|
27
|
+
labels.push(...[findMember(matrix, key)].flat());
|
|
28
|
+
|
|
29
|
+
for (const entry of entries) {
|
|
30
|
+
labels.push(findMember(entry, key));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return labels.find(
|
|
35
|
+
(label): label is string =>
|
|
36
|
+
typeof label === 'string' && (!label.startsWith('ubuntu-') || isARMLabel(label)),
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { collectRunCommands } from '../collect-run-commands.ts';
|
|
2
|
+
import { collectSteps } from '../collect-steps.ts';
|
|
3
|
+
import { findMember } from '../find-member.ts';
|
|
4
|
+
import { toUnwrappedCommand } from '../to-unwrapped-command.ts';
|
|
5
|
+
import type { Job } from '../types.ts';
|
|
6
|
+
import { findImageBuild } from './find-image-build.ts';
|
|
7
|
+
|
|
8
|
+
// What the job runs to build an image or a binary that ships. Beyond an image, a compiled binary
|
|
9
|
+
// of Go, Rust or Deno counts, which a PR job may build too and so is no fault there.
|
|
10
|
+
export function findShippedBuild(job: Job): string | undefined {
|
|
11
|
+
return (
|
|
12
|
+
findImageBuild(job) ??
|
|
13
|
+
collectSteps(job)
|
|
14
|
+
.flatMap((step) => {
|
|
15
|
+
const run = findMember(step, 'run');
|
|
16
|
+
|
|
17
|
+
return typeof run === 'string' ? collectRunCommands(run) : [];
|
|
18
|
+
})
|
|
19
|
+
.map((words) => findBinaryBuildName(toUnwrappedCommand(words.map((word) => word.text))))
|
|
20
|
+
.find((name) => name !== undefined)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function findBinaryBuildName(words: readonly string[]): string | undefined {
|
|
25
|
+
const [first, second] = words;
|
|
26
|
+
|
|
27
|
+
if (first === 'cargo' && second === 'build' && words.includes('--release')) {
|
|
28
|
+
return 'cargo build --release';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (first === 'go' && second === 'build' && words.includes('-o')) {
|
|
32
|
+
return 'go build -o';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return first === 'deno' && second === 'compile' ? 'deno compile' : undefined;
|
|
36
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { collectConditionTerms } from './collect-condition-terms.ts';
|
|
2
|
+
|
|
3
|
+
// What the release job must hold, as terms without whitespace. !cancelled() lets the job run when a
|
|
4
|
+
// test job was skipped for its files, and the two contains() terms stop it after a failed or
|
|
5
|
+
// cancelled test job.
|
|
6
|
+
const requiredTerms = [
|
|
7
|
+
"github.event_name=='push'",
|
|
8
|
+
'!cancelled()',
|
|
9
|
+
"!contains(needs.*.result,'failure')",
|
|
10
|
+
"!contains(needs.*.result,'cancelled')",
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
// Whether the condition runs a job only on a push, after every job it needs succeeded or was
|
|
14
|
+
// skipped.
|
|
15
|
+
export function isReleaseCondition(condition: unknown): boolean {
|
|
16
|
+
const terms = collectConditionTerms(condition);
|
|
17
|
+
|
|
18
|
+
return terms !== undefined && requiredTerms.every((term) => terms.includes(term));
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { findChexArgs } from '../../ci-plan/find-chex-args.ts';
|
|
2
|
+
import { parseChexArgs } from '../../cli/parse-chex-args.ts';
|
|
3
|
+
import { collectRunCommands } from '../collect-run-commands.ts';
|
|
4
|
+
|
|
5
|
+
// Whether a package script runs shared checks and nothing else. A script that adds a command of its
|
|
6
|
+
// own to a shared check leaves that command out of the checks that every run makes.
|
|
7
|
+
export function isSharedCheckScript(script: string | undefined): boolean {
|
|
8
|
+
if (script === undefined) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const commands = collectRunCommands(script);
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
commands.length > 0 &&
|
|
16
|
+
commands.every((words) => {
|
|
17
|
+
const args = findChexArgs(words.map((word) => word.text));
|
|
18
|
+
|
|
19
|
+
return args !== undefined && parseChexArgs(args).kind === 'checks';
|
|
20
|
+
})
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RepoMove, WorkflowContext } from '../types.ts';
|
|
2
|
+
import { findJobEnvironment } from './find-job-environment.ts';
|
|
3
|
+
|
|
4
|
+
// In tools, npm-publish.yml and each workflow but main.yml with a job in the release environment is
|
|
5
|
+
// a shared release workflow, which a repo's release job calls. The main run of tools is its own
|
|
6
|
+
// release workflow, so its test jobs hold to the rules of any other repo.
|
|
7
|
+
export function isSharedReleaseWorkflow(workflow: WorkflowContext, move: RepoMove): boolean {
|
|
8
|
+
return (
|
|
9
|
+
move === 'tools' &&
|
|
10
|
+
!workflow.path.endsWith('/main.yml') &&
|
|
11
|
+
(workflow.path.endsWith('/npm-publish.yml') ||
|
|
12
|
+
workflow.jobs.some((job) => findJobEnvironment(job) === 'release'))
|
|
13
|
+
);
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RepoMove } from '../types.ts';
|
|
2
|
+
|
|
3
|
+
// Whether a job's uses calls the named workflow file from zgeoff/tools, or by local path in tools
|
|
4
|
+
// itself. A repo's own copy of the workflow, or a fork's, does not count.
|
|
5
|
+
export function isWorkflowCall(uses: unknown, file: string, move: RepoMove): boolean {
|
|
6
|
+
const prefix = move === 'tools' ? String.raw`(?:\./|zgeoff/tools/)` : 'zgeoff/tools/';
|
|
7
|
+
|
|
8
|
+
return (
|
|
9
|
+
typeof uses === 'string' &&
|
|
10
|
+
new RegExp(`^${prefix}\\.github/workflows/${RegExp.escape(file)}(?:@.*)?$`, 'u').test(uses)
|
|
11
|
+
);
|
|
12
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { MovedRepo, WorkflowContext } from '../types.ts';
|
|
2
|
+
|
|
3
|
+
export const ruleNames = [
|
|
4
|
+
'main-only-job-in-pr',
|
|
5
|
+
'pr-gate-missing',
|
|
6
|
+
'main-run-concurrency',
|
|
7
|
+
'main-run-shape',
|
|
8
|
+
'release-shape',
|
|
9
|
+
'path-filter',
|
|
10
|
+
'hook-check-missing-in-ci',
|
|
11
|
+
'publish-outside-release',
|
|
12
|
+
] as const;
|
|
13
|
+
|
|
14
|
+
export type RuleName = (typeof ruleNames)[number];
|
|
15
|
+
|
|
16
|
+
export interface RuleFinding {
|
|
17
|
+
readonly rule: RuleName;
|
|
18
|
+
|
|
19
|
+
// Relative to the repo root; the workflow's own path when absent.
|
|
20
|
+
readonly path: string | undefined;
|
|
21
|
+
|
|
22
|
+
readonly line: number;
|
|
23
|
+
|
|
24
|
+
// Starts with the rule's name and a colon.
|
|
25
|
+
readonly message: string;
|
|
26
|
+
|
|
27
|
+
// The 1-based lines of the keys whose chex-allow comment turns the rule off for this finding.
|
|
28
|
+
readonly allowAt: readonly number[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type WorkflowRule = (workflow: WorkflowContext, repo: MovedRepo) => RuleFinding[];
|
|
@@ -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
|
+
}
|