Merge pull request #28441 from Sarabadu/fix-scaffolder-each-validation
fix: add validation for the `each` values
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-scaffolder-backend': patch
|
||||
---
|
||||
|
||||
Fixed an issue where invalid expressions or non-object values in `step.each` caused an error.
|
||||
@@ -991,6 +991,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 value on action jest-validated-action.each parameter, "${{parameters.data}}" cannot be resolved to a value',
|
||||
);
|
||||
expect(fakeActionHandler).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('secrets', () => {
|
||||
|
||||
@@ -310,13 +310,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 value on action ${action.id}.each parameter, "${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