Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(KONFLUX-1680): filter records on creationTimestamp #92

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/components/Activity/__tests__/ActivityTab.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jest.mock('react-router-dom', () => {
};
});

jest.mock('../../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

jest.mock('../../Workspace/useWorkspaceInfo', () => ({
Comment on lines +15 to 19
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a utility to mock useApplication hook,

Look at https://github.com/konflux-ci/konflux-ui/blob/main/src/utils/test-utils.tsx#L213 how to create mock hook utility

useWorkspaceInfo: jest.fn(() => ({ namespace: 'test-ns', workspace: 'test-ws' })),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jest.mock('../../commit-status', () => ({
useCommitStatus: () => ['-', true],
}));

jest.mock('../../../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

jest.mock('../../../../shared/components/table/TableComponent', () => {
return (props) => {
const { data, filters, selected, match, kindObj } = props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jest.mock('../../../../hooks/useComponents', () => ({
useComponent: jest.fn(),
}));

jest.mock('../../../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

const watchResourceMock = createK8sWatchResourceMock();
const useComponentsMock = useComponents as jest.Mock;
const componentMock = useComponent as jest.Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ jest.mock('../../../../utils/rbac', () => ({
useAccessReviewForModel: jest.fn(() => [true, true]),
}));

jest.mock('../../../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

jest.mock('react-router-dom', () => {
const actual = jest.requireActual('react-router-dom');
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { FilterIcon } from '@patternfly/react-icons/dist/esm/icons/filter-icon';
import { debounce } from 'lodash-es';
import { PipelineRunLabel } from '../../../consts/pipelinerun';
import { useComponents } from '../../../hooks/useComponents';
import { useApplication } from '../../../hooks/useApplications';
import { usePipelineRuns } from '../../../hooks/usePipelineRuns';
import { usePLRVulnerabilities } from '../../../hooks/useScanResults';
import { useSearchParam } from '../../../hooks/useSearchParam';
Expand Down Expand Up @@ -46,7 +46,7 @@ const PipelineRunsListView: React.FC<React.PropsWithChildren<PipelineRunsListVie
customFilter,
}) => {
const { namespace, workspace } = useWorkspaceInfo();
const [components, componentsLoaded] = useComponents(namespace, workspace, applicationName);
const [application, applicationLoaded] = useApplication(namespace, workspace, applicationName);
const [nameFilter, setNameFilter] = useSearchParam('name', '');
const [statusFilterExpanded, setStatusFilterExpanded] = React.useState<boolean>(false);
const [statusFiltersParam, setStatusFiltersParam] = useSearchParam('status', '');
Expand All @@ -60,29 +60,25 @@ const PipelineRunsListView: React.FC<React.PropsWithChildren<PipelineRunsListVie

const [pipelineRuns, loaded, error, getNextPage, { isFetchingNextPage, hasNextPage }] =
usePipelineRuns(
componentsLoaded ? namespace : null,
applicationLoaded ? namespace : null,
workspace,
React.useMemo(
() => ({
selector: {
filterByCreationTimestampAfter: application?.metadata?.creationTimestamp,
matchLabels: {
[PipelineRunLabel.APPLICATION]: applicationName,
...(!onLoadName &&
componentName && {
[PipelineRunLabel.COMPONENT]: componentName,
}),
},
...(!onLoadName && {
matchExpressions: [
{
key: `${PipelineRunLabel.COMPONENT}`,
operator: 'In',
values: componentName
? [componentName]
: components?.map((c) => c.metadata?.name),
},
],
...(onLoadName && {
filterByName: onLoadName.trim().toLowerCase(),
}),
...(onLoadName && { filterByName: onLoadName.trim().toLowerCase() }),
},
}),
[applicationName, componentName, components, onLoadName],
[applicationName, componentName, application, onLoadName],
),
);
const statusFilters = React.useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jest.mock('../../../../hooks/usePipelineRuns', () => ({
usePipelineRuns: jest.fn(),
}));

jest.mock('../../../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

jest.mock('../../../../hooks/useScanResults', () => ({
usePLRVulnerabilities: jest.fn(() => ({ vulnerabilities: {}, fetchedPipelineRuns: [] })),
}));
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/__tests__/useBuildPipelines.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { useTRPipelineRuns } from '../useTektonResults';

jest.mock('../useTektonResults');

jest.mock('../useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

const useK8sWatchResourceMock = createK8sWatchResourceMock();
const useTRPipelineRunsMock = useTRPipelineRuns as jest.Mock;

Expand Down
4 changes: 4 additions & 0 deletions src/hooks/__tests__/useLatestBuildPipelines.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { useTRPipelineRuns } from '../useTektonResults';

jest.mock('../useTektonResults');

jest.mock('../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

const useK8sWatchResourceMock = createK8sWatchResourceMock();
const useTRPipelineRunsMock = useTRPipelineRuns as jest.Mock;

Expand Down
4 changes: 4 additions & 0 deletions src/hooks/__tests__/usePACStatesForComponents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jest.mock('../../hooks/useApplicationPipelineGitHubApp', () => ({
})),
}));

jest.mock('../../hooks/useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

const useK8sWatchResourceMock = createK8sWatchResourceMock();
const useTRPipelineRunsMock = useTRPipelineRuns as jest.Mock;

Expand Down
4 changes: 4 additions & 0 deletions src/hooks/__tests__/usePipelineRuns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { useTRPipelineRuns, useTRTaskRuns } from '../useTektonResults';
jest.mock('../useTektonResults');
jest.mock('../useComponents');

jest.mock('../useApplications', () => ({
useApplication: jest.fn().mockReturnValue([{ metadata: { name: 'test' } }, true]),
}));

jest.mock('../../components/Workspace/useWorkspaceInfo', () => ({
useWorkspaceInfo: jest.fn(() => ({ namespace: 'test-ns', workspace: 'test-ws' })),
}));
Expand Down
17 changes: 5 additions & 12 deletions src/hooks/useBuildPipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { PipelineRunLabel, PipelineRunType } from '../consts/pipelinerun';
import { PipelineRunGroupVersionKind } from '../models';
import { PipelineRunKind } from '../types';
import { useApplication } from './useApplications';
import { usePipelineRuns } from './usePipelineRuns';
import { GetNextPage } from './useTektonResults';

Expand All @@ -15,32 +16,24 @@
limit?: number,
): [PipelineRunKind[], boolean, unknown, GetNextPage] => {
const { workspace } = useWorkspaceInfo();
const [application, applicationLoaded] = useApplication(namespace, workspace, applicationName);
const [pipelineRuns, loaded, plrError, getNextPage] = usePipelineRuns(
includeComponents && !componentNames?.length ? null : namespace,
!applicationLoaded && includeComponents && !componentNames?.length ? null : namespace,
workspace,
React.useMemo(
() => ({
selector: {
filterByCreationTimestampAfter: application?.metadata?.creationTimestamp,
matchLabels: {
[PipelineRunLabel.APPLICATION]: applicationName,
[PipelineRunLabel.PIPELINE_TYPE]: PipelineRunType.BUILD,
},
...(includeComponents &&
componentNames?.length > 0 && {
matchExpressions: [
{
key: PipelineRunLabel.COMPONENT,
operator: 'In',
values: componentNames,
},
],
}),
filterByCommit: commit ? commit : undefined,
},
// TODO: Add limit when filtering by component name AND only PLRs are returned: https://github.com/tektoncd/results/issues/620
// limit,
}),
[applicationName, includeComponents, componentNames, commit],
[applicationName, commit, application],

Check warning on line 36 in src/hooks/useBuildPipelines.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useBuildPipelines.ts#L36

Added line #L36 was not covered by tests
),
);

Expand Down
14 changes: 5 additions & 9 deletions src/hooks/useLatestBuildPipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { useWorkspaceInfo } from '../components/Workspace/useWorkspaceInfo';
import { PipelineRunLabel, PipelineRunType } from '../consts/pipelinerun';
import { PipelineRunKind } from '../types';
import { useApplication } from './useApplications';
import { usePipelineRuns } from './usePipelineRuns';

export const useLatestBuildPipelines = (
Expand All @@ -12,6 +13,7 @@ export const useLatestBuildPipelines = (
const { workspace } = useWorkspaceInfo();
const [foundNames, setFoundNames] = React.useState<string[]>([]);
const [latestBuilds, setLatestBuilds] = React.useState<PipelineRunKind[]>([]);
const [application, applicationLoaded] = useApplication(namespace, workspace, applicationName);

React.useEffect(() => {
setFoundNames([]);
Expand All @@ -23,25 +25,19 @@ export const useLatestBuildPipelines = (
);

const [pipelines, loaded, error, getNextPage] = usePipelineRuns(
neededNames?.length ? namespace : null,
applicationLoaded && neededNames?.length ? namespace : null,
workspace,
React.useMemo(
() => ({
selector: {
filterByCreationTimestampAfter: application?.metadata?.creationTimestamp,
matchLabels: {
[PipelineRunLabel.APPLICATION]: applicationName,
[PipelineRunLabel.PIPELINE_TYPE]: PipelineRunType.BUILD,
},
matchExpressions: [
{
key: PipelineRunLabel.COMPONENT,
operator: 'In',
values: neededNames,
},
],
},
}),
[applicationName, neededNames],
[applicationName, application],
),
);

Expand Down
7 changes: 1 addition & 6 deletions src/hooks/useLatestIntegrationTestPipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export const useLatestIntegrationTestPipelines = (
[PipelineRunLabel.PIPELINE_TYPE]: PipelineRunType.TEST,
},
matchExpressions: [
{
key: PipelineRunLabel.COMPONENT,
operator: 'In',
values: componentNames,
},
{
key: PipelineRunLabel.TEST_SERVICE_SCENARIO,
operator: 'In',
Expand All @@ -53,7 +48,7 @@ export const useLatestIntegrationTestPipelines = (
],
},
}),
[applicationName, componentNames, neededNames],
[applicationName, neededNames],
),
);

Expand Down
16 changes: 7 additions & 9 deletions src/hooks/usePACStatesForComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SAMPLE_ANNOTATION,
} from '../utils/component-utils';
import { useApplicationPipelineGitHubApp } from './useApplicationPipelineGitHubApp';
import { useApplication } from './useApplications';
import { PACState } from './usePACState';
import { usePipelineRuns } from './usePipelineRuns';

Expand Down Expand Up @@ -58,7 +59,8 @@ const usePACStatesForComponents = (components: ComponentKind[]): PacStatesForCom
);
const { name: prBotName } = useApplicationPipelineGitHubApp();
const namespace = components?.[0]?.metadata.namespace;
const application = components?.[0]?.spec.application;
const applicationName = components?.[0]?.spec.application;
const [application, applicationLoaded] = useApplication(namespace, workspace, applicationName);

React.useEffect(() => {
setComponentPacStates(() => getInitialPacStates(components));
Expand All @@ -75,27 +77,23 @@ const usePACStatesForComponents = (components: ComponentKind[]): PacStatesForCom
);

const [pipelineBuildRuns, pipelineBuildRunsLoaded, , getNextPage] = usePipelineRuns(
neededNames.length ? namespace : null,
applicationLoaded && neededNames.length ? namespace : null,
workspace,
React.useMemo(
() => ({
selector: {
filterByCreationTimestampAfter: application?.metadata?.creationTimestamp,
matchLabels: {
[PipelineRunLabel.PIPELINE_TYPE]: PipelineRunType.BUILD,
[PipelineRunLabel.APPLICATION]: application,
[PipelineRunLabel.APPLICATION]: applicationName,
[PipelineRunLabel.COMMIT_EVENT_TYPE_LABEL]: PipelineRunEventType.PUSH,
},
matchExpressions: [
{
key: PipelineRunLabel.COMPONENT,
operator: 'In',
values: neededNames,
},
{ key: PipelineRunLabel.PULL_REQUEST_NUMBER_LABEL, operator: 'DoesNotExist' },
],
},
}),
[application, neededNames],
[applicationName, application],
),
);

Expand Down
19 changes: 7 additions & 12 deletions src/hooks/usePipelineRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { K8sGroupVersionKind, K8sModelCommon, K8sResourceCommon, Selector } from
import { getCommitSha } from '../utils/commits-utils';
import { pipelineRunStatus, runStatus } from '../utils/pipeline-utils';
import { EQ } from '../utils/tekton-results';
import { useApplication } from './useApplications';
import { useComponents } from './useComponents';
import { GetNextPage, NextPageProps, useTRPipelineRuns, useTRTaskRuns } from './useTektonResults';

Expand Down Expand Up @@ -265,37 +266,31 @@ export const usePipelineRunsForCommit = (
limit?: number,
): [PipelineRunKind[], boolean, unknown, GetNextPage, NextPageProps] => {
const [components, componentsLoaded] = useComponents(namespace, workspace, applicationName);
const [application, applicationLoaded] = useApplication(namespace, workspace, applicationName);

const componentNames = React.useMemo(
() => (componentsLoaded ? components.map((c) => c.metadata?.name) : []),
[components, componentsLoaded],
);

const [pipelineRuns, plrsLoaded, plrError, getNextPage, nextPageProps] = usePipelineRuns(
namespace && applicationName && commit && componentsLoaded ? namespace : null,
namespace && applicationName && commit && componentsLoaded && applicationLoaded
? namespace
: null,
workspace,
React.useMemo(
() => ({
selector: {
filterByCreationTimestampAfter: application?.metadata?.creationTimestamp,
matchLabels: {
[PipelineRunLabel.APPLICATION]: applicationName,
},
...(componentsLoaded &&
componentNames?.length > 0 && {
matchExpressions: [
{
key: PipelineRunLabel.COMPONENT,
operator: 'In',
values: componentNames,
},
],
}),
filterByCommit: commit,
},
// TODO: Add limit when filtering by component name AND only PLRs are returned
// limit,
}),
[applicationName, commit, componentNames, componentsLoaded],
[applicationName, commit, application],
),
);

Expand Down
16 changes: 15 additions & 1 deletion src/utils/__tests__/tekton-results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,21 @@ describe('tekton-results', () => {
selectorToFilter({
filterByName: 'resource-name',
}),
).toStrictEqual('data.metadata.labels["filterByName"] == "resource-name"');
).toStrictEqual('data.metadata.name.startsWith("resource-name")');
});

it('should return creationTimestamp filter', () => {
expect(
selectorToFilter({
filterByCreationTimestampAfter: '2021-01-01T00:00:00Z',
}),
).toStrictEqual('data.metadata.creationTimestamp > "2021-01-01T00:00:00Z"');

expect(
selectorToFilter({
filterByCreationTimestampAfter: 'not a timestamp',
}),
).toStrictEqual('');
});

it('should return the label filter', () => {
Expand Down
Loading