From b47da885657ad9a09c1c36d351cfbc2eca0f2ac7 Mon Sep 17 00:00:00 2001 From: vinamra28 Date: Sat, 19 Feb 2022 18:02:19 +0530 Subject: [PATCH] Update UI to fetch README and YAML from Hub APIs As of now Hub UI was fetching the readme and yaml directly from the git provider such Github, Gitlab or Bitbucket. In case of private git repos, UI was incapable of fetching the content directly as it requires authentication. To solve this two endpoints were introduced by Hub API `/readme` and `/yaml` which will do the needful. Updating UI to make API calls to the Hub server instead of git provider directly. Signed-off-by: vinamra28 --- ui/src/api/index.ts | 17 ++++++++++------- ui/src/api/testutil.ts | 12 ++++++------ ui/src/store/resource.ts | 12 ++++++------ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ui/src/api/index.ts b/ui/src/api/index.ts index f7bddbf7dd..0174d1da87 100644 --- a/ui/src/api/index.ts +++ b/ui/src/api/index.ts @@ -32,8 +32,8 @@ export interface Api { resourceVersion(resourceId: number): Promise; versionUpdate(versionId: number): Promise; authentication(authCode: string): Promise; - readme(rawURL: string): Promise; - yaml(rawURL: string): Promise; + readme(name: string, version: string): Promise; + yaml(name: string, version: string): Promise; getRating(resourceId: number, token: string): Promise; setRating(resourceId: number, token: string, rating: number): Promise; getRefreshToken(refreshToken: string): Promise; @@ -98,19 +98,22 @@ export class Hub implements Api { } } - async readme(rawURL: string) { + async readme(name: string, version: string) { try { - const URL = rawURL.substring(0, rawURL.lastIndexOf('/') + 1); - return axios.get(`${URL}/README.md`).then((response) => response.data); + const URL = `${API_URL}/${API_VERSION}/resource/${name}/${version}/readme`; + return axios.get(URL.toLowerCase()).then((response) => response.data.readme); } catch (err) { return err.response; } } - async yaml(rawURL: string) { + async yaml(name: string, version: string) { try { const newLine = '\n'; - return axios.get(`${rawURL}`).then((response) => '```yaml' + newLine + response.data); + const URL = `${API_URL}/${API_VERSION}/resource/${name}/${version}/yaml`; + return axios + .get(URL.toLowerCase()) + .then((response) => '```yaml' + newLine + response.data.yaml); } catch (err) { return err.response; } diff --git a/ui/src/api/testutil.ts b/ui/src/api/testutil.ts index fa2df403be..e5ed3a0309 100644 --- a/ui/src/api/testutil.ts +++ b/ui/src/api/testutil.ts @@ -66,9 +66,9 @@ export class FakeHub implements Api { }); } - async readme(rawURL: string) { - const splitRawUrl = rawURL.split('/'); - const resourceName = splitRawUrl[splitRawUrl.length - 3]; + async readme(name: string, _: string) { + const splitRawUrl = name.split('/'); + const resourceName = splitRawUrl[splitRawUrl.length - 1]; const data = `${this.dataDir}/${resourceName}-Readme.md`; const ret = () => fs.readFileSync(data).toString(); @@ -77,9 +77,9 @@ export class FakeHub implements Api { }); } - async yaml(rawURL: string) { - const splitRawUrl = rawURL.split('/'); - const resourceName = splitRawUrl[splitRawUrl.length - 3]; + async yaml(name: string, _: string) { + const splitRawUrl = name.split('/'); + const resourceName = splitRawUrl[splitRawUrl.length - 1]; const data = `${this.dataDir}/${resourceName}.yaml`; const ret = () => fs.readFileSync(data).toString(); diff --git a/ui/src/store/resource.ts b/ui/src/store/resource.ts index 69db9f7c19..24b624ba73 100644 --- a/ui/src/store/resource.ts +++ b/ui/src/store/resource.ts @@ -349,10 +349,10 @@ export const ResourceStore = types const { api, resources } = self; const resource = resources.get(name); assert(resource); - const url = resource.displayVersion.rawURL; - assert(url); + const version = resource.displayVersion.version; + assert(version); - const readme = yield api.readme(url); + const readme = yield api.readme(name, version); resource.readme = readme; } catch (err) { self.err = err.toString(); @@ -367,10 +367,10 @@ export const ResourceStore = types const { api, resources } = self; const resource = resources.get(name); assert(resource); - const url = resource.displayVersion.rawURL; - assert(url); + const version = resource.displayVersion.version; + assert(version); - const yaml = yield api.yaml(url); + const yaml = yield api.yaml(name, version); resource.yaml = yaml; } catch (err) { self.err = err.toString();