diff --git a/plugins/scaffolder-backend/src/lib/templating/filters.ts b/plugins/scaffolder-backend/src/lib/templating/filters.ts index 204d0015d8..0400adf159 100644 --- a/plugins/scaffolder-backend/src/lib/templating/filters.ts +++ b/plugins/scaffolder-backend/src/lib/templating/filters.ts @@ -27,7 +27,15 @@ export const createDefaultFilters = ({ }): Record => { return { parseRepoUrl: url => parseRepoUrl(url as string, integrations), - parseEntityRef: ref => parseEntityRef(ref as string), + parseEntityRef: ( + ref, + defaultKind?: JsonValue, + defaultNamespace?: JsonValue, + ) => + parseEntityRef(ref as string, { + defaultKind: defaultKind?.toString(), + defaultNamespace: defaultNamespace?.toString(), + }), pick: (obj: JsonValue, key: JsonValue) => get(obj, key as string), projectSlug: repoUrl => { const { owner, repo } = parseRepoUrl(repoUrl as string, integrations); diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts index 5d4f7b73cc..bf40357cfe 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts @@ -713,32 +713,121 @@ describe('DefaultWorkflowRunner', () => { }); }); - it('provides the parseEntityRef filter', async () => { - const task = createMockTaskWithSpec({ - apiVersion: 'scaffolder.backstage.io/v1beta3', - steps: [ - { - id: 'test', - name: 'name', - action: 'output-action', - input: {}, + describe('parseEntityRef', () => { + it('parses entity ref', async () => { + const task = createMockTaskWithSpec({ + apiVersion: 'scaffolder.backstage.io/v1beta3', + steps: [ + { + id: 'test', + name: 'name', + action: 'output-action', + input: {}, + }, + ], + output: { + foo: '${{ parameters.entity | parseEntityRef }}', }, - ], - output: { - foo: '${{ parameters.entity | parseEntityRef }}', - }, - parameters: { - entity: 'component:default/ben', - }, + parameters: { + entity: 'component:default/ben', + }, + }); + + const { output } = await runner.execute(task); + + expect(output.foo).toEqual({ + kind: 'component', + namespace: 'default', + name: 'ben', + }); }); - const { output } = await runner.execute(task); + it('provides default kind for parsing entity ref', async () => { + const task = createMockTaskWithSpec({ + apiVersion: 'scaffolder.backstage.io/v1beta3', + steps: [ + { + id: 'test', + name: 'name', + action: 'output-action', + input: {}, + }, + ], + output: { + foo: "${{ parameters.entity | parseEntityRef('user') }}", + }, + parameters: { + entity: 'ben', + }, + }); - expect(output.foo).toEqual({ - kind: 'component', - namespace: 'default', - name: 'ben', + const { output } = await runner.execute(task); + + expect(output.foo).toEqual({ + kind: 'user', + namespace: 'default', + name: 'ben', + }); }); + + it('provides default kind and namespace for parsing entity ref', async () => { + const task = createMockTaskWithSpec({ + apiVersion: 'scaffolder.backstage.io/v1beta3', + steps: [ + { + id: 'test', + name: 'name', + action: 'output-action', + input: {}, + }, + ], + output: { + foo: "${{ parameters.entity | parseEntityRef('user', 'namespace-b') }}", + }, + parameters: { + entity: 'ben', + }, + }); + + const { output } = await runner.execute(task); + + expect(output.foo).toEqual({ + kind: 'user', + namespace: 'namespace-b', + name: 'ben', + }); + }); + + it.each(['undefined', 'null', 'None'])( + 'provides default namespace and kind as "%s" value for parsing entity ref', + async kind => { + const task = createMockTaskWithSpec({ + apiVersion: 'scaffolder.backstage.io/v1beta3', + steps: [ + { + id: 'test', + name: 'name', + action: 'output-action', + input: {}, + }, + ], + output: { + foo: `\${{ parameters.entity | parseEntityRef(${kind}, 'namespace-b') }}`, + }, + parameters: { + entity: 'resource:infra-workspace', + }, + }); + + const { output } = await runner.execute(task); + + expect(output.foo).toEqual({ + kind: 'resource', + namespace: 'namespace-b', + name: 'infra-workspace', + }); + }, + ); }); it('provides the pick filter', async () => {