fix(scaffolder): properly set isDryRun flag in dry-run mode

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2022-09-12 15:59:44 -04:00
parent 69242c3021
commit 0ecc9a6784
3 changed files with 81 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Properly set `ctx.isDryRun` when running actions in dry run mode. Also always log action inputs for debugging purposes when running in dry run mode.
@@ -56,9 +56,11 @@ describe('DefaultWorkflowRunner', () => {
const createMockTaskWithSpec = (
spec: TaskSpec,
secrets?: TaskSecrets,
isDryRun?: boolean,
): TaskContext => ({
spec,
secrets,
isDryRun,
complete: async () => {},
done: false,
emitLog: async () => {},
@@ -85,6 +87,7 @@ describe('DefaultWorkflowRunner', () => {
actionRegistry.register({
id: 'jest-validated-action',
description: 'Mock action for testing',
supportsDryRun: true,
handler: fakeActionHandler,
schema: {
input: {
@@ -640,4 +643,32 @@ describe('DefaultWorkflowRunner', () => {
});
});
});
describe('dry run', () => {
it('sets isDryRun flag correctly', async () => {
const task = createMockTaskWithSpec(
{
apiVersion: 'scaffolder.backstage.io/v1beta3',
parameters: {},
output: {},
steps: [
{
id: 'test',
name: 'name',
action: 'jest-validated-action',
input: { foo: 1 },
},
],
},
{
backstageToken: 'secret',
},
true,
);
await runner.execute(task);
expect(fakeActionHandler.mock.calls[0][0].isDryRun).toEqual(true);
});
});
});
@@ -236,25 +236,53 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
const action = this.options.actionRegistry.get(step.action);
const { taskLogger, streamLogger } = createStepLogger({ task, step });
if (task.isDryRun && !action.supportsDryRun) {
task.emitLog(
`Skipping because ${action.id} does not support dry-run`,
{
stepId: step.id,
status: 'skipped',
},
if (task.isDryRun) {
const redactedSecrets = Object.fromEntries(
Object.entries(task.secrets ?? {}).map(secret => [
secret[0],
'[REDACTED]',
]),
);
const outputSchema = action.schema?.output;
if (outputSchema) {
context.steps[step.id] = {
output: generateExampleOutput(outputSchema) as {
[name in string]: JsonValue;
const debugInput =
(step.input &&
this.render(
step.input,
{
...context,
secrets: redactedSecrets,
},
renderTemplate,
)) ??
{};
taskLogger.info(
`Running ${
action.id
} in dry-run mode with inputs (secrets redacted): ${JSON.stringify(
debugInput,
undefined,
2,
)}`,
);
if (!action.supportsDryRun) {
task.emitLog(
`Skipping because ${action.id} does not support dry-run`,
{
stepId: step.id,
status: 'skipped',
},
};
} else {
context.steps[step.id] = { output: {} };
);
const outputSchema = action.schema?.output;
if (outputSchema) {
context.steps[step.id] = {
output: generateExampleOutput(outputSchema) as {
[name in string]: JsonValue;
},
};
} else {
context.steps[step.id] = { output: {} };
}
continue;
}
continue;
}
// Secrets are only passed when templating the input to actions for security reasons
@@ -301,6 +329,7 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
},
templateInfo: task.spec.templateInfo,
user: task.spec.user,
isDryRun: task.isDryRun,
});
// Remove all temporary directories that were created when executing the action