Skip to content

Commit ed04674

Browse files
committed
Make sure that include:remote's aren't fetched during bash completion
1 parent 49aaa2e commit ed04674

File tree

5 files changed

+80
-76
lines changed

5 files changed

+80
-76
lines changed

src/commander.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as c from "ansi-colors";
22

3-
import { Job } from "./job";
4-
import { Parser } from "./parser";
3+
import {Job} from "./job";
4+
import {Parser} from "./parser";
55

66
export class Commander {
77

@@ -83,23 +83,22 @@ export class Commander {
8383
return whenPrio.indexOf(b.when) - whenPrio.indexOf(a.when);
8484
});
8585

86-
8786
let whenPadEnd = 0;
8887
parser.getJobs().forEach(j => whenPadEnd = Math.max(j.when.length, whenPadEnd));
8988

9089
let stagePadEnd = 0;
91-
parser.getStageNames().forEach(s => stagePadEnd = Math.max(s.length , stagePadEnd));
90+
parser.getStageNames().forEach(s => stagePadEnd = Math.max(s.length, stagePadEnd));
9291

9392
let descriptionPadEnd = 0;
9493
parser.getJobs().forEach(j => descriptionPadEnd = Math.max(j.getDescription().length, descriptionPadEnd));
9594

9695
for (const job of jobs) {
9796
const needs = job.needs;
98-
const allowFailure = job.allowFailure ? 'warning' : ''
97+
const allowFailure = job.allowFailure ? 'warning' : '';
9998
let jobLine = `${job.getJobNameString()} ${job.getDescription().padEnd(descriptionPadEnd)}`;
100-
jobLine += ` ${c.yellow(`${job.stage.padEnd(stagePadEnd)}`)} ${job.when.padEnd(whenPadEnd)} ${allowFailure.padEnd(7)}`
99+
jobLine += ` ${c.yellow(`${job.stage.padEnd(stagePadEnd)}`)} ${job.when.padEnd(whenPadEnd)} ${allowFailure.padEnd(7)}`;
101100
if (needs) {
102-
jobLine += ` [${c.blueBright(`${needs.join(',')}`)}]`
101+
jobLine += ` [${c.blueBright(`${needs.join(',')}`)}]`;
103102
}
104103
process.stdout.write(`${jobLine}\n`);
105104
}
@@ -112,15 +111,19 @@ export class Commander {
112111
jobs.push(foundJob);
113112

114113
if (needs) {
115-
// Recursive backwards traversal to find parant needs.
114+
// Recursive backwards traversal to find parent needs.
116115
let needed: string[] = [];
117-
if (foundJob.needs) needed = needed.concat(foundJob.needs);
116+
if (foundJob.needs) {
117+
needed = needed.concat(foundJob.needs);
118+
}
118119
while (needed.length > 0) {
119120
const need = needed.pop();
120121
if (need) {
121122
const needJob = parser.getJobByName(need);
122123
jobs.unshift(needJob);
123-
if (needJob.needs) needed = needed.concat(needJob.needs);
124+
if (needJob.needs) {
125+
needed = needed.concat(needJob.needs);
126+
}
124127
}
125128
}
126129
}
@@ -152,7 +155,7 @@ export class Commander {
152155
};
153156
const afterScripts: any = {
154157
warned: []
155-
}
158+
};
156159

157160
for (const job of jobs) {
158161
if (job.isStarted() && job.getAfterPrescriptsExitCode() !== 0) {
@@ -184,7 +187,7 @@ export class Commander {
184187
} else {
185188
process.stdout.write(`${c.blueBright(`${job.name}`)}, `);
186189
}
187-
}
190+
};
188191

