From 019616bcd7c9c9df6eedc5a07bd182618f840b87 Mon Sep 17 00:00:00 2001 From: Alexandre Le Foll Date: Wed, 2 Nov 2022 15:29:37 +1100 Subject: [PATCH] GroupStep (#1) --- examples/group.ts | 32 ++ .../__snapshots__/group.spec.ts.snap | 510 ++++++++++++++++++ src/__tests__/group.spec.ts | 35 ++ src/index.ts | 1 + src/steps/group.ts | 35 ++ 5 files changed, 613 insertions(+) create mode 100644 examples/group.ts create mode 100644 src/__tests__/__snapshots__/group.spec.ts.snap create mode 100644 src/__tests__/group.spec.ts create mode 100644 src/steps/group.ts diff --git a/examples/group.ts b/examples/group.ts new file mode 100644 index 0000000..cf620d8 --- /dev/null +++ b/examples/group.ts @@ -0,0 +1,32 @@ +import { Command, CommandStep, GroupStep, Pipeline } from '../src'; +import { YamlSerializer } from '../src/serializers/yaml'; + +async function main() { + const install = new Command('yarn', 2); + + const lint = new CommandStep([install, new Command('yarn lint', 1)]).withKey( + 'lint', + ); + const test = new CommandStep([install, new Command('yarn test', 2)]) + .withKey('test') + .dependsOn(lint); + const build = new CommandStep([install, new Command('yarn build', 5)]) + .withKey('build') + .dependsOn(lint); + const integration = new CommandStep([ + install, + new Command('yarn integration', 10), + ]) + .withKey('integration-test') + .dependsOn(build); + + const group = new GroupStep('My group').add(test).add(integration); + + const pipeline = new Pipeline('My pipeline').add(group).add(integration); + + console.log(await new YamlSerializer().serialize(pipeline)); + // console.log(await new YamlSerializer({ explicitDependencies: true }).serialize(pipeline)); + // console.log(await new DotSerializer().serialize(pipeline)); +} + +main(); diff --git a/src/__tests__/__snapshots__/group.spec.ts.snap b/src/__tests__/__snapshots__/group.spec.ts.snap new file mode 100644 index 0000000..0be3642 --- /dev/null +++ b/src/__tests__/__snapshots__/group.spec.ts.snap @@ -0,0 +1,510 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`buildkite-graph Steps Group group step dot 1`] = ` +"digraph \\"whatever\\" { + graph [ compound =true ]; +subgraph cluster_0 { + graph [ color = \\"black\\" ]; + \\"[object Object]\\" [ color = \\"grey\\" ]; +} + +} +" +`; + +exports[`buildkite-graph Steps Group group step json 1`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "label": "group", + "steps": Array [], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group group step json_depends_on 1`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_0", + "label": "group", + "steps": Array [], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group group step json_depends_on_accept_all 1`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_0", + "label": "group", + "steps": Array [], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group group step structure 1`] = `"* [object Object]"`; + +exports[`buildkite-graph Steps Group group step yaml 1`] = ` +"steps: + - group: group + label: group + steps: [] +" +`; + +exports[`buildkite-graph Steps Group group step yaml_depends_on 1`] = ` +"steps: + - group: group + label: group + key: _0 + steps: [] +" +`; + +exports[`buildkite-graph Steps Group group step yaml_depends_on_accept_all 1`] = ` +"steps: + - group: group + label: group + key: _0 + steps: [] +" +`; + +exports[`buildkite-graph Steps Group group step yaml_mutate 1`] = ` +"steps: + - group: group + label: group + key: _0 + steps: [] +" +`; + +exports[`buildkite-graph Steps Group step addition dot 1`] = ` +"digraph \\"whatever\\" { + graph [ compound =true ]; +subgraph cluster_0 { + graph [ color = \\"black\\" ]; + \\"[object Object]\\" [ color = \\"grey\\" ]; +} + +} +" +`; + +exports[`buildkite-graph Steps Group step addition dot 2`] = ` +"digraph \\"whatever\\" { + graph [ compound =true ]; +subgraph cluster_0 { + graph [ color = \\"black\\" ]; + \\"[object Object]\\" [ color = \\"grey\\" ]; +} + +} +" +`; + +exports[`buildkite-graph Steps Group step addition dot 3`] = ` +"digraph \\"whatever\\" { + graph [ compound =true ]; +subgraph cluster_0 { + graph [ color = \\"black\\" ]; + \\"[object Object]\\" [ color = \\"grey\\" ]; +} + +} +" +`; + +exports[`buildkite-graph Steps Group step addition json 1`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json 2`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + Object { + "command": "yarn test", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json 3`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + Object { + "command": "yarn test", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json_depends_on 1`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_0", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json_depends_on 2`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_1", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + Object { + "command": "yarn test", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json_depends_on 3`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_2", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + Object { + "command": "yarn test", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json_depends_on_accept_all 1`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_0", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json_depends_on_accept_all 2`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_1", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + Object { + "command": "yarn test", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition json_depends_on_accept_all 3`] = ` +Object { + "steps": Array [ + Object { + "group": "group", + "key": "_2", + "label": "group", + "steps": Array [ + Object { + "command": "yarn", + }, + Object { + "command": "yarn test", + }, + ], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group step addition structure 1`] = `"* [object Object]"`; + +exports[`buildkite-graph Steps Group step addition structure 2`] = `"* [object Object]"`; + +exports[`buildkite-graph Steps Group step addition structure 3`] = `"* [object Object]"`; + +exports[`buildkite-graph Steps Group step addition yaml 1`] = ` +"steps: + - group: group + label: group + steps: + - command: yarn +" +`; + +exports[`buildkite-graph Steps Group step addition yaml 2`] = ` +"steps: + - group: group + label: group + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml 3`] = ` +"steps: + - group: group + label: group + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_depends_on 1`] = ` +"steps: + - group: group + label: group + key: _0 + steps: + - command: yarn +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_depends_on 2`] = ` +"steps: + - group: group + label: group + key: _1 + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_depends_on 3`] = ` +"steps: + - group: group + label: group + key: _2 + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_depends_on_accept_all 1`] = ` +"steps: + - group: group + label: group + key: _0 + steps: + - command: yarn +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_depends_on_accept_all 2`] = ` +"steps: + - group: group + label: group + key: _1 + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_depends_on_accept_all 3`] = ` +"steps: + - group: group + label: group + key: _2 + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_mutate 1`] = ` +"steps: + - group: group + label: group + key: _0 + steps: + - command: yarn +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_mutate 2`] = ` +"steps: + - group: group + label: group + key: _1 + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group step addition yaml_mutate 3`] = ` +"steps: + - group: group + label: group + key: _2 + steps: + - command: yarn + - command: yarn test +" +`; + +exports[`buildkite-graph Steps Group with a label dot 1`] = ` +"digraph \\"whatever\\" { + graph [ compound =true ]; +subgraph cluster_0 { + graph [ color = \\"black\\" ]; + \\"[object Object]\\" [ color = \\"grey\\" ]; +} + +} +" +`; + +exports[`buildkite-graph Steps Group with a label json 1`] = ` +Object { + "steps": Array [ + Object { + "group": "Trigger another pipeline", + "label": "Trigger another pipeline", + "steps": Array [], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group with a label json_depends_on 1`] = ` +Object { + "steps": Array [ + Object { + "group": "Trigger another pipeline", + "key": "_0", + "label": "Trigger another pipeline", + "steps": Array [], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group with a label json_depends_on_accept_all 1`] = ` +Object { + "steps": Array [ + Object { + "group": "Trigger another pipeline", + "key": "_0", + "label": "Trigger another pipeline", + "steps": Array [], + }, + ], +} +`; + +exports[`buildkite-graph Steps Group with a label structure 1`] = `"* [object Object]"`; + +exports[`buildkite-graph Steps Group with a label yaml 1`] = ` +"steps: + - group: Trigger another pipeline + label: Trigger another pipeline + steps: [] +" +`; + +exports[`buildkite-graph Steps Group with a label yaml_depends_on 1`] = ` +"steps: + - group: Trigger another pipeline + label: Trigger another pipeline + key: _0 + steps: [] +" +`; + +exports[`buildkite-graph Steps Group with a label yaml_depends_on_accept_all 1`] = ` +"steps: + - group: Trigger another pipeline + label: Trigger another pipeline + key: _0 + steps: [] +" +`; + +exports[`buildkite-graph Steps Group with a label yaml_mutate 1`] = ` +"steps: + - group: Trigger another pipeline + label: Trigger another pipeline + key: _0 + steps: [] +" +`; diff --git a/src/__tests__/group.spec.ts b/src/__tests__/group.spec.ts new file mode 100644 index 0000000..c9e106a --- /dev/null +++ b/src/__tests__/group.spec.ts @@ -0,0 +1,35 @@ +import { CommandStep, GroupStep, Pipeline } from '../'; +import { createTest } from './helpers'; + +describe('buildkite-graph', () => { + describe('Steps', () => { + describe('Group', () => { + createTest('group step', () => [ + new Pipeline('whatever').add(new GroupStep('group')), + ]); + + createTest('with a label', () => [ + new Pipeline('whatever').add( + new GroupStep('another').withLabel('Trigger another pipeline'), + ), + ]); + + createTest('step addition', () => [ + new Pipeline('whatever').add( + new GroupStep('group').add(new CommandStep('yarn')), + ), + new Pipeline('whatever').add( + new GroupStep('group').add( + new CommandStep('yarn'), + new CommandStep('yarn test'), + ), + ), + new Pipeline('whatever').add( + new GroupStep('group') + .add(new CommandStep('yarn')) + .add(new CommandStep('yarn test')), + ), + ]); + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index c2d7356..da211bc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,6 +29,7 @@ export { export { BlockStep } from './steps/block'; export { Option, SelectField, TextField } from './steps/block/fields'; export { Command, CommandStep } from './steps/command'; +export { GroupStep } from './steps/group'; export { Plugin } from './steps/command/plugins'; export { TriggerStep } from './steps/trigger'; export const serializers = { diff --git a/src/steps/group.ts b/src/steps/group.ts new file mode 100644 index 0000000..24ca3f6 --- /dev/null +++ b/src/steps/group.ts @@ -0,0 +1,35 @@ +import type { + Serializable, + ToJsonSerializationOptions, +} from '../serialization'; +import { toJsonSerializationDefaultOptions } from '../serialization'; +import { Pipeline, PotentialStep } from '../index'; +import { LabeledStep } from '../base'; + +export class GroupStep extends LabeledStep implements Serializable { + public readonly steps: PotentialStep[] = []; + + private readonly pipeline: Pipeline = new Pipeline(''); + + constructor(label: string) { + super(); + + this.withLabel(label); + } + + add(...steps: PotentialStep[]): this { + this.pipeline.add(...steps); + + return this; + } + + async toJson( + opts: ToJsonSerializationOptions = toJsonSerializationDefaultOptions, + ): Promise> { + return { + group: this.label, + ...(await super.toJson(opts)), + steps: (await this.pipeline.toJson()).steps, + }; + } +}