Skip to content

Commit 35a8268

Browse files
fix: updated task creation logic in pipeline builder to support installation of tasks with name name
1 parent 7009f99 commit 35a8268

File tree

5 files changed

+124
-26
lines changed

5 files changed

+124
-26
lines changed

src/components/catalog/apis/artifactHub.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export const createArtifactHubTask = (
102102
namespace: string,
103103
version: string,
104104
isDevConsoleProxyAvailable?: boolean,
105+
customName?: string,
105106
) => {
106107
const fetchTask = async (): Promise<K8sResourceKind> => {
107108
if (isDevConsoleProxyAvailable) {
@@ -122,6 +123,9 @@ export const createArtifactHubTask = (
122123
return fetchTask()
123124
.then((task: K8sResourceKind) => {
124125
task.metadata.namespace = namespace;
126+
if (customName) {
127+
task.metadata.name = customName;
128+
}
125129
task.metadata.annotations = {
126130
...task.metadata.annotations,
127131
[TektonTaskAnnotation.installedFrom]: ARTIFACTHUB,
@@ -185,7 +189,7 @@ export const updateArtifactHubTask = async (
185189

186190
export const fetchArtifactHubTasks = async (
187191
query: string,
188-
limit: number = 20,
192+
limit = 20,
189193
): Promise<ArtifactHubTask[]> => {
190194
try {
191195
const response = await fetch(

src/components/pipeline-builder/PipelineBuilderForm.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { FormikProps } from 'formik';
99
import * as _ from 'lodash';
1010
import { useTranslation } from 'react-i18next';
11-
import { PipelineModel } from '../../models';
11+
import { PipelineModel, TaskModel } from '../../models';
1212
import {
1313
PipelineKind,
1414
PipelineTask,
@@ -42,7 +42,11 @@ import FormFooter from '../pipelines-details/multi-column-field/FormFooter';
4242
import { FlexForm, FormBody } from './form-utils';
4343
import SyncedEditorField from './SyncedEditorField';
4444
import PipelineQuickSearch from '../task-quicksearch/PipelineQuickSearch';
45-
import { useModal } from '@openshift-console/dynamic-plugin-sdk';
45+
import {
46+
k8sDelete,
47+
k8sGet,
48+
useModal,
49+
} from '@openshift-console/dynamic-plugin-sdk';
4650

4751
type PipelineBuilderFormProps = FormikProps<PipelineBuilderFormikValues> & {
4852
existingPipeline: PipelineKind;
@@ -105,7 +109,6 @@ const PipelineBuilderForm: React.FC<PipelineBuilderFormProps> = (props) => {
105109
const updateTasks = (changes: CleanupResults): void => {
106110
const { tasks, listTasks, finallyTasks, finallyListTasks, loadingTasks } =
107111
changes;
108-
109112
setFieldValue('formData', {
110113
...formData,
111114
tasks,
@@ -206,8 +209,28 @@ const PipelineBuilderForm: React.FC<PipelineBuilderFormProps> = (props) => {
206209
onRemoveTask={(taskName: string) => {
207210
launchModal(RemoveTaskModal, {
208211
taskName,
209-
onRemove: () => {
212+
onRemove: async () => {
210213
setSelectedTask(null);
214+
let taskExists = false;
215+
try {
216+
await k8sGet({
217+
model: TaskModel,
218+
name: taskName,
219+
ns: namespace,
220+
});
221+
taskExists = true;
222+
} catch (error) {
223+
console.warn(
224+
`Error fetching Task as task does not exist ${taskName}, so safely deleting from PipelineBuilder`,
225+
error,
226+
);
227+
}
228+
if (taskExists) {
229+
await k8sDelete({
230+
model: TaskModel,
231+
resource: { metadata: { name: taskName, namespace } },
232+
});
233+
}
211234
updateTasks(
212235
applyChange(
213236
taskGroup,

src/components/quick-search/utils/quick-search-utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const handleCta = async (
2020
closeModal();
2121
await callback({
2222
...callbackProps,
23-
selectedVersion: item.data?.version,
23+
selectedVersion: item.data?.version ?? item.data?.task?.version,
2424
selectedItem: item,
2525
});
2626
removeQueryArgument('catalogSearch');

src/components/task-quicksearch/PipelineQuickSearch.tsx

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
TaskProviders,
2121
updateTask,
2222
} from './pipeline-quicksearch-utils';
23+
import { safeName } from '../pipeline-builder/utils';
2324
import PipelineQuickSearchDetails from './PipelineQuickSearchDetails';
2425
import { CatalogServiceProvider } from '../catalog/service';
2526
import { CatalogService } from '../catalog/types';
@@ -64,6 +65,24 @@ const Contents: React.FC<
6465
useLoadingTaskCleanup(onUpdateTasks, taskGroup);
6566
useCleanupOnFailure(failedTasks, onUpdateTasks, taskGroup);
6667

68+
// Get all existing task names from taskGroup and installed tasks
69+
const getExistingTaskNames = (): string[] => {
70+
const taskNames = [
71+
...taskGroup.tasks.map((t) => t.name),
72+
...taskGroup.finallyTasks.map((t) => t.name),
73+
...taskGroup.listTasks.map((t) => t.name),
74+
...taskGroup.loadingTasks.map((t) => t.name),
75+
...taskGroup.finallyListTasks.map((t) => t.name),
76+
];
77+
// Add installed task names from catalog
78+
catalogService.items.forEach((catalogItem) => {
79+
if (catalogItem.data?.metadata?.name) {
80+
taskNames.push(catalogItem.data.metadata.name);
81+
}
82+
});
83+
return taskNames;
84+
};
85+
6786
const catalogServiceItems = catalogService.items.reduce((acc, item) => {
6887
const installedTask = findInstalledTask(catalogService.items, item);
6988

@@ -102,12 +121,30 @@ const Contents: React.FC<
102121
).catch(() => setFailedTasks([...failedTasks, item.data.name]));
103122
}
104123
} else {
105-
resolve(
106-
savedCallback.current({ metadata: { name: item.data.name } }),
107-
);
108-
createTask(selectedVersionUrl, namespace).catch(() =>
109-
setFailedTasks([...failedTasks, item.data.name]),
110-
);
124+
// Checking if task with same name already exists, if yes then create with a different name to avoid conflict
125+
const existingTaskNames = getExistingTaskNames();
126+
if (existingTaskNames.includes(item.data.name)) {
127+
const taskNameToUse = safeName(
128+
existingTaskNames,
129+
item.data.name,
130+
);
131+
createTask(selectedVersionUrl, namespace, taskNameToUse)
132+
.then(() =>
133+
resolve(
134+
savedCallback.current({
135+
metadata: { name: taskNameToUse },
136+
}),
137+
),
138+
)
139+
.catch(() => setFailedTasks([...failedTasks, taskNameToUse]));
140+
} else {
141+
resolve(
142+
savedCallback.current({ metadata: { name: item.data.name } }),
143+
);
144+
createTask(selectedVersionUrl, namespace).catch(() =>
145+
setFailedTasks([...failedTasks, item.data.name]),
146+
);
147+
}
111148
}
112149
} else {
113150
resolve(savedCallback.current(item.data));
@@ -143,19 +180,46 @@ const Contents: React.FC<
143180
);
144181
}
145182
} else {
146-
resolve(
147-
savedCallback.current({
148-
metadata: { name: item.data.task.name },
149-
}),
150-
);
151-
createArtifactHubTask(
152-
selectedVersionUrl,
153-
namespace,
154-
selectedVersion,
155-
isDevConsoleProxyAvailable,
156-
).catch(() =>
157-
setFailedTasks([...failedTasks, item.data.task.name]),
158-
);
183+
// Checking if task with same name already exists, if yes then create with a different name to avoid conflict
184+
const existingTaskNames = getExistingTaskNames();
185+
if (existingTaskNames.includes(item.data.task.name)) {
186+
const taskNameToUse = safeName(
187+
existingTaskNames,
188+
item.data.task.name,
189+
);
190+
createArtifactHubTask(
191+
selectedVersionUrl,
192+
namespace,
193+
selectedVersion,
194+
isDevConsoleProxyAvailable,
195+
taskNameToUse,
196+
)
197+
.then(() =>
198+
resolve(
199+
savedCallback.current({
200+
metadata: { name: taskNameToUse },
201+
}),
202+
),
203+
)
204+
.catch(() => setFailedTasks([...failedTasks, taskNameToUse]));
205+
} else {
206+
createArtifactHubTask(
207+
selectedVersionUrl,
208+
namespace,
209+
selectedVersion,
210+
isDevConsoleProxyAvailable,
211+
)
212+
.then(() =>
213+
resolve(
214+
savedCallback.current({
215+
metadata: { name: item.data.task.name },
216+
}),
217+
),
218+
)
219+
.catch(() =>
220+
setFailedTasks([...failedTasks, item.data.task.name]),
221+
);
222+
}
159223
}
160224
}
161225
});

src/components/task-quicksearch/pipeline-quicksearch-utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,19 @@ export const updateTask = async (
174174
});
175175
};
176176

177-
export const createTask = (url: string, namespace: string) => {
177+
export const createTask = (
178+
url: string,
179+
namespace: string,
180+
customName?: string,
181+
) => {
178182
return consoleFetch(url)
179183
.then(async (res) => {
180184
const yaml = await res.text();
181185
const task = load(yaml) as TaskKind;
182186
task.metadata.namespace = namespace;
187+
if (customName) {
188+
task.metadata.name = customName;
189+
}
183190
task.metadata.annotations = {
184191
...task.metadata.annotations,
185192
[TektonTaskAnnotation.installedFrom]: TEKTONHUB,

0 commit comments

Comments
 (0)