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
+7 -12
View File
@@ -2,19 +2,14 @@
'@backstage/plugin-scaffolder-backend': minor
---
Improved the `parseEntityRef` Scaffolder filter by introducing the ability for users to provide default kind and namespace values. The filter now takes
3 arguments:
Improved the `parseEntityRef` Scaffolder filter by introducing the ability for users to provide default kind and/or namespace values. The filter now takes
2 arguments, similarly to the original [parseEntityRef](<(https://github.com/backstage/backstage/blob/v1.17.2/packages/catalog-model/src/entity/ref.ts#L77)>):
1. Entity reference
2. (Optional) Default kind
3. (Optional) Default namespace
2. [Context optional object](https://github.com/backstage/backstage/blob/v1.17.2/packages/catalog-model/src/entity/ref.ts#L77)
So you can now provide default `kind` and/or `namespace`. Check out the following examples:
Check out the following examples:
- Without default values: `${{ parameters.entity | parseEntityRef | pick('name') }}`
- With default kind: `${{ parameters.entity | parseEntityRef('group') | pick('name') }}`
- With default kind and namespace: `${{ parameters.entity | parseEntityRef('group', 'another-namespace') | pick('name') }}`
- With default namespace:
- `${{ parameters.entity | parseEntityRef(null, 'another-namespace') | pick('name') }}`
- `${{ parameters.entity | parseEntityRef(undefined, 'another-namespace') | pick('name') }}`
- `${{ parameters.entity | parseEntityRef('', 'another-namespace') | pick('name') }}`
- `${{ parameters.entityRef | parseEntityRef | pick('name') }}`
- `${{ parameters.entityRef | parseEntityRef({ defaultKind:"group", defaultNamespace:"default" }) | pick('name') }}`
- `${{ parameters.entityRef | parseEntityRef({ defaultKind:"group" }) | pick('name') }}`
@@ -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 () => {