@zgeoff/chex 0.8.7 → 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 +1 -1
- package/lefthook.yml +13 -0
- 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/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,72 @@
|
|
|
1
|
+
import { findMember } from '../find-member.ts';
|
|
2
|
+
import { getTopLevelLine } from '../get-top-level-line.ts';
|
|
3
|
+
import type { MovedRepo, WorkflowContext } from '../types.ts';
|
|
4
|
+
import { buildRuleFinding } from './build-rule-finding.ts';
|
|
5
|
+
import { collectConditionTerms } from './collect-condition-terms.ts';
|
|
6
|
+
import { collectNeeds } from './collect-needs.ts';
|
|
7
|
+
import { isWorkflowCall } from './is-workflow-call.ts';
|
|
8
|
+
import type { RuleFinding } from './types.ts';
|
|
9
|
+
|
|
10
|
+
export function collectPrGateFindings(workflow: WorkflowContext, repo: MovedRepo): RuleFinding[] {
|
|
11
|
+
if (!workflow.path.endsWith('/pr.yml')) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const jobsLine = getTopLevelLine(workflow.lines, 'jobs');
|
|
16
|
+
const checks = workflow.jobs.find((job) => job.id === 'checks');
|
|
17
|
+
const gate = workflow.jobs.find((job) => job.id === 'pr');
|
|
18
|
+
const faults: { line: number; text: string }[] = [];
|
|
19
|
+
|
|
20
|
+
if (
|
|
21
|
+
checks === undefined ||
|
|
22
|
+
!isWorkflowCall(findMember(checks.definition, 'uses'), 'checks.yml', repo.move)
|
|
23
|
+
) {
|
|
24
|
+
faults.push({
|
|
25
|
+
line: checks?.line ?? jobsLine,
|
|
26
|
+
text: 'no job `checks` calls checks.yml; add `checks: { uses: zgeoff/tools/.github/workflows/checks.yml@<sha> }`',
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
gate === undefined ||
|
|
32
|
+
!isWorkflowCall(findMember(gate.definition, 'uses'), 'pr-gate.yml', repo.move)
|
|
33
|
+
) {
|
|
34
|
+
faults.push({
|
|
35
|
+
line: gate?.line ?? jobsLine,
|
|
36
|
+
text: 'no job `pr` calls pr-gate.yml; add it, since the ruleset requires `pr / checks`',
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
const needs = collectNeeds(gate);
|
|
40
|
+
const missing = workflow.jobs.filter((job) => job !== gate && !needs.includes(job.id));
|
|
41
|
+
const terms = collectConditionTerms(findMember(gate.definition, 'if'));
|
|
42
|
+
const needsInput = findMember(findMember(gate.definition, 'with'), 'needs');
|
|
43
|
+
|
|
44
|
+
if (missing.length > 0) {
|
|
45
|
+
faults.push({
|
|
46
|
+
line: gate.line,
|
|
47
|
+
text: `job \`pr\` does not need ${missing.map((job) => job.id).join(', ')}; list every other job in its needs, so a failure or a skip there reaches the gate`,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (terms?.length !== 1 || terms[0] !== 'always()') {
|
|
52
|
+
faults.push({
|
|
53
|
+
line: gate.line,
|
|
54
|
+
text: 'job `pr` has no `if: always()` alone, so a failed job skips the gate instead of failing it, or another term skips it after one; set `if: always()`',
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (
|
|
59
|
+
typeof needsInput !== 'string' ||
|
|
60
|
+
needsInput.replaceAll(/\s+/gu, '') !== `\${{toJSON(needs)}}`
|
|
61
|
+
) {
|
|
62
|
+
faults.push({
|
|
63
|
+
line: gate.line,
|
|
64
|
+
text: `job \`pr\` does not pass \`needs: \${{ toJSON(needs) }}\` to pr-gate.yml; pass it in \`with\`, so the gate reads every result`,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return faults.map((fault) =>
|
|
70
|
+
buildRuleFinding('pr-gate-missing', fault.line, fault.text, [fault.line]),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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, MovedRepo, RepoMove, WorkflowContext } from '../types.ts';
|
|
6
|
+
import { buildRuleFinding } from './build-rule-finding.ts';
|
|
7
|
+
import { collectReleaseJobs } from './collect-release-jobs.ts';
|
|
8
|
+
import { isSharedReleaseWorkflow } from './is-shared-release-workflow.ts';
|
|
9
|
+
import { isWorkflowCall } from './is-workflow-call.ts';
|
|
10
|
+
import type { RuleFinding } from './types.ts';
|
|
11
|
+
|
|
12
|
+
export function collectPublishFindings(workflow: WorkflowContext, repo: MovedRepo): RuleFinding[] {
|
|
13
|
+
if (isSharedReleaseWorkflow(workflow, repo.move)) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const releaseJobs = collectReleaseJobs(workflow.jobs, workflow.path.endsWith('/main.yml'));
|
|
18
|
+
|
|
19
|
+
return workflow.jobs
|
|
20
|
+
.filter((job) => !releaseJobs.has(job.id))
|
|
21
|
+
.flatMap((job) => {
|
|
22
|
+
const publish = findPublish(job, repo.move);
|
|
23
|
+
|
|
24
|
+
return publish === undefined
|
|
25
|
+
? []
|
|
26
|
+
: [
|
|
27
|
+
buildRuleFinding(
|
|
28
|
+
'publish-outside-release',
|
|
29
|
+
job.line,
|
|
30
|
+
`job ${job.id} publishes with ${publish}, outside the release job; move the publish into the release job of main.yml`,
|
|
31
|
+
[job.line],
|
|
32
|
+
),
|
|
33
|
+
];
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function findPublish(job: Job, move: RepoMove): string | undefined {
|
|
38
|
+
if (isWorkflowCall(findMember(job.definition, 'uses'), 'npm-publish.yml', move)) {
|
|
39
|
+
return 'npm-publish.yml';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return collectSteps(job)
|
|
43
|
+
.flatMap((step) => {
|
|
44
|
+
const run = findMember(step, 'run');
|
|
45
|
+
|
|
46
|
+
return typeof run === 'string' ? collectRunCommands(run) : [];
|
|
47
|
+
})
|
|
48
|
+
.map((words) => findPublishCommand(words.map((word) => word.text)))
|
|
49
|
+
.find((command) => command !== undefined);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const publishers = new Set(['npm', 'bun', 'pnpm', 'changeset']);
|
|
53
|
+
|
|
54
|
+
// A publisher starts the command, or follows npx, bunx or bun x. A word further on, as in
|
|
55
|
+
// `echo npm publish`, is text.
|
|
56
|
+
function findPublishCommand(words: readonly string[]): string | undefined {
|
|
57
|
+
const command = toUnwrappedCommand(words);
|
|
58
|
+
const runnerLength = countRunnerWords(command);
|
|
59
|
+
const launched = runnerLength === 0 ? command : dropLeadingOptions(command.slice(runnerLength));
|
|
60
|
+
const [publisher, ...rest] = launched;
|
|
61
|
+
|
|
62
|
+
return publisher !== undefined &&
|
|
63
|
+
publishers.has(publisher) &&
|
|
64
|
+
rest.find((word) => !word.startsWith('-')) === 'publish'
|
|
65
|
+
? `${publisher} publish`
|
|
66
|
+
: undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function countRunnerWords(command: readonly string[]): number {
|
|
70
|
+
const [first, second] = command;
|
|
71
|
+
|
|
72
|
+
if (first === 'npx' || first === 'bunx') {
|
|
73
|
+
return 1;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return first === 'bun' && second === 'x' ? 2 : 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function dropLeadingOptions(words: readonly string[]): string[] {
|
|
80
|
+
const index = words.findIndex((word) => !word.startsWith('-'));
|
|
81
|
+
|
|
82
|
+
return index === -1 ? [] : words.slice(index);
|
|
83
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { findMember } from '../find-member.ts';
|
|
2
|
+
import type { Job } from '../types.ts';
|
|
3
|
+
import { collectConditionTerms } from './collect-condition-terms.ts';
|
|
4
|
+
import { collectNeeds } from './collect-needs.ts';
|
|
5
|
+
import { findJobEnvironment } from './find-job-environment.ts';
|
|
6
|
+
|
|
7
|
+
// The jobs of a release: each job in the release environment, and each job that needs one and runs
|
|
8
|
+
// only when the release succeeded. In main.yml the job `release` counts too, because a job that
|
|
9
|
+
// calls a shared workflow can't set an environment.
|
|
10
|
+
export function collectReleaseJobs(jobs: readonly Job[], isMainRun: boolean): Set<string> {
|
|
11
|
+
const releaseJobs = new Set(
|
|
12
|
+
jobs
|
|
13
|
+
.filter((job) => findJobEnvironment(job) === 'release' || (isMainRun && job.id === 'release'))
|
|
14
|
+
.map((job) => job.id),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
let hasGrown = true;
|
|
18
|
+
|
|
19
|
+
while (hasGrown) {
|
|
20
|
+
hasGrown = false;
|
|
21
|
+
|
|
22
|
+
for (const job of jobs) {
|
|
23
|
+
if (!releaseJobs.has(job.id) && isFollowingRelease(job, releaseJobs)) {
|
|
24
|
+
releaseJobs.add(job.id);
|
|
25
|
+
|
|
26
|
+
hasGrown = true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return releaseJobs;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const statusFunctionPattern = /\b(?:always|cancelled|failure)\s*\(/iu;
|
|
35
|
+
|
|
36
|
+
// A job that needs a release job runs after the release succeeded, unless its condition names a
|
|
37
|
+
// status function, such as always(), which also runs it after a failed or skipped release. Then it
|
|
38
|
+
// must say that the release it needs succeeded.
|
|
39
|
+
function isFollowingRelease(job: Job, releaseJobs: ReadonlySet<string>): boolean {
|
|
40
|
+
const needed = collectNeeds(job).filter((need) => releaseJobs.has(need));
|
|
41
|
+
|
|
42
|
+
if (needed.length === 0) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const condition = findMember(job.definition, 'if');
|
|
47
|
+
|
|
48
|
+
if (typeof condition !== 'string' || !statusFunctionPattern.test(condition)) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const terms = collectConditionTerms(condition);
|
|
53
|
+
|
|
54
|
+
return needed.some((need) => terms?.includes(`needs.${need}.result=='success'`) === true);
|
|
55
|
+
}
|
|
@@ -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[];
|