Skip to content

Commit cef6478

Browse files
committed
Implemented rules
1 parent 1cb4570 commit cef6478

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

src/default_cmd.ts

+7
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,27 @@ exports.handler = async(argv: any) => {
2222
if (argv.list !== undefined) {
2323
const pipelineIid = predefinedVariables.getPipelineIid(cwd);
2424
const parser = new Parser(cwd, pipelineIid);
25+
await parser.initJobs();
26+
parser.validateNeedsTags();
27+
2528
await Commander.runList(parser);
2629
return;
2730
}
2831

2932
if (argv.job) {
3033
const pipelineIid = predefinedVariables.getPipelineIid(cwd);
3134
const parser = new Parser(cwd, pipelineIid);
35+
await parser.initJobs();
3236
parser.validateNeedsTags();
37+
3338
await Commander.runSingleJob(parser, argv.job as string, argv.needs as boolean);
3439
} else {
3540
predefinedVariables.incrementPipelineIid(cwd);
3641
const pipelineIid = predefinedVariables.getPipelineIid(cwd);
3742
const parser = new Parser(cwd, pipelineIid);
43+
await parser.initJobs();
3844
parser.validateNeedsTags();
45+
3946
await Commander.runPipeline(parser, argv.manual as string[] || []);
4047
}
4148
};

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const argv = yargs
4444
const cwd = a.cwd as string || process.cwd();
4545
const pipelineIid = predefinedVariables.getPipelineIid(cwd);
4646
const parser = new Parser(cwd, pipelineIid, true);
47+
await parser.initJobs();
4748
return parser.getJobNames();
4849
})
4950
.argv;

src/job.ts

+27-3
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, execSync} from "child_process";
33
import * as deepExtend from "deep-extend";
44
import * as fs from "fs-extra";
55
import * as glob from "glob";
@@ -19,8 +19,6 @@ export class Job {
1919
public readonly name: string;
2020
public readonly needs: string[] | null;
2121
public readonly stage: string;
22-
public readonly allowFailure: boolean;
23-
public readonly when: string;
2422
public readonly maxJobNameLength: number;
2523
public readonly stageIndex: number;
2624

@@ -32,6 +30,10 @@ export class Job {
3230
private readonly scripts: string[] = [];
3331
private readonly variables: { [key: string]: string };
3432
private readonly predefinedVariables: { [key: string]: string };
33+
private readonly rules: any;
34+
35+
public allowFailure: boolean;
36+
public when: string;
3537

3638
private prescriptsExitCode = 0;
3739
private afterScriptsExitCode = 0;
@@ -86,6 +88,7 @@ export class Job {
8688
this.allowFailure = jobData.allow_failure || false;
8789
this.variables = jobData.variables || {};
8890
this.needs = jobData.needs || null;
91+
this.rules = jobData.rules || null;
8992

9093
this.predefinedVariables = {
9194
CI_COMMIT_SHORT_SHA: "a33bd89c", // Changes
@@ -119,6 +122,27 @@ export class Job {
119122
};
120123
}
121124

125+
public async initRules() {
126+
if (!this.rules) {
127+
return
128+
}
129+
for (const rule of this.rules) {
130+
try {
131+
if (rule['if']) {
132+
const output = execSync(`[ ${rule['if']} ] && exit 0 || exit 1`, {cwd: this.cwd, env: this.getEnvs(), shell: 'bash'});
133+
if (output.length > 0) {
134+
process.stderr.write(`Rule output ${output}`);
135+
}
136+
}
137+
this.when = rule['when'] ? rule['when'] : this.when;
138+
this.allowFailure = rule['allowFailure'] ? rule['allowFailure'] : this.allowFailure;
139+
break;
140+
} catch (e) {
141+
// By pass rule on exit 1
142+
}
143+
}
144+
}
145+
122146
public getPrescriptsExitCode() {
123147
return this.prescriptsExitCode;
124148
}

src/parser.ts

+14
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ export class Parser {
1919
private readonly stages: Map<string, Stage> = new Map();
2020
private readonly cwd: string;
2121
private readonly bashCompletionPhase: boolean;
22+
private readonly pipelineIid: number;
23+
private readonly gitlabData: any;
2224

2325
public constructor(cwd: any, pipelineIid: number, bashCompletionPhase: boolean = false) {
2426
let path = '';
2527
let yamlDataList: any[] = [];
2628

2729
this.bashCompletionPhase = bashCompletionPhase;
2830
this.cwd = cwd;
31+
this.pipelineIid = pipelineIid;
2932

3033
path = `${cwd}/.gitlab-ci.yml`;
3134
const gitlabCiData = Parser.loadYaml(path);
@@ -65,6 +68,14 @@ export class Parser {
6568
this.maxJobNameLength = Math.max(this.maxJobNameLength, key.length);
6669
}
6770

71+
this.gitlabData = gitlabData;
72+
}
73+
74+
public async initJobs() {
75+
const pipelineIid = this.pipelineIid;
76+
const cwd = this.cwd;
77+
const gitlabData = this.gitlabData;
78+
const promises = [];
6879
// Generate jobs and put them into stages
6980
for (const [key, value] of Object.entries(gitlabData)) {
7081
if (this.illigalJobNames.includes(key) || key[0] === ".") {
@@ -73,6 +84,7 @@ export class Parser {
7384

7485
const jobId = predefinedVariables.getJobId(cwd);
7586
const job = new Job(value, key, gitlabData.stages, cwd, gitlabData, pipelineIid, jobId, this.maxJobNameLength);
87+
promises.push(job.initRules());
7688
const stage = this.stages.get(job.stage);
7789
if (stage) {
7890
stage.addJob(job);
@@ -85,6 +97,8 @@ export class Parser {
8597

8698
this.jobs.set(key, job);
8799
}
100+
101+
await Promise.all(promises);
88102
}
89103

90104
public static loadYaml(filePath: string): any {

0 commit comments

Comments
 (0)