Skip to content

Commit

Permalink
feat(ordering): execute commands using correct priority order
Browse files Browse the repository at this point in the history
  • Loading branch information
Izak88 committed Oct 19, 2017
1 parent 7c79080 commit 89e5cf6
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ export enum CommandType {
after_script = 'after_script'
}

export enum CommandTypePriority {
git = 1,
before_install = 2,
install = 3,
before_script = 4,
script = 5,
before_cache = 6,
restore_cache = 7,
store_cache = 8,
after_success = 9,
after_failure = 10,
before_deploy = 11,
deploy = 12,
after_deploy = 13,
after_script = 14
}

export enum JobStage {
test = 'test',
deploy = 'deploy'
Expand Down
36 changes: 23 additions & 13 deletions src/api/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { generateRandomId, getFilePath } from './utils';
import { getRepositoryByBuildId } from './db/repository';
import { Observable } from 'rxjs';
import { green, red, bold, yellow, blue, cyan } from 'chalk';
import { CommandType, Command } from './config';
import { CommandType, Command, CommandTypePriority } from './config';
import { JobProcess } from './process-manager';

export interface Job {
Expand All @@ -26,6 +26,18 @@ export interface ProcessOutput {
data: any;
}

export function prepareCommands(proc: JobProcess, allowed: CommandType[]): any {
let commands = proc.commands.filter(command => allowed.findIndex(c => c === command.type) !== -1);
return commands.sort((a, b) => {
if (CommandTypePriority[a.type] > CommandTypePriority[b.type]) {
return 1;
} else if (CommandTypePriority[a.type] < CommandTypePriority[b.type]) {
return -1;
}
return proc.commands.indexOf(a) - proc.commands.indexOf(b);
});
}

export function startBuildProcess(
proc: JobProcess,
variables: string[],
Expand All @@ -43,18 +55,16 @@ export function startBuildProcess(
.concat(variables)
.filter(Boolean);

const gitCommands = proc.commands.filter(command => command.type === CommandType.git);
const installCommands = proc.commands.filter(command => {
return command.type === CommandType.before_install || command.type === CommandType.install;
});
const scriptCommands = proc.commands.filter(command => {
return command.type === CommandType.before_script || command.type === CommandType.script ||
command.type === CommandType.after_success || command.type === CommandType.after_failure;
});
const deployCommands = proc.commands.filter(command => {
return command.type === CommandType.before_deploy || command.type === CommandType.deploy ||
command.type === CommandType.after_deploy || command.type === CommandType.after_script;
});
const gitTypes = [CommandType.git];
const installTypes = [CommandType.before_install, CommandType.install];
const scriptTypes = [CommandType.before_script, CommandType.script,
CommandType.after_success, CommandType.after_failure];
const deployTypes = [CommandType.before_deploy, CommandType.deploy,
CommandType.after_deploy, CommandType.after_script];
const gitCommands = prepareCommands(proc, gitTypes);
const installCommands = prepareCommands(proc, installTypes);
const scriptCommands = prepareCommands(proc, scriptTypes);
const deployCommands = prepareCommands(proc, deployTypes);

let restoreCache: Observable<any> = Observable.empty();
let saveCache: Observable<any> = Observable.empty();
Expand Down
187 changes: 187 additions & 0 deletions tests/unit/070_job_processes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { prepareCommands } from '../../src/api/process';
import { CommandType } from '../../src/api/config';

chai.use(chaiAsPromised);
const expect = chai.expect;

describe('Test preparing commands', () => {
it(`filter and sort commands for git type`, () => {
const process: any = {
build_id: 502,
job_id: 2123,
status: 'queued',
commands:
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
{ command: 'git fetch origin master', type: 'git' },
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
{ command: 'nvm install $NODE_VERSION', type: 'install' },
{ command: 'npm install', type: 'install' },
{ command: 'npm run-script test', type: 'script' } ],
cache: [ 'node_modules' ],
repo_name: 'Izak88/d3-bundle',
branch: 'master',
env: [ 'NODE_VERSION=8' ],
image_name: 'abstruse',
exposed_ports: null,
log: []
};

const types = [CommandType.git];
let commands = prepareCommands(process, types);

expect(commands[0].command).to.include('git clone https://github.com/Izak88/d3-bundle.');
expect(commands[1].command).to.equals('git fetch origin master');
expect(commands[2].command).to.include('git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5');
expect(commands.length).to.equals(3);
});

it(`filter and sort commands for install and script`, () => {
const process: any = {
build_id: 502,
job_id: 2123,
status: 'queued',
commands:
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
{ command: 'git fetch origin master', type: 'git' },
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
{ command: 'nvm install $NODE_VERSION', type: 'install' },
{ command: 'npm install', type: 'install' },
{ command: 'npm run-script test', type: 'script' } ],
cache: [ 'node_modules' ],
repo_name: 'Izak88/d3-bundle',
branch: 'master',
env: [ 'NODE_VERSION=8' ],
image_name: 'abstruse',
exposed_ports: null,
log: []
};

const types = [CommandType.install, CommandType.script];
let commands = prepareCommands(process, types);

expect(commands[0].command).to.equals('nvm install $NODE_VERSION');
expect(commands[1].command).to.equals('npm install');
expect(commands[2].command).to.equals('npm run-script test');
expect(commands.length).to.equals(3);
});

it(`filter and sort commands for install and script`, () => {
const process: any = {
build_id: 502,
job_id: 2123,
status: 'queued',
commands:
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
{ command: 'git fetch origin master', type: 'git' },
{ command: 'npm run-script test', type: 'script' },
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
{ command: 'nvm install $NODE_VERSION', type: 'install' },
{ command: 'npm install', type: 'install' } ],
cache: [ 'node_modules' ],
repo_name: 'Izak88/d3-bundle',
branch: 'master',
env: [ 'NODE_VERSION=8' ],
image_name: 'abstruse',
exposed_ports: null,
log: []
};

const types = [CommandType.install, CommandType.script];
let commands = prepareCommands(process, types);

expect(commands[0].command).to.equals('nvm install $NODE_VERSION');
expect(commands[1].command).to.equals('npm install');
expect(commands[2].command).to.equals('npm run-script test');
expect(commands.length).to.equals(3);
});

it(`filter and sort commands for install and script`, () => {
const process: any = {
build_id: 502,
job_id: 2123,
status: 'queued',
commands:
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
{ command: 'git fetch origin master', type: 'git' },
{ command: 'npm run-script test', type: 'script' },
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
{ command: 'node ./test.js', type: 'script' },
{ command: 'node deploy.js', type: 'deploy' },
{ command: 'node success.js', type: 'after_success' },
{ command: 'node deploy2.js', type: 'deploy' },
{ command: 'node after_failure.js', type: 'after_failure' },
{ command: 'node before_deploy.js', type: 'before_deploy' },
{ command: 'node after_success.js', type: 'after_success' },
{ command: 'node install.js', type: 'install' },
{ command: 'node after_deploy2.js', type: 'after_deploy' },
{ command: 'node after_script.js', type: 'after_script' },
{ command: 'npm install', type: 'install' } ],
cache: [ 'node_modules' ],
repo_name: 'Izak88/d3-bundle',
branch: 'master',
env: [ 'NODE_VERSION=8' ],
image_name: 'abstruse',
exposed_ports: null,
log: []
};

const types = [CommandType.git, CommandType.script, CommandType.after_failure,
CommandType.deploy, CommandType.after_success, CommandType.before_deploy];
let commands = prepareCommands(process, types);

expect(commands[0].command).to.include('git clone https://github.com/Izak88/d3-bundle.');
expect(commands[1].command).to.equals('git fetch origin master');
expect(commands[2].command).to.include('git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5');
expect(commands[3].command).to.equals('npm run-script test');
expect(commands[4].command).to.equals('node ./test.js');
expect(commands[5].command).to.equals('node success.js');
expect(commands[6].command).to.equals('node after_success.js');
expect(commands[7].command).to.equals('node after_failure.js');
expect(commands[8].command).to.equals('node before_deploy.js');
expect(commands[9].command).to.equals('node deploy.js');
expect(commands[10].command).to.equals('node deploy2.js');
expect(commands.length).to.equals(11);
});

it(`filter and sort commands for install and script`, () => {
const process: any = {
build_id: 502,
job_id: 2123,
status: 'queued',
commands:
[ { command: 'git clone https://github.com/Izak88/d3-bundle.git -b master .', type: 'git' },
{ command: 'git fetch origin master', type: 'git' },
{ command: 'npm run-script test', type: 'script' },
{ command: 'git checkout 582e1e2ece8ec7a866885913dd4d8d088068edd5 .', type: 'git' },
{ command: 'node ./test.js', type: 'script' },
{ command: 'node deploy.js', type: 'deploy' },
{ command: 'node success.js', type: 'after_success' },
{ command: 'node deploy2.js', type: 'deploy' },
{ command: 'node after_failure.js', type: 'after_failure' },
{ command: 'node before_deploy.js', type: 'before_deploy' },
{ command: 'node after_success.js', type: 'after_success' },
{ command: 'node install.js', type: 'install' },
{ command: 'node after_deploy2.js', type: 'after_deploy' },
{ command: 'node after_script.js', type: 'after_script' },
{ command: 'npm install', type: 'install' } ],
cache: [ 'node_modules' ],
repo_name: 'Izak88/d3-bundle',
branch: 'master',
env: [ 'NODE_VERSION=8' ],
image_name: 'abstruse',
exposed_ports: null,
log: []
};

const types = [CommandType.install, CommandType.after_success];
let commands = prepareCommands(process, types);

expect(commands[0].command).to.equals('node install.js');
expect(commands[1].command).to.equals('npm install');
expect(commands[2].command).to.equals('node success.js');
expect(commands[3].command).to.equals('node after_success.js');
expect(commands.length).to.equals(4);
});
});

0 comments on commit 89e5cf6

Please sign in to comment.