From ce02d7dca9482c8bc425c261299ad0ee886a0cb1 Mon Sep 17 00:00:00 2001 From: chunweii <47494777+chunweii@users.noreply.github.com> Date: Tue, 10 Oct 2023 22:09:45 +0800 Subject: [PATCH 1/2] Fetch settings json directly without api --- src/app/core/services/github.service.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/core/services/github.service.ts b/src/app/core/services/github.service.ts index 1b9042842..adfc74aa3 100644 --- a/src/app/core/services/github.service.ts +++ b/src/app/core/services/github.service.ts @@ -36,6 +36,9 @@ const { Octokit } = require('@octokit/rest'); const CATCHER_ORG = 'CATcher-org'; const CATCHER_REPO = 'CATcher'; const UNABLE_TO_OPEN_IN_BROWSER = 'Unable to open this issue in Browser'; +function getSettingsUrl(org: string, repoName: string): string { + return `https://raw.githubusercontent.com/${org}/${repoName}/master/settings.json`; +} let ORG_NAME = ''; let MOD_ORG = ''; @@ -398,10 +401,8 @@ export class GithubService { * @return Observable representing session information. */ fetchSettingsFile(): Observable { - return from( - octokit.repos.getContents({ owner: MOD_ORG, repo: DATA_REPO, path: 'settings.json', headers: GithubService.IF_NONE_MATCH_EMPTY }) - ).pipe( - map((rawData) => JSON.parse(atob(rawData['data']['content']))), + return from(fetch(getSettingsUrl(MOD_ORG, DATA_REPO))).pipe( + mergeMap((res) => res.json()), catchError((err) => throwError('Failed to fetch settings file.')) ); } From 636350b54c16553d85fc9c78b559bb28da8435eb Mon Sep 17 00:00:00 2001 From: chunweii <47494777+chunweii@users.noreply.github.com> Date: Tue, 24 Oct 2023 02:02:44 +0800 Subject: [PATCH 2/2] Use rest api first, then raw githubusercontent REST API should be more reliable, but it has a rate limit for unauthenticated requests. Let's use REST API call for the fetching of settings.json, with a failsafe being raw.githubusercontent.com. --- src/app/core/services/github.service.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/app/core/services/github.service.ts b/src/app/core/services/github.service.ts index adfc74aa3..0e954e113 100644 --- a/src/app/core/services/github.service.ts +++ b/src/app/core/services/github.service.ts @@ -396,14 +396,29 @@ export class GithubService { ); } + private fetchSettingsFromRawUrl(): Observable { + return from(fetch(getSettingsUrl(MOD_ORG, DATA_REPO))).pipe( + mergeMap((res) => res.json()), + catchError((err) => throwError('Failed to fetch settings file.')) + ); + } + /** * Fetches the data file that is regulates session information. * @return Observable representing session information. */ fetchSettingsFile(): Observable { - return from(fetch(getSettingsUrl(MOD_ORG, DATA_REPO))).pipe( - mergeMap((res) => res.json()), - catchError((err) => throwError('Failed to fetch settings file.')) + return from( + octokit.repos.getContents({ owner: MOD_ORG, repo: DATA_REPO, path: 'settings.json', headers: GithubService.IF_NONE_MATCH_EMPTY }) + ).pipe( + map((rawData) => JSON.parse(atob(rawData['data']['content']))), + catchError((err) => { + this.logger.error( + 'GithubService: Failed to fetch settings file via REST API. Trying to fetch using raw.githubusercontent.com: ', + err + ); + return this.fetchSettingsFromRawUrl(); + }) ); }