From 154b86e1da0bb1eb2f4773f32c4c4eec9c430f97 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 28 Sep 2021 04:45:09 +0200 Subject: [PATCH] feat: think we're at feature parity Signed-off-by: blam --- .../fixtures/test-v1beta3/template.yaml | 47 +++++++++---------- .../tasks/DefaultWorkflowRunner.test.ts | 29 ++++++++++++ .../scaffolder/tasks/DefaultWorkflowRunner.ts | 15 +++++- .../src/scaffolder/tasks/types.ts | 4 +- 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/plugins/scaffolder-backend/fixtures/test-v1beta3/template.yaml b/plugins/scaffolder-backend/fixtures/test-v1beta3/template.yaml index 007e23a3d6..01bd5c2e4f 100644 --- a/plugins/scaffolder-backend/fixtures/test-v1beta3/template.yaml +++ b/plugins/scaffolder-backend/fixtures/test-v1beta3/template.yaml @@ -7,28 +7,27 @@ metadata: spec: type: website parameters: - - name: Enter some stuff - description: Enter some stuff - properties: - inputString: - type: string - title: string input test - inputObject: - type: object - title: object input test - properties: - first: - type: string - title: first - second: - type: number - title: second + - name: Enter some stuff + description: Enter some stuff + properties: + inputString: + type: string + title: string input test + inputObject: + type: object + title: object input test + properties: + first: + type: string + title: first + second: + type: number + title: second steps: - - id: debug - name: Debug - action: debug:log - input: - message: ${{ parameters.inputString }} - extra: ${{ parameters.inputObject }} - - + - id: debug + if: ${{ true === true }} + name: Debug + action: debug:log + input: + message: ${{ parameters.inputString }} + extra: ${{ parameters.inputObject }} diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.test.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.test.ts index 6447782654..d1e2c7e098 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.test.ts @@ -136,6 +136,7 @@ describe('DefaultWorkflowRunner', () => { expect(fakeActionHandler).toHaveBeenCalledTimes(1); }); }); + describe('conditionals', () => { it('should execute steps conditionally', async () => { const task = createMockTaskWithSpec({ @@ -340,4 +341,32 @@ describe('DefaultWorkflowRunner', () => { expect(output.foo).toEqual('BACKSTAGE'); }); }); + + describe('filters', () => { + it('provides the parseRepoUrl filter', async () => { + const task = createMockTaskWithSpec({ + apiVersion: 'backstage.io/v1beta3', + steps: [ + { + id: 'test', + name: 'name', + action: 'output-action', + input: {}, + }, + ], + output: { + foo: '${{parameters.repoUrl | parseRepoUrl}}', + }, + parameters: { + repoUrl: 'github.com?repo=repo&owner=owner', + }, + }); + + const { output } = await runner.execute(task); + + expect(output.foo.host).toEqual('github.com'); + expect(output.foo.owner).toEqual('owner'); + expect(output.foo.repo).toEqual('repo'); + }); + }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.ts index c08363857d..30b855ac30 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/DefaultWorkflowRunner.ts @@ -32,6 +32,7 @@ import { InputError } from '@backstage/errors'; import { PassThrough } from 'stream'; import { isTruthy } from './helper'; import { validate as validateJsonSchema } from 'jsonschema'; +import { parseRepoUrl } from '../actions/builtin/publish/util'; type Options = { workingDirectory: string; @@ -80,8 +81,6 @@ export class DefaultWorkflowRunner implements WorkflowRunner { private readonly nunjucks: nunjucks.Environment; constructor(private readonly options: Options) { - // TODO(blam): Probably need the repo helper here. - // Or we move to returning Objects in the RepoUrlPickerV2 or something? this.nunjucks = nunjucks.configure({ autoescape: false, tags: { @@ -89,6 +88,18 @@ export class DefaultWorkflowRunner implements WorkflowRunner { variableEnd: '}}', }, }); + + // TODO(blam): let's work out how we can deprecate these. + // We shouln't really need to be exposing these now we can deal with + // objects in the params block + this.nunjucks.addFilter('parseRepoUrl', repoUrl => { + return JSON.stringify(parseRepoUrl(repoUrl, this.options.integrations)); + }); + + this.nunjucks.addFilter('projectSlug', repoUrl => { + const { owner, repo } = parseRepoUrl(repoUrl, this.options.integrations); + return `${owner}/${repo}`; + }); } private render(input: T, context: TemplateContext): T { diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts index 2ab38edec9..08baaca7f0 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts @@ -69,7 +69,7 @@ export interface TaskSpecV1beta3 { baseUrl?: string; parameters: JsonObject; steps: TaskStep[]; - output: { [name: string]: string }; + output: { [name: string]: JsonValue }; } export type TaskSpec = TaskSpecV1beta2 | TaskSpecV1beta3; @@ -141,7 +141,7 @@ export interface TaskStore { }: TaskStoreGetEventsOptions): Promise<{ events: DbTaskEventRow[] }>; } -export type WorkflowResponse = { output: { [name: string]: JsonValue } }; +export type WorkflowResponse = { output: { [key: string]: JsonObject } }; export interface WorkflowRunner { execute(task: Task): Promise; }