@@ -69,6 +69,8 @@ export type UseTaskRunsOptions = {
6969 pipelineRunFinished ?: boolean ;
7070 pipelineRunManagedBy ?: string ;
7171 skipFetch ?: boolean ;
72+ name ?: string /* used for fetching a single task run by metadata.name */ ;
73+ limit ?: number /* used for fetching a limited number of task runs */ ;
7274} ;
7375
7476export const useTaskRuns = (
@@ -78,7 +80,7 @@ export const useTaskRuns = (
7880 pipelineRunUid ?: string ,
7981 options ?: UseTaskRunsOptions ,
8082) : [ TaskRunKind [ ] , boolean , boolean , Error | undefined , boolean , boolean ] => {
81- const { pipelineRunFinished, pipelineRunManagedBy, skipFetch } =
83+ const { pipelineRunFinished, pipelineRunManagedBy, skipFetch, name , limit } =
8284 options || { } ;
8385 const selector : Selector = useMemo ( ( ) => {
8486 if ( pipelineRunName && pipelineRunUid ) {
@@ -106,9 +108,10 @@ export const useTaskRuns = (
106108 error ,
107109 pendingAdmission ,
108110 proxyUnavailable ,
109- ] = useTaskRuns2 (
111+ ] = useRuns < TaskRunKind > (
112+ TASK_RUN_GVK ,
110113 namespace ,
111- { selector, skipFetch } ,
114+ { selector, skipFetch, name , limit } ,
112115 pipelineRunFinished ,
113116 pipelineRunManagedBy ,
114117 ) ;
@@ -150,24 +153,6 @@ export const useTaskRuns = (
150153 ) ;
151154} ;
152155
153- export const useTaskRuns2 = (
154- namespace : string ,
155- options ?: {
156- selector ?: Selector ;
157- limit ?: number ;
158- skipFetch ?: boolean ;
159- } ,
160- pipelineRunFinished ?: boolean ,
161- pipelineRunManagedBy ?: string ,
162- ) : [ TaskRunKind [ ] , boolean , boolean , Error | undefined , boolean , boolean ] =>
163- useRuns < TaskRunKind > (
164- TASK_RUN_GVK ,
165- namespace ,
166- options ,
167- pipelineRunFinished ,
168- pipelineRunManagedBy ,
169- ) ;
170-
171156export const useApprovalTasks = (
172157 namespace : string ,
173158 pipelineRunName ?: string ,
@@ -205,32 +190,14 @@ export const usePipelineRuns = (
205190 options ?: {
206191 selector ?: Selector ;
207192 limit ?: number ;
193+ name ?: string ;
208194 } ,
209195) : [ PipelineRunKind [ ] , boolean , boolean , Error | undefined , boolean , boolean ] =>
210- useRuns < PipelineRunKind > ( PIPELINE_RUN_GVK , namespace , options ) ;
211-
212- export const usePipelineRun = (
213- namespace : string ,
214- pipelineRunName : string ,
215- ) : [ PipelineRunKind , boolean , boolean , Error | undefined ] => {
216- const result = usePipelineRuns (
217- namespace ,
218- useMemo (
219- ( ) => ( {
220- name : pipelineRunName ,
221- limit : 1 ,
222- } ) ,
223- [ pipelineRunName ] ,
224- ) ,
225- ) as unknown as [ PipelineRunKind [ ] , boolean , boolean , Error | undefined ] ;
226-
227- return [
228- result [ 0 ] ?. [ 0 ] ,
229- result [ 1 ] ,
230- result [ 2 ] ,
231- result [ 0 ] ?. [ 0 ] ? undefined : result [ 3 ] ,
232- ] ;
233- } ;
196+ useRuns < PipelineRunKind > ( PIPELINE_RUN_GVK , namespace , {
197+ selector : options ?. selector ,
198+ limit : options ?. limit /* similar to one present in UseTaskRunsOptions */ ,
199+ name : options ?. name /* similar to one present in UseTaskRunsOptions */ ,
200+ } ) ;
234201
235202export const useRuns = < Kind extends K8sResourceKind > (
236203 groupVersionKind : K8sGroupVersionKind ,
@@ -380,84 +347,45 @@ export const useRuns = <Kind extends K8sResourceKind>(
380347 optionsMemo ?. skipFetch ,
381348 ) ;
382349
383- return useMemo ( ( ) => {
384- // dedupe PLR by name since UIDs differ between hub and spoke clusters; for other cases(TR) dedupe by UID
350+ // dedupe PLR by name since UIDs differ between hub and spoke clusters; for other cases(TR) dedupe by UID
385351
386- const rResources : Kind [ ] =
387- runs && trResources
388- ? ! isTaskRunQuery
389- ? uniqBy ( [ ...runs , ...trResources ] , ( r ) => r . metadata . name )
390- : uniqBy ( [ ...runs , ...trResources ] , ( r ) => r . metadata . uid )
391- : runs || trResources ;
352+ const rResources : Kind [ ] =
353+ runs && trResources
354+ ? ! isTaskRunQuery
355+ ? uniqBy ( [ ...runs , ...trResources ] , ( r ) => r . metadata . name )
356+ : uniqBy ( [ ...runs , ...trResources ] , ( r ) => r . metadata . uid )
357+ : runs || trResources ;
392358
393- /* Refactoring the nesting as it is causing cognitive damage */
394- let resolvedError : Error | undefined = undefined ;
359+ /* Refactoring the nesting as it is causing cognitive damage */
360+ let resolvedError : Error | undefined = undefined ;
395361
396- if ( namespace ) {
397- if ( queryTr ) {
398- if ( isList ) {
399- resolvedError = trError && effectiveError ;
400- } else {
401- // when searching by name, return an error if we have no result
402- if ( trError && trLoaded && ! trResources ?. length ) {
403- resolvedError = effectiveError ;
404- }
405- }
362+ if ( namespace ) {
363+ if ( queryTr ) {
364+ if ( isList ) {
365+ resolvedError = trError && effectiveError ;
406366 } else {
407- resolvedError = effectiveError ;
367+ // when searching by name, return an error if we have no result
368+ if ( trError && trLoaded && ! trResources ?. length ) {
369+ resolvedError = effectiveError ;
370+ }
408371 }
409- } else if ( effectiveError ) {
372+ } else {
410373 resolvedError = effectiveError ;
411374 }
375+ } else if ( effectiveError ) {
376+ resolvedError = effectiveError ;
377+ }
412378
413- const pendingAdmission = shouldUseMultiCluster ? mcPendingAdmission : false ;
414- const proxyUnavailable = shouldUseMultiCluster ? mcProxyUnavailable : false ;
415-
416- const isTrLoaded = trLoaded || ! ! trError ;
417- return [
418- rResources ,
419- effectiveLoaded ,
420- isTrLoaded ,
421- resolvedError ,
422- pendingAdmission ,
423- proxyUnavailable ,
424- ] ;
425- } , [
426- runs ,
427- trResources ,
428- loaded ,
429- trLoaded ,
430- isTektonResultEnabled ,
431- namespace ,
432- queryTr ,
433- isList ,
434- trError ,
435- error ,
436- ] ) ;
437- } ;
438-
439- export const useTaskRun = (
440- namespace : string ,
441- taskRunName : string ,
442- ) : [ TaskRunKind , boolean , boolean , string ] => {
443- const result = useTaskRuns2 (
444- namespace ,
445- useMemo (
446- ( ) => ( {
447- name : taskRunName ,
448- limit : 1 ,
449- } ) ,
450- [ taskRunName ] ,
451- ) ,
452- ) as unknown as [ TaskRunKind [ ] , boolean , boolean , string ] ;
379+ const pendingAdmission = shouldUseMultiCluster ? mcPendingAdmission : false ;
380+ const proxyUnavailable = shouldUseMultiCluster ? mcProxyUnavailable : false ;
453381
454- return useMemo (
455- ( ) => [
456- result [ 0 ] ?. [ 0 ] ,
457- result [ 1 ] ,
458- result [ 2 ] ,
459- result [ 0 ] ?. [ 0 ] ? undefined : result [ 3 ] ,
460- ] ,
461- [ result ] ,
462- ) ;
382+ const isTrLoaded = trLoaded || ! ! trError ;
383+ return [
384+ rResources ,
385+ effectiveLoaded ,
386+ isTrLoaded ,
387+ resolvedError ,
388+ pendingAdmission ,
389+ proxyUnavailable ,
390+ ] ;
463391} ;
0 commit comments