From 8f26e01f06d707277e8dd28bdf33c2a3ff110e86 Mon Sep 17 00:00:00 2001 From: Anatolii Bazko Date: Mon, 5 Aug 2024 15:33:52 +0200 Subject: [PATCH] Fix remarks Signed-off-by: Anatolii Bazko --- build/dockerfiles/Dockerfile | 2 +- .../__tests__/airGapSamplesApi.spec.ts | 7 ++- .../services/airGapSampleApi.ts | 47 +++++++++---------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/build/dockerfiles/Dockerfile b/build/dockerfiles/Dockerfile index a596ed506..290e94177 100644 --- a/build/dockerfiles/Dockerfile +++ b/build/dockerfiles/Dockerfile @@ -42,7 +42,7 @@ RUN yarn build RUN yarn workspace @eclipse-che/dashboard-backend install --production # Prepare air-gapped resources -ARG GITHUB_TOKEN=$GITHUB_TOKEN +# ARG GITHUB_TOKEN=$GITHUB_TOKEN COPY build/dockerfiles/airgap.sh /dashboard/airgap.sh RUN /dashboard/airgap.sh -d /dashboard/packages/devfile-registry/air-gap diff --git a/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/airGapSamplesApi.spec.ts b/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/airGapSamplesApi.spec.ts index d14b2dc50..94cd7262f 100644 --- a/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/airGapSamplesApi.spec.ts +++ b/packages/dashboard-backend/src/devworkspaceClient/services/__tests__/airGapSamplesApi.spec.ts @@ -19,10 +19,9 @@ describe('Getting Started Samples API Service', () => { beforeEach(() => { jest.resetModules(); - airGapSampleApiService = new AirGapSampleApiService(); - airGapSampleApiService.getAirGapResourcesDir = jest - .fn() - .mockReturnValue(path.join(__dirname, 'fixtures', 'air-gap')); + airGapSampleApiService = new AirGapSampleApiService( + path.join(__dirname, 'fixtures', 'air-gap'), + ); }); afterEach(() => { diff --git a/packages/dashboard-backend/src/devworkspaceClient/services/airGapSampleApi.ts b/packages/dashboard-backend/src/devworkspaceClient/services/airGapSampleApi.ts index 8f0d7bf94..74dc72c97 100644 --- a/packages/dashboard-backend/src/devworkspaceClient/services/airGapSampleApi.ts +++ b/packages/dashboard-backend/src/devworkspaceClient/services/airGapSampleApi.ts @@ -18,16 +18,29 @@ import path from 'path'; import { IAirGapSampleApi } from '@/devworkspaceClient/types'; import { isLocalRun } from '@/localRun'; -// See build/dockerfiles/Dockerfile for the path -const airGapResourcesDir = '/public/dashboard/devfile-registry/air-gap'; - export class AirGapSampleApiService implements IAirGapSampleApi { + protected readonly airGapResourcesDir: string; + protected readonly airGapIndexFilePath: string; + protected readonly samples: Array; + constructor(airGapResourcesDir?: string) { + this.airGapResourcesDir = airGapResourcesDir + ? airGapResourcesDir + : isLocalRun() + ? path.join( + __dirname, + '../../../dashboard-frontend/lib/public/dashboard/devfile-registry/air-gap', + ) + : '/public/dashboard/devfile-registry/air-gap'; + this.airGapIndexFilePath = path.join(this.airGapResourcesDir, 'index.json'); + this.samples = this.readAirGapIndex(); + } + async list(): Promise> { - return this.readAirGapIndex(); + return this.samples; } async downloadProject(name: string): Promise { - const sample = this.readAirGapIndex().find(sample => sample.displayName === name); + const sample = this.samples.find(sample => sample.displayName === name); if (sample) { return this.download(sample.project?.zip?.filename); } @@ -37,7 +50,7 @@ export class AirGapSampleApiService implements IAirGapSampleApi { } async downloadDevfile(name: string): Promise { - const sample = this.readAirGapIndex().find(sample => sample.displayName === name); + const sample = this.samples.find(sample => sample.displayName === name); if (sample) { return this.download(sample.devfile?.filename); } @@ -46,27 +59,13 @@ export class AirGapSampleApiService implements IAirGapSampleApi { throw new Error(`Sample not found`); } - getAirGapResourcesDir(): string { - return isLocalRun() - ? path.join( - __dirname, - '../../../dashboard-frontend/lib/public/dashboard/devfile-registry/air-gap', - ) - : airGapResourcesDir; - } - - private getAirGapIndexFilePath(): string { - return path.join(this.getAirGapResourcesDir(), 'index.json'); - } - private readAirGapIndex(): Array { - const airGapIndexFilePath = this.getAirGapIndexFilePath(); - if (!fs.existsSync(airGapIndexFilePath)) { + if (!fs.existsSync(this.airGapIndexFilePath)) { return []; } try { - const data = fs.readFileSync(airGapIndexFilePath, 'utf8'); + const data = fs.readFileSync(this.airGapIndexFilePath, 'utf8'); return JSON.parse(data) as api.IAirGapSample[]; } catch (e) { console.error(e, 'Failed to read air-gap index.json'); @@ -80,10 +79,10 @@ export class AirGapSampleApiService implements IAirGapSampleApi { throw new Error(`filename not defined`); } - const filepath = path.resolve(this.getAirGapResourcesDir(), filename); + const filepath = path.resolve(this.airGapResourcesDir, filename); // This is a security check to ensure that the file is within the airGapResourcesDir - if (!filepath.startsWith(this.getAirGapResourcesDir())) { + if (!filepath.startsWith(this.airGapResourcesDir)) { console.error(`Invalid file path: ${filepath}`); throw new Error(`Invalid file path`); }