From a7dc50e774296b59cd75b2ced96880b911a882ba Mon Sep 17 00:00:00 2001 From: Alan Greene Date: Tue, 20 Feb 2024 17:55:54 +0000 Subject: [PATCH] Add support for displayName from childReferences Update utils to support pulling the pipeline task `displayName` from the run's `status.childReferences`. This adds support for dynamic matrix display names, and provides a consistent location to retrieve the resolve display name value after param substitution etc. has taken place. --- packages/utils/src/utils/index.js | 29 ++++++++++++---- packages/utils/src/utils/index.test.js | 47 +++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/packages/utils/src/utils/index.js b/packages/utils/src/utils/index.js index 6c72b02f4..cd68517fa 100644 --- a/packages/utils/src/utils/index.js +++ b/packages/utils/src/utils/index.js @@ -1,5 +1,5 @@ /* -Copyright 2019-2023 The Tekton Authors +Copyright 2019-2024 The Tekton Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -448,8 +448,9 @@ export function getPlaceholderTaskRun({ clusterTasks, pipelineTask, tasks }) { }; } -function addDashboardLabels({ pipelineTask, taskRun }) { - const { description, displayName } = pipelineTask; +function addDashboardLabels({ displayName, pipelineTask, taskRun }) { + const { description, displayName: pipelineTaskDisplayName } = pipelineTask; + const displayNameToUse = displayName || pipelineTaskDisplayName; // eslint-disable-next-line no-param-reassign taskRun.metadata.labels = { ...taskRun.metadata.labels, @@ -458,9 +459,9 @@ function addDashboardLabels({ pipelineTask, taskRun }) { [labelConstants.DASHBOARD_DESCRIPTION]: description } : null), - ...(displayName + ...(displayNameToUse ? { - [labelConstants.DASHBOARD_DISPLAY_NAME]: displayName + [labelConstants.DASHBOARD_DISPLAY_NAME]: displayNameToUse } : null) }; @@ -475,6 +476,7 @@ export function getTaskRunsWithPlaceholders({ tasks }) { let pipelineTasks = []; + const childReferences = pipelineRun?.status?.childReferences || []; if (pipelineRun?.status?.pipelineSpec?.tasks) { pipelineTasks = pipelineTasks.concat(pipelineRun.status.pipelineSpec.tasks); @@ -502,8 +504,23 @@ export function getTaskRunsWithPlaceholders({ pipelineTask.name ); + const displayNames = childReferences.reduce((acc, childReference) => { + if ( + childReference.displayName && + childReference.pipelineTaskName === pipelineTask.name + ) { + acc[childReference.name] = childReference.displayName; + } + return acc; + }, {}); realTaskRuns.forEach(taskRun => { - taskRunsToDisplay.push(addDashboardLabels({ pipelineTask, taskRun })); + taskRunsToDisplay.push( + addDashboardLabels({ + displayName: displayNames[taskRun.metadata.name], + pipelineTask, + taskRun + }) + ); }); if (!realTaskRuns.length) { diff --git a/packages/utils/src/utils/index.test.js b/packages/utils/src/utils/index.test.js index 8525b589a..783b619ba 100644 --- a/packages/utils/src/utils/index.test.js +++ b/packages/utils/src/utils/index.test.js @@ -1,5 +1,5 @@ /* -Copyright 2019-2023 The Tekton Authors +Copyright 2019-2024 The Tekton Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -844,6 +844,51 @@ describe('getTaskRunsWithPlaceholders', () => { const runs = getTaskRunsWithPlaceholders({ pipelineRun, taskRuns }); expect(runs).toEqual([taskRun, finallyTaskRun]); }); + + it('handles displayName in childReferences', () => { + const displayName = 'aDisplayName'; + const pipelineTaskName = 'aPipelineTaskName'; + const taskRunName = 'aTaskRunName'; + + const pipelineRun = { + spec: { + pipelineRef: { + name: 'dummy-pipeline' + } + }, + status: { + childReferences: [ + { + displayName, + name: taskRunName, + pipelineTaskName + } + ], + pipelineSpec: { + tasks: [ + { + name: pipelineTaskName + } + ] + } + } + }; + const taskRun = { + metadata: { + labels: { + [labels.PIPELINE_TASK]: pipelineTaskName + }, + name: taskRunName + } + }; + const runs = getTaskRunsWithPlaceholders({ + pipelineRun, + taskRuns: [taskRun] + }); + expect(runs[0].metadata.labels[labels.DASHBOARD_DISPLAY_NAME]).toEqual( + displayName + ); + }); }); describe('taskRunHasWarning', () => {