Skip to content

Commit 89e5cf6

Browse files
committed
feat(ordering): execute commands using correct priority order
1 parent 7c79080 commit 89e5cf6

File tree

3 files changed

+227
-13
lines changed

3 files changed

+227
-13
lines changed

src/api/config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ export enum CommandType {
2121
after_script = 'after_script'
2222
}
2323

24+
export enum CommandTypePriority {
25+
git = 1,
26+
before_install = 2,
27+
install = 3,
28+
before_script = 4,
29+
script = 5,
30+
before_cache = 6,
31+
restore_cache = 7,
32+
store_cache = 8,
33+
after_success = 9,
34+
after_failure = 10,
35+
before_deploy = 11,
36+
deploy = 12,
37+
after_deploy = 13,
38+
after_script = 14
39+
}
40+
2441
export enum JobStage {
2542
test = 'test',
2643
deploy = 'deploy'

src/api/process.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { generateRandomId, getFilePath } from './utils';
44
import { getRepositoryByBuildId } from './db/repository';
55
import { Observable } from 'rxjs';
66
import { green, red, bold, yellow, blue, cyan } from 'chalk';
7-
import { CommandType, Command } from './config';
7+
import { CommandType, Command, CommandTypePriority } from './config';
88
import { JobProcess } from './process-manager';
99

1010
export interface Job {
@@ -26,6 +26,18 @@ export interface ProcessOutput {
2626
data: any;
2727
}
2828

29+
export function prepareCommands(proc: JobProcess, allowed: CommandType[]): any {
30+
let commands = proc.commands.filter(command => allowed.findIndex(c => c === command.type) !== -1);
31+
return commands.sort((a, b) => {
32+
if (CommandTypePriority[a.type] > CommandTypePriority[b.type]) {
33+
return 1;
34+
} else if (CommandTypePriority[a.type] < CommandTypePriority[b.type]) {
35+
return -1;
36+
}
37+
return proc.commands.indexOf(a) - proc.commands.indexOf(b);
38+
});
39+
}
40+
2941
export function startBuildProcess(
3042
proc: JobProcess,
3143
variables: string[],
@@ -43,18 +55,16 @@ export function startBuildProcess(
4355
.concat(variables)
4456
.filter(Boolean);
4557

46-
const gitCommands = proc.commands.filter(command => command.type === CommandType.git);
47-
const installCommands = proc.commands.filter(command => {
48-
return command.type === CommandType.before_install || command.type === CommandType.install;
49-
});
50-
const scriptCommands = proc.commands.filter(command => {
51-
return command.type === CommandType.before_script || command.type === CommandType.script ||
52-
command.type === CommandType.after_success || command.type === CommandType.after_failure;
53-
});
54-
const deployCommands = proc.commands.filter(command => {
55-
return command.type === CommandType.before_deploy || command.type === CommandType.deploy ||
56-
command.type === CommandType.after_deploy || command.type === CommandType.after_script;
57-
});
58+
const gitTypes = [CommandType.git];
59+
const installTypes = [CommandType.before_install, CommandType.install];
60+
const scriptTypes = [CommandType.before_script, CommandType.script,
61+
CommandType.after_success, CommandType.after_failure];
62+
const deployTypes = [CommandType.before_deploy, CommandType.deploy,
63+
CommandType.after_deploy, CommandType.after_script];
64+
const gitCommands = prepareCommands(proc, gitTypes);
65+
const installCommands = prepareCommands(proc, installTypes);
66+
const scriptCommands = prepareCommands(proc, scriptTypes);
67+
const deployCommands = prepareCommands(proc, deployTypes);
5868

5969
let restoreCache: Observable<any> = Observable.empty();
6070
let saveCache: Observable<any> = Observable.empty();
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import * as chai from 'chai';
2+
import * as chaiAsPromised from 'chai-as-promised';
3+
import { prepareCommands } from '../../src/api/process';
4+
import { CommandType } from '../../src/api/config';
5+
6+
chai.use(chaiAsPromised);
7+
const expect = chai.expect;
8+
9+
describe('Test preparing commands', () => {
10+
it(`filter and sort commands for git type`, () => {
11+
const process: any = {
12+
build_id: 502,
13+
job_id: 2123,
14+
status: 'queued',
15+
commands:
16+
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
17+
{ command: 'git fetch origin master', type: 'git' },
18+
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
19+
{ command: 'nvm install $NODE_VERSION', type: 'install' },
20+
{ command: 'npm install', type: 'install' },
21+
{ command: 'npm run-script test', type: 'script' } ],
22+
cache: [ 'node_modules' ],
23+
repo_name: 'Izak88/d3-bundle',
24+
branch: 'master',
25+
env: [ 'NODE_VERSION=8' ],
26+
image_name: 'abstruse',
27+
exposed_ports: null,
28+
log: []
29+
};
30+
31+
const types = [CommandType.git];
32+
let commands = prepareCommands(process, types);
33+
34+
expect(commands[0].command).to.include('git clone https://github.com/Izak88/d3-bundle.');
35+
expect(commands[1].command).to.equals('git fetch origin master');
36+
expect(commands[2].command).to.include('git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5');
37+
expect(commands.length).to.equals(3);
38+
});
39+
40+
it(`filter and sort commands for install and script`, () => {
41+
const process: any = {
42+
build_id: 502,
43+
job_id: 2123,
44+
status: 'queued',
45+
commands:
46+
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
47+
{ command: 'git fetch origin master', type: 'git' },
48+
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
49+
{ command: 'nvm install $NODE_VERSION', type: 'install' },
50+
{ command: 'npm install', type: 'install' },
51+
{ command: 'npm run-script test', type: 'script' } ],
52+
cache: [ 'node_modules' ],
53+
repo_name: 'Izak88/d3-bundle',
54+
branch: 'master',
55+
env: [ 'NODE_VERSION=8' ],
56+
image_name: 'abstruse',
57+
exposed_ports: null,
58+
log: []
59+
};
60+
61+
const types = [CommandType.install, CommandType.script];
62+
let commands = prepareCommands(process, types);
63+
64+
expect(commands[0].command).to.equals('nvm install $NODE_VERSION');
65+
expect(commands[1].command).to.equals('npm install');
66+
expect(commands[2].command).to.equals('npm run-script test');
67+
expect(commands.length).to.equals(3);
68+
});
69+
70+
it(`filter and sort commands for install and script`, () => {
71+
const process: any = {
72+
build_id: 502,
73+
job_id: 2123,
74+
status: 'queued',
75+
commands:
76+
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
77+
{ command: 'git fetch origin master', type: 'git' },
78+
{ command: 'npm run-script test', type: 'script' },
79+
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
80+
{ command: 'nvm install $NODE_VERSION', type: 'install' },
81+
{ command: 'npm install', type: 'install' } ],
82+
cache: [ 'node_modules' ],
83+
repo_name: 'Izak88/d3-bundle',
84+
branch: 'master',
85+
env: [ 'NODE_VERSION=8' ],
86+
image_name: 'abstruse',
87+
exposed_ports: null,
88+
log: []
89+
};
90+
91+
const types = [CommandType.install, CommandType.script];
92+
let commands = prepareCommands(process, types);
93+
94+
expect(commands[0].command).to.equals('nvm install $NODE_VERSION');
95+
expect(commands[1].command).to.equals('npm install');
96+
expect(commands[2].command).to.equals('npm run-script test');
97+
expect(commands.length).to.equals(3);
98+
});
99+
100+
it(`filter and sort commands for install and script`, () => {
101+
const process: any = {
102+
build_id: 502,
103+
job_id: 2123,
104+
status: 'queued',
105+
commands:
106+
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
107+
{ command: 'git fetch origin master', type: 'git' },
108+
{ command: 'npm run-script test', type: 'script' },
109+
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
110+
{ command: 'node ./test.js', type: 'script' },
111+
{ command: 'node deploy.js', type: 'deploy' },
112+
{ command: 'node success.js', type: 'after_success' },
113+
{ command: 'node deploy2.js', type: 'deploy' },
114+
{ command: 'node after_failure.js', type: 'after_failure' },
115+
{ command: 'node before_deploy.js', type: 'before_deploy' },
116+
{ command: 'node after_success.js', type: 'after_success' },
117+
{ command: 'node install.js', type: 'install' },
118+
{ command: 'node after_deploy2.js', type: 'after_deploy' },
119+
{ command: 'node after_script.js', type: 'after_script' },
120+
{ command: 'npm install', type: 'install' } ],
121+
cache: [ 'node_modules' ],
122+
repo_name: 'Izak88/d3-bundle',
123+
branch: 'master',
124+
env: [ 'NODE_VERSION=8' ],
125+
image_name: 'abstruse',
126+
exposed_ports: null,
127+
log: []
128+
};
129+
130+
const types = [CommandType.git, CommandType.script, CommandType.after_failure,
131+
CommandType.deploy, CommandType.after_success, CommandType.before_deploy];
132+
let commands = prepareCommands(process, types);
133+
134+
expect(commands[0].command).to.include('git clone https://github.com/Izak88/d3-bundle.');
135+
expect(commands[1].command).to.equals('git fetch origin master');
136+
expect(commands[2].command).to.include('git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5');
137+
expect(commands[3].command).to.equals('npm run-script test');
138+
expect(commands[4].command).to.equals('node ./test.js');
139+
expect(commands[5].command).to.equals('node success.js');
140+
expect(commands[6].command).to.equals('node after_success.js');
141+
expect(commands[7].command).to.equals('node after_failure.js');
142+
expect(commands[8].command).to.equals('node before_deploy.js');
143+
expect(commands[9].command).to.equals('node deploy.js');
144+
expect(commands[10].command).to.equals('node deploy2.js');
145+
expect(commands.length).to.equals(11);
146+
});
147+
148+
it(`filter and sort commands for install and script`, () => {
149+
const process: any = {
150+
build_id: 502,
151+
job_id: 2123,
152+
status: 'queued',
153+
commands:
154+
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
155+
{ command: 'git fetch origin master', type: 'git' },
156+
{ command: 'npm run-script test', type: 'script' },
157+
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
158+
{ command: 'node ./test.js', type: 'script' },
159+
{ command: 'node deploy.js', type: 'deploy' },
160+
{ command: 'node success.js', type: 'after_success' },
161+
{ command: 'node deploy2.js', type: 'deploy' },
162+
{ command: 'node after_failure.js', type: 'after_failure' },
163+
{ command: 'node before_deploy.js', type: 'before_deploy' },
164+
{ command: 'node after_success.js', type: 'after_success' },
165+
{ command: 'node install.js', type: 'install' },
166+
{ command: 'node after_deploy2.js', type: 'after_deploy' },
167+
{ command: 'node after_script.js', type: 'after_script' },
168+
{ command: 'npm install', type: 'install' } ],
169+
cache: [ 'node_modules' ],
170+
repo_name: 'Izak88/d3-bundle',
171+
branch: 'master',
172+
env: [ 'NODE_VERSION=8' ],
173+
image_name: 'abstruse',
174+
exposed_ports: null,
175+
log: []
176+
};
177+
178+
const types = [CommandType.install, CommandType.after_success];
179+
let commands = prepareCommands(process, types);
180+
181+
expect(commands[0].command).to.equals('node install.js');
182+
expect(commands[1].command).to.equals('npm install');
183+
expect(commands[2].command).to.equals('node success.js');
184+
expect(commands[3].command).to.equals('node after_success.js');
185+
expect(commands.length).to.equals(4);
186+
});
187+
});

0 commit comments

Comments
 (0)