From 459307b81d690c76f4ccfaa86d54bb6db59ecbd7 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 23 Feb 2021 16:22:59 +0100 Subject: [PATCH] scaffolder-backend: separate out TemplateActionRegistry and types Signed-off-by: Johan Haals --- .../src/scaffolder/stages/legacy.ts | 2 +- .../src/scaffolder/tasks/TaskWorker.ts | 2 +- .../tasks/TemplateActionRegistry.ts | 41 ++++++++++++++++++ .../src/scaffolder/tasks/TemplateConverter.ts | 42 ------------------- .../src/scaffolder/tasks/types.ts | 16 +++++++ .../scaffolder-backend/src/service/router.ts | 6 +-- 6 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 plugins/scaffolder-backend/src/scaffolder/tasks/TemplateActionRegistry.ts diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/legacy.ts b/plugins/scaffolder-backend/src/scaffolder/stages/legacy.ts index efba57a063..271618b981 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/legacy.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/legacy.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { TemplateActionRegistry } from '../tasks/TemplateConverter'; +import { TemplateActionRegistry } from '../tasks/TemplateActionRegistry'; import { FilePreparer, PreparerBuilder } from './prepare'; import Docker from 'dockerode'; import { TemplaterBuilder, TemplaterValues } from './templater'; diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts index 9d0c0863aa..af8f85f371 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/TaskWorker.ts @@ -21,7 +21,7 @@ import { JsonValue, JsonObject } from '@backstage/config'; import { TaskBroker, Task } from './types'; import fs from 'fs-extra'; import path from 'path'; -import { TemplateActionRegistry } from './TemplateConverter'; +import { TemplateActionRegistry } from './TemplateActionRegistry'; import * as handlebars from 'handlebars'; type Options = { diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateActionRegistry.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateActionRegistry.ts new file mode 100644 index 0000000000..9d6140fbfe --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateActionRegistry.ts @@ -0,0 +1,41 @@ +/* + * Copyright 2021 Spotify AB + * + * 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 { TemplateAction } from './types'; +import { ConflictError, NotFoundError } from '@backstage/backend-common'; + +export class TemplateActionRegistry { + private readonly actions = new Map(); + + register(action: TemplateAction) { + if (this.actions.has(action.id)) { + throw new ConflictError( + `Template action with ID '${action.id}' has already been registered`, + ); + } + this.actions.set(action.id, action); + } + + get(actionId: string): TemplateAction { + const action = this.actions.get(actionId); + if (!action) { + throw new NotFoundError( + `Template action with ID '${actionId}' is not registered.`, + ); + } + return action; + } +} diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateConverter.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateConverter.ts index 7f76ac33d0..7d824a7d53 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateConverter.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/TemplateConverter.ts @@ -15,13 +15,8 @@ */ import { resolve as resolvePath, dirname } from 'path'; -import { JsonValue } from '@backstage/config'; import { TemplateEntityV1alpha1 } from '@backstage/catalog-model'; -import { Logger } from 'winston'; -import { Writable } from 'stream'; - import { TaskSpec } from './types'; -import { ConflictError, NotFoundError } from '@backstage/backend-common'; import { getTemplaterKey, joinGitUrlPath, @@ -95,40 +90,3 @@ export function templateEntityToSpec( }, }; } - -type ActionContext = { - logger: Logger; - logStream: Writable; - - workspacePath: string; - parameters: { [name: string]: JsonValue }; - output(name: string, value: JsonValue): void; -}; - -type TemplateAction = { - id: string; - handler: (ctx: ActionContext) => Promise; -}; - -export class TemplateActionRegistry { - private readonly actions = new Map(); - - register(action: TemplateAction) { - if (this.actions.has(action.id)) { - throw new ConflictError( - `Template action with ID '${action.id}' has already been registered`, - ); - } - this.actions.set(action.id, action); - } - - get(actionId: string): TemplateAction { - const action = this.actions.get(actionId); - if (!action) { - throw new NotFoundError( - `Template action with ID '${actionId}' is not registered.`, - ); - } - return action; - } -} diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts index 27a1b62d04..6e0ecd8600 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts @@ -15,6 +15,8 @@ */ import { JsonValue, JsonObject } from '@backstage/config'; +import { Logger } from 'winston'; +import { Writable } from 'stream'; export type Status = | 'open' @@ -112,3 +114,17 @@ export interface TaskStore { after, }: TaskStoreGetEventsOptions): Promise<{ events: DbTaskEventRow[] }>; } + +export type ActionContext = { + logger: Logger; + logStream: Writable; + + workspacePath: string; + parameters: { [name: string]: JsonValue }; + output(name: string, value: JsonValue): void; +}; + +export type TemplateAction = { + id: string; + handler: (ctx: ActionContext) => Promise; +}; diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index 65b9039e27..cc483f3bd0 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -38,10 +38,8 @@ import { StorageTaskBroker, TaskWorker, } from '../scaffolder/tasks'; -import { - TemplateActionRegistry, - templateEntityToSpec, -} from '../scaffolder/tasks/TemplateConverter'; +import { templateEntityToSpec } from '../scaffolder/tasks/TemplateConverter'; +import { TemplateActionRegistry } from '../scaffolder/tasks/TemplateActionRegistry'; import { registerLegacyActions } from '../scaffolder/stages/legacy'; import { getWorkingDirectory } from './helpers'; import {