From 19b80da2132c5aeb10c780c2166288b217477249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=B8vring?= Date: Wed, 8 Nov 2023 09:40:25 +0100 Subject: [PATCH] Revert "Removes forgiving data source" This reverts commit 46af18632ba4c2a03554d67aa80d39dc376e3ca3. --- src/composition.ts | 12 ++++--- .../domain/ForgivingProjectDataSource.ts | 33 +++++++++++++++++++ src/features/projects/domain/index.ts | 1 + 3 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 src/features/projects/domain/ForgivingProjectDataSource.ts 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"