scaffolder-backend: separate out TemplateActionRegistry and types

Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-02-23 16:22:59 +01:00
committed by Johan Haals
parent b2ce7f18c0
commit 459307b81d
6 changed files with 61 additions and 48 deletions
@@ -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';
@@ -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 = {
@@ -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<string, TemplateAction>();
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;
}
}
@@ -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<void>;
};
export class TemplateActionRegistry {
private readonly actions = new Map<string, TemplateAction>();
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;
}
}
@@ -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<void>;
};
@@ -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 {