feat(git): allow commit-and-push to different target branch (#19680)

This commit is contained in:
Markus Schulz 2023-01-24 08:07:16 +01:00 committed by GitHub
parent 0940582336
commit 56e9270b74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 7 deletions

View file

@ -52,6 +52,7 @@ import type {
CommitResult, CommitResult,
CommitSha, CommitSha,
LocalConfig, LocalConfig,
PushFilesConfig,
StatusResult, StatusResult,
StorageConfig, StorageConfig,
TreeItem, TreeItem,
@ -1029,11 +1030,12 @@ export async function prepareCommit({
} }
export async function pushCommit({ export async function pushCommit({
branchName, sourceRef,
targetRef,
files, files,
}: CommitFilesConfig): Promise<boolean> { }: PushFilesConfig): Promise<boolean> {
await syncGit(); await syncGit();
logger.debug(`Pushing branch ${branchName}`); logger.debug(`Pushing refSpec ${sourceRef}:${targetRef ?? sourceRef}`);
let result = false; let result = false;
try { try {
const pushOptions: TaskOptions = { const pushOptions: TaskOptions = {
@ -1045,14 +1047,14 @@ export async function pushCommit({
} }
const pushRes = await gitRetry(() => const pushRes = await gitRetry(() =>
git.push('origin', `${branchName}:${branchName}`, pushOptions) git.push('origin', `${sourceRef}:${targetRef ?? sourceRef}`, pushOptions)
); );
delete pushRes.repo; delete pushRes.repo;
logger.debug({ result: pushRes }, 'git push'); logger.debug({ result: pushRes }, 'git push');
incLimitedValue('Commits'); incLimitedValue('Commits');
result = true; result = true;
} catch (err) /* istanbul ignore next */ { } catch (err) /* istanbul ignore next */ {
handleCommitError(files, branchName, err); handleCommitError(files, sourceRef, err);
} }
return result; return result;
} }
@ -1081,7 +1083,10 @@ export async function commitFiles(
try { try {
const commitResult = await prepareCommit(commitConfig); const commitResult = await prepareCommit(commitConfig);
if (commitResult) { if (commitResult) {
const pushResult = await pushCommit(commitConfig); const pushResult = await pushCommit({
sourceRef: commitConfig.branchName,
files: commitConfig.files,
});
if (pushResult) { if (pushResult) {
const { branchName } = commitConfig; const { branchName } = commitConfig;
const { commitSha } = commitResult; const { commitSha } = commitResult;

View file

@ -71,13 +71,20 @@ export interface FileDeletion {
export type FileChange = FileAddition | FileDeletion; export type FileChange = FileAddition | FileDeletion;
export interface CommitFilesConfig { export interface CommitFilesConfig {
baseBranch?: string;
branchName: string; branchName: string;
files: FileChange[]; files: FileChange[];
message: string; message: string | string[];
force?: boolean; force?: boolean;
platformCommit?: boolean; platformCommit?: boolean;
} }
export interface PushFilesConfig {
sourceRef: string;
targetRef?: string;
files: FileChange[];
}
export type BranchName = string; export type BranchName = string;
export interface CommitResult { export interface CommitResult {

View file

@ -44,6 +44,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(commitFiles).toHaveBeenCalledWith({ expect(commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',
@ -64,6 +65,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(platform.commitFiles).toHaveBeenCalledWith({ expect(platform.commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',
@ -86,6 +88,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(commitFiles).toHaveBeenCalledWith({ expect(commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',
@ -109,6 +112,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(commitFiles).toHaveBeenCalledWith({ expect(commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',
@ -133,6 +137,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(commitFiles).toHaveBeenCalledWith({ expect(commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',
@ -158,6 +163,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(commitFiles).toHaveBeenCalledWith({ expect(commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',
@ -182,6 +188,7 @@ describe('workers/repository/config-migration/branch/create', () => {
expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch); expect(checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(commitFiles).toHaveBeenCalledWith({ expect(commitFiles).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config', branchName: 'renovate/migrate-config',
baseBranch: 'master',
files: [ files: [
{ {
type: 'addition', type: 'addition',

View file

@ -34,6 +34,7 @@ export async function createConfigMigrationBranch(
migratedConfigData migratedConfigData
); );
return commitAndPush({ return commitAndPush({
baseBranch: config.defaultBranch!,
branchName: getMigrationBranchName(config), branchName: getMigrationBranchName(config),
files: [ files: [
{ {

View file

@ -126,6 +126,7 @@ describe('workers/repository/config-migration/branch/rebase', () => {
], ],
message: `Migrate config ${filename}`, message: `Migrate config ${filename}`,
platformCommit: false, platformCommit: false,
baseBranch: 'master',
}); });
} }
); );

View file

@ -51,6 +51,7 @@ export async function rebaseMigrationBranch(
migratedConfigData migratedConfigData
); );
return commitAndPush({ return commitAndPush({
baseBranch: config.defaultBranch!,
branchName, branchName,
files: [ files: [
{ {

View file

@ -34,6 +34,7 @@ export async function createOnboardingBranch(
} }
return commitAndPush({ return commitAndPush({
baseBranch: config.baseBranch,
branchName: config.onboardingBranch!, branchName: config.onboardingBranch!,
files: [ files: [
{ {

View file

@ -62,6 +62,7 @@ export async function rebaseOnboardingBranch(
// TODO #7154 // TODO #7154
return commitAndPush({ return commitAndPush({
baseBranch: config.baseBranch,
branchName: config.onboardingBranch!, branchName: config.onboardingBranch!,
files: [ files: [
{ {

View file

@ -4,6 +4,7 @@ exports[`workers/repository/update/branch/commit commitFilesToBranch commits fil
[ [
[ [
{ {
"baseBranch": undefined,
"branchName": "renovate/some-branch", "branchName": "renovate/some-branch",
"files": [ "files": [
{ {
@ -24,6 +25,7 @@ exports[`workers/repository/update/branch/commit commitFilesToBranch commits via
[ [
[ [
{ {
"baseBranch": undefined,
"branchName": "renovate/some-branch", "branchName": "renovate/some-branch",
"files": [ "files": [
{ {

View file

@ -52,6 +52,7 @@ export function commitFilesToBranch(
// API will know whether to create new branch or not // API will know whether to create new branch or not
return commitAndPush({ return commitAndPush({
baseBranch: config.baseBranch,
branchName: config.branchName, branchName: config.branchName,
files: updatedFiles, files: updatedFiles,
message: config.commitMessage!, message: config.commitMessage!,