scaffolder-backend: move catalog:register action from legacy to builtin

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-02-24 15:22:02 +01:00
committed by Johan Haals
parent 0d1468a095
commit a6591ef03f
3 changed files with 28 additions and 28 deletions
@@ -19,28 +19,19 @@ import { FilePreparer, PreparerBuilder } from './prepare';
import Docker from 'dockerode';
import { TemplaterBuilder, TemplaterValues } from './templater';
import { PublisherBuilder } from './publish';
import { CatalogApi } from '@backstage/catalog-client';
import { getEntityName } from '@backstage/catalog-model';
type Options = {
dockerClient: Docker;
preparers: PreparerBuilder;
templaters: TemplaterBuilder;
publishers: PublisherBuilder;
catalogClient: CatalogApi;
};
export function registerLegacyActions(
registry: TemplateActionRegistry,
options: Options,
) {
const {
dockerClient,
preparers,
templaters,
publishers,
catalogClient,
} = options;
const { dockerClient, preparers, templaters, publishers } = options;
registry.register({
id: 'legacy:prepare',
@@ -113,21 +104,4 @@ export function registerLegacyActions(
}
},
});
registry.register({
id: 'catalog:register',
async handler(ctx) {
const { catalogInfoUrl } = ctx.parameters;
ctx.logger.info(`Registering ${catalogInfoUrl} in the catalog`);
const result = await catalogClient.addLocation({
type: 'url',
target: catalogInfoUrl as string,
});
if (result.entities.length >= 1) {
const { kind, name, namespace } = getEntityName(result.entities[0]);
ctx.output('entityRef', `${kind}:${namespace}/${name}`);
}
},
});
}
@@ -22,6 +22,8 @@ import { TemplateAction } from './types';
import { InputError, UrlReader } from '@backstage/backend-common';
import { ScmIntegrations } from '@backstage/integration';
import { JsonValue } from '@backstage/config';
import { CatalogApi } from '@backstage/catalog-client';
import { getEntityName } from '@backstage/catalog-model';
async function fetchContents({
urlReader,
@@ -174,3 +176,26 @@ export function createFetchCookiecutterAction(options: {
},
};
}
export function createCatalogRegisterAction(options: {
catalogClient: CatalogApi;
}): TemplateAction {
const { catalogClient } = options;
return {
id: 'catalog:register',
async handler(ctx) {
const { catalogInfoUrl } = ctx.parameters;
ctx.logger.info(`Registering ${catalogInfoUrl} in the catalog`);
const result = await catalogClient.addLocation({
type: 'url',
target: catalogInfoUrl as string,
});
if (result.entities.length >= 1) {
const { kind, name, namespace } = getEntityName(result.entities[0]);
ctx.output('entityRef', `${kind}:${namespace}/${name}`);
}
},
};
}
@@ -57,6 +57,7 @@ import {
import {
createFetchPlainAction,
createFetchCookiecutterAction,
createCatalogRegisterAction,
} from '../scaffolder/tasks/builtin';
import { ScmIntegrations } from '@backstage/integration';
@@ -129,7 +130,6 @@ export async function createRouter(
preparers,
publishers,
templaters,
catalogClient,
});
actionRegistry.register(
createFetchPlainAction({
@@ -145,6 +145,7 @@ export async function createRouter(
templaters,
}),
);
actionRegistry.register(createCatalogRegisterAction({ catalogClient }));
worker.start();