From 17d626cad4d6191bc15138c51f87541501847471 Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Thu, 3 Aug 2023 10:34:25 +0300 Subject: [PATCH] Fix variable passing Signed-off-by: Alex Eftimie --- .../tasks/NunjucksWorkflowRunner.test.ts | 66 ++++++++++++++++++- .../tasks/NunjucksWorkflowRunner.ts | 11 +++- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts index 8fbbd45b94..253a6c4388 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts @@ -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', () => { diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts index a6c486c58d..b0b8fd6d09 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts @@ -315,7 +315,9 @@ export class NunjucksWorkflowRunner implements WorkflowRunner { const iterations = new Array(); 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, )) ?? {};