From ef875b07c330ba0163945bd0c8315e58b203e92e Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 30 Aug 2022 15:15:33 +0200 Subject: [PATCH] Add experimental ScaffolderPlugin Signed-off-by: Johan Haals --- .../src/ScaffolderPlugin.ts | 138 ++++++++++++++++++ plugins/scaffolder-backend/src/index.ts | 1 + 2 files changed, 139 insertions(+) create mode 100644 plugins/scaffolder-backend/src/ScaffolderPlugin.ts diff --git a/plugins/scaffolder-backend/src/ScaffolderPlugin.ts b/plugins/scaffolder-backend/src/ScaffolderPlugin.ts new file mode 100644 index 0000000000..5f69484179 --- /dev/null +++ b/plugins/scaffolder-backend/src/ScaffolderPlugin.ts @@ -0,0 +1,138 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + configServiceRef, + createBackendPlugin, + databaseServiceRef, + loggerServiceRef, + loggerToWinstonLogger, + permissionsServiceRef, + urlReaderServiceRef, + httpRouterServiceRef, + createExtensionPoint, +} from '@backstage/backend-plugin-api'; +import { ScmIntegrations } from '@backstage/integration'; +import { catalogServiceRef } from '@backstage/plugin-catalog-node'; +import { TemplateFilter } from './lib'; +import { createBuiltinActions, TaskBroker, TemplateAction } from './scaffolder'; +import { createRouter } from './service/router'; + +/** + * Catalog plugin options + * @alpha + */ +export type ScaffolderPluginOptions = { + actions?: TemplateAction[]; + taskWorkers?: number; + taskBroker?: TaskBroker; + additionalTemplateFilters?: Record; +}; + +/** + * @alpha + * TODO: MOVE to catalog-node before merge. + */ +interface ScaffolderActionsExtensionPoint { + addActions(...actions: TemplateAction[]): void; +} + +class ScaffolderActionsExtensionPointImpl + implements ScaffolderActionsExtensionPoint +{ + #actions = new Array>(); + addActions(...actions: TemplateAction[]): void { + this.#actions.push(...actions); + } + get actions() { + return this.#actions; + } +} + +/** + * @alpha + * TODO: MOVE to catalog-node before merge. + */ +export const scaffolderActionsExtensionPoint = + createExtensionPoint({ + id: 'scaffolder.actions', + }); + +/** + * Catalog plugin + * @alpha + */ +export const scaffolderPlugin = createBackendPlugin({ + id: 'scaffolder', + register(env, options: ScaffolderPluginOptions) { + const actionsExtensions = new ScaffolderActionsExtensionPointImpl(); + env.registerExtensionPoint( + scaffolderActionsExtensionPoint, + actionsExtensions, + ); + + env.registerInit({ + deps: { + logger: loggerServiceRef, + config: configServiceRef, + reader: urlReaderServiceRef, + permissions: permissionsServiceRef, + database: databaseServiceRef, + httpRouter: httpRouterServiceRef, + catalogClient: catalogServiceRef, + }, + async init({ + logger, + config, + reader, + database, + httpRouter, + catalogClient, + }) { + const { additionalTemplateFilters, taskBroker, taskWorkers } = options; + const log = loggerToWinstonLogger(logger); + + const actions = options.actions || [ + ...actionsExtensions.actions, + ...createBuiltinActions({ + integrations: ScmIntegrations.fromConfig(config), + catalogClient, + reader, + config, + additionalTemplateFilters, + }), + ]; + + const actionIds = actions.map(action => action.id).join(', '); + log.info( + `Starting scaffolder with the following actions enabled ${actionIds}`, + ); + + const router = await createRouter({ + logger: log, + config, + database, + catalogClient, + reader, + actions, + taskBroker, + taskWorkers, + additionalTemplateFilters, + }); + httpRouter.use(router); + }, + }); + }, +}); diff --git a/plugins/scaffolder-backend/src/index.ts b/plugins/scaffolder-backend/src/index.ts index a1032449d5..a511aa8776 100644 --- a/plugins/scaffolder-backend/src/index.ts +++ b/plugins/scaffolder-backend/src/index.ts @@ -25,3 +25,4 @@ export * from './service/router'; export * from './lib'; export * from './processor'; export * from './extension'; +export { scaffolderPlugin } from './ScaffolderPlugin';