Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: standardize workflow job parameters #176

Merged
merged 4 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/lib/Components/Workflow/exports/Workflow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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';
Expand Down Expand Up @@ -72,22 +71,20 @@ export class Workflow implements Generable, Conditional {

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

return {
when: generatedWhen,
const workflowContents: WorkflowContentsShape = {
jobs: generatedWorkflowJobs,
};

generatedWhen ? (workflowContents.when = generatedWhen) : null;

return workflowContents;
}

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

Expand Down
17 changes: 0 additions & 17 deletions src/lib/Components/Workflow/exports/WorkflowJob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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,
Expand All @@ -18,26 +16,18 @@ 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),
};
}

Expand All @@ -54,11 +44,4 @@ 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));
}
}
18 changes: 17 additions & 1 deletion src/lib/Components/Workflow/exports/WorkflowJobAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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 @@ -25,14 +27,21 @@ export abstract class WorkflowJobAbstract implements Generable {
let parameters: WorkflowJobParametersShape | undefined;

if (this.parameters) {
const { matrix, ...jobParameters } = this.parameters;
const { matrix, preSteps, postSteps, ...jobParameters } = this.parameters;
parameters = jobParameters;

if (matrix) {
parameters.matrix = {
parameters: matrix,
};
}

if (preSteps) {
parameters['pre-steps'] = this.generateSteps(preSteps, flatten);
}
if (postSteps) {
parameters['post-steps'] = this.generateSteps(postSteps, flatten);
}
}

return parameters;
Expand All @@ -42,6 +51,13 @@ 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;
}
6 changes: 2 additions & 4 deletions src/lib/Components/Workflow/types/Workflow.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { WorkflowJobShape } from './WorkflowJob.types';

export type WorkflowsShape = {
[workflowName: string]: {
when: AnyConditionShape;
when?: AnyConditionShape;
jobs: WorkflowJobShape[];
};
};

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

Expand All @@ -23,8 +23,6 @@ export type UnknownWorkflowJobShape = {
parameters?: { [key: string]: unknown };
name?: string;
type?: 'approval';
// 'pre-steps'?: { [key: string]: unknown }[];
// 'post-steps'?: { [key: string]: unknown }[];
};

export type WorkflowDependencies = {
Expand Down
7 changes: 2 additions & 5 deletions src/lib/Components/Workflow/types/WorkflowJob.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface WorkflowJobParameters
requires?: ListParameter;
name?: StringParameter;
context?: ListParameter;
preSteps?: StepsParameter;
postSteps?: StepsParameter;
/**
* {@link https://circleci.com/docs/2.0/configuration-reference/#filters} Filter workflow job's execution by branch or git tag.
*/
Expand All @@ -35,9 +37,6 @@ export interface WorkflowJobParameters
* An "approval" type job is a special job which pauses the workflow. This "job" is not defined outside of the workflow, you may enter any potential name for the job name. As long as the parameter of "type" is present and equal to "approval" this job will act as a placeholder that awaits user input to continue.
*/
type?: approval;

pre_steps?: StepsParameter;
post_steps?: StepsParameter;
}

export type approval = 'approval';
Expand All @@ -62,8 +61,6 @@ export interface WorkflowJobParametersShape {
type?: approval;
filters?: FilterParameter;
matrix?: WorkflowMatrixShape;
'pre-steps'?: AnyCommandShape[];
'post-steps'?: AnyCommandShape[];
[key: string]:
| JobParameterTypes
| WorkflowMatrixShape
Expand Down
38 changes: 17 additions & 21 deletions tests/Workflow.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as CircleCI from '../src/index';
import * as YAML from 'yaml';

describe('Instantiate Workflow', () => {
const docker = new CircleCI.executors.DockerExecutor('cimg/node:lts');
Expand Down Expand Up @@ -289,28 +290,23 @@ 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',
},
[helloWorld],
[helloWorld],
);
myWorkflow.addJob(job, {
name: 'custom-name',
preSteps: [helloWorld],
postSteps: [helloWorld],
});
it('Should match the expected output', () => {
const expected = {
'my-workflow': {
jobs: [
{
'my-job': {
name: 'custom-name',
'post-steps': [{ run: 'echo hello world' }],
'pre-steps': [{ run: 'echo hello world' }],
},
},
],
},
};
const expectedYaml = `my-workflow:
jobs:
- my-job:
name: custom-name
pre-steps:
- run: echo hello world
post-steps:
- run: echo hello world
`;
const expected = YAML.parse(expectedYaml);

const generatedWorkflow = myWorkflow.generate(true);
expect(generatedWorkflow).toEqual(expected);
});
Expand Down