Skip to content

Commit 3645ea0

Browse files
authored
fix: Allow repeated include inputs interpolation (#1294)
* test: Add test case for repeated inputs interpolations * fix: Allow repeated input interpolations We have to make the first two chars optional, because the string might have already been consumed up to the `$[[` by a previous iteration of the `replace` call. The last char has to exclude the `$` so that the current iteration of the `replace` does not consume the beginning of what is supposed to be part of the next iteration. This means that the current iteration might not have a `lastChar` if it is immediately followed by the `$[[` of the next iteration, so `lastChar` has to be optional as well.
1 parent 3e45470 commit 3645ea0

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

src/parser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class Parser {
308308

309309
const interpolatedConfigurations = JSON.stringify(uninterpolatedConfigurations)
310310
.replace(
311-
/(?<firstChar>.)(?<secondChar>.)\$\[\[\s*inputs.(?<interpolationKey>[\w-]+)\s*\|?\s*(?<interpolationFunctions>.*?)\s*\]\](?<lastChar>.)/g // https://regexr.com/81c16
311+
/(?<firstChar>.)?(?<secondChar>.)?\$\[\[\s*inputs.(?<interpolationKey>[\w-]+)\s*\|?\s*(?<interpolationFunctions>.*?)\s*\]\](?<lastChar>[^$])?/g // https://regexr.com/81c16
312312
, (_: string, firstChar: string, secondChar: string, interpolationKey: string, interpolationFunctions: string, lastChar: string) => {
313313
const configFilePath = path.relative(process.cwd(), filePath);
314314
const context = {
@@ -318,6 +318,9 @@ export class Parser {
318318
configFilePath,
319319
...ctx,
320320
};
321+
firstChar ??= "";
322+
secondChar ??= "";
323+
lastChar ??= "";
321324

322325
const {inputValue, inputType} = parseIncludeInputs(context);
323326
const firstTwoChar = firstChar + secondChar;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
spec:
3+
inputs:
4+
name:
5+
---
6+
job $[[ inputs.name ]] $[[ inputs.name ]]:
7+
script:
8+
- echo $[[ inputs.name ]]$[[ inputs.name ]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
include:
3+
- local: "/.gitlab-ci-input-template.yml"
4+
inputs:
5+
name: foo
6+
stages:
7+
- test

tests/test-cases/include-inputs/integration.include-inputs.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,25 @@ scan-website:
135135
expect(writeStreams.stdoutLines[0]).toEqual(expected);
136136
});
137137

138+
test("include-inputs interpolation repeated", async () => {
139+
const writeStreams = new WriteStreamsMock();
140+
await handler({
141+
cwd: "tests/test-cases/include-inputs/input-templates/interpolation-repeat",
142+
preview: true,
143+
}, writeStreams);
144+
145+
const expected = `---
146+
stages:
147+
- .pre
148+
- test
149+
- .post
150+
job foo foo:
151+
script:
152+
- echo foofoo`;
153+
154+
expect(writeStreams.stdoutLines[0]).toEqual(expected);
155+
});
156+
138157
test("include-inputs inputs validation for array", async () => {
139158
try {
140159
const writeStreams = new WriteStreamsMock();

0 commit comments

Comments
 (0)