diff --git a/.changeset/hungry-llamas-tie.md b/.changeset/hungry-llamas-tie.md new file mode 100644 index 0000000000..0f3be39c3f --- /dev/null +++ b/.changeset/hungry-llamas-tie.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Provide information about the user into scaffolder template action's context diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 44bf45061b..0d5fbcb31f 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -31,6 +31,7 @@ import { TaskSpec } from '@backstage/plugin-scaffolder-common'; import { TaskSpecV1beta3 } from '@backstage/plugin-scaffolder-common'; import { TemplateInfo } from '@backstage/plugin-scaffolder-common'; import { UrlReader } from '@backstage/backend-common'; +import { UserEntity } from '@backstage/catalog-model'; import { Writable } from 'stream'; // @public @@ -44,6 +45,10 @@ export type ActionContext = { createTemporaryDirectory(): Promise; templateInfo?: TemplateInfo; isDryRun?: boolean; + user?: { + entity?: UserEntity; + ref?: string; + }; }; // @public diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/types.ts b/plugins/scaffolder-backend/src/scaffolder/actions/types.ts index ff15300b4a..666f7a0d50 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/types.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/types.ts @@ -18,8 +18,9 @@ import { Logger } from 'winston'; import { Writable } from 'stream'; import { JsonValue, JsonObject } from '@backstage/types'; import { Schema } from 'jsonschema'; -import { TaskSecrets } from '../tasks/types'; +import { TaskSecrets } from '../tasks'; import { TemplateInfo } from '@backstage/plugin-scaffolder-common'; +import { UserEntity } from '@backstage/catalog-model'; /** * ActionContext is passed into scaffolder actions. @@ -45,6 +46,20 @@ export type ActionContext = { * This will only ever be true if the actions as marked as supporting dry-runs. */ isDryRun?: boolean; + + /** + * The user which triggered the action. + */ + user?: { + /** + * The decorated entity from the Catalog + */ + entity?: UserEntity; + /** + * An entity ref for the author of the task + */ + ref?: string; + }; }; /** @public */ diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts index 3627d83aa7..8745b7afdf 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.test.ts @@ -66,7 +66,7 @@ describe('DefaultWorkflowRunner', () => { }); beforeEach(() => { - winston.format.simple(); // put logform the require cache before mocking fs + winston.format.simple(); // put logform in the require.cache before mocking fs mockFs({ '/tmp': mockFs.directory(), ...realFiles, @@ -169,6 +169,21 @@ describe('DefaultWorkflowRunner', () => { it('should pass metadata through', async () => { const entityRef = `template:default/templateName`; + + const userEntity: UserEntity = { + apiVersion: 'backstage.io/v1beta1', + kind: 'User', + metadata: { + name: 'user', + }, + spec: { + profile: { + displayName: 'Bogdan Nechyporenko', + email: 'bnechyporenko@company.com', + }, + }, + }; + const task = createMockTaskWithSpec({ apiVersion: 'scaffolder.backstage.io/v1beta3', parameters: {}, @@ -182,6 +197,9 @@ describe('DefaultWorkflowRunner', () => { }, ], templateInfo: { entityRef }, + user: { + entity: userEntity, + }, }); await runner.execute(task); @@ -189,6 +207,10 @@ describe('DefaultWorkflowRunner', () => { expect(fakeActionHandler.mock.calls[0][0].templateInfo).toEqual({ entityRef, }); + + expect(fakeActionHandler.mock.calls[0][0].user).toEqual({ + entity: userEntity, + }); }); it('should pass token through', async () => { diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts index 97e37fd3eb..6e35f7c69b 100644 --- a/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/NunjucksWorkflowRunner.ts @@ -300,6 +300,7 @@ export class NunjucksWorkflowRunner implements WorkflowRunner { stepOutput[name] = value; }, templateInfo: task.spec.templateInfo, + user: task.spec.user, }); // Remove all temporary directories that were created when executing the action