Merge pull request #25012 from Rbillon59/master

[Github Scaffolder Module] Add boolean flag to discard authors information in createPullRequest action
This commit is contained in:
Fredrik Adelöw
2024-06-04 17:27:27 +02:00
committed by GitHub
5 changed files with 69 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-github': minor
---
Allow empty author info in createPullRequest action for Github
@@ -392,6 +392,7 @@ export const createPublishGithubPullRequestAction: (
forceFork?: boolean | undefined;
gitAuthorName?: string | undefined;
gitAuthorEmail?: string | undefined;
forceEmptyGitAuthor?: boolean | undefined;
},
JsonObject
>;
@@ -1026,5 +1026,40 @@ describe('createPublishGithubPullRequestAction', () => {
],
});
});
it('discards author name and email if forceEmptyGitAuthor is set', async () => {
input.forceEmptyGitAuthor = true;
const clientFactory = jest.fn(async () => fakeClient as any);
const githubCredentialsProvider: GithubCredentialsProvider = {
getCredentials: jest.fn(),
};
const instanceWithConfig = createPublishGithubPullRequestAction({
integrations,
githubCredentialsProvider,
clientFactory,
});
await instanceWithConfig.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',
},
},
},
],
});
});
});
});
@@ -146,6 +146,7 @@ export const createPublishGithubPullRequestAction = (
forceFork?: boolean;
gitAuthorName?: string;
gitAuthorEmail?: string;
forceEmptyGitAuthor?: boolean;
}>({
id: 'publish:github:pull-request',
examples,
@@ -246,6 +247,12 @@ export const createPublishGithubPullRequestAction = (
description:
"Sets the default author email for the commit. The default value is the authenticated user or 'scaffolder@backstage.io'",
},
forceEmptyGitAuthor: {
type: 'boolean',
title: 'Force Empty Git Author',
description:
'Forces the author to be empty. This is useful when using a Github App, it permit the commit to be verified on Github',
},
},
},
output: {
@@ -287,6 +294,7 @@ export const createPublishGithubPullRequestAction = (
forceFork,
gitAuthorEmail,
gitAuthorName,
forceEmptyGitAuthor,
} = ctx.input;
const { owner, repo, host } = parseRepoUrl(repoUrl, integrations);
@@ -384,23 +392,25 @@ export const createPublishGithubPullRequestAction = (
config?.getOptionalString('scaffolder.defaultAuthor.email'),
};
if (gitAuthorInfo.name || gitAuthorInfo.email) {
if (Array.isArray(createOptions.changes)) {
createOptions.changes = createOptions.changes.map(change => ({
...change,
author: {
name: gitAuthorInfo.name || 'Scaffolder',
email: gitAuthorInfo.email || 'scaffolder@backstage.io',
},
}));
} else {
createOptions.changes = {
...createOptions.changes,
author: {
name: gitAuthorInfo.name || 'Scaffolder',
email: gitAuthorInfo.email || 'scaffolder@backstage.io',
},
};
if (!forceEmptyGitAuthor) {
if (gitAuthorInfo.name || gitAuthorInfo.email) {
if (Array.isArray(createOptions.changes)) {
createOptions.changes = createOptions.changes.map(change => ({
...change,
author: {
name: gitAuthorInfo.name || 'Scaffolder',
email: gitAuthorInfo.email || 'scaffolder@backstage.io',
},
}));
} else {
createOptions.changes = {
...createOptions.changes,
author: {
name: gitAuthorInfo.name || 'Scaffolder',
email: gitAuthorInfo.email || 'scaffolder@backstage.io',
},
};
}
}
}
+1
View File
@@ -289,6 +289,7 @@ export const createPublishGithubPullRequestAction: (
forceFork?: boolean | undefined;
gitAuthorName?: string | undefined;
gitAuthorEmail?: string | undefined;
forceEmptyGitAuthor?: boolean | undefined;
},
JsonObject
>;