fix: add validation for the each values
Signed-off-by: Juan Pablo Garcia Ripa <sarabadu@gmail.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-scaffolder-backend': patch
|
||||
---
|
||||
|
||||
add validation for `each` values
|
||||
@@ -988,6 +988,27 @@ describe('NunjucksWorkflowRunner', () => {
|
||||
);
|
||||
expect(fakeActionHandler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should validate each parameter renders to a valid value', async () => {
|
||||
const task = createMockTaskWithSpec({
|
||||
apiVersion: 'scaffolder.backstage.io/v1beta3',
|
||||
steps: [
|
||||
{
|
||||
id: 'test',
|
||||
name: 'name',
|
||||
each: '${{parameters.data}}',
|
||||
action: 'jest-validated-action',
|
||||
input: { foo: '${{each.value}}' },
|
||||
},
|
||||
],
|
||||
output: {},
|
||||
parameters: {},
|
||||
});
|
||||
await expect(runner.execute(task)).rejects.toThrow(
|
||||
'Invalid each value passed to action jest-validated-action, "${{parameters.data}}" cannot be resolved to a value',
|
||||
);
|
||||
expect(fakeActionHandler).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('secrets', () => {
|
||||
|
||||
@@ -304,13 +304,21 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const resolvedEach =
|
||||
step.each && this.render(step.each, context, renderTemplate);
|
||||
|
||||
if (step.each && !resolvedEach) {
|
||||
throw new InputError(
|
||||
`Invalid each value passed to action ${action.id}, "${step.each}" cannot be resolved to a value`,
|
||||
);
|
||||
}
|
||||
|
||||
const iterations = (
|
||||
step.each
|
||||
? Object.entries(this.render(step.each, context, renderTemplate)).map(
|
||||
([key, value]) => ({
|
||||
each: { key, value },
|
||||
}),
|
||||
)
|
||||
resolvedEach
|
||||
? Object.entries(resolvedEach).map(([key, value]) => ({
|
||||
each: { key, value },
|
||||
}))
|
||||
: [{}]
|
||||
).map(i => ({
|
||||
...i,
|
||||
|
||||
Reference in New Issue
Block a user