handle step.if: false

Signed-off-by: Matt Benson <gudnabrsam@gmail.com>
This commit is contained in:
Matt Benson
2024-10-04 15:57:41 -05:00
parent 660cb1c108
commit f1f0076b06
3 changed files with 54 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
handle step.if: false
@@ -407,6 +407,48 @@ describe('NunjucksWorkflowRunner', () => {
expect(output.result).toBeUndefined();
});
describe('should apply boolean step conditions', () => {
it('executes when true', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
steps: [
{
id: 'conditional',
name: 'conditional',
action: 'output-action',
if: true,
},
],
output: {
result: '${{ steps.conditional.output.mock }}',
},
parameters: {},
});
const { output } = await runner.execute(task);
expect(output.result).toBe('backstage');
});
it('skips when false', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
steps: [
{
id: 'conditional',
name: 'conditional',
action: 'output-action',
if: false,
},
],
output: {
result: '${{ steps.conditional.output.mock }}',
},
parameters: {},
});
const { output } = await runner.execute(task);
expect(output.result).toBeUndefined();
});
});
});
describe('templating', () => {
@@ -244,14 +244,14 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
}
try {
if (step.if) {
const ifResult = this.render(step.if, context, renderTemplate);
if (!isTruthy(ifResult)) {
await stepTrack.skipFalsy();
return;
}
if (
step.if === false ||
(typeof step.if === 'string' &&
!isTruthy(this.render(step.if, context, renderTemplate)))
) {
await stepTrack.skipFalsy();
return;
}
const action: TemplateAction<JsonObject> =
this.options.actionRegistry.get(step.action);
const { taskLogger, streamLogger } = createStepLogger({