Skip to content

Commit

Permalink
feat: added pre and post steps to workflow jobs (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaryt authored Aug 15, 2022
1 parent 3a4875d commit 5b4bea6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 29 deletions.
32 changes: 25 additions & 7 deletions src/lib/Components/Workflow/exports/Workflow.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Generable } from '../..';
import { GenerableType } from '../../../Config/exports/Mapping';
import { Command } from '../../Commands/types/Command.types';
import { Job } from '../../Job';
import { When } from '../../Logic';
import { Conditional } from '../../Logic/exports/Conditional';
import { WorkflowJobParameters, WorkflowsShape } from '../types';
import {
WorkflowContentsShape,
WorkflowJobParameters,
WorkflowsShape,
} from '../types';
import { WorkflowJob } from './WorkflowJob';
import { WorkflowJobAbstract } from './WorkflowJobAbstract';
import { WorkflowJobApproval } from './WorkflowJobApproval';
Expand Down Expand Up @@ -46,30 +51,43 @@ export class Workflow implements Generable, Conditional {
);
}
}

/**
* Generate Workflow Shape.
* @returns The generated JSON for the Workflow.
*/
generate(flatten?: boolean): WorkflowsShape {
return {
[this.name]: this.generateContents(flatten),
};
}

/**
* Generate contents of the Workflow.
*/
generateContents(flatten?: boolean): WorkflowContentsShape {
const generatedWorkflowJobs = this.jobs.map((job) => {
return job.generate(flatten);
});

const generatedWhen = this.when?.generate();

return {
[this.name]: {
when: generatedWhen,
jobs: generatedWorkflowJobs,
},
when: generatedWhen,
jobs: generatedWorkflowJobs,
};
}

/**
* Add a Job to the current Workflow. Chainable
*/
addJob(job: Job, parameters?: WorkflowJobParameters): this {
this.jobs.push(new WorkflowJob(job, parameters));
addJob(
job: Job,
parameters?: WorkflowJobParameters,
pre_steps?: Command[],
post_steps?: Command[],
): this {
this.jobs.push(new WorkflowJob(job, parameters, pre_steps, post_steps));
return this;
}

Expand Down
24 changes: 24 additions & 0 deletions src/lib/Components/Workflow/exports/WorkflowJob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { OrbRef } from '../../../Orb/exports/OrbRef';
import { AnyCommandShape } from '../../Commands/types/Command.types';
import { Job } from '../../Job';
import { StepsParameter } from '../../Parameters/types';
import { JobParameterLiteral } from '../../Parameters/types/CustomParameterLiterals.types';
import {
WorkflowJobContentsShape,
WorkflowJobParameters,
WorkflowJobShape,
} from '../types/WorkflowJob.types';
Expand All @@ -15,13 +18,27 @@ import { WorkflowJobAbstract } from './WorkflowJobAbstract';
*/
export class WorkflowJob extends WorkflowJobAbstract {
job: Job | OrbRef<JobParameterLiteral>;
pre_steps?: StepsParameter;
post_steps?: StepsParameter;

constructor(
job: Job | OrbRef<JobParameterLiteral>,
parameters?: Exclude<WorkflowJobParameters, 'type'>,
pre_steps?: StepsParameter,
post_steps?: StepsParameter,
) {
super(parameters);
this.job = job;
this.pre_steps = pre_steps;
this.post_steps = post_steps;
}

generateContents(flatten?: boolean): WorkflowJobContentsShape {
return {
...super.generateContents(flatten),
'pre-steps': this.generateSteps(this.pre_steps, flatten),
'post-steps': this.generateSteps(this.post_steps, flatten),
};
}

generate(flatten?: boolean): WorkflowJobShape {
Expand All @@ -37,4 +54,11 @@ export class WorkflowJob extends WorkflowJobAbstract {
get name(): string {
return this.job.name;
}

private generateSteps(
steps?: StepsParameter,
flatten?: boolean,
): AnyCommandShape[] | undefined {
return steps?.map((step) => step.generate(flatten));
}
}
19 changes: 3 additions & 16 deletions src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { GenerableType } from '../../../Config/exports/Mapping';
import { AnyCommandShape } from '../../Commands/types/Command.types';
import { Generable } from '../../index';
import { StepsParameter } from '../../Parameters/types';
import {
WorkflowJobContentsShape,
WorkflowJobParameters,
Expand All @@ -22,17 +20,13 @@ export abstract class WorkflowJobAbstract implements Generable {
this.parameters = parameters;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
generateContents(flatten?: boolean): WorkflowJobContentsShape {
let parameters: WorkflowJobParametersShape | undefined;

if (this.parameters) {
const { matrix, pre_steps, post_steps, ...jobParameters } =
this.parameters;
parameters = {
...jobParameters,
'pre-steps': this.generateSteps(pre_steps, flatten),
'post-steps': this.generateSteps(post_steps, flatten),
};
const { matrix, ...jobParameters } = this.parameters;
parameters = jobParameters;

if (matrix) {
parameters.matrix = {
Expand All @@ -48,13 +42,6 @@ export abstract class WorkflowJobAbstract implements Generable {
return GenerableType.WORKFLOW_JOB;
}

private generateSteps(
steps?: StepsParameter,
flatten?: boolean,
): AnyCommandShape[] | undefined {
return steps?.map((step) => step.generate(flatten));
}

abstract get name(): string;
abstract generate(flatten?: boolean): WorkflowJobShape;
}
5 changes: 5 additions & 0 deletions src/lib/Components/Workflow/types/Workflow.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export type WorkflowsShape = {
};
};

export type WorkflowContentsShape = {
when: AnyConditionShape;
jobs: WorkflowJobShape[];
};

export type UnknownWorkflowShape = {
jobs: { [key: string]: unknown }[];
};
Expand Down
14 changes: 8 additions & 6 deletions tests/Workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ describe('Instantiate a Workflow with sequential jobs', () => {
expect(generatedWorkflow).toEqual(expected);
});
});

describe('Instantiate a Workflow with 2 jobs', () => {
const docker = new CircleCI.executors.DockerExecutor('cimg/node:lts');
const helloWorld = new CircleCI.commands.Run({ command: 'echo hello world' });
Expand Down Expand Up @@ -290,11 +289,14 @@ describe('Add pre/post steps to workflow', () => {
});
const job = new CircleCI.Job('my-job', docker, [helloWorld]);
const myWorkflow = new CircleCI.Workflow('my-workflow');
myWorkflow.addJob(job, {
name: 'custom-name',
pre_steps: [helloWorld],
post_steps: [helloWorld],
});
myWorkflow.addJob(
job,
{
name: 'custom-name',
},
[helloWorld],
[helloWorld],
);
it('Should match the expected output', () => {
const expected = {
'my-workflow': {
Expand Down

0 comments on commit 5b4bea6

Please sign in to comment.