Merge pull request #27592 from fabiovincenzi/scaffolder-taskID

Add `taskId` to `ActionContext` as well as `NunjuckContext`
This commit is contained in:
Ben Lambert
2024-12-20 08:07:48 +01:00
committed by GitHub
6 changed files with 70 additions and 2 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-scaffolder-backend': minor
'@backstage/plugin-scaffolder-node': patch
---
Added the ability to use `${{ context.task.id }}` in nunjucks templating, as well as `ctx.task.id` in actions to get the current task ID.
@@ -707,6 +707,34 @@ describe('NunjucksWorkflowRunner', () => {
expect(output.foo).toEqual('BACKSTAGE');
});
it('should include task ID in the templated context', async () => {
const task = createMockTaskWithSpec({
apiVersion: 'scaffolder.backstage.io/v1beta3',
steps: [
{
id: 'test',
name: 'name',
action: 'jest-mock-action',
input: {
values: {
taskId: '${{context.task.id}}',
},
},
},
],
output: {},
parameters: {},
});
await runner.execute(task);
expect(fakeActionHandler).toHaveBeenCalledWith(
expect.objectContaining({
input: { values: { taskId: 'test-workspace' } },
}),
);
});
});
describe('redactions', () => {
@@ -80,6 +80,11 @@ type TemplateContext = {
ref?: string;
};
each?: JsonValue;
context: {
task: {
id: string;
};
};
};
type CheckpointState =
@@ -367,6 +372,9 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
await action.handler({
input: iteration.input,
task: {
id: await task.getWorkspaceName(),
},
secrets: task.secrets ?? {},
// TODO(blam): move to LoggerService and away from Winston
logger: loggerToWinstonLogger(taskLogger),
@@ -490,6 +498,11 @@ export class NunjucksWorkflowRunner implements WorkflowRunner {
parameters: task.spec.parameters,
steps: {},
user: task.spec.user,
context: {
task: {
id: taskId,
},
},
};
const [decision]: PolicyDecision[] =
@@ -45,6 +45,9 @@ export const createMockActionContext = <
input: {} as TActionInput,
checkpoint: jest.fn(),
getInitiatorCredentials: () => Promise.resolve(credentials),
task: {
id: 'mock-task-id',
},
};
const createDefaultWorkspace = () => ({
@@ -58,8 +61,15 @@ export const createMockActionContext = <
};
}
const { input, logger, logStream, secrets, templateInfo, workspacePath } =
options;
const {
input,
logger,
logStream,
secrets,
templateInfo,
workspacePath,
task,
} = options;
return {
...defaultContext,
@@ -71,6 +81,7 @@ export const createMockActionContext = <
...(logStream && { logStream }),
...(input && { input }),
...(secrets && { secrets }),
...(task && { task }),
templateInfo,
};
};
+3
View File
@@ -41,6 +41,9 @@ export type ActionContext<
): void;
createTemporaryDirectory(): Promise<string>;
getInitiatorCredentials(): Promise<BackstageCredentials>;
task: {
id: string;
};
templateInfo?: TemplateInfo;
isDryRun?: boolean;
user?: {
@@ -57,6 +57,13 @@ export type ActionContext<
*/
getInitiatorCredentials(): Promise<BackstageCredentials>;
/**
* Task information
*/
task: {
id: string;
};
templateInfo?: TemplateInfo;
/**