189192
if (preScripts.never.length !== 0) {
190193
process.stdout.write(`${c.magenta("not started")} `);

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {CommandModule} from "yargs";
21
import * as yargs from "yargs";
2+
import {CommandModule} from "yargs";
3+
import * as defaultCmd from "./default_cmd";
34

45
import {Parser} from "./parser";
5-
import * as defaultCmd from "./default_cmd";
66
import * as predefinedVariables from "./predefined_variables";
77

88
process.on('uncaughtException', (err) => {
@@ -43,7 +43,7 @@ const argv = yargs
4343
.completion("completion", false, async (current, a) => {
4444
const cwd = a.cwd as string || process.cwd();
4545
const pipelineIid = predefinedVariables.getPipelineIid(cwd);
46-
const parser = new Parser(cwd, pipelineIid);
46+
const parser = new Parser(cwd, pipelineIid, true);
4747
return parser.getJobNames();
4848
})
4949
.argv;

src/job.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as c from "ansi-colors";
2-
import { spawn } from "child_process";
2+
import {spawn} from "child_process";
33
import * as deepExtend from "deep-extend";
44
import * as fs from "fs-extra";
55
import * as glob from "glob";
@@ -236,7 +236,7 @@ export class Job {
236236
const outputFilesPath = this.getOutputFilesPath();
237237

238238
return new Promise((resolve, reject) => {
239-
const bash = spawn("bash", { cwd: this.cwd, env: this.getEnvs() });
239+
const bash = spawn("bash", {cwd: this.cwd, env: this.getEnvs()});
240240
bash.on("error", (err) => {
241241
reject(err);
242242
});
@@ -293,7 +293,7 @@ export class Job {
293293
}
294294

295295
private getEnvs(): { [key: string]: string } {
296-
const envs: {[key: string]: string} = {...this.globals.variables || {}, ...this.variables, ...process.env, ...this.predefinedVariables};
296+
const envs: { [key: string]: string } = {...this.globals.variables || {}, ...this.variables, ...process.env, ...this.predefinedVariables};
297297
const regex = /\${(.*?)}/g;
298298
let exec;
299299

src/parser.ts

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,30 @@
11
import * as c from "ansi-colors";
2+
import {execSync} from "child_process";
23
import * as deepExtend from "deep-extend";
34
import * as fs from "fs-extra";
45
import * as yaml from "yaml";
5-
6-
import { execSync } from "child_process";
6+
import {Job} from "./job";
77
import * as predefinedVariables from "./predefined_variables";
8-
import { Job } from "./job";
9-
import { Stage } from "./stage";
8+
import {Stage} from "./stage";
109

1110
export class Parser {
1211

13-
public static loadYaml(filePath: string): any {
14-
const gitlabCiLocalYmlPath = `${filePath}`;
15-
if (!fs.existsSync(gitlabCiLocalYmlPath)) {
16-
return {};
17-
}
18-
19-
const fileContent = fs.readFileSync(`${filePath}`, "utf8");
20-
const descRegEx = /#.*?@Description\s?(.*)\s(.*)?:/gm
21-
const parse = yaml.parse(fileContent) || {};
22-
23-
let match;
24-
while (match = descRegEx.exec(fileContent)) {
25-
if (match[1] && match[2]) {
26-
parse[match[2]].description = match[1]
27-
}
28-
}
29-
30-
return parse;
31-
}
32-
12+
public readonly maxJobNameLength: number = 0;
3313
private readonly illigalJobNames = [
3414
"include", "local_configuration", "image", "services",
3515
"stages", "pages", "types", "before_script", "default",
3616
"after_script", "variables", "cache", "include",
3717
];
3818
private readonly jobs: Map<string, Job> = new Map();
39-
40-
public readonly maxJobNameLength: number = 0;
4119
private readonly stages: Map<string, Stage> = new Map();
4220
private readonly cwd: string;
21+
private readonly bashCompletionPhase: boolean;
4322

44-
public constructor(cwd: any, pipelineIid: number) {
23+
public constructor(cwd: any, pipelineIid: number, bashCompletionPhase: boolean = false) {
4524
let path = '';
4625
let yamlDataList: any[] = [];
4726

27+
this.bashCompletionPhase = bashCompletionPhase;
4828
this.cwd = cwd;
4929

5030
path = `${cwd}/.gitlab-ci.yml`;
@@ -107,41 +87,24 @@ export class Parser {
10787
}
10888
}
10989

110-
private prepareIncludes(doc: any): any[] {
111-
let includeDatas: any[] = [];
112-
const cwd = this.cwd;
113-
114-
for (const value of doc["include"] || []) {
115-
if (value["local"]) {
116-
const localDoc = Parser.loadYaml(`${this.cwd}/${value.local}`);
117-
includeDatas = includeDatas.concat(this.prepareIncludes(localDoc));
118-
} else if (value["file"]) {
119-
const ref = value["ref"] || "master";
120-
const file = value["file"];
121-
const project = value["project"];
122-
const gitlabCiLocalPath = `${cwd}/.gitlab-ci-local/includes/${project}/${ref}/`;
123-
124-
const domainRegExp = /^.*@(.*):.*\(fetch\)/;
125-
const output = execSync(`git remote -v`, {
126-
cwd: `${this.cwd}`
127-
});
128-
const exec = domainRegExp.exec(`${output}`);
129-
const gitDomain = exec ? exec[1] : null;
130-
fs.ensureDirSync(gitlabCiLocalPath);
131-
execSync(`git archive --remote=git@${gitDomain}:${project}.git ${ref} --format=zip ${file} | gunzip -c - > ${file}`, {
132-
cwd: gitlabCiLocalPath
133-
});
90+
public static loadYaml(filePath: string): any {
91+
const gitlabCiLocalYmlPath = `${filePath}`;
92+
if (!fs.existsSync(gitlabCiLocalYmlPath)) {
93+
return {};
94+
}
13495

135-
const fileDoc = Parser.loadYaml(`${cwd}/.gitlab-ci-local/includes/${project}/${ref}/${file}`);
136-
includeDatas = includeDatas.concat(this.prepareIncludes(fileDoc));
96+
const fileContent = fs.readFileSync(`${filePath}`, "utf8");
97+
const descRegEx = /#.*?@Description\s?(.*)\s(.*)?:/gm;
98+
const parse = yaml.parse(fileContent) || {};
13799

138-
} else {
139-
process.stderr.write(`Didn't understand include ${JSON.stringify(value)}\n`);
100+
let match;
101+
while (match = descRegEx.exec(fileContent)) {
102+
if (match[1] && match[2]) {
103+
parse[match[2]].description = match[1];
140104
}
141105
}
142106

143-
includeDatas.push(doc);
144-
return includeDatas;
107+
return parse;
145108
}
146109

147110
public getJobByName(name: string): Job {
@@ -192,4 +155,42 @@ export class Parser {
192155

193156
}
194157
}
158+
159+
private prepareIncludes(doc: any): any[] {
160+
let includeDatas: any[] = [];
161+
const cwd = this.cwd;
162+
163+
for (const value of doc["include"] || []) {
164+
if (value["local"]) {
165+
const localDoc = Parser.loadYaml(`${this.cwd}/${value.local}`);
166+
includeDatas = includeDatas.concat(this.prepareIncludes(localDoc));
167+
} else if (value["file"]) {
168+
const ref = value["ref"] || "master";
169+
const file = value["file"];
170+
const project = value["project"];
171+
const gitlabCiLocalPath = `${cwd}/.gitlab-ci-local/includes/${project}/${ref}/`;
172+
173+
const domainRegExp = /^.*@(.*):.*\(fetch\)/;
174+
const output = execSync(`git remote -v`, {
175+
cwd: `${this.cwd}`
176+
});
177+
const exec = domainRegExp.exec(`${output}`);
178+
const gitDomain = exec ? exec[1] : null;
179+
fs.ensureDirSync(gitlabCiLocalPath);
180+
if (!this.bashCompletionPhase || !fs.existsSync(`${cwd}/.gitlab-ci-local/includes/${project}/${ref}/${file}`)) {
181+
execSync(`git archive --remote=git@${gitDomain}:${project}.git ${ref} --format=zip ${file} | gunzip -c - > ${file}`, {
182+
cwd: gitlabCiLocalPath
183+
});
184+
}
185+
const fileDoc = Parser.loadYaml(`${cwd}/.gitlab-ci-local/includes/${project}/${ref}/${file}`);
186+
includeDatas = includeDatas.concat(this.prepareIncludes(fileDoc));
187+
188+
} else {
189+
process.stderr.write(`Didn't understand include ${JSON.stringify(value)}\n`);
190+
}
191+
}
192+
193+
includeDatas.push(doc);
194+
return includeDatas;
195+
}
195196
}

src/stage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Job } from "./job";
1+
import {Job} from "./job";
22

33
export class Stage {
44

0 commit comments

Comments
 (0)