diff --git a/.changeset/eight-tools-sleep.md b/.changeset/eight-tools-sleep.md new file mode 100644 index 0000000000..9de43e8862 --- /dev/null +++ b/.changeset/eight-tools-sleep.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +This change is for adding the option of inputs on the `github:actions:dispatch` Backstage Action. This will allow users to pass data from Backstage to the GitHub Action. diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.test.ts index b725f825fe..6cdb2ee58c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.test.ts @@ -66,7 +66,7 @@ describe('github:actions:dispatch', () => { }); }); - it('should call the githubApis for creating WorkflowDispatch', async () => { + it('should call the githubApis for creating WorkflowDispatch without an input object', async () => { mockGithubClient.rest.actions.createWorkflowDispatch.mockResolvedValue({ data: { foo: 'bar', @@ -90,4 +90,31 @@ describe('github:actions:dispatch', () => { ref: branchOrTagName, }); }); + + it('should call the githubApis for creating WorkflowDispatch with an input object', async () => { + mockGithubClient.rest.actions.createWorkflowDispatch.mockResolvedValue({ + data: { + foo: 'bar', + }, + }); + + const repoUrl = 'github.com?repo=repo&owner=owner'; + const workflowId = 'dispatch_workflow'; + const branchOrTagName = 'main'; + const workflowInputs = '{ "foo": "bar" }'; + const ctx = Object.assign({}, mockContext, { + input: { repoUrl, workflowId, branchOrTagName, workflowInputs }, + }); + await action.handler(ctx); + + expect( + mockGithubClient.rest.actions.createWorkflowDispatch, + ).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + workflow_id: workflowId, + ref: branchOrTagName, + inputs: workflowInputs, + }); + }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.ts index eed74f2d04..1f7f0f60b9 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/github/githubActionsDispatch.ts @@ -36,6 +36,7 @@ export function createGithubActionsDispatchAction(options: { repoUrl: string; workflowId: string; branchOrTagName: string; + workflowInputs?: { [key: string]: string }; }>({ id: 'github:actions:dispatch', description: @@ -61,11 +62,18 @@ export function createGithubActionsDispatchAction(options: { 'The git branch or tag name used to dispatch the workflow', type: 'string', }, + workflowInputs: { + title: 'Workflow Inputs', + description: + 'Inputs keys and values to send to GitHub Action configured on the workflow file. The maximum number of properties is 10. ', + type: 'object', + }, }, }, }, async handler(ctx) { - const { repoUrl, workflowId, branchOrTagName } = ctx.input; + const { repoUrl, workflowId, branchOrTagName, workflowInputs } = + ctx.input; ctx.logger.info( `Dispatching workflow ${workflowId} for repo ${repoUrl} on ${branchOrTagName}`, @@ -78,6 +86,7 @@ export function createGithubActionsDispatchAction(options: { repo, workflow_id: workflowId, ref: branchOrTagName, + inputs: workflowInputs, }); ctx.logger.info(`Workflow ${workflowId} dispatched successfully`);