refactor(scaffolder-backend): parseEntityRef filter takes context object arg

Signed-off-by: Enrico Alvarenga <enricomalvarenga@gmail.com>
This commit is contained in:
Enrico Alvarenga
2023-08-28 10:24:43 -07:00
parent b5f239b50b
commit bc799fbaeb
3 changed files with 73 additions and 31 deletions
@@ -15,7 +15,7 @@
*/
import { parseEntityRef } from '@backstage/catalog-model';
import { ScmIntegrations } from '@backstage/integration';
import { JsonValue } from '@backstage/types';
import type { JsonObject, JsonValue } from '@backstage/types';
import { TemplateFilter } from '..';
import { parseRepoUrl } from '../../scaffolder/actions/builtin/publish/util';
import get from 'lodash/get';
@@ -27,15 +27,8 @@ export const createDefaultFilters = ({
}): Record<string, TemplateFilter> => {
return {
parseRepoUrl: url => parseRepoUrl(url as string, integrations),
parseEntityRef: (
ref,
defaultKind?: JsonValue,
defaultNamespace?: JsonValue,
) =>
parseEntityRef(ref as string, {
defaultKind: defaultKind?.toString(),
defaultNamespace: defaultNamespace?.toString(),
}),
parseEntityRef: (ref, context?) =>
parseEntityRef(ref as string, context as JsonObject),
pick: (obj: JsonValue, key: JsonValue) => get(obj, key as string),
projectSlug: repoUrl => {
const { owner, repo } = parseRepoUrl(repoUrl as string, integrations);
@@ -754,7 +754,7 @@ describe('DefaultWorkflowRunner', () => {
},
],
output: {
foo: "${{ parameters.entity | parseEntityRef('user') }}",
foo: `\${{ parameters.entity | parseEntityRef({ defaultKind:"user" }) }}`,
},
parameters: {
entity: 'ben',
@@ -770,6 +770,34 @@ describe('DefaultWorkflowRunner', () => {
});
});
it('provides default 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({ defaultNamespace:"namespace-b" }) }}`,
},
parameters: {
entity: 'user:ben',
},
});
const { output } = await runner.execute(task);
expect(output.foo).toEqual({
kind: 'user',
namespace: 'namespace-b',
name: 'ben',
});
});
it('provides default kind and namespace for parsing entity ref', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
@@ -782,7 +810,7 @@ describe('DefaultWorkflowRunner', () => {
},
],
output: {
foo: "${{ parameters.entity | parseEntityRef('user', 'namespace-b') }}",
foo: `\${{ parameters.entity | parseEntityRef({ defaultKind:"user", defaultNamespace:"namespace-b" }) }}`,
},
parameters: {
entity: 'ben',
@@ -798,8 +826,8 @@ describe('DefaultWorkflowRunner', () => {
});
});
it.each(['undefined', 'null', 'None'])(
'provides default namespace and kind as "%s" value for parsing entity ref',
it.each(['undefined', 'null', 'None', 'group', 0, '{}', '[]'])(
'ignores invalid context "%s" for parsing entity refF',
async kind => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
@@ -812,22 +840,48 @@ describe('DefaultWorkflowRunner', () => {
},
],
output: {
foo: `\${{ parameters.entity | parseEntityRef(${kind}, 'namespace-b') }}`,
foo: `\${{ parameters.entity | parseEntityRef(${kind}) }}`,
},
parameters: {
entity: 'resource:infra-workspace',
entity: 'user:default/ben',
},
});
const { output } = await runner.execute(task);
expect(output.foo).toEqual({
kind: 'resource',
namespace: 'namespace-b',
name: 'infra-workspace',
kind: 'user',
namespace: 'default',
name: 'ben',
});
},
);
it('fails when unable to parse 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({ defaultNamespace:"namespace-b" }) }}`,
},
parameters: {
entity: 'ben',
},
});
const { output } = await runner.execute(task);
expect(output.foo).toEqual(
`\${{ parameters.entity | parseEntityRef({ defaultNamespace:"namespace-b" }) }}`,
);
});
});
it('provides the pick filter', async () => {