feat: add default branch prop for publish:github

Signed-off-by: shoukoo <shoukoo.koike@gmail.com>
This commit is contained in:
shoukoo
2021-06-30 19:38:58 +10:00
parent 66da60bc43
commit c2db794f5a
7 changed files with 95 additions and 5 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/backend-common': patch
'@backstage/plugin-scaffolder-backend': patch
---
add defaultBranch property for publish GitHub action
+2 -1
View File
@@ -183,8 +183,9 @@ export class Git {
logger?: Logger | undefined;
}) => Git;
// (undocumented)
init({ dir }: {
init({ dir, defaultBranch, }: {
dir: string;
defaultBranch?: string;
}): Promise<void>;
// (undocumented)
merge({ dir, theirs, ours, author, committer, }: {
+3 -1
View File
@@ -201,14 +201,16 @@ describe('Git', () => {
describe('init', () => {
it('should call isomorphic-git with the correct arguments', async () => {
const dir = '/some/mock/dir';
const defaultBranch = 'master';
const git = Git.fromAuth({});
await git.init({ dir });
await git.init({ dir, defaultBranch });
expect(isomorphic.init).toHaveBeenCalledWith({
fs,
dir,
defaultBranch,
});
});
});
+8 -1
View File
@@ -150,12 +150,19 @@ export class Git {
});
}
async init({ dir }: { dir: string }): Promise<void> {
async init({
dir,
defaultBranch = 'master',
}: {
dir: string;
defaultBranch?: string;
}): Promise<void> {
this.config.logger?.info(`Init git repository {dir=${dir}}`);
return git.init({
fs,
dir,
defaultBranch,
});
}
@@ -175,6 +175,36 @@ describe('publish:github', () => {
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,
});
});
it('should call initRepoAndPush with the correct defaultBranch main', async () => {
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 action.handler({
...mockContext,
input: {
...mockContext.input,
defaultBranch: 'main',
},
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContext.workspacePath,
remoteUrl: 'https://github.com/clone/url.git',
defaultBranch: 'main',
auth: { username: 'x-access-token', password: 'tokenlols' },
logger: mockContext.logger,
});
@@ -430,4 +460,34 @@ describe('publish:github', () => {
'https://github.com/html/url/blob/master',
);
});
it('should use main as default branch', async () => {
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 action.handler({
...mockContext,
input: {
...mockContext.input,
defaultBranch: 'main',
},
});
expect(mockContext.output).toHaveBeenCalledWith(
'remoteUrl',
'https://github.com/clone/url.git',
);
expect(mockContext.output).toHaveBeenCalledWith(
'repoContentsUrl',
'https://github.com/html/url/blob/main',
);
});
});
@@ -45,6 +45,7 @@ export function createPublishGithubAction(options: {
repoUrl: string;
description?: string;
access?: string;
defaultBranch?: string;
sourcePath?: string;
repoVisibility: 'private' | 'internal' | 'public';
collaborators: Collaborator[];
@@ -77,6 +78,11 @@ export function createPublishGithubAction(options: {
type: 'string',
enum: ['private', 'public', 'internal'],
},
defaultBranch: {
title: 'Default Branch',
type: 'string',
description: `Sets the default branch on the repository. The default value is 'master'`,
},
sourcePath: {
title:
'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.',
@@ -131,6 +137,7 @@ export function createPublishGithubAction(options: {
description,
access,
repoVisibility = 'private',
defaultBranch = 'master',
collaborators,
topics,
} = ctx.input;
@@ -239,11 +246,12 @@ export function createPublishGithubAction(options: {
}
const remoteUrl = newRepo.clone_url;
const repoContentsUrl = `${newRepo.html_url}/blob/master`;
const repoContentsUrl = `${newRepo.html_url}/blob/${defaultBranch}`;
await initRepoAndPush({
dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath),
remoteUrl,
defaultBranch,
auth: {
username: 'x-access-token',
password: token,
@@ -257,6 +265,7 @@ export function createPublishGithubAction(options: {
client,
repoName: newRepo.name,
logger: ctx.logger,
defaultBranch,
});
} catch (e) {
throw new Error(
@@ -24,11 +24,13 @@ export async function initRepoAndPush({
remoteUrl,
auth,
logger,
defaultBranch = 'master',
}: {
dir: string;
remoteUrl: string;
auth: { username: string; password: string };
logger: Logger;
defaultBranch?: string;
}): Promise<void> {
const git = Git.fromAuth({
username: auth.username,
@@ -38,6 +40,7 @@ export async function initRepoAndPush({
await git.init({
dir,
defaultBranch,
});
const paths = await globby(['./**', './**/.*', '!.git'], {
@@ -74,6 +77,7 @@ type BranchProtectionOptions = {
owner: string;
repoName: string;
logger: Logger;
defaultBranch?: string;
};
export const enableBranchProtectionOnDefaultRepoBranch = async ({
@@ -81,6 +85,7 @@ export const enableBranchProtectionOnDefaultRepoBranch = async ({
client,
owner,
logger,
defaultBranch = 'master',
}: BranchProtectionOptions): Promise<void> => {
const tryOnce = async () => {
try {
@@ -97,7 +102,7 @@ export const enableBranchProtectionOnDefaultRepoBranch = async ({
},
owner,
repo: repoName,
branch: 'master',
branch: defaultBranch,
required_status_checks: { strict: true, contexts: [] },
restrictions: null,
enforce_admins: true,