Merge remote-tracking branch 'origin/master' into rec-tasks-version-1
This commit is contained in:
@@ -15,7 +15,14 @@
|
||||
*/
|
||||
|
||||
import { Git, getVoidLogger } from '@backstage/backend-common';
|
||||
import { commitAndPushRepo, initRepoAndPush } from './gitHelpers';
|
||||
import {
|
||||
commitAndPushRepo,
|
||||
initRepoAndPush,
|
||||
commitAndPushBranch,
|
||||
addFiles,
|
||||
createBranch,
|
||||
cloneRepo,
|
||||
} from './gitHelpers';
|
||||
|
||||
jest.mock('@backstage/backend-common', () => ({
|
||||
Git: {
|
||||
@@ -29,10 +36,14 @@ jest.mock('@backstage/backend-common', () => ({
|
||||
fetch: jest.fn(),
|
||||
addRemote: jest.fn(),
|
||||
push: jest.fn(),
|
||||
clone: jest.fn(),
|
||||
}),
|
||||
},
|
||||
getVoidLogger: jest.requireActual('@backstage/backend-common').getVoidLogger,
|
||||
}));
|
||||
jest.mock('fs-extra', () => ({
|
||||
cpSync: jest.fn(),
|
||||
}));
|
||||
|
||||
const mockedGit = Git.fromAuth({
|
||||
logger: getVoidLogger(),
|
||||
@@ -303,3 +314,333 @@ describe('commitAndPushRepo', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('cloneRepo', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('with minimal parameters', () => {
|
||||
beforeEach(async () => {
|
||||
await cloneRepo({
|
||||
url: 'git@github.com:test/repo.git',
|
||||
dir: '/tmp/repo/dir/',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('clone the repo', () => {
|
||||
expect(mockedGit.clone).toHaveBeenCalledWith({
|
||||
url: 'git@github.com:test/repo.git',
|
||||
dir: '/tmp/repo/dir/',
|
||||
ref: undefined,
|
||||
depth: undefined,
|
||||
noCheckout: undefined,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('with token', async () => {
|
||||
await cloneRepo({
|
||||
url: 'git@github.com:test/repo.git',
|
||||
dir: '/tmp/repo/dir/',
|
||||
auth: {
|
||||
token: 'test-token',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.clone).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
url: 'git@github.com:test/repo.git',
|
||||
ref: undefined,
|
||||
depth: undefined,
|
||||
noCheckout: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('allows overriding the default branch', async () => {
|
||||
await cloneRepo({
|
||||
url: 'git@github.com:test/repo.git',
|
||||
dir: '/tmp/repo/dir/',
|
||||
ref: 'trunk',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.clone).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
url: 'git@github.com:test/repo.git',
|
||||
ref: 'trunk',
|
||||
depth: undefined,
|
||||
noCheckout: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('allows overriding the depth', async () => {
|
||||
await cloneRepo({
|
||||
url: 'git@github.com:test/repo.git',
|
||||
dir: '/tmp/repo/dir/',
|
||||
ref: 'trunk',
|
||||
depth: 2,
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.clone).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
url: 'git@github.com:test/repo.git',
|
||||
ref: 'trunk',
|
||||
depth: 2,
|
||||
noCheckout: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('allows overriding the noCheckout', async () => {
|
||||
await cloneRepo({
|
||||
url: 'git@github.com:test/repo.git',
|
||||
dir: '/tmp/repo/dir/',
|
||||
ref: 'trunk',
|
||||
depth: 2,
|
||||
noCheckout: true,
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.clone).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
url: 'git@github.com:test/repo.git',
|
||||
ref: 'trunk',
|
||||
depth: 2,
|
||||
noCheckout: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('createBranch', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('with minimal parameters', () => {
|
||||
beforeEach(async () => {
|
||||
await createBranch({
|
||||
dir: '/tmp/repo/dir/',
|
||||
ref: 'trunk',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('create the branch', () => {
|
||||
expect(mockedGit.checkout).toHaveBeenCalledWith({
|
||||
ref: 'trunk',
|
||||
dir: '/tmp/repo/dir/',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('with token', async () => {
|
||||
await createBranch({
|
||||
dir: '/tmp/repo/dir/',
|
||||
ref: 'trunk',
|
||||
auth: {
|
||||
token: 'test-token',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.checkout).toHaveBeenCalledWith({
|
||||
ref: 'trunk',
|
||||
dir: '/tmp/repo/dir/',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('addFiles', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('with minimal parameters', () => {
|
||||
beforeEach(async () => {
|
||||
await addFiles({
|
||||
dir: '/tmp/repo/dir/',
|
||||
filepath: '.',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('add files', () => {
|
||||
expect(mockedGit.add).toHaveBeenCalledWith({
|
||||
filepath: '.',
|
||||
dir: '/tmp/repo/dir/',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('with token', async () => {
|
||||
await addFiles({
|
||||
dir: '/tmp/repo/dir/',
|
||||
filepath: '.',
|
||||
auth: {
|
||||
token: 'test-token',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.add).toHaveBeenCalledWith({
|
||||
filepath: '.',
|
||||
dir: '/tmp/repo/dir/',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('commitAndPushBranch', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('with minimal parameters', () => {
|
||||
beforeEach(async () => {
|
||||
await commitAndPushBranch({
|
||||
dir: '/tmp/repo/dir/',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
commitMessage: 'commit message',
|
||||
});
|
||||
});
|
||||
|
||||
it('create commit', () => {
|
||||
expect(mockedGit.commit).toHaveBeenCalledWith({
|
||||
message: 'commit message',
|
||||
dir: '/tmp/repo/dir/',
|
||||
author: {
|
||||
name: 'Scaffolder',
|
||||
email: 'scaffolder@backstage.io',
|
||||
},
|
||||
committer: {
|
||||
name: 'Scaffolder',
|
||||
email: 'scaffolder@backstage.io',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('pushes to the remote', () => {
|
||||
expect(mockedGit.push).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
remote: 'origin',
|
||||
remoteRef: 'refs/heads/master',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('with token', async () => {
|
||||
await commitAndPushBranch({
|
||||
dir: '/tmp/repo/dir/',
|
||||
auth: {
|
||||
token: 'test-token',
|
||||
},
|
||||
commitMessage: 'commit message',
|
||||
gitAuthorInfo: {
|
||||
name: 'gitCommitter',
|
||||
email: 'gitCommitter@backstage.io',
|
||||
},
|
||||
logger: getVoidLogger(),
|
||||
});
|
||||
|
||||
expect(mockedGit.commit).toHaveBeenCalledWith({
|
||||
message: 'commit message',
|
||||
dir: '/tmp/repo/dir/',
|
||||
author: {
|
||||
name: 'gitCommitter',
|
||||
email: 'gitCommitter@backstage.io',
|
||||
},
|
||||
committer: {
|
||||
name: 'gitCommitter',
|
||||
email: 'gitCommitter@backstage.io',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.push).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
remote: 'origin',
|
||||
remoteRef: 'refs/heads/master',
|
||||
});
|
||||
});
|
||||
|
||||
it('allows overriding the default branch', async () => {
|
||||
await commitAndPushBranch({
|
||||
dir: '/tmp/repo/dir/',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
commitMessage: 'commit message',
|
||||
branch: 'trunk',
|
||||
});
|
||||
|
||||
expect(mockedGit.commit).toHaveBeenCalledWith({
|
||||
message: 'commit message',
|
||||
dir: '/tmp/repo/dir/',
|
||||
author: {
|
||||
name: 'Scaffolder',
|
||||
email: 'scaffolder@backstage.io',
|
||||
},
|
||||
committer: {
|
||||
name: 'Scaffolder',
|
||||
email: 'scaffolder@backstage.io',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.push).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
remote: 'origin',
|
||||
remoteRef: 'refs/heads/trunk',
|
||||
});
|
||||
});
|
||||
|
||||
it('allows overriding the remoteRef', async () => {
|
||||
await commitAndPushBranch({
|
||||
dir: '/tmp/repo/dir/',
|
||||
auth: {
|
||||
username: 'test-user',
|
||||
password: 'test-password',
|
||||
},
|
||||
commitMessage: 'commit message',
|
||||
remoteRef: 'ABC123',
|
||||
});
|
||||
|
||||
expect(mockedGit.commit).toHaveBeenCalledWith({
|
||||
message: 'commit message',
|
||||
dir: '/tmp/repo/dir/',
|
||||
author: {
|
||||
name: 'Scaffolder',
|
||||
email: 'scaffolder@backstage.io',
|
||||
},
|
||||
committer: {
|
||||
name: 'Scaffolder',
|
||||
email: 'scaffolder@backstage.io',
|
||||
},
|
||||
});
|
||||
|
||||
expect(mockedGit.push).toHaveBeenCalledWith({
|
||||
dir: '/tmp/repo/dir/',
|
||||
remote: 'origin',
|
||||
remoteRef: 'ABC123',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -134,3 +134,123 @@ export async function commitAndPushRepo(input: {
|
||||
|
||||
return { commitHash };
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function cloneRepo(options: {
|
||||
url: string;
|
||||
dir: 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 | undefined;
|
||||
ref?: string | undefined;
|
||||
depth?: number | undefined;
|
||||
noCheckout?: boolean | undefined;
|
||||
}): Promise<void> {
|
||||
const { url, dir, auth, logger, ref, depth, noCheckout } = options;
|
||||
|
||||
const git = Git.fromAuth({
|
||||
...auth,
|
||||
logger,
|
||||
});
|
||||
|
||||
await git.clone({ url, dir, ref, depth, noCheckout });
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function createBranch(options: {
|
||||
dir: string;
|
||||
ref: 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 | undefined;
|
||||
}): Promise<void> {
|
||||
const { dir, ref, auth, logger } = options;
|
||||
const git = Git.fromAuth({
|
||||
...auth,
|
||||
logger,
|
||||
});
|
||||
|
||||
await git.checkout({ dir, ref });
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function addFiles(options: {
|
||||
dir: string;
|
||||
filepath: 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 | undefined;
|
||||
}): Promise<void> {
|
||||
const { dir, filepath, auth, logger } = options;
|
||||
const git = Git.fromAuth({
|
||||
...auth,
|
||||
logger,
|
||||
});
|
||||
|
||||
await git.add({ dir, filepath });
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function commitAndPushBranch(options: {
|
||||
dir: 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 | undefined;
|
||||
commitMessage: string;
|
||||
gitAuthorInfo?: { name?: string; email?: string };
|
||||
branch?: string;
|
||||
remoteRef?: string;
|
||||
remote?: string;
|
||||
}): Promise<{ commitHash: string }> {
|
||||
const {
|
||||
dir,
|
||||
auth,
|
||||
logger,
|
||||
commitMessage,
|
||||
gitAuthorInfo,
|
||||
branch = 'master',
|
||||
remoteRef,
|
||||
remote = 'origin',
|
||||
} = options;
|
||||
const git = Git.fromAuth({
|
||||
...auth,
|
||||
logger,
|
||||
});
|
||||
|
||||
// use provided info if possible, otherwise use fallbacks
|
||||
const authorInfo = {
|
||||
name: gitAuthorInfo?.name ?? 'Scaffolder',
|
||||
email: gitAuthorInfo?.email ?? 'scaffolder@backstage.io',
|
||||
};
|
||||
|
||||
const commitHash = await git.commit({
|
||||
dir,
|
||||
message: commitMessage,
|
||||
author: authorInfo,
|
||||
committer: authorInfo,
|
||||
});
|
||||
|
||||
await git.push({
|
||||
dir,
|
||||
remote,
|
||||
remoteRef: remoteRef ?? `refs/heads/${branch}`,
|
||||
});
|
||||
|
||||
return { commitHash };
|
||||
}
|
||||
|
||||
@@ -25,5 +25,12 @@ export {
|
||||
} from './executeShellCommand';
|
||||
export { fetchContents, fetchFile } from './fetch';
|
||||
export { type ActionContext, type TemplateAction } from './types';
|
||||
export { initRepoAndPush, commitAndPushRepo } from './gitHelpers';
|
||||
export {
|
||||
initRepoAndPush,
|
||||
commitAndPushRepo,
|
||||
commitAndPushBranch,
|
||||
addFiles,
|
||||
createBranch,
|
||||
cloneRepo,
|
||||
} from './gitHelpers';
|
||||
export { parseRepoUrl, getRepoSourceDirectory } from './util';
|
||||
|
||||
Reference in New Issue
Block a user