diff --git a/src/composition.ts b/src/composition.ts index af8645e5..b5432314 100644 --- a/src/composition.ts +++ b/src/composition.ts @@ -15,6 +15,7 @@ import { } from "@/features/projects/data" import { CachingProjectDataSource, + ForgivingProjectDataSource, ProjectRepository, SessionValidatingProjectDataSource } from "@/features/projects/domain" @@ -175,10 +176,13 @@ export const projectRepository = new ProjectRepository( export const projectDataSource = new CachingProjectDataSource( new SessionValidatingProjectDataSource( sessionValidator, - new GitHubProjectDataSource( - userGitHubClient, - GITHUB_ORGANIZATION_NAME - ) + new ForgivingProjectDataSource({ + accessTokenReader: accessTokenService, + projectDataSource: new GitHubProjectDataSource( + userGitHubClient, + GITHUB_ORGANIZATION_NAME + ) + }) ), projectRepository ) diff --git a/src/features/projects/domain/ForgivingProjectDataSource.ts b/src/features/projects/domain/ForgivingProjectDataSource.ts new file mode 100644 index 00000000..82f5c40f --- /dev/null +++ b/src/features/projects/domain/ForgivingProjectDataSource.ts @@ -0,0 +1,33 @@ +import Project from "./Project" +import IProjectDataSource from "./IProjectDataSource" + +interface IAccessTokenReader { + getAccessToken(): Promise +} + +type ForgivingProjectDataSourceConfig = { + readonly accessTokenReader: IAccessTokenReader + readonly projectDataSource: IProjectDataSource +} + +export default class ForgivingProjectDataSource implements IProjectDataSource { + private readonly accessTokenReader: IAccessTokenReader + private readonly projectDataSource: IProjectDataSource + + constructor(config: ForgivingProjectDataSourceConfig) { + this.accessTokenReader = config.accessTokenReader + this.projectDataSource = config.projectDataSource + } + + async getProjects(): Promise { + try { + await this.accessTokenReader.getAccessToken() + } catch { + // If we cannot get an access token, we show an empty list of projects. + // It is common for guest users that we cannot get an access token because they + // have been incorrectly configured to have access to non-existing repositories. + return [] + } + return this.projectDataSource.getProjects() + } +} diff --git a/src/features/projects/domain/index.ts b/src/features/projects/domain/index.ts index c70203d7..f770e7c7 100644 --- a/src/features/projects/domain/index.ts +++ b/src/features/projects/domain/index.ts @@ -1,4 +1,5 @@ export { default as CachingProjectDataSource } from "./CachingProjectDataSource" +export { default as ForgivingProjectDataSource } from "./ForgivingProjectDataSource" export { default as getSelection } from "./getSelection" export type { default as IProjectConfig } from "./IProjectConfig" export type { default as IProjectDataSource } from "./IProjectDataSource"