Add experimental ScaffolderPlugin

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Johan Haals
2022-08-30 15:15:33 +02:00
parent 9c5cab8036
commit ef875b07c3
2 changed files with 139 additions and 0 deletions
@@ -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<any>[];
taskWorkers?: number;
taskBroker?: TaskBroker;
additionalTemplateFilters?: Record<string, TemplateFilter>;
};
/**
* @alpha
* TODO: MOVE to catalog-node before merge.
*/
interface ScaffolderActionsExtensionPoint {
addActions(...actions: TemplateAction<any>[]): void;
}
class ScaffolderActionsExtensionPointImpl
implements ScaffolderActionsExtensionPoint
{
#actions = new Array<TemplateAction<any>>();
addActions(...actions: TemplateAction<any>[]): void {
this.#actions.push(...actions);
}
get actions() {
return this.#actions;
}
}
/**
* @alpha
* TODO: MOVE to catalog-node before merge.
*/
export const scaffolderActionsExtensionPoint =
createExtensionPoint<ScaffolderActionsExtensionPoint>({
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);
},
});
},
});
+1
View File
@@ -25,3 +25,4 @@ export * from './service/router';
export * from './lib';
export * from './processor';
export * from './extension';
export { scaffolderPlugin } from './ScaffolderPlugin';