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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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