Merge pull request #26982 from mbenson/scaffolder_step_if_false

handle step.if: false
This commit is contained in:
Ben Lambert
2024-10-08 08:49:09 +02:00
committed by GitHub
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({