feat: support Bearer auth header at git commands

Support Bearer-based Authorization header for auth
with token only (no username).

Signed-off-by: Patrick Jungermann <Patrick.Jungermann@gmail.com>
This commit is contained in:
Patrick Jungermann
2022-07-22 12:33:02 +02:00
parent 593dea6710
commit 3b7930b3e5
6 changed files with 145 additions and 26 deletions
@@ -33,8 +33,6 @@ jest.mock('@backstage/backend-common', () => ({
}));
const mockedGit = Git.fromAuth({
username: 'test-user',
password: 'test-password',
logger: getVoidLogger(),
});
@@ -101,6 +99,22 @@ describe('initRepoAndPush', () => {
});
});
it('with token', async () => {
await initRepoAndPush({
dir: '/test/repo/dir/',
remoteUrl: 'git@github.com:test/repo.git',
auth: {
token: 'test-token',
},
logger: getVoidLogger(),
});
expect(mockedGit.init).toHaveBeenCalledWith({
dir: '/test/repo/dir/',
defaultBranch: 'master',
});
});
it('allows overriding the default branch', async () => {
await initRepoAndPush({
dir: '/test/repo/dir/',
@@ -83,15 +83,17 @@ export async function initRepoAndPush({
}: {
dir: string;
remoteUrl: string;
auth: { username: string; password: string };
// For use cases where token has to be used with Basic Auth
// it has to be provided as password together with a username
// which may be a fixed value defined by the provider.
auth: { username: string; password: string } | { token: string };
logger: Logger;
defaultBranch?: string;
commitMessage?: string;
gitAuthorInfo?: { name?: string; email?: string };
}): Promise<void> {
const git = Git.fromAuth({
username: auth.username,
password: auth.password,
...auth,
logger,
});
@@ -137,7 +139,10 @@ export async function commitAndPushRepo({
remoteRef,
}: {
dir: string;
auth: { username: string; password: string };
// For use cases where token has to be used with Basic Auth
// it has to be provided as password together with a username
// which may be a fixed value defined by the provider.
auth: { username: string; password: string } | { token: string };
logger: Logger;
commitMessage: string;
gitAuthorInfo?: { name?: string; email?: string };
@@ -145,8 +150,7 @@ export async function commitAndPushRepo({
remoteRef?: string;
}): Promise<void> {
const git = Git.fromAuth({
username: auth.username,
password: auth.password,
...auth,
logger,
});