Fix variable passing

Signed-off-by: Alex Eftimie <alex.eftimie@getyourguide.com>
This commit is contained in:
Alex Eftimie
2023-08-03 10:34:25 +03:00
parent 5a432881d7
commit 17d626cad4
2 changed files with 72 additions and 5 deletions
@@ -574,7 +574,7 @@ describe('DefaultWorkflowRunner', () => {
});
describe('each', () => {
it('should run a step repeatedly', async () => {
it('should run a step repeatedly - flat values', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
steps: [
@@ -583,7 +583,7 @@ describe('DefaultWorkflowRunner', () => {
name: 'name',
each: '${{parameters.colors}}',
action: 'jest-mock-action',
input: { color: '${{each}}' },
input: { color: '${{each.value}}' },
},
],
output: {},
@@ -604,6 +604,68 @@ describe('DefaultWorkflowRunner', () => {
expect.objectContaining({ input: { color: 'red' } }),
);
});
it('should run a step repeatedly - object list', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
steps: [
{
id: 'test',
name: 'name',
each: '${{parameters.settings}}',
action: 'jest-mock-action',
input: {
key: '${{each.key}}',
value: '${{each.value}}',
},
},
],
output: {},
parameters: {
settings: [{ color: 'blue' }],
},
});
await runner.execute(task);
expect(fakeActionHandler).toHaveBeenCalledWith(
expect.objectContaining({
input: { key: '0', value: { color: 'blue' } },
}),
);
});
it('should run a step repeatedly - object', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
steps: [
{
id: 'test',
name: 'name',
each: '${{parameters.settings}}',
action: 'jest-mock-action',
input: { key: '${{each.key}}', value: '${{each.value}}' },
},
],
output: {},
parameters: {
settings: { color: 'blue', transparent: 'yes' },
},
});
await runner.execute(task);
expect(fakeActionHandler).toHaveBeenCalledWith(
expect.objectContaining({
input: { key: 'color', value: 'blue' },
}),
);
expect(fakeActionHandler).toHaveBeenCalledWith(
expect.objectContaining({
input: { key: 'transparent', value: 'yes' },
}),
);
});
});
describe('secrets', () => {
@@ -315,7 +315,9 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
const iterations = new Array<JsonValue>();
if (step.each) {
const each = await this.render(step.each, context, renderTemplate);
iterations.push(...each);
iterations.push(
...Object.keys(each).map((key: any) => [key, each[key]]),
);
} else {
iterations.push({});
}
@@ -324,13 +326,16 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
for (const iteration of iterations) {
if (step.each) {
taskLogger.info(`Running step each: ${iteration}`);
context.each = iteration;
const iterationContext = {
...context,
each: { key: iteration[0], value: iteration[1] },
};
// re-render input with the modified context that includes each
actionInput =
(step.input &&
this.render(
step.input,
{ ...context, secrets: task.secrets ?? {} },
{ ...iterationContext, secrets: task.secrets ?? {} },
renderTemplate,
)) ??
{};