forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid pre-fetching tekton hub task versions
- Loading branch information
1 parent
cadd0da
commit a6852ae
Showing
15 changed files
with
360 additions
and
143 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
frontend/packages/pipelines-plugin/src/components/catalog/apis/__tests__/tektonHub.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { setUtilsConfig } from '@console/dynamic-plugin-sdk/src/app/configSetup'; | ||
import { appInternalFetch } from '@console/internal/co-fetch'; | ||
import { sampleTektonHubCatalogItem } from '../../../../test-data/catalog-item-data'; | ||
import { | ||
TektonHubTaskVersion, | ||
getApiResponse, | ||
getHubUIPath, | ||
TEKTON_HUB_ENDPOINT, | ||
} from '../tektonHub'; | ||
|
||
const emptyHeaders = new Headers(); | ||
const apiResults = ({ | ||
ok, | ||
status, | ||
versions, | ||
}: { | ||
ok: boolean; | ||
status?: number; | ||
versions?: TektonHubTaskVersion[]; | ||
}) => | ||
Promise.resolve({ | ||
headers: emptyHeaders, | ||
ok, | ||
...(status && { status }), | ||
json: () => ({ | ||
data: { | ||
versions, | ||
}, | ||
}), | ||
}); | ||
|
||
setUtilsConfig({ appFetch: appInternalFetch }); | ||
|
||
describe('getApiResponse', () => { | ||
beforeEach(() => { | ||
window.fetch = jest.fn(() => | ||
apiResults({ | ||
ok: true, | ||
status: 200, | ||
versions: sampleTektonHubCatalogItem.attributes.versions, | ||
}), | ||
); | ||
window.console.warn = jest.fn(() => ''); | ||
}); | ||
|
||
afterEach(() => { | ||
(window.fetch as jest.Mock).mockClear(); | ||
(window.console.warn as jest.Mock).mockClear(); | ||
}); | ||
|
||
it('should throw error if the response status is not 200', async () => { | ||
window.fetch = jest.fn(() => Promise.resolve({ status: 404, headers: emptyHeaders })); | ||
try { | ||
await getApiResponse('testurl'); | ||
fail('Expect an error if the response status is not 200'); | ||
} catch (e) { | ||
expect(e?.code).toBe(404); | ||
} | ||
}); | ||
|
||
it('should throw error if the response ok is false', async () => { | ||
window.fetch = jest.fn(() => | ||
Promise.resolve({ ok: false, status: 500, headers: emptyHeaders }), | ||
); | ||
|
||
try { | ||
await getApiResponse('testurl'); | ||
fail('Expect an error if the response ok is false'); | ||
} catch (e) { | ||
expect(e?.response?.ok).toBe(false); | ||
} | ||
}); | ||
|
||
it('should return a valid response', async () => { | ||
const response = await getApiResponse('testurl'); | ||
expect(response.data).toBeDefined(); | ||
expect(response.data.versions).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('getHubUIPath', () => { | ||
it('should return null if the path is not set', () => { | ||
expect(getHubUIPath('')).toBeNull(); | ||
expect(getHubUIPath(null)).toBeNull(); | ||
expect(getHubUIPath(undefined)).toBeNull(); | ||
}); | ||
|
||
it('should return a value if the path is set', () => { | ||
expect(getHubUIPath('test')).not.toBeNull(); | ||
expect(getHubUIPath('test-path')).toBe(`${TEKTON_HUB_ENDPOINT}/test-path`); | ||
}); | ||
}); |
70 changes: 70 additions & 0 deletions
70
frontend/packages/pipelines-plugin/src/components/catalog/apis/tektonHub.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { coFetch } from '@console/internal/co-fetch'; | ||
import useApiResponse, { ApiResult } from '../hooks/useApiResponse'; | ||
|
||
export type TektonHubItem = { | ||
id: number; | ||
name: string; | ||
}; | ||
export type TektonHubCategory = TektonHubItem; | ||
|
||
export type TektonHubTag = TektonHubItem; | ||
|
||
export type TektonHubPlatform = TektonHubItem; | ||
|
||
export type TektonHubCatalog = TektonHubItem & { | ||
type: string; | ||
}; | ||
|
||
export type TektonHubTaskVersion = { | ||
id: number; | ||
version: string; | ||
hubURLPath: string; | ||
rawURL: string; | ||
webURL: string; | ||
platforms: TektonHubPlatform[]; | ||
}; | ||
|
||
export type TektonHubLatestVersion = TektonHubTaskVersion & { | ||
displayName: string; | ||
description: string; | ||
minPipelinesVersion: string; | ||
updatedAt: string; | ||
}; | ||
|
||
export type TektonHubTask = { | ||
id: number; | ||
name: string; | ||
categories: TektonHubCategory[]; | ||
catalog: TektonHubCatalog; | ||
platforms: TektonHubPlatform[]; | ||
kind: string; | ||
latestVersion: TektonHubLatestVersion; | ||
tags: TektonHubTag[]; | ||
rating: number; | ||
}; | ||
|
||
export const TEKTON_HUB_API_ENDPOINT = 'https://api.hub.tekton.dev/v1'; | ||
export const TEKTON_HUB_ENDPOINT = `https://hub.tekton.dev`; | ||
export const TEKTON_HUB_INTEGRATION_KEY = 'enable-devconsole-integration'; | ||
|
||
export const getHubUIPath = (path: string = ''): string => { | ||
if (!path) { | ||
return null; | ||
} | ||
return path ? `${TEKTON_HUB_ENDPOINT}/${path}` : TEKTON_HUB_ENDPOINT; | ||
}; | ||
|
||
export const getApiResponse = async (url: string) => (await coFetch(url)).json(); | ||
|
||
export const useTektonHubResources = (hasPermission: boolean): ApiResult<TektonHubTask[]> => { | ||
return useApiResponse<TektonHubTask>(`${TEKTON_HUB_API_ENDPOINT}/resources`, hasPermission); | ||
}; | ||
|
||
export const getTektonHubTaskVersions = async ( | ||
resourceId: string, | ||
): Promise<TektonHubTaskVersion[]> => { | ||
const response = await getApiResponse( | ||
`${TEKTON_HUB_API_ENDPOINT}/resource/${resourceId}/versions`, | ||
); | ||
return response?.data?.versions ?? []; | ||
}; |
3 changes: 1 addition & 2 deletions
3
frontend/packages/pipelines-plugin/src/components/catalog/catalog-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
frontend/packages/pipelines-plugin/src/components/catalog/const.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.