Skip to content

Commit b72ef62

Browse files
authored
Merge pull request #113 from firecow/fix-multidimensional-scripts
Scripts can be multidimensional, lets support that
2 parents 3d99beb + 2c679df commit b72ef62

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

src/job-expanders.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,23 @@ export function image(gitlabData: any) {
5656
});
5757
}
5858

59+
const expandMultidimension = (inputArr: any) => {
60+
const arr = [];
61+
for (const line of inputArr) {
62+
if (typeof line == "string") {
63+
arr.push(line);
64+
} else {
65+
line.forEach((l: string) => arr.push(l));
66+
}
67+
}
68+
return arr;
69+
};
70+
5971
export function beforeScripts(gitlabData: any) {
6072
Utils.forEachRealJob(gitlabData, (_, jobData) => {
6173
const expandedBeforeScripts = [].concat(jobData.before_script || (gitlabData.default || {}).before_script || gitlabData.before_script || []);
6274
if (expandedBeforeScripts.length > 0) {
63-
jobData.before_script = expandedBeforeScripts;
75+
jobData.before_script = expandMultidimension(expandedBeforeScripts);
6476
}
6577
});
6678
}
@@ -69,7 +81,7 @@ export function afterScripts(gitlabData: any) {
6981
Utils.forEachRealJob(gitlabData, (_, jobData) => {
7082
const expandedAfterScripts = [].concat(jobData.after_script || (gitlabData.default || {}).after_script || gitlabData.after_script || []);
7183
if (expandedAfterScripts.length > 0) {
72-
jobData.after_script = expandedAfterScripts;
84+
jobData.after_script = expandMultidimension(expandedAfterScripts);
7385
}
7486
});
7587
}
@@ -80,6 +92,8 @@ export function scripts(gitlabData: any) {
8092
throw new ExitError(`${blueBright(jobName)} must have script specified`);
8193
}
8294
jobData.script = typeof jobData.script === "string" ? [jobData.script] : jobData.script;
83-
jobData.script = jobData.script ?? [];
95+
if (jobData.script) {
96+
jobData.script = expandMultidimension(jobData.script);
97+
}
8498
});
8599
}

src/job.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export class Job {
274274
let time;
275275
let endTime;
276276

277-
if (scripts.length === 0) {
277+
if (scripts.length === 0 || scripts[0] == null) {
278278
return 0;
279279
}
280280

tests/cases.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ test('before-script <test-job>', async () => {
170170
expect(mockProcessExit).toBeCalledTimes(0);
171171
});
172172

173+
test('script-multidimension <test-job>', async () => {
174+
await defaultCmd.handler({
175+
cwd: 'tests/test-cases/script-multidimension',
176+
job: 'test-job'
177+
});
178+
expect(mockProcessStdout).toHaveBeenCalledWith("Test something\n");
179+
expect(mockProcessStdout).toHaveBeenCalledWith("Test something else\n");
180+
expect(mockProcessStderr).toBeCalledTimes(0);
181+
expect(mockProcessExit).toBeCalledTimes(0);
182+
});
183+
173184
test('before-script-default <test-job>', async () => {
174185
await defaultCmd.handler({
175186
cwd: 'tests/test-cases/before-script-default',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[remote "origin"]
2+
url = [email protected]/gcl/test-case.git
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
test-job:
3+
script:
4+
- - echo "Test something"
5+
- - echo "Test something else"

0 commit comments

Comments
 (0)