Skip to content

Commit

Permalink
Merge pull request #15 from cloud-native-toolkit/fix/params-and-works…
Browse files Browse the repository at this point in the history
…paces

fix: issue with params and workspaces for pipelines
  • Loading branch information
nathanagood authored Sep 10, 2023
2 parents e352ef9 + 84ec775 commit 76254d2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ export class TaskBuilder {
}

export class PipelineBuilder {
private readonly _scope?: Construct;
private readonly _id?: string;
private readonly _scope: Construct;
private readonly _id: string;
private _name?: string;
private _description?: string;
private _tasks?: TaskBuilder[];
Expand All @@ -517,7 +517,7 @@ export class PipelineBuilder {
* Gets the name of the pipeline
*/
public get name(): string {
return this._name!;
return this._name || this._id;
}

/**
Expand Down Expand Up @@ -546,8 +546,9 @@ export class PipelineBuilder {
*/
public buildPipeline(): void {
// TODO: validate the object
const pipelineParams: PipelineParam[] = new Array<PipelineParam>();
const pipelineWorkspaces: PipelineWorkspace[] = new Array<PipelineWorkspace>();

const pipelineParams = new Map<string, PipelineParam>();
const pipelineWorkspaces = new Map<string, PipelineWorkspace>();
const pipelineTasks: PipelineTask[] = new Array<PipelineTask>();

this._tasks?.forEach(t => {
Expand All @@ -556,9 +557,9 @@ export class PipelineBuilder {
const taskWorkspaces: PipelineTaskWorkspace[] = new Array<TaskWorkspace>();

t.parameters?.forEach(p => {
const pp = pipelineParams.find((value, index, obj) => value.name == obj[index].name);
const pp = pipelineParams.get(p.name!);
if (!pp) {
pipelineParams.push({
pipelineParams.set(p.name!, {
name: p.name,
type: p.type,
});
Expand All @@ -573,9 +574,9 @@ export class PipelineBuilder {
t.workspaces?.forEach((w) => {
// Only add the workspace on the pipeline level if it is not already
// there...
const ws = pipelineWorkspaces.find((value, index, obj) => value.name == obj[index].name);
const ws = pipelineWorkspaces.get(w.name!);
if (!ws) {
pipelineWorkspaces.push({
pipelineWorkspaces.set(w.name!, {
name: w.name,
description: w.description,
});
Expand Down Expand Up @@ -604,8 +605,8 @@ export class PipelineBuilder {
},
spec: {
description: this._description,
params: pipelineParams,
workspaces: pipelineWorkspaces,
params: Array.from(pipelineParams.values()),
workspaces: Array.from(pipelineWorkspaces.values()),
tasks: pipelineTasks,
},
});
Expand Down
87 changes: 87 additions & 0 deletions test/__snapshots__/pipelinebuilder.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions test/pipelinebuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ class MyTestChart extends Chart {
}
}

class MySecondTestChart extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

const myTask = new TaskBuilder(this, 'git-clone')
.withName('fetch-source')
.withWorkspace(new WorkspaceBuilder('output')
.withName('shared-data')
.withDescription('The files cloned by the task'))
.withWorkspace(new WorkspaceBuilder('ssh-credentials')
.withName('ssh-creds')
.withDescription('The SSH files for credentials'))
.withWorkspace(new WorkspaceBuilder('config')
.withName('config-data')
.withDescription('The files for configuration for stuff'))
.withStringParam(new ParameterBuilder('url').withPiplineParameter('repo-url', ''))
.withStringParam(new ParameterBuilder('name').withPiplineParameter('your-name', ''))
.withStringParam(new ParameterBuilder('color').withPiplineParameter('your-color', ''))
.withStringParam(new ParameterBuilder('quest').withPiplineParameter('your-quest', ''));

new PipelineBuilder(this, 'my-test-pipeline-2')
.withName('clone-build-push')
.withDescription('This pipeline closes a repository, builds a Docker image, etc.')
.withTask(myTask)
.buildPipeline();
}
}

class MyTestChartWithDups extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);
Expand Down Expand Up @@ -63,6 +91,13 @@ describe('PipelineBuilderTest', () => {
expect(results).toMatchSnapshot();
});

test('PipelineBuilderWithComplexTasks', () => {
const app = Testing.app();
const chart = new MySecondTestChart(app, 'my-second-test-chart');
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});

test('PipelineBuilderWithDuplicates', () => {
const app = Testing.app();
const chart = new MyTestChartWithDups(app, 'test-chart');
Expand Down

0 comments on commit 76254d2

Please sign in to comment.