chore(plugins/scaffolder-backend-module-githu): adding support to override default author commit for github PRs

Signed-off-by: Matheus Castiglioni <mahenrique94@users.noreply.github.com>
This commit is contained in:
Matheus Castiglioni
2024-05-03 20:27:36 -03:00
parent 54591d3701
commit 52ab24122a
5 changed files with 218 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-github': minor
---
Adding support to change the default commit author for pull-request github action"
@@ -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',
},
},
],
});
@@ -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',
},
},
],
@@ -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<GithubPullRequestActionInput>;
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',
},
},
],
});
});
});
});
@@ -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;
}