From df122310a059c1a33d322fc952297c0ce5d72e40 Mon Sep 17 00:00:00 2001 From: secustor Date: Tue, 16 Jan 2024 17:15:40 +0100 Subject: [PATCH 1/4] feat(catalog): allow setting EntityDataParser using CatalogProcessingExtensionPoint Signed-off-by: secustor --- .changeset/polite-zoos-pay.md | 6 ++++++ plugins/catalog-backend/src/service/CatalogPlugin.ts | 9 +++++++++ plugins/catalog-node/api-report-alpha.md | 3 +++ plugins/catalog-node/src/extensions.ts | 2 ++ 4 files changed, 20 insertions(+) create mode 100644 .changeset/polite-zoos-pay.md diff --git a/.changeset/polite-zoos-pay.md b/.changeset/polite-zoos-pay.md new file mode 100644 index 0000000000..1a6a0c69a7 --- /dev/null +++ b/.changeset/polite-zoos-pay.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-catalog-backend': minor +'@backstage/plugin-catalog-node': minor +--- + +Allow setting EntityDataParser using CatalogProcessingExtensionPoint diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index 0800d41340..10ca57d7e1 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -28,11 +28,13 @@ import { } from '@backstage/plugin-catalog-node/alpha'; import { CatalogProcessor, + CatalogProcessorParser, EntityProvider, ScmLocationAnalyzer, } from '@backstage/plugin-catalog-node'; import { loggerToWinstonLogger } from '@backstage/backend-common'; import { PlaceholderResolver } from '../modules'; +import { defaultEntityDataParser } from '../modules/util/parse'; class CatalogProcessingExtensionPointImpl implements CatalogProcessingExtensionPoint @@ -40,6 +42,7 @@ class CatalogProcessingExtensionPointImpl #processors = new Array(); #entityProviders = new Array(); #placeholderResolvers: Record = {}; + entityDataParser: CatalogProcessorParser = defaultEntityDataParser; addProcessor( ...processors: Array> @@ -61,6 +64,10 @@ class CatalogProcessingExtensionPointImpl this.#placeholderResolvers[key] = resolver; } + setEntityDataParser(parser: CatalogProcessorParser): void { + this.entityDataParser = parser; + } + get processors() { return this.#processors; } @@ -164,6 +171,8 @@ export const catalogPlugin = createBackendPlugin({ }); builder.addProcessor(...processingExtensions.processors); builder.addEntityProvider(...processingExtensions.entityProviders); + builder.setEntityDataParser(processingExtensions.entityDataParser); + Object.entries(processingExtensions.placeholderResolvers).forEach( ([key, resolver]) => builder.setPlaceholderResolver(key, resolver), ); diff --git a/plugins/catalog-node/api-report-alpha.md b/plugins/catalog-node/api-report-alpha.md index 74339b1bc9..a231e43885 100644 --- a/plugins/catalog-node/api-report-alpha.md +++ b/plugins/catalog-node/api-report-alpha.md @@ -5,6 +5,7 @@ ```ts import { CatalogApi } from '@backstage/catalog-client'; import { CatalogProcessor } from '@backstage/plugin-catalog-node'; +import { CatalogProcessorParser } from '@backstage/plugin-catalog-node'; import { EntitiesSearchFilter } from '@backstage/plugin-catalog-node'; import { Entity } from '@backstage/catalog-model'; import { EntityProvider } from '@backstage/plugin-catalog-node'; @@ -54,6 +55,8 @@ export interface CatalogProcessingExtensionPoint { addProcessor( ...processors: Array> ): void; + // (undocumented) + setEntityDataParser(parser: CatalogProcessorParser): void; } // @alpha (undocumented) diff --git a/plugins/catalog-node/src/extensions.ts b/plugins/catalog-node/src/extensions.ts index 7aa9264030..e52468bcab 100644 --- a/plugins/catalog-node/src/extensions.ts +++ b/plugins/catalog-node/src/extensions.ts @@ -18,6 +18,7 @@ import { createExtensionPoint } from '@backstage/backend-plugin-api'; import { Entity } from '@backstage/catalog-model'; import { CatalogProcessor, + CatalogProcessorParser, EntitiesSearchFilter, EntityProvider, PlaceholderResolver, @@ -37,6 +38,7 @@ export interface CatalogProcessingExtensionPoint { ...providers: Array> ): void; addPlaceholderResolver(key: string, resolver: PlaceholderResolver): void; + setEntityDataParser(parser: CatalogProcessorParser): void; } /** From 7cfcddd9aaf3828c854799e5cc3635abba2708cc Mon Sep 17 00:00:00 2001 From: secustor Date: Thu, 18 Jan 2024 00:00:44 +0100 Subject: [PATCH 2/4] feat: throw error if extensions point tries to set data parser multiple times Signed-off-by: secustor --- plugins/catalog-backend/src/service/CatalogPlugin.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index 10ca57d7e1..91d3fa0ecc 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -42,7 +42,7 @@ class CatalogProcessingExtensionPointImpl #processors = new Array(); #entityProviders = new Array(); #placeholderResolvers: Record = {}; - entityDataParser: CatalogProcessorParser = defaultEntityDataParser; + entityDataParser?: CatalogProcessorParser; addProcessor( ...processors: Array> @@ -65,6 +65,11 @@ class CatalogProcessingExtensionPointImpl } setEntityDataParser(parser: CatalogProcessorParser): void { + if (this.entityDataParser) { + throw new Error( + 'Attempted to install second EntityDataParser. Only one can be set.', + ); + } this.entityDataParser = parser; } @@ -171,7 +176,9 @@ export const catalogPlugin = createBackendPlugin({ }); builder.addProcessor(...processingExtensions.processors); builder.addEntityProvider(...processingExtensions.entityProviders); - builder.setEntityDataParser(processingExtensions.entityDataParser); + builder.setEntityDataParser( + processingExtensions.entityDataParser ?? defaultEntityDataParser, + ); Object.entries(processingExtensions.placeholderResolvers).forEach( ([key, resolver]) => builder.setPlaceholderResolver(key, resolver), From c66fce65c43428c1944b2a1ac35db5840f6722f2 Mon Sep 17 00:00:00 2001 From: secustor Date: Tue, 23 Jan 2024 16:41:16 +0100 Subject: [PATCH 3/4] refactor(catalog): use private field and rely on default parser provided by build() Signed-off-by: secustor --- .../src/service/CatalogPlugin.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index 91d3fa0ecc..c65dadafae 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -34,7 +34,6 @@ import { } from '@backstage/plugin-catalog-node'; import { loggerToWinstonLogger } from '@backstage/backend-common'; import { PlaceholderResolver } from '../modules'; -import { defaultEntityDataParser } from '../modules/util/parse'; class CatalogProcessingExtensionPointImpl implements CatalogProcessingExtensionPoint @@ -42,7 +41,7 @@ class CatalogProcessingExtensionPointImpl #processors = new Array(); #entityProviders = new Array(); #placeholderResolvers: Record = {}; - entityDataParser?: CatalogProcessorParser; + #entityDataParser?: CatalogProcessorParser; addProcessor( ...processors: Array> @@ -65,12 +64,12 @@ class CatalogProcessingExtensionPointImpl } setEntityDataParser(parser: CatalogProcessorParser): void { - if (this.entityDataParser) { + if (this.#entityDataParser) { throw new Error( 'Attempted to install second EntityDataParser. Only one can be set.', ); } - this.entityDataParser = parser; + this.#entityDataParser = parser; } get processors() { @@ -84,6 +83,10 @@ class CatalogProcessingExtensionPointImpl get placeholderResolvers() { return this.#placeholderResolvers; } + + get entityDataParser() { + return this.#entityDataParser; + } } class CatalogAnalysisExtensionPointImpl @@ -176,9 +179,10 @@ export const catalogPlugin = createBackendPlugin({ }); builder.addProcessor(...processingExtensions.processors); builder.addEntityProvider(...processingExtensions.entityProviders); - builder.setEntityDataParser( - processingExtensions.entityDataParser ?? defaultEntityDataParser, - ); + + if (processingExtensions.entityDataParser) { + builder.setEntityDataParser(processingExtensions.entityDataParser); + } Object.entries(processingExtensions.placeholderResolvers).forEach( ([key, resolver]) => builder.setPlaceholderResolver(key, resolver), From b2f2276ccd091c86c36a26eea63fff9bb2f83e73 Mon Sep 17 00:00:00 2001 From: secustor Date: Mon, 19 Feb 2024 13:16:05 +0100 Subject: [PATCH 4/4] feat: move custom data parser to CatalogModel extension point Signed-off-by: secustor --- .changeset/polite-zoos-pay.md | 2 +- .../src/service/CatalogPlugin.ts | 33 ++++++++++--------- plugins/catalog-node/api-report-alpha.md | 3 +- plugins/catalog-node/src/extensions.ts | 7 +++- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.changeset/polite-zoos-pay.md b/.changeset/polite-zoos-pay.md index 1a6a0c69a7..9596d1356c 100644 --- a/.changeset/polite-zoos-pay.md +++ b/.changeset/polite-zoos-pay.md @@ -3,4 +3,4 @@ '@backstage/plugin-catalog-node': minor --- -Allow setting EntityDataParser using CatalogProcessingExtensionPoint +Allow setting EntityDataParser using CatalogModelExtensionPoint diff --git a/plugins/catalog-backend/src/service/CatalogPlugin.ts b/plugins/catalog-backend/src/service/CatalogPlugin.ts index 7f7d07e6c8..1b4a6fba51 100644 --- a/plugins/catalog-backend/src/service/CatalogPlugin.ts +++ b/plugins/catalog-backend/src/service/CatalogPlugin.ts @@ -49,7 +49,6 @@ class CatalogProcessingExtensionPointImpl unprocessedEntity: Entity; errors: Error[]; }) => Promise | void; - #entityDataParser?: CatalogProcessorParser; addProcessor( ...processors: Array> @@ -80,15 +79,6 @@ class CatalogProcessingExtensionPointImpl this.#onProcessingErrorHandler = handler; } - setEntityDataParser(parser: CatalogProcessorParser): void { - if (this.#entityDataParser) { - throw new Error( - 'Attempted to install second EntityDataParser. Only one can be set.', - ); - } - this.#entityDataParser = parser; - } - get processors() { return this.#processors; } @@ -104,10 +94,6 @@ class CatalogProcessingExtensionPointImpl get onProcessingErrorHandler() { return this.#onProcessingErrorHandler; } - - get entityDataParser() { - return this.#entityDataParser; - } } class CatalogAnalysisExtensionPointImpl @@ -152,6 +138,21 @@ class CatalogModelExtensionPointImpl implements CatalogModelExtensionPoint { get fieldValidators() { return this.#fieldValidators; } + + #entityDataParser?: CatalogProcessorParser; + + setEntityDataParser(parser: CatalogProcessorParser): void { + if (this.#entityDataParser) { + throw new Error( + 'Attempted to install second EntityDataParser. Only one can be set.', + ); + } + this.#entityDataParser = parser; + } + + get entityDataParser() { + return this.#entityDataParser; + } } /** @@ -221,8 +222,8 @@ export const catalogPlugin = createBackendPlugin({ builder.addProcessor(...processingExtensions.processors); builder.addEntityProvider(...processingExtensions.entityProviders); - if (processingExtensions.entityDataParser) { - builder.setEntityDataParser(processingExtensions.entityDataParser); + if (modelExtensions.entityDataParser) { + builder.setEntityDataParser(modelExtensions.entityDataParser); } Object.entries(processingExtensions.placeholderResolvers).forEach( diff --git a/plugins/catalog-node/api-report-alpha.md b/plugins/catalog-node/api-report-alpha.md index d92e1b12c4..17ad6e7c1e 100644 --- a/plugins/catalog-node/api-report-alpha.md +++ b/plugins/catalog-node/api-report-alpha.md @@ -28,6 +28,7 @@ export const catalogAnalysisExtensionPoint: ExtensionPoint): void; } @@ -71,8 +72,6 @@ export interface CatalogProcessingExtensionPoint { errors: Error[]; }) => Promise | void, ): void; - // (undocumented) - setEntityDataParser(parser: CatalogProcessorParser): void; } // @alpha (undocumented) diff --git a/plugins/catalog-node/src/extensions.ts b/plugins/catalog-node/src/extensions.ts index 5890349a95..09a7e1d894 100644 --- a/plugins/catalog-node/src/extensions.ts +++ b/plugins/catalog-node/src/extensions.ts @@ -38,7 +38,6 @@ export interface CatalogProcessingExtensionPoint { ...providers: Array> ): void; addPlaceholderResolver(key: string, resolver: PlaceholderResolver): void; - setEntityDataParser(parser: CatalogProcessorParser): void; setOnProcessingErrorHandler( handler: (event: { unprocessedEntity: Entity; @@ -57,6 +56,12 @@ export interface CatalogModelExtensionPoint { * @param validators - The (subset of) validators to set */ setFieldValidators(validators: Partial): void; + + /** + * Sets the entity data parser which is used to read raw data from locations + * @param parser - Parser which will used to extract entities from raw data + */ + setEntityDataParser(parser: CatalogProcessorParser): void; } /**