Skip to content

Commit

Permalink
feat(bulk-import) : resolving comments
Browse files Browse the repository at this point in the history
  • Loading branch information
its-mitesh-kumar committed Dec 23, 2024
1 parent 636dc6e commit ea7ffa3
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 43 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ declare namespace Components {
export type SizePerIntegrationQueryParam = number;
export type SizePerIntegrationQueryParamDeprecated = number;
export type SizeQueryParam = number;
export type SortColumnQueryParam = string;
export type SortColumnQueryParam =
| 'repository.name'
| 'repository.organization'
| 'repository.url'
| 'lastUpdate'
| 'status';
export type SortOrderQueryParam = 'asc' | 'desc';
}
export interface QueryParameters {
Expand Down Expand Up @@ -332,7 +337,12 @@ declare namespace Paths {
export type Search = string;
export type Size = number;
export type SizePerIntegration = number;
export type SortColumn = string;
export type SortColumn =
| 'repository.name'
| 'repository.organization'
| 'repository.url'
| 'lastUpdate'
| 'status';
export type SortOrder = 'asc' | 'desc';
}
export interface QueryParameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,17 @@ const OPENAPI = `
"sortColumnQueryParam": {
"in": "query",
"name": "sortColumn",
"description": "The name of the column to sort by",
"description": "The allowed values for sorting columns:\\n- repository.name: Sort by repository name.\\n- repository.organization: Sort by organization URL.\\n- repository.url: Sort by repository URL.\\n- lastUpdate: Sort by the last time the catalog-info.yaml was updated.\\n- status: Sort by the status of the catalog-info.yaml.\\n",
"required": false,
"schema": {
"type": "string"
"enum": [
"repository.name",
"repository.organization",
"repository.url",
"lastUpdate",
"status"
],
"default": "repository.name"
}
},
"sortOrderQueryParam": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,3 @@ export function getNestedValue<T>(obj: T, path: string): any {
obj,
);
}
export enum SortingOrderEnum {
ASC = 'asc',
DESC = 'desc',
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,23 @@ components:
sortColumnQueryParam:
in: query
name: sortColumn
description: The name of the column to sort by
description: |
The allowed values for sorting columns:
- `repository.name`: Sort by repository name.
- `repository.organization`: Sort by organization URL.
- `repository.url`: Sort by repository URL.
- `lastUpdate`: Sort by the last time the catalog-info.yaml was updated.
- `status`: Sort by the status of the catalog-info.yaml.
required: false
schema:
type: string
enum:
- repository.name
- repository.organization
- repository.url
- lastUpdate
- status
default: repository.name

sortOrderQueryParam:
in: query
name: sortOrder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
getNestedValue,
logErrorIfNeeded,
paginateArray,
SortingOrderEnum,
} from '../../../helpers';
import {
DefaultPageNumber,
Expand All @@ -52,18 +51,27 @@ type FindAllImportsResponse =

function sortImports(
imports: Components.Schemas.Import[],
sortColumn: string = 'repository.name',
sortOrder: string = SortingOrderEnum.ASC,
sortColumn: Components.Parameters.SortColumnQueryParam = 'repository.name',
sortOrder: Components.Parameters.SortOrderQueryParam = 'asc',
) {
imports.sort((a, b) => {
const value1 = getNestedValue(a, sortColumn);
const value2 = getNestedValue(b, sortColumn);
// Handle cases where values are undefined
if (value1 === undefined && value2 === undefined) return 0;
if (value1 === undefined) return SortingOrderEnum.ASC ? -1 : 1;
if (value2 === undefined) return SortingOrderEnum.ASC ? 1 : -1;
if (value1 === undefined) return sortOrder === 'asc' ? -1 : 1;
if (value2 === undefined) return sortOrder === 'asc' ? 1 : -1;

if (sortColumn === 'lastUpdate') {
const date1 = new Date(value1); // Convert string to Date object
const date2 = new Date(value2); // Convert string to Date object
// Compare dates
return sortOrder === 'asc'
? date2.getTime() - date1.getTime()
: date1.getTime() - date2.getTime();
}
// Compare values based on sort order
return SortingOrderEnum.ASC === sortOrder
return sortOrder === 'asc'
? value1.localeCompare(value2)
: value2.localeCompare(value1);
});
Expand All @@ -83,8 +91,8 @@ export async function findAllImports(
search?: string;
pageNumber?: number;
pageSize?: number;
sortColumn?: string;
sortOrder?: string;
sortColumn?: Components.Parameters.SortColumnQueryParam;
sortOrder?: Components.Parameters.SortOrderQueryParam;
},
): Promise<HandlerResponse<FindAllImportsResponse>> {
const apiVersion = requestHeaders?.apiVersion ?? 'v1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ describe('BulkImportBackendClient', () => {
1,
2,
'',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
AddedRepositoryColumnNameEnum.repositoryName,
SortingOrderEnum.Asc,
);
expect(jobs).toEqual(mockGetImportJobs);
});
Expand All @@ -324,8 +324,8 @@ describe('BulkImportBackendClient', () => {
1,
2,
'cup',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
AddedRepositoryColumnNameEnum.repositoryName,
SortingOrderEnum.Asc,
);
expect(jobs).toEqual(
mockGetImportJobs.imports.filter(r =>
Expand All @@ -340,8 +340,8 @@ describe('BulkImportBackendClient', () => {
1,
2,
'',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
AddedRepositoryColumnNameEnum.repositoryName,
SortingOrderEnum.Asc,
),
).resolves.toEqual(expect.objectContaining([]));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export const RepositoriesList = () => {
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const [order, setOrder] = React.useState<SortingOrderEnum>(
SortingOrderEnum.ASC,
SortingOrderEnum.Asc,
);
const [orderBy, setOrderBy] = React.useState<string>(
AddedRepositoryColumnNameEnum.repoName,
AddedRepositoryColumnNameEnum.repositoryName,
);
const { openDialog, setOpenDialog, deleteComponent } = useDeleteDialog();
const { openDrawer, setOpenDrawer, drawerData } = useDrawer();
Expand All @@ -65,7 +65,7 @@ export const RepositoriesList = () => {
pageNumber + 1,
rowsPerPage,
debouncedSearch,
AddedRepositoryColumnNameEnum[orderByColumn] || '',
AddedRepositoryColumnNameEnum[orderByColumn],
order,
);

Expand All @@ -89,7 +89,7 @@ export const RepositoriesList = () => {
property: string,
) => {
const isAsc = orderBy === property && order === 'asc';
setOrder(isAsc ? SortingOrderEnum.DESC : SortingOrderEnum.ASC);
setOrder(isAsc ? SortingOrderEnum.Desc : SortingOrderEnum.Asc);
setOrderBy(property);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ describe('useAddedRepositories', () => {
1,
5,
'',
AddedRepositoryColumnNameEnum.repoName,
SortingOrderEnum.ASC,
AddedRepositoryColumnNameEnum.repositoryName,
SortingOrderEnum.Asc,
),
);
await waitFor(() => {
Expand Down
9 changes: 6 additions & 3 deletions workspaces/bulk-import/plugins/bulk-import/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,19 @@ export enum RepositoryStatus {
'REPO_EMPTY' = 'REPO_EMPTY',
'CODEOWNERS_FILE_NOT_FOUND_IN_REPO' = 'CODEOWNERS_FILE_NOT_FOUND_IN_REPO',
}

export enum AddedRepositoryColumnNameEnum {
repoName = 'repository.name',
repoName = 'repository.name', // because of property name
repositoryName = 'repository.name',
organizationUrl = 'repository.organization',
repoUrl = 'repository.url',
catalogInfoYamlLastUpdated = 'lastUpdate',
catalogInfoYamlStatus = 'status',
}

export enum SortingOrderEnum {
ASC = 'asc',
DESC = 'desc',
Asc = 'asc',
Desc = 'desc',
}
export enum RepositorySelection {
Repository = 'repository',
Expand Down

0 comments on commit ea7ffa3

Please sign in to comment.