Skip to content

Commit 2169c1d

Browse files
authored
Expand variables in rules:changes (#1295)
* test: Check that variables are expanded in rules:changes * fix: Expand variables in `rules:changes:` * refactor: Extract `JobRule` type from `Job` class
1 parent 1e453cc commit 2169c1d

File tree

5 files changed

+69
-28
lines changed

5 files changed

+69
-28
lines changed

src/job.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ const dateFormatter = new Intl.DateTimeFormat(undefined, {
6464
hour12: false,
6565
});
6666

67+
export type JobRule = {
68+
if?: string;
69+
when?: string;
70+
changes?: string[] | {paths: string[]};
71+
exists?: string[];
72+
allow_failure?: boolean;
73+
variables?: {[name: string]: string};
74+
};
75+
6776
export class Job {
6877

6978
static readonly illegalJobNames = new Set([
@@ -78,13 +87,7 @@ export class Job {
7887
readonly dependencies: string[] | null;
7988
readonly environment?: {name: string; url: string | null; deployment_tier: string | null; action: string | null};
8089
readonly jobId: number;
81-
readonly rules?: {
82-
if: string;
83-
when: string;
84-
exists: string[];
85-
allow_failure: boolean;
86-
variables: {[key: string]: string};
87-
}[];
90+
readonly rules?: JobRule[];
8891

8992
readonly allowFailure: boolean | {
9093
exit_codes: number | number[];
@@ -181,6 +184,22 @@ export class Job {
181184
predefinedVariables["CI_REGISTRY"] = `local-registry.${this.gitData.remote.host}`;
182185
predefinedVariables["CI_REGISTRY_IMAGE"] = `$CI_REGISTRY/${this._variables["CI_PROJECT_PATH"].toLowerCase()}`;
183186

187+
// Expand variables in rules:changes
188+
if (this.rules && expandVariables) {
189+
const expanded = Utils.expandVariables(this._variables);
190+
this.rules.forEach((rule, ruleIdx, rules) => {
191+
const changes = Array.isArray(rule.changes) ? rule.changes : rule.changes?.paths;
192+
if (!changes) {
193+
return;
194+
}
195+
196+
changes.forEach((change, changeIdx, changes) => {
197+
changes[changeIdx] = Utils.expandText(change, expanded);
198+
});
199+
rules[ruleIdx].changes = changes;
200+
});
201+
}
202+
184203
// Find environment matched variables
185204
if (this.environment && expandVariables) {
186205
const expanded = Utils.expandVariables(this._variables);

src/utils.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import chalk from "chalk";
2-
import {Job} from "./job";
2+
import {Job, JobRule} from "./job";
33
import * as fs from "fs-extra";
44
import checksum from "checksum";
55
import base64url from "base64url";
@@ -14,14 +14,7 @@ import path from "path";
1414

1515
type RuleResultOpt = {
1616
cwd: string;
17-
rules: {
18-
if?: string;
19-
when?: string;
20-
changes?: string[] | {paths: string[]};
21-
exists?: string[];
22-
allow_failure?: boolean;
23-
variables?: {[name: string]: string};
24-
}[];
17+
rules: JobRule[];
2518
variables: {[key: string]: string};
2619
};
2720

tests/test-cases/rules-changes/.gitlab-ci-rules:changes:paths.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ alpine:
77
- "foo"
88
script:
99
- echo "Job is running"
10+
11+
matrix:
12+
image: alpine
13+
parallel:
14+
matrix:
15+
- FILE:
16+
- foo
17+
rules:
18+
- changes:
19+
paths:
20+
- "${FILE}"
21+
script:
22+
- echo "Job is running"

tests/test-cases/rules-changes/.gitlab-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@ alpine:
66
- "foo"
77
script:
88
- echo "Job is running"
9+
10+
matrix:
11+
image: alpine
12+
parallel:
13+
matrix:
14+
- FILE:
15+
- foo
16+
rules:
17+
- changes:
18+
- "${FILE}"
19+
script:
20+
- echo "Job is running"

tests/test-cases/rules-changes/integration.rules-changes.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ test("rules:changes (has changes))", async () => {
1818
cwd: "tests/test-cases/rules-changes",
1919
}, writeStreams);
2020

21-
expect(writeStreams.stdoutLines.join("\n")).toContain(
22-
chalk`{blueBright alpine} {greenBright >} Job is running\n`
23-
);
21+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining([
22+
chalk`{blueBright alpine } {greenBright >} Job is running`,
23+
chalk`{blueBright matrix: [foo]} {greenBright >} Job is running`,
24+
]));
2425
});
2526

2627
test("rules:changes:paths (has changes)", async () => {
@@ -34,9 +35,10 @@ test("rules:changes:paths (has changes)", async () => {
3435
file: ".gitlab-ci-rules:changes:paths.yml",
3536
}, writeStreams);
3637

37-
expect(writeStreams.stdoutLines.join("\n")).toContain(
38-
chalk`{blueBright alpine} {greenBright >} Job is running\n`
39-
);
38+
expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining([
39+
chalk`{blueBright alpine } {greenBright >} Job is running`,
40+
chalk`{blueBright matrix: [foo]} {greenBright >} Job is running`,
41+
]));
4042
});
4143

4244
test("rules:changes (no changes)", async () => {
@@ -49,9 +51,10 @@ test("rules:changes (no changes)", async () => {
4951
cwd: "tests/test-cases/rules-changes",
5052
}, writeStreams);
5153

52-
expect(writeStreams.stdoutLines.join("\n")).not.toContain(
53-
chalk`{blueBright alpine} {greenBright >} Job is running\n`
54-
);
54+
expect(writeStreams.stdoutLines).not.toEqual(expect.arrayContaining([
55+
chalk`{blueBright alpine } {greenBright >} Job is running`,
56+
chalk`{blueBright matrix: [foo]} {greenBright >} Job is running`,
57+
]));
5558
});
5659

5760
test("rules:changes:paths (no changes)", async () => {
@@ -65,7 +68,8 @@ test("rules:changes:paths (no changes)", async () => {
6568
file: ".gitlab-ci-rules:changes:paths.yml",
6669
}, writeStreams);
6770

68-
expect(writeStreams.stdoutLines.join("\n")).not.toContain(
69-
chalk`{blueBright alpine} {greenBright >} Job is running\n`
70-
);
71+
expect(writeStreams.stdoutLines).not.toEqual(expect.arrayContaining([
72+
chalk`{blueBright alpine } {greenBright >} Job is running`,
73+
chalk`{blueBright matrix: [foo]} {greenBright >} Job is running`,
74+
]));
7175
});

0 commit comments

Comments
 (0)