diff --git a/.changeset/nasty-papayas-heal.md b/.changeset/nasty-papayas-heal.md new file mode 100644 index 0000000000..cfeacb3238 --- /dev/null +++ b/.changeset/nasty-papayas-heal.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-github': minor +--- + +Adding support to change the default commit author for pull-request github action" 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 dd07c88a70..e881ae3192 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 @@ -134,6 +134,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -174,6 +178,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -213,6 +221,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -252,6 +264,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -298,6 +314,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -338,6 +358,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -378,6 +402,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -424,6 +452,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -470,6 +502,54 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, + }, + ], + }); + + expect(fakeClient.rest.pulls.requestReviewers).not.toHaveBeenCalled(); + 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); + }); + + it('Create a pull request with a git author', async () => { + const input = yaml.parse(examples[9].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', + }, + }, + author: { + email: 'foo@bar.example', + name: 'Foo Bar', + }, }, ], }); @@ -491,7 +571,7 @@ describe('publish:github:pull-request examples', () => { irrelevant: { 'bar.txt': 'Nothing to see here' }, }, }); - const input = yaml.parse(examples[9].example).steps[0].input; + const input = yaml.parse(examples[10].example).steps[0].input; await action.handler({ ...mockContext, @@ -517,6 +597,10 @@ describe('publish:github:pull-request examples', () => { mode: '100644', }, }, + author: { + email: 'foo@bar.example', + name: 'Foo Bar', + }, }, ], }); 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 d6138b262b..f3042a7e15 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.examples.ts @@ -178,6 +178,25 @@ export const examples: TemplateExample[] = [ ], }), }, + { + description: 'Create a pull request with a git author', + example: yaml.stringify({ + steps: [ + { + action: 'publish:github:pull-request', + name: 'Create a pull reuqest', + input: { + repoUrl: 'github.com?repo=repo&owner=owner', + branchName: 'new-app', + title: 'Create my new app', + description: 'This PR is really good', + gitAuthorName: 'Foo Bar', + gitAuthorEmail: 'foo@bar.example', + }, + }, + ], + }), + }, { description: 'Create a pull request with all parameters', example: yaml.stringify({ @@ -198,6 +217,8 @@ export const examples: TemplateExample[] = [ reviewers: ['foobar'], teamReviewers: ['team-foo'], commitMessage: 'Commit for foo changes', + gitAuthorName: 'Foo Bar', + gitAuthorEmail: 'foo@bar.example', }, }, ], diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts index c9d1498187..a38b070bdf 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.test.ts @@ -155,6 +155,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -212,6 +216,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -271,6 +279,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -322,6 +334,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -449,6 +465,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '120000', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -498,6 +518,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100755', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -557,6 +581,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100755', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -612,6 +640,10 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], }); @@ -657,10 +689,64 @@ describe('createPublishGithubPullRequestAction', () => { mode: '100644', }, }, + author: { + email: 'scaffolder@backstage.io', + name: 'Scaffolder', + }, }, ], forceFork: true, }); }); }); + + describe('with author', () => { + let input: GithubPullRequestActionInput; + let ctx: ActionContext; + + beforeEach(() => { + input = { + repoUrl: 'github.com?owner=myorg&repo=myrepo', + title: 'Create my new app', + branchName: 'new-app', + description: 'This PR is really good', + gitAuthorEmail: 'foo@bar.example', + gitAuthorName: 'Foo Bar', + }; + + mockDir.setContent({ + [workspacePath]: { 'file.txt': 'Hello there!' }, + }); + + ctx = createMockActionContext({ input, workspacePath }); + }); + + it('creates a pull request', async () => { + await instance.handler(ctx); + + expect(fakeClient.createPullRequest).toHaveBeenCalledWith({ + owner: 'myorg', + repo: 'myrepo', + title: 'Create my new app', + head: 'new-app', + body: 'This PR is really good', + changes: [ + { + commit: 'Create my new app', + files: { + 'file.txt': { + content: Buffer.from('Hello there!').toString('base64'), + encoding: 'base64', + mode: '100644', + }, + }, + author: { + email: 'foo@bar.example', + name: 'Foo Bar', + }, + }, + ], + }); + }); + }); }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index b4aa03b602..0a256d36d5 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -136,6 +136,8 @@ export const createPublishGithubPullRequestAction = ( commitMessage?: string; update?: boolean; forceFork?: boolean; + gitAuthorName?: string; + gitAuthorEmail?: string; }>({ id: 'publish:github:pull-request', examples, @@ -223,6 +225,18 @@ export const createPublishGithubPullRequestAction = ( title: 'Force Fork', description: 'Create pull request from a fork', }, + gitAuthorName: { + type: 'string', + title: 'Default Author Name', + description: + "Sets the default author name for the commit. The default value is 'Scaffolder'", + }, + gitAuthorEmail: { + type: 'string', + title: 'Default Author Email', + description: + "Sets the default author email for the commit. The default value is 'scaffolder@backstage.io'", + }, }, }, output: { @@ -262,6 +276,8 @@ export const createPublishGithubPullRequestAction = ( commitMessage, update, forceFork, + gitAuthorEmail = 'scaffolder@backstage.io', + gitAuthorName = 'Scaffolder', } = ctx.input; const { owner, repo, host } = parseRepoUrl(repoUrl, integrations); @@ -328,6 +344,10 @@ export const createPublishGithubPullRequestAction = ( { files, commit: commitMessage ?? title, + author: { + name: gitAuthorName, + email: gitAuthorEmail, + }, }, ], body: description, @@ -336,6 +356,7 @@ export const createPublishGithubPullRequestAction = ( update, forceFork, }; + if (targetBranchName) { createOptions.base = targetBranchName; }