feat(plugin-scaffolder-backend): add defaultCommitMessage config option

Signed-off-by: Andrew Ellis <awellis89@gmail.com>
This commit is contained in:
Andrew Ellis
2021-07-29 15:28:07 -06:00
parent c49b012d55
commit 6cf48c6098
12 changed files with 223 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': minor
---
Add the `scaffolder.defaultCommitMessage`, which defaults to `Initial commit`, so it can be customized.
+2
View File
@@ -256,6 +256,8 @@ scaffolder:
# defaultAuthor:
# name: Scaffolder
# email: scaffolder@backstage.io
# Use to customize the default commit message when new components are created
# defaultCommitMessage: 'Initial commit'
github:
token: ${GITHUB_TOKEN}
visibility: public # or 'internal' or 'private'
+4
View File
@@ -24,6 +24,10 @@ export interface Config {
name?: string;
email?: string;
};
/**
* The commit message used when new components are created.
*/
defaultCommitMessage?: string;
github?: {
[key: string]: string;
/**
@@ -62,6 +62,7 @@ export async function initRepoAndPush({
auth,
logger,
defaultBranch = 'master',
commitMessage = 'Initial commit',
gitAuthorInfo,
}: {
dir: string;
@@ -69,6 +70,7 @@ export async function initRepoAndPush({
auth: { username: string; password: string };
logger: Logger;
defaultBranch?: string;
commitMessage?: string;
gitAuthorInfo?: { name?: string; email?: string };
}): Promise<void> {
const git = Git.fromAuth({
@@ -100,7 +102,7 @@ export async function initRepoAndPush({
await git.commit({
dir,
message: 'Initial commit',
message: commitMessage,
author: authorInfo,
committer: authorInfo,
});
@@ -233,6 +233,44 @@ describe('publish:azure', () => {
});
});
it('should call initRepoAndPush with the configured defaultCommitMessage', async () => {
const customAuthorConfig = new ConfigReader({
integrations: {
azure: [
{ host: 'dev.azure.com', token: 'tokenlols' },
{ host: 'myazurehostnotoken.com' },
],
},
scaffolder: {
defaultCommitMessage: 'Test commit message',
},
});
const customAuthorIntegrations = ScmIntegrations.fromConfig(
customAuthorConfig,
);
const customAuthorAction = createPublishAzureAction({
integrations: customAuthorIntegrations,
config: customAuthorConfig,
});
mockGitClient.createRepository.mockImplementation(() => ({
remoteUrl: 'https://dev.azure.com/organization/project/_git/repo',
}));
await customAuthorAction.handler(mockContext);
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://dev.azure.com/organization/project/_git/repo',
auth: { username: 'notempty', password: 'tokenlols' },
logger: mockContext.logger,
defaultBranch: 'master',
commitMessage: 'Test commit message',
gitAuthorInfo: { email: undefined, name: undefined },
});
});
it('should call output with the remoteUrl and the repoContentsUrl', async () => {
mockGitClient.createRepository.mockImplementation(() => ({
remoteUrl: 'https://dev.azure.com/organization/project/_git/repo',
@@ -139,6 +139,9 @@ export function createPublishAzureAction(options: {
password: integrationConfig.config.token,
},
logger: ctx.logger,
commitMessage: config.getOptionalString(
'scaffolder.defaultCommitMessage',
),
gitAuthorInfo,
});
@@ -407,6 +407,74 @@ describe('publish:bitbucket', () => {
});
});
it('should call initAndPush with the configured defaultCommitMessage', async () => {
const customAuthorConfig = new ConfigReader({
integrations: {
bitbucket: [
{
host: 'bitbucket.org',
token: 'tokenlols',
},
{
host: 'hosted.bitbucket.com',
token: 'thing',
apiBaseUrl: 'https://hosted.bitbucket.com/rest/api/1.0',
},
{
host: 'notoken.bitbucket.com',
},
],
},
scaffolder: {
defaultCommitMessage: 'Test commit message',
},
});
const customAuthorIntegrations = ScmIntegrations.fromConfig(
customAuthorConfig,
);
const customAuthorAction = createPublishBitbucketAction({
integrations: customAuthorIntegrations,
config: customAuthorConfig,
});
server.use(
rest.post(
'https://api.bitbucket.org/2.0/repositories/owner/repo',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
links: {
html: {
href: 'https://bitbucket.org/owner/repo',
},
clone: [
{
name: 'https',
href: 'https://bitbucket.org/owner/cloneurl',
},
],
},
}),
),
),
);
await customAuthorAction.handler(mockContext);
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://bitbucket.org/owner/cloneurl',
auth: { username: 'x-token-auth', password: 'tokenlols' },
logger: mockContext.logger,
defaultBranch: 'master',
commitMessage: 'Test commit message',
gitAuthorInfo: { email: undefined, name: undefined },
});
});
it('should call outputs with the correct urls', async () => {
server.use(
rest.post(
@@ -304,6 +304,9 @@ export function createPublishBitbucketAction(options: {
},
defaultBranch,
logger: ctx.logger,
commitMessage: config.getOptionalString(
'scaffolder.defaultCommitMessage',
),
gitAuthorInfo,
});
@@ -260,6 +260,51 @@ describe('publish:github', () => {
});
});
it('should call initRepoAndPush with the configured defaultCommitMessage', async () => {
const customAuthorConfig = new ConfigReader({
integrations: {
github: [
{ host: 'github.com', token: 'tokenlols' },
{ host: 'ghe.github.com' },
],
},
scaffolder: {
defaultCommitMessage: 'Test commit message',
},
});
const customAuthorIntegrations = ScmIntegrations.fromConfig(
customAuthorConfig,
);
const customAuthorAction = createPublishGithubAction({
integrations: customAuthorIntegrations,
config: customAuthorConfig,
});
mockGithubClient.users.getByUsername.mockResolvedValue({
data: { type: 'User' },
});
mockGithubClient.repos.createForAuthenticatedUser.mockResolvedValue({
data: {
clone_url: 'https://github.com/clone/url.git',
html_url: 'https://github.com/html/url',
},
});
await customAuthorAction.handler(mockContext);
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://github.com/clone/url.git',
defaultBranch: 'master',
auth: { username: 'x-access-token', password: 'tokenlols' },
logger: mockContext.logger,
commitMessage: 'Test commit message',
gitAuthorInfo: { email: undefined, name: undefined },
});
});
it('should add access for the team when it starts with the owner', async () => {
mockGithubClient.users.getByUsername.mockResolvedValue({
data: { type: 'User' },
@@ -264,6 +264,9 @@ export function createPublishGithubAction(options: {
password: token,
},
logger: ctx.logger,
commitMessage: config.getOptionalString(
'scaffolder.defaultCommitMessage',
),
gitAuthorInfo,
});
@@ -219,6 +219,52 @@ describe('publish:gitlab', () => {
});
});
it('should call initRepoAndPush with the configured defaultCommitMessage', async () => {
const customAuthorConfig = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
{
host: 'hosted.gitlab.com',
apiBaseUrl: 'https://api.hosted.gitlab.com',
},
],
},
scaffolder: {
defaultCommitMessage: 'Test commit message',
},
});
const customAuthorIntegrations = ScmIntegrations.fromConfig(
customAuthorConfig,
);
const customAuthorAction = createPublishGitlabAction({
integrations: customAuthorIntegrations,
config: customAuthorConfig,
});
mockGitlabClient.Namespaces.show.mockResolvedValue({ id: 1234 });
mockGitlabClient.Projects.create.mockResolvedValue({
http_url_to_repo: 'http://mockurl.git',
});
await customAuthorAction.handler(mockContext);
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'http://mockurl.git',
auth: { username: 'oauth2', password: 'tokenlols' },
logger: mockContext.logger,
defaultBranch: 'master',
commitMessage: 'Test commit message',
gitAuthorInfo: { email: undefined, name: undefined },
});
});
it('should call output with the remoteUrl and repoContentsUrl', async () => {
mockGitlabClient.Namespaces.show.mockResolvedValue({ id: 1234 });
mockGitlabClient.Projects.create.mockResolvedValue({
@@ -137,6 +137,9 @@ export function createPublishGitlabAction(options: {
password: integrationConfig.config.token,
},
logger: ctx.logger,
commitMessage: config.getOptionalString(
'scaffolder.defaultCommitMessage',
),
gitAuthorInfo,
});