chore: simplifying the code a bit by just using lodash under the hood for the pick method
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -7,4 +7,4 @@ Provide some more default filters out of the box and refactoring how the filters
|
||||
- `parseEntityRef` will take an string entity triplet and return a parsed object.
|
||||
- `pick` will allow you to reference a specific property in the piped object.
|
||||
|
||||
So you can now combine things like this: `${{ parameters.entity | parseEntityRef | pick('name') }}` to get the name of a specific entity, or `${{ parameters.repoUrl | parseRepoUrl | pick('owner) }}` to get the owner of a repo.
|
||||
So you can now combine things like this: `${{ parameters.entity | parseEntityRef | pick('name') }}` to get the name of a specific entity, or `${{ parameters.repoUrl | parseRepoUrl | pick('owner') }}` to get the owner of a repo.
|
||||
|
||||
@@ -18,6 +18,7 @@ import { ScmIntegrations } from '@backstage/integration';
|
||||
import { JsonValue } from '@backstage/types';
|
||||
import { TemplateFilter } from '..';
|
||||
import { parseRepoUrl } from '../../scaffolder/actions/builtin/publish/util';
|
||||
import get from 'lodash/get';
|
||||
|
||||
export const createDefaultFilters = ({
|
||||
integrations,
|
||||
@@ -27,19 +28,7 @@ export const createDefaultFilters = ({
|
||||
return {
|
||||
parseRepoUrl: url => parseRepoUrl(url as string, integrations),
|
||||
parseEntityRef: ref => parseEntityRef(ref as string),
|
||||
pick: (obj: JsonValue, key: JsonValue) => {
|
||||
if (
|
||||
typeof obj === 'object' &&
|
||||
!Array.isArray(obj) &&
|
||||
typeof key === 'string'
|
||||
) {
|
||||
return obj?.[key];
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Invalid arguments to pick filter, expected object and string, got ${typeof obj} and ${typeof key}`,
|
||||
);
|
||||
},
|
||||
pick: (obj: JsonValue, key: JsonValue) => get(obj, key as string),
|
||||
projectSlug: repoUrl => {
|
||||
const { owner, repo } = parseRepoUrl(repoUrl as string, integrations);
|
||||
return `${owner}/${repo}`;
|
||||
|
||||
@@ -50,10 +50,7 @@ export function createFetchTemplateAction(options: {
|
||||
additionalTemplateGlobals,
|
||||
} = options;
|
||||
|
||||
const templateFilters = {
|
||||
...createDefaultFilters({ integrations }),
|
||||
...additionalTemplateFilters,
|
||||
};
|
||||
const defaultTemplateFilters = createDefaultFilters({ integrations });
|
||||
|
||||
return createTemplateAction<{
|
||||
url: string;
|
||||
@@ -237,7 +234,10 @@ export function createFetchTemplateAction(options: {
|
||||
|
||||
const renderTemplate = await SecureTemplater.loadRenderer({
|
||||
cookiecutterCompat: ctx.input.cookiecutterCompat,
|
||||
templateFilters,
|
||||
templateFilters: {
|
||||
...defaultTemplateFilters,
|
||||
...additionalTemplateFilters,
|
||||
},
|
||||
templateGlobals: additionalTemplateGlobals,
|
||||
});
|
||||
|
||||
|
||||
@@ -750,6 +750,36 @@ describe('DefaultWorkflowRunner', () => {
|
||||
|
||||
expect(output.foo).toEqual('component');
|
||||
});
|
||||
|
||||
it('should allow deep nesting of picked objects', async () => {
|
||||
const task = createMockTaskWithSpec({
|
||||
apiVersion: 'scaffolder.backstage.io/v1beta3',
|
||||
steps: [
|
||||
{
|
||||
id: 'test',
|
||||
name: 'name',
|
||||
action: 'output-action',
|
||||
input: {},
|
||||
},
|
||||
],
|
||||
output: {
|
||||
foo: '${{ parameters.entity | pick("something.deeply.nested") }}',
|
||||
},
|
||||
parameters: {
|
||||
entity: {
|
||||
something: {
|
||||
deeply: {
|
||||
nested: 'component',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const { output } = await runner.execute(task);
|
||||
|
||||
expect(output.foo).toEqual('component');
|
||||
});
|
||||
});
|
||||
|
||||
describe('dry run', () => {
|
||||
|
||||
Reference in New Issue
Block a user