catalog-node: allow location analyzer to be configured as a factory as well

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-05-03 11:43:07 +02:00
parent f2a2a83b8d
commit 2e20518cd2
3 changed files with 42 additions and 9 deletions
@@ -38,6 +38,7 @@ import {
} from '@backstage/plugin-catalog-node';
import { merge } from 'lodash';
import { Permission } from '@backstage/plugin-permission-common';
import { ForwardedError } from '@backstage/errors';
class CatalogProcessingExtensionPointImpl
implements CatalogProcessingExtensionPoint
@@ -164,14 +165,24 @@ export const catalogPlugin = createBackendPlugin({
processingExtensions,
);
let locationAnalyzer: LocationAnalyzer | undefined = undefined;
let locationAnalyzerFactory:
| ((options: {
scmLocationAnalyzers: ScmLocationAnalyzer[];
}) => Promise<{ locationAnalyzer: LocationAnalyzer }>)
| undefined = undefined;
const scmLocationAnalyzers = new Array<ScmLocationAnalyzer>();
env.registerExtensionPoint(catalogAnalysisExtensionPoint, {
setLocationAnalyzer(analyzer: LocationAnalyzer) {
if (locationAnalyzer) {
setLocationAnalyzer(analyzerOrFactory) {
if (locationAnalyzerFactory) {
throw new Error('LocationAnalyzer has already been set');
}
locationAnalyzer = analyzer;
if (typeof analyzerOrFactory === 'function') {
locationAnalyzerFactory = analyzerOrFactory;
} else {
locationAnalyzerFactory = async () => ({
locationAnalyzer: analyzerOrFactory,
});
}
},
addScmLocationAnalyzer(analyzer: ScmLocationAnalyzer) {
scmLocationAnalyzers.push(analyzer);
@@ -240,7 +251,12 @@ export const catalogPlugin = createBackendPlugin({
Object.entries(processingExtensions.placeholderResolvers).forEach(
([key, resolver]) => builder.setPlaceholderResolver(key, resolver),
);
if (locationAnalyzer) {
if (locationAnalyzerFactory) {
const { locationAnalyzer } = await locationAnalyzerFactory({
scmLocationAnalyzers,
}).catch(e => {
throw new ForwardedError('Failed to create LocationAnalyzer', e);
});
builder.setLocationAnalyzer(locationAnalyzer);
} else {
builder.addLocationAnalyzers(...scmLocationAnalyzers);
+7 -1
View File
@@ -22,7 +22,13 @@ import { Validators } from '@backstage/catalog-model';
// @alpha (undocumented)
export interface CatalogAnalysisExtensionPoint {
addScmLocationAnalyzer(analyzer: ScmLocationAnalyzer): void;
setLocationAnalyzer(analyzer: LocationAnalyzer): void;
setLocationAnalyzer(
analyzerOrFactory:
| LocationAnalyzer
| ((options: { scmLocationAnalyzers: ScmLocationAnalyzer[] }) => Promise<{
locationAnalyzer: LocationAnalyzer;
}>),
): void;
}
// @alpha (undocumented)
+14 -3
View File
@@ -81,10 +81,21 @@ export const catalogProcessingExtensionPoint =
*/
export interface CatalogAnalysisExtensionPoint {
/**
* Replaces the entire location analyzer with a new one. This will cause any
* SCM analyzers added through `addScmLocationAnalyzer` to be ignored.
* Replaces the entire location analyzer with a new one.
*
* @remarks
*
* By providing a factory function you can access all the SCM analyzers that
* have been added through `addScmLocationAnalyzer`. If you provide a
* `LocationAnalyzer` directly, the SCM analyzers will be ignored.
*/
setLocationAnalyzer(analyzer: LocationAnalyzer): void;
setLocationAnalyzer(
analyzerOrFactory:
| LocationAnalyzer
| ((options: {
scmLocationAnalyzers: ScmLocationAnalyzer[];
}) => Promise<{ locationAnalyzer: LocationAnalyzer }>),
): void;
/**
* Adds an analyzer for a specific SCM type to the default location analyzer.