Skip to content

Commit

Permalink
Add referencingTask method to TaskBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: snehajais22 <[email protected]>
  • Loading branch information
snehajais22 committed Jul 15, 2024
1 parent 5cc33a9 commit ed3c7b5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
33 changes: 33 additions & 0 deletions API.md

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

31 changes: 25 additions & 6 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ export class TaskBuilder {
private _steps?: TaskStepBuilder[];
private _name?: string;
private _description?: string;
private _taskref?: string;
// These were initially arrays, but converted them to maps so that if
// multiple values are added that the last one will win.
private _workspaces = new Map<string, WorkspaceBuilder>;
Expand Down Expand Up @@ -828,6 +829,24 @@ export class TaskBuilder {
return this._runafter;
}

/**
* Sets the taskRef field of the 'Task'. Use only for tasks within pipelines:
* overrides logicalID as the name of the 'Task' in its individual yaml.
* @param taskRef
*/
public referencingTask(taskRef: string): TaskBuilder {
this._taskref = taskRef;
return this;
}

/**
* Gets the taskRef field of the `Task` for use within a pipeline.
* If not set, the 'Task' id is used.
*/
public get taskRef(): string {
return this._taskref || this._id;
}

/**
* Builds the `Task`.
*/
Expand Down Expand Up @@ -861,7 +880,7 @@ export class TaskBuilder {

const props: TaskProps = {
metadata: {
name: this.logicalID,
name: this.taskRef,
labels: this._labels,
annotations: this._annotations,
},
Expand Down Expand Up @@ -1084,13 +1103,13 @@ export class PipelineBuilder {
if (opts.includeDependencies) {
// Build the task if the user has asked for the dependencies to be
// built along with the pipeline, but only if we haven't already
// built the task yet.
// built the taskRef yet.
if (!taskList.find(it => {
return it == t.logicalID;
return it == t.taskRef;
})) {
t.buildTask();
}
taskList.push(t.logicalID);
taskList.push(t.taskRef);
}
});

Expand All @@ -1114,7 +1133,7 @@ function createOrderedPipelineTask(t: TaskBuilder, after: string[], params: Task
return {
name: t.name,
taskRef: {
name: t.logicalID,
name: t.taskRef,
},
runAfter: after,
params: params,
Expand All @@ -1124,7 +1143,7 @@ function createOrderedPipelineTask(t: TaskBuilder, after: string[], params: Task
return {
name: t.name,
taskRef: {
name: t.logicalID,
name: t.taskRef,
},
params: params,
workspaces: ws,
Expand Down
7 changes: 5 additions & 2 deletions test/pipelinebuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,14 @@ class MyTestChartWithSimilarTasks extends Chart {
.withValue(fromPipelineParam(pipelineParam));

const myTask = new TaskBuilder(this, 'fetch-source')
.referencingTask('fetch-source')
.withName('git-clone')
.withWorkspace(myWorkspace)
.withStringParam(urlParam)
;

const myTask2 = new TaskBuilder(this, 'fetch-source')
const myTask2 = new TaskBuilder(this, 'fetch-source-2')
.referencingTask('fetch-source')
.withName('git-clone-2')
.withWorkspace(myWorkspace)
.withStringParam(urlParam);
Expand All @@ -317,7 +319,8 @@ class MyTestChartWithRunAfter extends Chart {
.withName('fetch-source')
.specifyRunAfter([]);

const secondTask = new TaskBuilder(this, 'git-clone')
const secondTask = new TaskBuilder(this, 'git-clone-2')
.referencingTask('git-clone')
.withName('fetch-again');

const thirdTask = new TaskBuilder(this, 'print-readme')
Expand Down

0 comments on commit ed3c7b5

Please sign in to comment.