Merge pull request #13588 from acierto/master
Provide information about the user into scaffolder template action's context
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-scaffolder-backend': patch
|
||||
---
|
||||
|
||||
Provide information about the user into scaffolder template action's context
|
||||
@@ -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<Input extends JsonObject> = {
|
||||
createTemporaryDirectory(): Promise<string>;
|
||||
templateInfo?: TemplateInfo;
|
||||
isDryRun?: boolean;
|
||||
user?: {
|
||||
entity?: UserEntity;
|
||||
ref?: string;
|
||||
};
|
||||
};
|
||||
|
||||
// @public
|
||||
|
||||
@@ -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<Input extends JsonObject> = {
|
||||
* 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 */
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user