Skip to content

Commit

Permalink
feat(tekton-results): update to use ownerReference id
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinandan13jan committed Nov 12, 2024
1 parent 298ce7a commit 6fcc21b
Show file tree
Hide file tree
Showing 26 changed files with 219 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ const getResultsSummary = (ECs, ecLoaded) => {

export const SecurityEnterpriseContractTab: React.FC<
React.PropsWithChildren<{
pipelineRunName: string;
pipelineRunUID: string;
pipelineRun: string;
}>
> = ({ pipelineRunName, pipelineRunUID }) => {
const [ecResult, ecResultLoaded] = useEnterpriseContractResults(pipelineRunName, pipelineRunUID);
> = ({ pipelineRun }) => {
const [ecResult, ecResultLoaded] = useEnterpriseContractResults(pipelineRun);

const [nameFilter, setNameFilter] = useSearchParam('name', '');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ describe('SecurityEnterpriseContractTab', () => {
it('should render empty state for security tab when pods are missing', () => {
mockUseEnterpriseContractResults.mockReturnValue([undefined, true]);

routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
screen.getByTestId('security-tab-empty-state');
});

it('should render component security tab', () => {
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
screen.getByText('Missing CVE scan results');
});

it('should filter out results based on the name input field', () => {
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
screen.getByText('Missing CVE scan results');
fireEvent.input(screen.getByPlaceholderText('Filter by rule...'), {
target: { value: 'No tasks' },
Expand All @@ -49,9 +43,7 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should filter out based on the status dropdown', async () => {
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
screen.getByText('Missing CVE scan results');
fireEvent.click(screen.getByRole('button', { name: 'Status filter menu' }));
fireEvent.click(screen.getByLabelText('Success'));
Expand All @@ -61,9 +53,7 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should show empty state when no search result found', () => {
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
screen.getByText('Missing CVE scan results');
fireEvent.click(screen.getByRole('button', { name: 'Status filter menu' }));
fireEvent.click(screen.getByLabelText('Failed'));
Expand All @@ -78,9 +68,7 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should sort by Status', () => {
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
const status = screen.getAllByTestId('rule-status');
expect(status[0].textContent.trim()).toEqual('Failed');
fireEvent.click(screen.getAllByText('Status')[1]);
Expand All @@ -89,9 +77,7 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should render result summary', () => {
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
const resultSummary = screen.getByTestId('result-summary');
const status = resultSummary.getElementsByTagName('span');
expect(status[0].textContent.trim()).toBe('Failed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('useEnterpriseContractResultFromLogs', () => {

it('should parse valid rules to json', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
useEnterpriseContractResultFromLogs('dummy-abcd'),
);
await waitForNextUpdate();
expect(mockCommmonFetchJSON).toHaveBeenCalled();
Expand All @@ -69,9 +69,7 @@ describe('useEnterpriseContractResultFromLogs', () => {
mockGetTaskRunLogs.mockReturnValue(`asdfcdfadsf
[report-json] { "components": [] }
`);
const { result } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
);
const { result } = renderHook(() => useEnterpriseContractResultFromLogs('dummy-abcd'));
const [, loaded] = result.current;
expect(mockCommmonFetchJSON).toHaveBeenCalled();
expect(loaded).toBe(true);
Expand All @@ -82,7 +80,7 @@ describe('useEnterpriseContractResultFromLogs', () => {

it('should filter out all 404 image url components from EC results', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
useEnterpriseContractResultFromLogs('dummy-abcd'),
);
await waitForNextUpdate();
const [ecResult, loaded] = result.current;
Expand All @@ -95,7 +93,7 @@ describe('useEnterpriseContractResultFromLogs', () => {
mockCommmonFetchJSON.mockRejectedValue(new Error('Api error'));

const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
useEnterpriseContractResultFromLogs('dummy-abcd'),
);
await waitForNextUpdate();
const [ecResult, loaded] = result.current;
Expand Down Expand Up @@ -126,7 +124,7 @@ describe('useEnterpriseContractResultFromLogs', () => {
`);

const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
useEnterpriseContractResultFromLogs('dummy-abcd'),
);
const [, loaded] = result.current;
expect(mockCommmonFetchJSON).toHaveBeenCalled();
Expand Down Expand Up @@ -179,7 +177,7 @@ describe('useEnterpriseContractResults', () => {

it('should return enterprise contract results', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResults('dummy-abcd', 'pipelinerun-test'),
useEnterpriseContractResults('dummy-abcd'),
);
expect(result.current[0]).toEqual(undefined);
expect(result.current[1]).toEqual(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { commonFetchJSON, getK8sResourceURL } from '@openshift/dynamic-plugin-sdk-utils';
import { useTaskRuns } from '../../hooks/useTaskRuns';
import { PipelineRunModel } from '../../models';
import { PodModel } from '../../models/pod';
import { getTaskRunLog } from '../../utils/tekton-results';
import { useWorkspaceInfo } from '../../utils/workspace-context-utils';
Expand All @@ -14,7 +15,6 @@ import { extractEcResultsFromTaskRunLogs } from './utils';

export const useEnterpriseContractResultFromLogs = (
pipelineRunName: string,
pipelineRunUID: string,
): [ComponentEnterpriseContractResult[], boolean] => {
const { namespace, workspace } = useWorkspaceInfo();
const [taskRun, loaded, error] = useTaskRuns(namespace, pipelineRunName, 'verify');
Expand Down Expand Up @@ -67,13 +67,16 @@ export const useEnterpriseContractResultFromLogs = (
React.useEffect(() => {
let unmount = false;
if (fetchTknLogs) {
const pipelineRunUID = taskRun[0]?.metadata?.ownerReferences?.find(
(reference) => reference.kind === PipelineRunModel.kind,

Check warning on line 71 in src/components/EnterpriseContractView/useEnterpriseContractResultFromLogs.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/EnterpriseContractView/useEnterpriseContractResultFromLogs.tsx#L71

Added line #L71 was not covered by tests
)?.uid;
const fetch = async () => {
try {
const logs = await getTaskRunLog(
workspace,
taskRun[0].metadata.namespace,
pipelineRunUID,
taskRun[0],
taskRun[0].metadata?.uid,
);
if (unmount) return;
const json = extractEcResultsFromTaskRunLogs(logs);
Expand All @@ -95,7 +98,7 @@ export const useEnterpriseContractResultFromLogs = (
return () => {
unmount = true;
};
}, [fetchTknLogs, taskRun, workspace, pipelineRunUID]);
}, [fetchTknLogs, taskRun, workspace]);

const ecResult = React.useMemo(() => {
// filter out components for which ec didn't execute because invalid image URL
Expand Down Expand Up @@ -160,9 +163,8 @@ export const mapEnterpriseContractResultData = (

export const useEnterpriseContractResults = (
pipelineRunName: string,
pipelineRunUID: string,
): [UIEnterpriseContractData[], boolean] => {
const [ec, ecLoaded] = useEnterpriseContractResultFromLogs(pipelineRunName, pipelineRunUID);
const [ec, ecLoaded] = useEnterpriseContractResultFromLogs(pipelineRunName);
const ecResult = React.useMemo(() => {
return ecLoaded && ec ? mapEnterpriseContractResultData(ec) : undefined;
}, [ec, ecLoaded]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ export const PipelineRunDetailsView: React.FC<
key: 'security',
label: 'Security',
component: (
<SecurityEnterpriseContractTab
pipelineRunName={pipelineRun.metadata.name}
pipelineRunUID={pipelineRun.metadata.uid}
/>
<SecurityEnterpriseContractTab pipelineRun={pipelineRun.metadata.name} />
),
},
]
Expand Down
12 changes: 2 additions & 10 deletions src/components/PipelineRunDetailsView/PipelineRunSidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ import { isTaskNode } from './visualization/utils/pipelinerun-graph-utils';

type Props = {
scrollIntoView?: (node: Node) => void;
pipelineRunUID: string;
};

const PipelineRunSidePanel: React.FC<React.PropsWithChildren<Props>> = ({
scrollIntoView,
pipelineRunUID,
}) => {
const PipelineRunSidePanel: React.FC<React.PropsWithChildren<Props>> = ({ scrollIntoView }) => {
const [[selectedId], setSelectedIds] = useVisualizationState<string[]>(SELECTION_STATE, []);
const controller = useVisualizationController();

Expand All @@ -32,11 +28,7 @@ const PipelineRunSidePanel: React.FC<React.PropsWithChildren<Props>> = ({
}, [controller, selectedId]);

const panel = taskNode ? (
<TaskRunPanel
onClose={() => setSelectedIds([])}
taskNode={taskNode}
pipelineRunUID={pipelineRunUID}
/>
<TaskRunPanel onClose={() => setSelectedIds([])} taskNode={taskNode} />
) : null;

const isExpanded = !!panel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ const PipelineRunVisualization = ({ pipelineRun, error, taskRuns }) => {
layoutFactory={layoutFactory}
model={model}
>
<PipelineRunSidePanel
scrollIntoView={scrollIntoView}
pipelineRunUID={pipelineRun?.metadata?.uid}
/>
<PipelineRunSidePanel scrollIntoView={scrollIntoView} />
</VisualizationFactory>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('PipelineRunSidePanel', () => {
it('should render nothing by default', () => {
const setPropsFn = jest.fn();

render(<PipelineRunSidePanel pipelineRunUID="pipelinerun-test" />, {
render(<PipelineRunSidePanel />, {
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
Expand All @@ -42,7 +42,7 @@ describe('PipelineRunSidePanel', () => {

const setPropsFn = jest.fn();

render(<PipelineRunSidePanel pipelineRunUID="pipelinerun-test" />, {
render(<PipelineRunSidePanel />, {
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
Expand All @@ -68,16 +68,13 @@ describe('PipelineRunSidePanel', () => {

const setPropsFn = jest.fn();

render(
<PipelineRunSidePanel pipelineRunUID="pipelinerun-test" scrollIntoView={scrollIntoViewFn} />,
{
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
</SidePanelContext.Provider>
),
},
);
render(<PipelineRunSidePanel scrollIntoView={scrollIntoViewFn} />, {
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
</SidePanelContext.Provider>
),
});

expect(setPropsFn).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ import './TaskRunPanel.scss';
type Props = {
onClose: () => void;
taskNode: GraphElement<ElementModel, PipelineRunNodeData>;
pipelineRunUID: string;
};

const TaskRunPanel: React.FC<React.PropsWithChildren<Props>> = ({
taskNode,
onClose,
pipelineRunUID,
}) => {
const TaskRunPanel: React.FC<React.PropsWithChildren<Props>> = ({ taskNode, onClose }) => {
const task = taskNode.getData().task;
const taskRun = taskNode.getData().taskRun;
const { status } = taskNode.getData();
Expand Down Expand Up @@ -69,7 +64,6 @@ const TaskRunPanel: React.FC<React.PropsWithChildren<Props>> = ({
taskRun={taskRun}
namespace={taskNode.getData().namespace}
status={status}
pipelineRunUID={pipelineRunUID}
/>
</DrawerPanelBody>
</Tab>
Expand Down
15 changes: 2 additions & 13 deletions src/components/TaskRunDetailsView/TaskRunDetailsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,26 +117,15 @@ export const TaskRunDetailsView: React.FC<React.PropsWithChildren<TaskRunDetails
body={JSON.stringify(plrError)}
/>
) : (
<TaskRunLogsTab pipelineRunUID={pipelineRun?.metadata?.uid} taskRun={taskRun} />
<TaskRunLogsTab taskRun={taskRun} />
),
},
...(isEnterpriseContract
? [
{
key: 'security',
label: 'Security',
component:
(plrLoaded && plrError) || (plrLoaded && !pipelineRun) ? (
<ErrorEmptyState
title={`Unable to load pipelineRun ${plrName}`}
body={JSON.stringify(plrError)}
/>
) : (
<SecurityEnterpriseContractTab
pipelineRunName={plrName}
pipelineRunUID={pipelineRun?.metadata?.uid}
/>
),
component: <SecurityEnterpriseContractTab pipelineRun={plrName} />,
},
]
: []),
Expand Down
15 changes: 2 additions & 13 deletions src/components/TaskRunDetailsView/tabs/TaskRunLogsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ import TaskRunLogs from '../../TaskRuns/TaskRunLogs';

export type TaskRunLogProps = {
taskRun: TaskRunKind;
pipelineRunUID: string;
};

const TaskRunLogsTab: React.FC<React.PropsWithChildren<TaskRunLogProps>> = ({
taskRun,
pipelineRunUID,
}) => {
const TaskRunLogsTab: React.FC<React.PropsWithChildren<TaskRunLogProps>> = ({ taskRun }) => {
const status = taskRunStatus(taskRun);
const namespace = taskRun.metadata?.namespace;

return (
<TaskRunLogs
taskRun={taskRun}
status={status}
namespace={namespace}
pipelineRunUID={pipelineRunUID}
/>
);
return <TaskRunLogs taskRun={taskRun} status={status} namespace={namespace} />;
};

export default TaskRunLogsTab;
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('TaskRunLogs', () => {
],
},
}}
pipelineRunUID="test-id"
/>,
{
wrapper: BrowserRouter,
Expand Down
Loading

0 comments on commit 6fcc21b

Please sign in to comment.