|
1 | | -import assert from "node:assert"; |
| 1 | +import chalk from "chalk-template"; |
| 2 | +import assert, {AssertionError} from "node:assert"; |
2 | 3 | import deepExtend from "deep-extend"; |
| 4 | +import {Need} from "./job.js"; |
| 5 | + |
| 6 | +type MatrixSelector = NonNullable<Need["parallel"]>["matrix"]; |
| 7 | +type MatrixVariables = {[key: string]: string}; |
3 | 8 |
|
4 | 9 | export function isPlainParallel (jobData: any) { |
5 | 10 | return Number.isInteger(jobData.parallel); |
6 | 11 | } |
7 | 12 |
|
| 13 | +// Expand a parallel.matrix-shaped selector into concrete {key: value} bindings. |
| 14 | +// Mirrors GitLab's expansion: scalars are wrapped to single-element arrays, and |
| 15 | +// arrays produce a cartesian product across keys within an entry. |
| 16 | +export function expandMatrixSelector (matrix: MatrixSelector): MatrixVariables[] { |
| 17 | + const expanded: MatrixVariables[] = []; |
| 18 | + for (const entry of matrix) { |
| 19 | + let inner: MatrixVariables[] = [{}]; |
| 20 | + for (const [key, raw] of Object.entries(entry)) { |
| 21 | + const values = Array.isArray(raw) ? raw : [raw]; |
| 22 | + const next: MatrixVariables[] = []; |
| 23 | + for (const clone of inner) { |
| 24 | + for (const v of values) { |
| 25 | + next.push({...clone, [key]: String(v)}); |
| 26 | + } |
| 27 | + } |
| 28 | + inner = next; |
| 29 | + } |
| 30 | + expanded.push(...inner); |
| 31 | + } |
| 32 | + return expanded; |
| 33 | +} |
| 34 | + |
| 35 | +// Returns true if `producerVars` matches at least one expanded selector entry. |
| 36 | +// Match semantics: every key in the selector entry must equal the producer's value; |
| 37 | +// the producer may have extra keys not mentioned in the selector. |
| 38 | +export function matrixSelectorMatches (producerVars: MatrixVariables | null, matrix: MatrixSelector): boolean { |
| 39 | + if (!producerVars) return false; |
| 40 | + return expandMatrixSelector(matrix).some(sel => |
| 41 | + Object.entries(sel).every(([k, v]) => String(producerVars[k]) === String(v)), |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +// Find the consumer's `need` (if any) that this `producer` job satisfies. |
| 46 | +// Used by both the executor (waitFor) and the artifacts producer pipeline so |
| 47 | +// the two paths apply the same matching rules. |
| 48 | +export function findMatchingNeed (consumerNeeds: ReadonlyArray<Need> | null, producerName: string, producerBaseName: string): Need | undefined { |
| 49 | + return consumerNeeds?.find(n => n.job === producerName || n.job === producerBaseName); |
| 50 | +} |
| 51 | + |
| 52 | +// `$[[ matrix.IDENTIFIER ]]` — see https://docs.gitlab.com/ci/yaml/matrix_expressions/. |
| 53 | +// Identifier charset matches upstream gitlab-org/gitlab's MatrixInterpolator regex |
| 54 | +// `[a-zA-Z0-9_-]+` (letters, digits, underscore, hyphen). |
| 55 | +const MATRIX_EXPR_RE = /\$\[\[\s*matrix\.([a-zA-Z0-9_-]+)\s*\]\]/g; |
| 56 | +const MATRIX_EXPR_TEST_RE = /\$\[\[\s*matrix\./; |
| 57 | + |
| 58 | +function valueContainsMatrixExpr (v: unknown): boolean { |
| 59 | + if (Array.isArray(v)) return v.some(valueContainsMatrixExpr); |
| 60 | + if (typeof v !== "string") return false; |
| 61 | + return MATRIX_EXPR_TEST_RE.test(v); |
| 62 | +} |
| 63 | + |
| 64 | +// Detect whether any need's parallel.matrix selector contains a `$[[ matrix.X ]]` expression. |
| 65 | +// Used to surface a clear error if a non-parallel-matrix consumer references such expressions. |
| 66 | +export function needsContainMatrixExpressions (needs: Need[]): boolean { |
| 67 | + return needs.some(n => |
| 68 | + n.parallel?.matrix?.some(entry => Object.values(entry).some(valueContainsMatrixExpr)) ?? false, |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +function substituteMatrixExpr (value: unknown, consumerVars: MatrixVariables, jobName: string): unknown { |
| 73 | + if (Array.isArray(value)) return value.map(v => substituteMatrixExpr(v, consumerVars, jobName)); |
| 74 | + if (typeof value !== "string") return value; |
| 75 | + return value.replace(MATRIX_EXPR_RE, (_, key) => { |
| 76 | + if (!(key in consumerVars)) { |
| 77 | + // Wording mirrors upstream gitlab-org/gitlab's MatrixInterpolator error |
| 78 | + // ("'X' does not exist in matrix configuration") so log greps line up. |
| 79 | + throw new AssertionError({message: chalk`{blueBright ${jobName}}: '{yellow ${key}}' does not exist in matrix configuration`}); |
| 80 | + } |
| 81 | + return consumerVars[key]; |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +// Substitute `$[[ matrix.X ]]` references inside each need's parallel.matrix |
| 86 | +// selector, using the consumer permutation's own matrix bindings. Returns a |
| 87 | +// shallow-cloned needs array (only entries with parallel.matrix are rebuilt); |
| 88 | +// non-substituted fields on each need are shared with the input by reference. |
| 89 | +export function resolveNeedsMatrixExpressions (needs: Need[], consumerVars: MatrixVariables, jobName: string): Need[] { |
| 90 | + return needs.map(n => { |
| 91 | + if (!n?.parallel?.matrix) return n; |
| 92 | + return { |
| 93 | + ...n, |
| 94 | + parallel: { |
| 95 | + ...n.parallel, |
| 96 | + matrix: n.parallel.matrix.map(entry => |
| 97 | + Object.fromEntries(Object.entries(entry).map(([k, v]) => [k, substituteMatrixExpr(v, consumerVars, jobName)])), |
| 98 | + ) as MatrixSelector, |
| 99 | + }, |
| 100 | + }; |
| 101 | + }); |
| 102 | +} |
| 103 | + |
8 | 104 | export function matrixVariablesList (jobData: any, jobName: string): {[key: string]: string}[] | null[] { |
9 | 105 | if (isPlainParallel(jobData)) { |
10 | 106 | return new Array(jobData.parallel).fill(null); |
|
0 commit comments