feat(scaffolder-backend): allow default kind and namespace args on parseEntityRef filter

Signed-off-by: Enrico Alvarenga <enricomalvarenga@gmail.com>
This commit is contained in:
Enrico Alvarenga
2023-08-24 16:06:11 -07:00
parent 126e862ece
commit 7261199291
2 changed files with 119 additions and 22 deletions
@@ -27,7 +27,15 @@ export const createDefaultFilters = ({
}): Record<string, TemplateFilter> => {
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);
@@ -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 () => {