Skip to content

Commit

Permalink
Update UI to fetch README and YAML from Hub APIs
Browse files Browse the repository at this point in the history
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 <vinjain@redhat.com>
  • Loading branch information
vinamra28 authored and tekton-robot committed Feb 28, 2022
1 parent 005c458 commit e946915
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
17 changes: 10 additions & 7 deletions ui/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export interface Api {
resourceVersion(resourceId: number): Promise<IVersion>;
versionUpdate(versionId: number): Promise<IVersion>;
authentication(authCode: string): Promise<AuthResponse>;
readme(rawURL: string): Promise<string>;
yaml(rawURL: string): Promise<string>;
readme(resourceKey: string, version: string): Promise<string>;
yaml(resourceKey: string, version: string): Promise<string>;
getRating(resourceId: number, token: string): Promise<Rating>;
setRating(resourceId: number, token: string, rating: number): Promise<void | null>;
getRefreshToken(refreshToken: string): Promise<ITokenInfo>;
Expand Down Expand Up @@ -98,19 +98,22 @@ export class Hub implements Api {
}
}

async readme(rawURL: string) {
async readme(resourceKey: 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/${resourceKey}/${version}/readme`;
return axios.get(URL.toLowerCase()).then((response) => response.data.data.readme);
} catch (err) {
return err.response;
}
}

async yaml(rawURL: string) {
async yaml(resourceKey: string, version: string) {
try {
const newLine = '\n';
return axios.get(`${rawURL}`).then((response) => '```yaml' + newLine + response.data);
const URL = `${API_URL}/${API_VERSION}/resource/${resourceKey}/${version}/yaml`;
return axios
.get(URL.toLowerCase())
.then((response) => '```yaml' + newLine + response.data.data.yaml);
} catch (err) {
return err.response;
}
Expand Down
12 changes: 6 additions & 6 deletions ui/src/api/testutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(resourceKey: string, _: string) {
const splitRawUrl = resourceKey.split('/');
const resourceName = splitRawUrl[splitRawUrl.length - 1];
const data = `${this.dataDir}/${resourceName}-Readme.md`;

const ret = () => fs.readFileSync(data).toString();
Expand All @@ -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(resourceKey: string, _: string) {
const splitRawUrl = resourceKey.split('/');
const resourceName = splitRawUrl[splitRawUrl.length - 1];
const data = `${this.dataDir}/${resourceName}.yaml`;

const ret = () => fs.readFileSync(data).toString();
Expand Down
12 changes: 6 additions & 6 deletions ui/src/store/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit e946915

Please sign in to comment.