Skip to content

Commit a7118db

Browse files
authored
fix: Bad expansion for nested rules reference (#1596)
1 parent bf655d1 commit a7118db

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/data-expander.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,13 @@ function normalizeGlobalVariables (gitlabData: any) {
234234
export function flattenLists (gitlabData: any) {
235235
traverse(gitlabData, ({parent, key, value, meta}) => {
236236
if (parent != null && key != null && Array.isArray(value)) {
237-
parent[key] = value.flat(9);
238-
assert(!parent[key].some(Array.isArray), chalk`This Gitlab CI configuration is invalid: {blueBright ${meta.nodePath}} config should be string or a nested array of strings up to 10 level deep`);
237+
assert(!value.flat(9).some(Array.isArray), chalk`This Gitlab CI configuration is invalid: {blueBright ${meta.nodePath}} config should be string or a nested array of strings up to 10 level deep`);
239238
}
240239
}, {cycleHandling: true});
240+
241+
traverse(gitlabData, ({parent, key, value}) => {
242+
if (parent != null && key != null && Array.isArray(value)) {
243+
parent[key] = value.flat(9);
244+
}
245+
}, {cycleHandling: false});
241246
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
.base_template:
3+
variables:
4+
MY_VAR: MY_VAL
5+
rules:
6+
- if: $MY_VAR == "MY_VAL"
7+
when: always
8+
9+
.job_template1:
10+
extends: [.base_template]
11+
rules: !reference [.base_template, rules]
12+
13+
.job_template2:
14+
extends: [.base_template]
15+
rules:
16+
- !reference [.base_template, rules]
17+
18+
test-job:
19+
extends: [.job_template2]
20+
script:
21+
- echo "hello world job"
22+
rules: !reference [.job_template2, rules]

tests/test-cases/reference/integration.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,32 @@ test-job:
205205

206206
expect(writeStreams.stdoutLines.join("\n")).toEqual(expected.trim());
207207
});
208+
209+
it("should have a lower precedence than a local scope", async () => {
210+
const writeStreams = new WriteStreamsMock();
211+
await handler({
212+
preview: true,
213+
file: ".gitlab-ci-3.yml",
214+
cwd: "tests/test-cases/reference",
215+
}, writeStreams);
216+
217+
const expected = `
218+
---
219+
stages:
220+
- .pre
221+
- build
222+
- test
223+
- deploy
224+
- .post
225+
test-job:
226+
variables:
227+
MY_VAR: MY_VAL
228+
rules:
229+
- if: $MY_VAR == "MY_VAL"
230+
when: always
231+
script:
232+
- echo "hello world job"
233+
`;
234+
235+
expect(writeStreams.stdoutLines.join("\n")).toEqual(expected.trim());
236+
});

0 commit comments

Comments
 (0)