Skip to content

Commit

Permalink
fix(pipeline-run): port tekton results version upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
sahil143 committed Dec 2, 2024
1 parent ca6331a commit a8e86db
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { useTaskRuns } from '../../hooks/useTaskRuns';
import { commonFetchJSON, getK8sResourceURL } from '../../k8s';
import { PodModel } from '../../models/pod';
import { getPipelineRunFromTaskRunOwnerRef } from '../../utils/common-utils';
import { getTaskRunLog } from '../../utils/tekton-results';
import { useWorkspaceInfo } from '../Workspace/useWorkspaceInfo';
import {
Expand Down Expand Up @@ -65,10 +66,12 @@ export const useEnterpriseContractResultFromLogs = (
if (fetchTknLogs) {
const fetch = async () => {
try {
const pid = getPipelineRunFromTaskRunOwnerRef(taskRun[0])?.uid;
const logs = await getTaskRunLog(
workspace,
taskRun[0].metadata.namespace,
taskRun[0].metadata.name,
taskRun[0].metadata.uid,
pid,
);
if (unmount) return;
const json = extractEcResultsFromTaskRunLogs(logs);
Expand Down
32 changes: 27 additions & 5 deletions src/hooks/__tests__/useTektonResults.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// import { QueryClientProvider } from '@tanstack/react-query';
// import { act, renderHook as rtlRenderHook } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react-hooks';
import { PipelineRunModel } from '../../models';
import { TaskRunKind } from '../../types';
import {
// TektonResultsOptions,
getPipelineRuns,
Expand Down Expand Up @@ -180,6 +182,16 @@ describe('useTektonResults', () => {
});
});

const mockTR = {
metadata: {
name: 'sample-task-run',
uid: 'sample-task-run-id',
ownerReferences: [
{ kind: PipelineRunModel.kind, uid: 'sample-pipeline-run-id', name: 'sample-pipeline-run' },
],
},
} as TaskRunKind;

describe('useTRTaskRunLog', () => {
it('should not attempt to get task run log', () => {
renderHook(() => useTRTaskRunLog(null, null));
Expand All @@ -188,14 +200,19 @@ describe('useTektonResults', () => {
renderHook(() => useTRTaskRunLog('test-ns', null));
expect(getTaskRunLogMock).not.toHaveBeenCalled();

renderHook(() => useTRTaskRunLog(null, 'sample-task-run'));
renderHook(() => useTRTaskRunLog(null, mockTR));
expect(getTaskRunLogMock).not.toHaveBeenCalled();
});

it('should return task run log', async () => {
getTaskRunLogMock.mockReturnValue('sample log');
const { result, waitFor } = renderHook(() => useTRTaskRunLog('test-ns', 'sample-task-run'));
expect(getTaskRunLogMock).toHaveBeenCalledWith('test-ws', 'test-ns', 'sample-task-run');
const { result, waitFor } = renderHook(() => useTRTaskRunLog('test-ns', mockTR));
expect(getTaskRunLogMock).toHaveBeenCalledWith(
'test-ws',
'test-ns',
'sample-task-run-id',
'sample-pipeline-run-id',
);
expect(result.current).toEqual([null, false, undefined]);
await waitFor(() => result.current[1]);
expect(result.current).toEqual(['sample log', true, undefined]);
Expand All @@ -206,8 +223,13 @@ describe('useTektonResults', () => {
getTaskRunLogMock.mockImplementation(() => {
throw error;
});
const { result } = renderHook(() => useTRTaskRunLog('test-ns', 'sample-task-run'));
expect(getTaskRunLogMock).toHaveBeenCalledWith('test-ws', 'test-ns', 'sample-task-run');
const { result } = renderHook(() => useTRTaskRunLog('test-ns', mockTR));
expect(getTaskRunLogMock).toHaveBeenCalledWith(
'test-ws',
'test-ns',
'sample-task-run-id',
'sample-pipeline-run-id',
);
expect(result.current).toEqual([null, false, error]);
});
});
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useTektonResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { useInfiniteQuery } from '@tanstack/react-query';
import { useWorkspaceInfo } from '../components/Workspace/useWorkspaceInfo';
import { PipelineRunKind, TaskRunKind } from '../types';
import { getPipelineRunFromTaskRunOwnerRef } from '../utils/common-utils';
import {
TektonResultsOptions,
getTaskRunLog,
Expand Down Expand Up @@ -61,16 +62,18 @@ export const useTRTaskRuns = (

export const useTRTaskRunLog = (
namespace: string,
taskRunName: string,
taskRun: TaskRunKind,
): [string, boolean, unknown] => {
const { workspace } = useWorkspaceInfo();
const [result, setResult] = React.useState<[string, boolean, unknown]>([null, false, undefined]);
const taskRunUid = taskRun.metadata.uid;
const pipelineRunUid = getPipelineRunFromTaskRunOwnerRef(taskRun)?.uid;
React.useEffect(() => {
let disposed = false;
if (namespace && taskRunName) {
if (namespace && taskRunUid) {
void (async () => {
try {
const log = await getTaskRunLog(workspace, namespace, taskRunName);
const log = await getTaskRunLog(workspace, namespace, taskRunUid, pipelineRunUid);
if (!disposed) {
setResult([log, true, undefined]);
}
Expand All @@ -84,6 +87,6 @@ export const useTRTaskRunLog = (
return () => {
disposed = true;
};
}, [workspace, namespace, taskRunName]);
}, [workspace, namespace, taskRunUid, pipelineRunUid]);
return result;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export const TektonTaskRunLog: React.FC<React.PropsWithChildren<TektonTaskRunLog
}) => {
const scrollPane = React.useRef<HTMLDivElement>();
const taskName = taskRun?.spec.taskRef?.name ?? taskRun?.metadata.name;
const [trResults, trLoaded, trError] = useTRTaskRunLog(
taskRun.metadata.namespace,
taskRun.metadata.name,
);
const [trResults, trLoaded, trError] = useTRTaskRunLog(taskRun.metadata.namespace, taskRun);

React.useEffect(() => {
setCurrentLogsGetter(() => scrollPane.current?.innerText);
Expand Down
14 changes: 11 additions & 3 deletions src/shared/components/pipeline-run-logs/logs/logs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { saveAs } from 'file-saver';
import { commonFetchText } from '../../../../k8s';
import { K8sGetResource } from '../../../../k8s/k8s-fetch';
import { getK8sResourceURL } from '../../../../k8s/k8s-utils';
import { PipelineRunModel } from '../../../../models';
import { PodModel } from '../../../../models/pod';
import { TaskRunKind } from '../../../../types';
import { getTaskRunLog } from '../../../../utils/tekton-results';
Expand Down Expand Up @@ -146,9 +147,16 @@ export const getDownloadAllLogsCallback = (
]);
}
} else {
allLogs += await getTaskRunLog(workspace, namespace, currTask).then(
(log) => `${tasks[currTask].name.toUpperCase()}\n\n${log}\n\n`,
);
const taskRun = taskRuns.find((t) => t.metadata.name === currTask);
const pipelineRunUID = taskRun?.metadata?.ownerReferences?.find(
(res) => res.kind === PipelineRunModel.kind,
)?.uid;
allLogs += await getTaskRunLog(
workspace,
namespace,
pipelineRunUID,
taskRun?.metadata?.uid,
).then((log) => `${tasks[currTask].name.toUpperCase()}\n\n${log}\n\n`);
}
}
const buffer = new LineBuffer();
Expand Down
Loading

0 comments on commit a8e86db

Please sign in to comment.