From 3b3729f299588162a4cce0f94449b0bac9807d42 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Wed, 14 Jul 2021 13:46:06 +0200 Subject: [PATCH] Throws an error when there is no triplet Signed-off-by: Camila Belo --- packages/techdocs-common/src/stages/publish/awsS3.ts | 8 +++++++- .../src/stages/publish/azureBlobStorage.ts | 9 ++++++++- .../techdocs-common/src/stages/publish/helpers.test.ts | 8 ++++++++ packages/techdocs-common/src/stages/publish/helpers.ts | 8 ++++++++ .../src/stages/publish/migrations/GoogleMigration.ts | 9 ++++++++- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/techdocs-common/src/stages/publish/awsS3.ts b/packages/techdocs-common/src/stages/publish/awsS3.ts index 55e968baac..2942e33380 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.ts @@ -323,7 +323,13 @@ export class AwsS3Publish implements PublisherBase { await Promise.all( allObjects.map(f => limiter(async file => { - const newPath = lowerCaseEntityTripletInStoragePath(file); + let newPath; + try { + newPath = lowerCaseEntityTripletInStoragePath(file); + } catch (e) { + this.logger.warn(e.message); + return; + } // If all parts are already lowercase, ignore. if (file === newPath) { diff --git a/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts b/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts index 4e012a40d5..7ee67a0016 100644 --- a/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts +++ b/packages/techdocs-common/src/stages/publish/azureBlobStorage.ts @@ -313,7 +313,14 @@ export class AzureBlobStoragePublish implements PublisherBase { originalPath: string, removeOriginal: boolean, ) { - const newPath = lowerCaseEntityTripletInStoragePath(originalPath); + let newPath; + try { + newPath = lowerCaseEntityTripletInStoragePath(originalPath); + } catch (e) { + this.logger.warn(e.message); + return; + } + if (originalPath === newPath) return; try { this.logger.debug(`Migrating ${originalPath}`); diff --git a/packages/techdocs-common/src/stages/publish/helpers.test.ts b/packages/techdocs-common/src/stages/publish/helpers.test.ts index a650b59a54..27a56cd06d 100644 --- a/packages/techdocs-common/src/stages/publish/helpers.test.ts +++ b/packages/techdocs-common/src/stages/publish/helpers.test.ts @@ -90,4 +90,12 @@ describe('lowerCaseEntityTripletInStoragePath', () => { const actualPath = lowerCaseEntityTripletInStoragePath(originalPath); expect(actualPath).toBe('default/component/backstage/assets/IMAGE.png'); }); + + it('throws error when there is no triplet', () => { + const originalPath = '/default/component/IMAGE.png'; + const error = `Encountered file unmanaged by TechDocs ${originalPath}. Skipping.`; + expect(() => + lowerCaseEntityTripletInStoragePath(originalPath), + ).toThrowError(error); + }); }); diff --git a/packages/techdocs-common/src/stages/publish/helpers.ts b/packages/techdocs-common/src/stages/publish/helpers.ts index d26a4d71f1..ed681eb8ba 100644 --- a/packages/techdocs-common/src/stages/publish/helpers.ts +++ b/packages/techdocs-common/src/stages/publish/helpers.ts @@ -97,6 +97,14 @@ export const getFileTreeRecursively = async ( export const lowerCaseEntityTripletInStoragePath = ( originalPath: string, ): string => { + const trimmedPath = + originalPath[0] === '/' ? originalPath.substring(1) : originalPath; + const matches = trimmedPath.match(/\//g) || []; + if (matches.length <= 2) { + throw new Error( + `Encountered file unmanaged by TechDocs ${originalPath}. Skipping.`, + ); + } const [namespace, kind, name, ...parts] = originalPath.split('/'); const lowerNamespace = namespace.toLowerCase(); const lowerKind = kind.toLowerCase(); diff --git a/packages/techdocs-common/src/stages/publish/migrations/GoogleMigration.ts b/packages/techdocs-common/src/stages/publish/migrations/GoogleMigration.ts index f1dac0c6e6..cc35b351ea 100644 --- a/packages/techdocs-common/src/stages/publish/migrations/GoogleMigration.ts +++ b/packages/techdocs-common/src/stages/publish/migrations/GoogleMigration.ts @@ -38,7 +38,14 @@ export class MigrateWriteStream extends Writable { _write(file: File, _encoding: BufferEncoding, next: Function) { let shouldCallNext = true; - const newFile = lowerCaseEntityTripletInStoragePath(file.name); + let newFile; + try { + newFile = lowerCaseEntityTripletInStoragePath(file.name); + } catch (e) { + this.logger.warn(e.message); + next(); + return; + } // If all parts are already lowercase, ignore. if (newFile === file.name) {