From 251b21fe6c776564fb3cc4f273c7dea4b838d34d Mon Sep 17 00:00:00 2001 From: pmckl Date: Tue, 15 Apr 2025 23:45:42 +0200 Subject: [PATCH] add example with test Signed-off-by: pmckl --- .../githubPullRequest.examples.test.ts | 53 +++++++++++++++++++ .../src/actions/githubPullRequest.examples.ts | 18 +++++++ 2 files changed, 71 insertions(+) diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.test.ts index fb2763db51..bbbbed6511 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.test.ts @@ -32,6 +32,9 @@ const mockOctokit = { pulls: { requestReviewers: jest.fn(), }, + issues: { + addAssignees: jest.fn(), + }, }, }; @@ -61,6 +64,7 @@ describe('publish:github:pull-request examples', () => { createPullRequest: jest.Mock; rest: { pulls: { requestReviewers: jest.Mock }; + issues: { addAssignees: jest.Mock }; }; }; const mockDir = createMockDirectory(); @@ -90,6 +94,9 @@ describe('publish:github:pull-request examples', () => { pulls: { requestReviewers: jest.fn(async (_: any) => ({ data: {} })), }, + issues: { + addAssignees: jest.fn(async (_: any) => ({ data: {} })), + }, }, }; @@ -716,4 +723,50 @@ describe('publish:github:pull-request examples', () => { ); expect(mockContext.output).toHaveBeenCalledWith('pullRequestNumber', 123); }); + + it('Create a pull request with assignees', async () => { + const input = yaml.parse(examples[14].example).steps[0].input; + + await action.handler({ + ...mockContext, + workspacePath, + input, + }); + + expect(fakeClient.createPullRequest).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + title: 'Create my new app', + body: 'This PR is really good', + head: 'new-app', + draft: undefined, + changes: [ + { + commit: 'Create my new app', + files: { + 'file.txt': { + content: Buffer.from('Hello there!').toString('base64'), + encoding: 'base64', + mode: '100644', + }, + }, + }, + ], + }); + + expect(fakeClient.rest.issues.addAssignees).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + issue_number: 123, + assignees: ['foobar'], + }); + + expect(mockContext.output).toHaveBeenCalledTimes(3); + expect(mockContext.output).toHaveBeenCalledWith('targetBranchName', 'main'); + expect(mockContext.output).toHaveBeenCalledWith( + 'remoteUrl', + 'https://github.com/myorg/myrepo/pull/123', + ); + expect(mockContext.output).toHaveBeenCalledWith('pullRequestNumber', 123); + }); }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.ts index e35e6d9a81..8fd5b7543c 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.ts @@ -283,4 +283,22 @@ export const examples: TemplateExample[] = [ ], }), }, + { + description: 'Create a pull request with assignees', + example: yaml.stringify({ + steps: [ + { + action: 'publish:github:pull-request', + name: 'Create a pull request with reviewers', + input: { + repoUrl: 'github.com?repo=repo&owner=owner', + branchName: 'new-app', + title: 'Create my new app', + description: 'This PR is really good', + assignees: ['foobar'], + }, + }, + ], + }), + }, ];