From 0c8f60ffe38376eb950b8ec5f7ab7c4218ebef94 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Mon, 5 Jun 2023 16:45:40 +0200 Subject: [PATCH 01/20] fix(worker): don't coerce valid semver versions --- lib/workers/repository/process/lookup/filter.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/workers/repository/process/lookup/filter.ts b/lib/workers/repository/process/lookup/filter.ts index 0cae02cd3eefab..50d1959f0df3db 100644 --- a/lib/workers/repository/process/lookup/filter.ts +++ b/lib/workers/repository/process/lookup/filter.ts @@ -80,7 +80,10 @@ export function filterVersions( 'Falling back to npm semver syntax for allowedVersions' ); filteredVersions = filteredVersions.filter((v) => - semver.satisfies(semver.coerce(v.version)!, allowedVersions) + semver.satisfies( + semver.valid(v.version) ? v.version : semver.coerce(v.version)!, + allowedVersions + ) ); } else if ( config.versioning === poetryVersioning.id && From 5027f9b00ac559053d6859af3d8e957d681e22dd Mon Sep 17 00:00:00 2001 From: philipabed Date: Sun, 22 Sep 2024 15:39:37 +0300 Subject: [PATCH 02/20] rebaseWhen support automerging --- docs/usage/configuration-options.md | 1 + lib/config/options/index.ts | 8 +++++++- .../repository/update/branch/reuse.spec.ts | 19 +++++++++++++++++++ lib/workers/repository/update/branch/reuse.ts | 12 ++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index f42d539ffa2c0b..e4638c0ede9915 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -3538,6 +3538,7 @@ By default this label is `"rebase"` but you can configure it to anything you wan Possible values and meanings: - `auto`: Renovate will autodetect the best setting. It will use `behind-base-branch` if configured to automerge or repository has been set to require PRs to be up to date. Otherwise, `conflicted` will be used instead +- `automerging`: Renovate will use `behind-base-branch` if configured to automerge, Otherwise, `never` will be used instead - `never`: Renovate will never rebase the branch or update it unless manually requested - `conflicted`: Renovate will rebase only if the branch is conflicted - `behind-base-branch`: Renovate will rebase whenever the branch falls 1 or more commit behind its base branch diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index da33196557f253..142736ec2c7f82 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -1801,7 +1801,13 @@ const options: RenovateOptions[] = [ name: 'rebaseWhen', description: 'Controls when Renovate rebases an existing branch.', type: 'string', - allowedValues: ['auto', 'never', 'conflicted', 'behind-base-branch'], + allowedValues: [ + 'auto', + 'never', + 'conflicted', + 'behind-base-branch', + 'automerging', + ], default: 'auto', }, { diff --git a/lib/workers/repository/update/branch/reuse.spec.ts b/lib/workers/repository/update/branch/reuse.spec.ts index 8f47e9782763ed..1a68656f241f94 100644 --- a/lib/workers/repository/update/branch/reuse.spec.ts +++ b/lib/workers/repository/update/branch/reuse.spec.ts @@ -262,5 +262,24 @@ describe('workers/repository/update/branch/reuse', () => { expect(config.rebaseWhen).toBe('auto'); expect(result.rebaseWhen).toBe('conflicted'); }); + + it('converts rebaseWhen=automerging to behind-base-branch', async () => { + config.rebaseWhen = 'automerging'; + config.automerge = true; + scm.branchExists.mockResolvedValueOnce(true); + scm.isBranchBehindBase.mockResolvedValueOnce(false); + const result = await shouldReuseExistingBranch(config); + expect(config.rebaseWhen).toBe('automerging'); + expect(result.rebaseWhen).toBe('behind-base-branch'); + }); + + it('converts rebaseWhen=automerging to never', async () => { + config.rebaseWhen = 'automerging'; + scm.branchExists.mockResolvedValueOnce(true); + scm.isBranchBehindBase.mockResolvedValueOnce(false); + const result = await shouldReuseExistingBranch(config); + expect(config.rebaseWhen).toBe('automerging'); + expect(result.rebaseWhen).toBe('never'); + }); }); }); diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index a9450e8f60a3d2..bb561b78edd29b 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -55,6 +55,18 @@ export async function shouldReuseExistingBranch( result.rebaseWhen = 'behind-base-branch'; } } + + if (result.rebaseWhen === 'automerging') { + if (result.automerge === true) { + logger.debug( + 'Converting rebaseWhen=automerging to rebaseWhen=behind-base-branch because automerge=true', + ); + result.rebaseWhen = 'behind-base-branch'; + } else { + result.rebaseWhen = 'never'; + } + } + if (result.rebaseWhen === 'auto') { logger.debug( 'Converting rebaseWhen=auto to rebaseWhen=conflicted because no rule for converting to rebaseWhen=behind-base-branch applies', From 7037c87d7e8c52b2aa71eeb0956e9fb3091ea919 Mon Sep 17 00:00:00 2001 From: philipabed Date: Sun, 22 Sep 2024 17:04:51 +0300 Subject: [PATCH 03/20] honor keep updated --- lib/workers/repository/update/branch/reuse.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index bb561b78edd29b..0c4ddfbd418912 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -62,6 +62,11 @@ export async function shouldReuseExistingBranch( 'Converting rebaseWhen=automerging to rebaseWhen=behind-base-branch because automerge=true', ); result.rebaseWhen = 'behind-base-branch'; + } else if (await shouldKeepUpdated(result, baseBranch, branchName)) { + logger.debug( + 'Converting rebaseWhen=automerging to rebaseWhen=behind-base-branch because keep-updated label is set', + ); + result.rebaseWhen = 'behind-base-branch'; } else { result.rebaseWhen = 'never'; } From 43dcf466f3a6ce3e60595234c0d1464dd60e6395 Mon Sep 17 00:00:00 2001 From: philipabed Date: Sun, 22 Sep 2024 17:55:39 +0300 Subject: [PATCH 04/20] honor keep updated --- lib/workers/repository/update/branch/reuse.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/workers/repository/update/branch/reuse.spec.ts b/lib/workers/repository/update/branch/reuse.spec.ts index 1a68656f241f94..88c90724623792 100644 --- a/lib/workers/repository/update/branch/reuse.spec.ts +++ b/lib/workers/repository/update/branch/reuse.spec.ts @@ -273,6 +273,17 @@ describe('workers/repository/update/branch/reuse', () => { expect(result.rebaseWhen).toBe('behind-base-branch'); }); + it('converts rebaseWhen=automerging to behind-base-branch', async () => { + config.rebaseWhen = 'automerging'; + config.keepUpdatedLabel = 'keep-updated'; + config.automerge = false; + scm.branchExists.mockResolvedValueOnce(true); + scm.isBranchBehindBase.mockResolvedValueOnce(false); + const result = await shouldReuseExistingBranch(config); + expect(config.rebaseWhen).toBe('automerging'); + expect(result.rebaseWhen).toBe('behind-base-branch'); + }); + it('converts rebaseWhen=automerging to never', async () => { config.rebaseWhen = 'automerging'; scm.branchExists.mockResolvedValueOnce(true); @@ -281,5 +292,8 @@ describe('workers/repository/update/branch/reuse', () => { expect(config.rebaseWhen).toBe('automerging'); expect(result.rebaseWhen).toBe('never'); }); + + + }); }); From 540d1466ebbe9502aac5d26919b86761b7226d20 Mon Sep 17 00:00:00 2001 From: philipabed Date: Sun, 22 Sep 2024 17:56:02 +0300 Subject: [PATCH 05/20] honor keep updated --- lib/workers/repository/update/branch/reuse.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workers/repository/update/branch/reuse.spec.ts b/lib/workers/repository/update/branch/reuse.spec.ts index 88c90724623792..86da2d6810f0ed 100644 --- a/lib/workers/repository/update/branch/reuse.spec.ts +++ b/lib/workers/repository/update/branch/reuse.spec.ts @@ -273,7 +273,7 @@ describe('workers/repository/update/branch/reuse', () => { expect(result.rebaseWhen).toBe('behind-base-branch'); }); - it('converts rebaseWhen=automerging to behind-base-branch', async () => { + it('converts rebaseWhen=automerging to behind-base-branch if keep-updated', async () => { config.rebaseWhen = 'automerging'; config.keepUpdatedLabel = 'keep-updated'; config.automerge = false; From 07d93072c8e9a6fdf62a8f89d5b52315cbe5fadd Mon Sep 17 00:00:00 2001 From: philipabed Date: Tue, 22 Oct 2024 10:52:19 +0300 Subject: [PATCH 06/20] honor keep updated --- lib/workers/repository/update/branch/reuse.spec.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.spec.ts b/lib/workers/repository/update/branch/reuse.spec.ts index 86da2d6810f0ed..7eaeddc36f0d72 100644 --- a/lib/workers/repository/update/branch/reuse.spec.ts +++ b/lib/workers/repository/update/branch/reuse.spec.ts @@ -268,7 +268,9 @@ describe('workers/repository/update/branch/reuse', () => { config.automerge = true; scm.branchExists.mockResolvedValueOnce(true); scm.isBranchBehindBase.mockResolvedValueOnce(false); + const result = await shouldReuseExistingBranch(config); + expect(config.rebaseWhen).toBe('automerging'); expect(result.rebaseWhen).toBe('behind-base-branch'); }); @@ -279,7 +281,10 @@ describe('workers/repository/update/branch/reuse', () => { config.automerge = false; scm.branchExists.mockResolvedValueOnce(true); scm.isBranchBehindBase.mockResolvedValueOnce(false); + platform.getBranchPr.mockResolvedValueOnce(pr); + const result = await shouldReuseExistingBranch(config); + expect(config.rebaseWhen).toBe('automerging'); expect(result.rebaseWhen).toBe('behind-base-branch'); }); @@ -288,12 +293,11 @@ describe('workers/repository/update/branch/reuse', () => { config.rebaseWhen = 'automerging'; scm.branchExists.mockResolvedValueOnce(true); scm.isBranchBehindBase.mockResolvedValueOnce(false); + const result = await shouldReuseExistingBranch(config); + expect(config.rebaseWhen).toBe('automerging'); expect(result.rebaseWhen).toBe('never'); }); - - - }); }); From 9b36d1bef292b027329e57b0dcba11cefd567c39 Mon Sep 17 00:00:00 2001 From: philipabed Date: Tue, 22 Oct 2024 14:52:21 +0300 Subject: [PATCH 07/20] honor automerging behind base branch --- lib/workers/repository/update/pr/body/config-description.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/workers/repository/update/pr/body/config-description.ts b/lib/workers/repository/update/pr/body/config-description.ts index 9249247ad25696..7b2dd9f8437fab 100644 --- a/lib/workers/repository/update/pr/body/config-description.ts +++ b/lib/workers/repository/update/pr/body/config-description.ts @@ -23,7 +23,7 @@ export function getPrConfigDescription(config: BranchConfig): string { } prBody += '\n\n'; prBody += emojify(':recycle: **Rebasing**: '); - if (config.rebaseWhen === 'behind-base-branch') { + if (config.rebaseWhen === 'behind-base-branch' || config.rebaseWhen === 'automerging') { prBody += 'Whenever PR is behind base branch'; } else if (config.rebaseWhen === 'never' || config.stopUpdating) { prBody += 'Never'; From bcbe43eba2c1e771aa8964f16884f2dc35a5fa05 Mon Sep 17 00:00:00 2001 From: philipabed Date: Tue, 22 Oct 2024 15:06:58 +0300 Subject: [PATCH 08/20] honor automerging behind base branch --- lib/workers/repository/update/pr/body/config-description.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/workers/repository/update/pr/body/config-description.ts b/lib/workers/repository/update/pr/body/config-description.ts index 7b2dd9f8437fab..251c5789afa0f6 100644 --- a/lib/workers/repository/update/pr/body/config-description.ts +++ b/lib/workers/repository/update/pr/body/config-description.ts @@ -23,7 +23,10 @@ export function getPrConfigDescription(config: BranchConfig): string { } prBody += '\n\n'; prBody += emojify(':recycle: **Rebasing**: '); - if (config.rebaseWhen === 'behind-base-branch' || config.rebaseWhen === 'automerging') { + if ( + config.rebaseWhen === 'behind-base-branch' || + config.rebaseWhen === 'automerging' + ) { prBody += 'Whenever PR is behind base branch'; } else if (config.rebaseWhen === 'never' || config.stopUpdating) { prBody += 'Never'; From 25b2ee950ca32b16e0f0f5e56f0cf5a21e45bf63 Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 11:47:34 +0200 Subject: [PATCH 09/20] refactor this --- lib/workers/repository/update/branch/reuse.ts | 73 +++++++------------ .../update/pr/body/config-description.ts | 13 ++-- 2 files changed, 36 insertions(+), 50 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index 0c4ddfbd418912..cb32f18292ad76 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -37,51 +37,11 @@ export async function shouldReuseExistingBranch( return result; } logger.debug(`Branch already exists`); - if (result.rebaseWhen === 'auto') { - if (result.automerge === true) { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because automerge=true', - ); - result.rebaseWhen = 'behind-base-branch'; - } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because platform is configured to require up-to-date branches', - ); - result.rebaseWhen = 'behind-base-branch'; - } else if (await shouldKeepUpdated(result, baseBranch, branchName)) { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because keep-updated label is set', - ); - result.rebaseWhen = 'behind-base-branch'; - } - } + const keepUpdated = await shouldKeepUpdated(result, baseBranch, branchName); + // handle auto/automerging + await updateRebaseWhenAuto(result, baseBranch, branchName, keepUpdated) - if (result.rebaseWhen === 'automerging') { - if (result.automerge === true) { - logger.debug( - 'Converting rebaseWhen=automerging to rebaseWhen=behind-base-branch because automerge=true', - ); - result.rebaseWhen = 'behind-base-branch'; - } else if (await shouldKeepUpdated(result, baseBranch, branchName)) { - logger.debug( - 'Converting rebaseWhen=automerging to rebaseWhen=behind-base-branch because keep-updated label is set', - ); - result.rebaseWhen = 'behind-base-branch'; - } else { - result.rebaseWhen = 'never'; - } - } - - if (result.rebaseWhen === 'auto') { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=conflicted because no rule for converting to rebaseWhen=behind-base-branch applies', - ); - result.rebaseWhen = 'conflicted'; - } - if ( - result.rebaseWhen === 'behind-base-branch' || - (await shouldKeepUpdated(result, baseBranch, branchName)) - ) { + if (result.rebaseWhen === 'behind-base-branch' || keepUpdated) { if (await scm.isBranchBehindBase(branchName, baseBranch)) { logger.debug(`Branch is behind base branch and needs rebasing`); // We can rebase the branch only if no PR or PR can be rebased @@ -110,7 +70,7 @@ export async function shouldReuseExistingBranch( logger.debug(`Branch is not mergeable and needs rebasing`); if ( result.rebaseWhen === 'never' && - !(await shouldKeepUpdated(result, baseBranch, branchName)) + !keepUpdated ) { logger.debug('Rebasing disabled by config'); result.reuseExistingBranch = true; @@ -153,3 +113,26 @@ export async function shouldReuseExistingBranch( result.isModified = false; return result; } + +async function updateRebaseWhenAuto(result: BranchConfig, baseBranch: string, branchName: string, keepUpdated: boolean): Promise { + + // Helper function for logging and assigning + const setRebaseWhen = (value: string, reason: string) => { + logger.debug(`Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${value} because ${reason}`); + result.rebaseWhen = value; + }; + + if (result.rebaseWhen === 'auto' || result.rebaseWhen === 'automerging') { + if (result.automerge === true) { + setRebaseWhen('behind-base-branch', 'automerge=true'); + } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { + setRebaseWhen('behind-base-branch', 'platform is configured to require up-to-date branches'); + } else if (keepUpdated) { + setRebaseWhen('behind-base-branch', 'keep-updated label is set'); + } else if (result.rebaseWhen === 'automerging') { + setRebaseWhen('never', 'no keep-updated label and automerging is set'); + } else { + setRebaseWhen('conflicted', 'no rule for behind-base-branch applies'); + } + } +} diff --git a/lib/workers/repository/update/pr/body/config-description.ts b/lib/workers/repository/update/pr/body/config-description.ts index 251c5789afa0f6..f3c7e728cc630c 100644 --- a/lib/workers/repository/update/pr/body/config-description.ts +++ b/lib/workers/repository/update/pr/body/config-description.ts @@ -21,14 +21,17 @@ export function getPrConfigDescription(config: BranchConfig): string { prBody += 'Disabled by config. Please merge this manually once you are satisfied.'; } + + let rebaseWhenCfg = config.rebaseWhen; + if (config.rebaseWhen === 'automerging') { + rebaseWhenCfg = config.automerge ? 'behind-base-branch' : 'never'; + } + prBody += '\n\n'; prBody += emojify(':recycle: **Rebasing**: '); - if ( - config.rebaseWhen === 'behind-base-branch' || - config.rebaseWhen === 'automerging' - ) { + if (rebaseWhenCfg === 'behind-base-branch') { prBody += 'Whenever PR is behind base branch'; - } else if (config.rebaseWhen === 'never' || config.stopUpdating) { + } else if (rebaseWhenCfg === 'never' || config.stopUpdating) { prBody += 'Never'; } else { prBody += 'Whenever PR becomes conflicted'; From 7f506b14643ef3d1b799f95dd3b70c3f2a0dec3b Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 11:48:54 +0200 Subject: [PATCH 10/20] refactor this --- lib/workers/repository/update/branch/reuse.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index cb32f18292ad76..2480a76dcbfe74 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -39,7 +39,7 @@ export async function shouldReuseExistingBranch( logger.debug(`Branch already exists`); const keepUpdated = await shouldKeepUpdated(result, baseBranch, branchName); // handle auto/automerging - await updateRebaseWhenAuto(result, baseBranch, branchName, keepUpdated) + await updateRebaseWhenAuto(result, baseBranch, branchName, keepUpdated); if (result.rebaseWhen === 'behind-base-branch' || keepUpdated) { if (await scm.isBranchBehindBase(branchName, baseBranch)) { @@ -68,10 +68,7 @@ export async function shouldReuseExistingBranch( if ((await scm.isBranchModified(branchName, baseBranch)) === false) { logger.debug(`Branch is not mergeable and needs rebasing`); - if ( - result.rebaseWhen === 'never' && - !keepUpdated - ) { + if (result.rebaseWhen === 'never' && !keepUpdated) { logger.debug('Rebasing disabled by config'); result.reuseExistingBranch = true; result.isModified = false; @@ -114,11 +111,17 @@ export async function shouldReuseExistingBranch( return result; } -async function updateRebaseWhenAuto(result: BranchConfig, baseBranch: string, branchName: string, keepUpdated: boolean): Promise { - +async function updateRebaseWhenAuto( + result: BranchConfig, + baseBranch: string, + branchName: string, + keepUpdated: boolean, +): Promise { // Helper function for logging and assigning const setRebaseWhen = (value: string, reason: string) => { - logger.debug(`Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${value} because ${reason}`); + logger.debug( + `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${value} because ${reason}`, + ); result.rebaseWhen = value; }; @@ -126,7 +129,10 @@ async function updateRebaseWhenAuto(result: BranchConfig, baseBranch: string, br if (result.automerge === true) { setRebaseWhen('behind-base-branch', 'automerge=true'); } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { - setRebaseWhen('behind-base-branch', 'platform is configured to require up-to-date branches'); + setRebaseWhen( + 'behind-base-branch', + 'platform is configured to require up-to-date branches', + ); } else if (keepUpdated) { setRebaseWhen('behind-base-branch', 'keep-updated label is set'); } else if (result.rebaseWhen === 'automerging') { From 29b1df0651cd34cd5e500d11f0b1613c47dfa522 Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 11:55:15 +0200 Subject: [PATCH 11/20] pretty --- .../update/pr/body/config-description.spec.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/workers/repository/update/pr/body/config-description.spec.ts b/lib/workers/repository/update/pr/body/config-description.spec.ts index 25e835bde61f04..b4776340c4e50a 100644 --- a/lib/workers/repository/update/pr/body/config-description.spec.ts +++ b/lib/workers/repository/update/pr/body/config-description.spec.ts @@ -106,5 +106,22 @@ describe('workers/repository/update/pr/body/config-description', () => { `**Automerge**: Disabled because a matching PR was automerged previously.`, ); }); + + it('renders automerging with automerge', () => { + const res = getPrConfigDescription({ + ...config, + automerge: true, + rebaseWhen: 'automerging', + }); + expect(res).toContain(`**Rebasing**: Whenever PR is behind base branch`); + }); + + it('renders automerging without automerge', () => { + const res = getPrConfigDescription({ + ...config, + rebaseWhen: 'automerging', + }); + expect(res).toContain(`**Rebasing**: Never`); + }); }); }); From 1acb56ff58f151def031a54ffd320d3ccec1e5cb Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 12:33:36 +0200 Subject: [PATCH 12/20] pretty --- lib/workers/repository/update/branch/reuse.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index 2480a76dcbfe74..3445905c229457 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -39,7 +39,7 @@ export async function shouldReuseExistingBranch( logger.debug(`Branch already exists`); const keepUpdated = await shouldKeepUpdated(result, baseBranch, branchName); // handle auto/automerging - await updateRebaseWhenAuto(result, baseBranch, branchName, keepUpdated); + await updateRebaseWhenAuto(result, keepUpdated); if (result.rebaseWhen === 'behind-base-branch' || keepUpdated) { if (await scm.isBranchBehindBase(branchName, baseBranch)) { @@ -113,12 +113,10 @@ export async function shouldReuseExistingBranch( async function updateRebaseWhenAuto( result: BranchConfig, - baseBranch: string, - branchName: string, keepUpdated: boolean, ): Promise { // Helper function for logging and assigning - const setRebaseWhen = (value: string, reason: string) => { + const setRebaseWhen = (value: string, reason: string): void => { logger.debug( `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${value} because ${reason}`, ); From 898639dccc6d29f938f1a1de63fca4280ba57018 Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 13:27:53 +0200 Subject: [PATCH 13/20] small refactor for rebaseWhen value setter --- lib/workers/repository/update/branch/reuse.ts | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index a9450e8f60a3d2..db6ec092350e24 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -37,34 +37,10 @@ export async function shouldReuseExistingBranch( return result; } logger.debug(`Branch already exists`); - if (result.rebaseWhen === 'auto') { - if (result.automerge === true) { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because automerge=true', - ); - result.rebaseWhen = 'behind-base-branch'; - } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because platform is configured to require up-to-date branches', - ); - result.rebaseWhen = 'behind-base-branch'; - } else if (await shouldKeepUpdated(result, baseBranch, branchName)) { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because keep-updated label is set', - ); - result.rebaseWhen = 'behind-base-branch'; - } - } - if (result.rebaseWhen === 'auto') { - logger.debug( - 'Converting rebaseWhen=auto to rebaseWhen=conflicted because no rule for converting to rebaseWhen=behind-base-branch applies', - ); - result.rebaseWhen = 'conflicted'; - } - if ( - result.rebaseWhen === 'behind-base-branch' || - (await shouldKeepUpdated(result, baseBranch, branchName)) - ) { + const keepUpdated = await shouldKeepUpdated(result, baseBranch, branchName); + await determineRebaseWhenValue(result, keepUpdated); + + if (result.rebaseWhen === 'behind-base-branch' || keepUpdated) { if (await scm.isBranchBehindBase(branchName, baseBranch)) { logger.debug(`Branch is behind base branch and needs rebasing`); // We can rebase the branch only if no PR or PR can be rebased @@ -91,10 +67,7 @@ export async function shouldReuseExistingBranch( if ((await scm.isBranchModified(branchName, baseBranch)) === false) { logger.debug(`Branch is not mergeable and needs rebasing`); - if ( - result.rebaseWhen === 'never' && - !(await shouldKeepUpdated(result, baseBranch, branchName)) - ) { + if (result.rebaseWhen === 'never' && !keepUpdated) { logger.debug('Rebasing disabled by config'); result.reuseExistingBranch = true; result.isModified = false; @@ -136,3 +109,37 @@ export async function shouldReuseExistingBranch( result.isModified = false; return result; } + +/** + * This method updates rebaseWhen value when it's set to auto(default) + * + * @param result BranchConfig + * @param keepUpdated boolean + */ +async function determineRebaseWhenValue( + result: BranchConfig, + keepUpdated: boolean, +): Promise { + // Helper function for logging and assigning + const setRebaseWhen = (value: string, reason: string): void => { + logger.debug( + `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${value} because ${reason}`, + ); + result.rebaseWhen = value; + }; + + if (result.rebaseWhen === 'auto') { + if (result.automerge === true) { + setRebaseWhen('behind-base-branch', 'automerge=true'); + } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { + setRebaseWhen( + 'behind-base-branch', + 'platform is configured to require up-to-date branches', + ); + } else if (keepUpdated) { + setRebaseWhen('behind-base-branch', 'keep-updated label is set'); + } else { + setRebaseWhen('conflicted', 'no rule for behind-base-branch applies'); + } + } +} From 5ba81a013910b5b1f44c5b7ac9fc5e0cd6da147f Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 14:11:32 +0200 Subject: [PATCH 14/20] small refactor for rebaseWhen value setter --- lib/workers/repository/update/branch/reuse.ts | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index db6ec092350e24..9a915480b9008b 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -120,26 +120,24 @@ async function determineRebaseWhenValue( result: BranchConfig, keepUpdated: boolean, ): Promise { - // Helper function for logging and assigning - const setRebaseWhen = (value: string, reason: string): void => { - logger.debug( - `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${value} because ${reason}`, - ); - result.rebaseWhen = value; - }; - if (result.rebaseWhen === 'auto') { + let reason; + + let newValue = 'behind-base-branch'; if (result.automerge === true) { - setRebaseWhen('behind-base-branch', 'automerge=true'); + reason = 'automerge=true'; } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { - setRebaseWhen( - 'behind-base-branch', - 'platform is configured to require up-to-date branches', - ); + reason = 'platform is configured to require up-to-date branches'; } else if (keepUpdated) { - setRebaseWhen('behind-base-branch', 'keep-updated label is set'); + reason = 'keep-updated label is set'; } else { - setRebaseWhen('conflicted', 'no rule for behind-base-branch applies'); + newValue = 'conflicted'; + reason = 'no rule for behind-base-branch applies'; } + + logger.debug( + `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${newValue} because ${reason}`, + ); + result.rebaseWhen = newValue; } } From d2fcf5dcbd3684abba07bc9d8ff6f8e1cdeeb54a Mon Sep 17 00:00:00 2001 From: philipabed Date: Mon, 28 Oct 2024 16:30:05 +0200 Subject: [PATCH 15/20] prettier --- lib/workers/repository/update/branch/reuse.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index 5602a35f0057e8..04aafd8296adfb 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -126,7 +126,10 @@ async function determineRebaseWhenValue( let newValue = 'behind-base-branch'; if (result.automerge === true) { reason = 'automerge=true'; - } else if (result.rebaseWhen !== 'automerging' && await platform.getBranchForceRebase?.(result.baseBranch)) { + } else if ( + result.rebaseWhen !== 'automerging' && + (await platform.getBranchForceRebase?.(result.baseBranch)) + ) { reason = 'platform is configured to require up-to-date branches'; } else if (keepUpdated) { reason = 'keep-updated label is set'; From c5cbd55a707f28c7928a0befc1107a1661f85818 Mon Sep 17 00:00:00 2001 From: philipabed Date: Wed, 30 Oct 2024 13:50:39 +0200 Subject: [PATCH 16/20] calculate rebaseWhen in one place only. --- .../repository/update/pr/body/config-description.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/workers/repository/update/pr/body/config-description.ts b/lib/workers/repository/update/pr/body/config-description.ts index f3c7e728cc630c..9249247ad25696 100644 --- a/lib/workers/repository/update/pr/body/config-description.ts +++ b/lib/workers/repository/update/pr/body/config-description.ts @@ -21,17 +21,11 @@ export function getPrConfigDescription(config: BranchConfig): string { prBody += 'Disabled by config. Please merge this manually once you are satisfied.'; } - - let rebaseWhenCfg = config.rebaseWhen; - if (config.rebaseWhen === 'automerging') { - rebaseWhenCfg = config.automerge ? 'behind-base-branch' : 'never'; - } - prBody += '\n\n'; prBody += emojify(':recycle: **Rebasing**: '); - if (rebaseWhenCfg === 'behind-base-branch') { + if (config.rebaseWhen === 'behind-base-branch') { prBody += 'Whenever PR is behind base branch'; - } else if (rebaseWhenCfg === 'never' || config.stopUpdating) { + } else if (config.rebaseWhen === 'never' || config.stopUpdating) { prBody += 'Never'; } else { prBody += 'Whenever PR becomes conflicted'; From 70fe92d32d2a49912dffcca26c6f2209ab0a2917 Mon Sep 17 00:00:00 2001 From: philipabed Date: Wed, 30 Oct 2024 13:52:52 +0200 Subject: [PATCH 17/20] remove tests --- .../update/pr/body/config-description.spec.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/lib/workers/repository/update/pr/body/config-description.spec.ts b/lib/workers/repository/update/pr/body/config-description.spec.ts index b4776340c4e50a..25e835bde61f04 100644 --- a/lib/workers/repository/update/pr/body/config-description.spec.ts +++ b/lib/workers/repository/update/pr/body/config-description.spec.ts @@ -106,22 +106,5 @@ describe('workers/repository/update/pr/body/config-description', () => { `**Automerge**: Disabled because a matching PR was automerged previously.`, ); }); - - it('renders automerging with automerge', () => { - const res = getPrConfigDescription({ - ...config, - automerge: true, - rebaseWhen: 'automerging', - }); - expect(res).toContain(`**Rebasing**: Whenever PR is behind base branch`); - }); - - it('renders automerging without automerge', () => { - const res = getPrConfigDescription({ - ...config, - rebaseWhen: 'automerging', - }); - expect(res).toContain(`**Rebasing**: Never`); - }); }); }); From 89c8b34569e5c598f4e81918aec7ae9122c01f03 Mon Sep 17 00:00:00 2001 From: philipabed Date: Wed, 30 Oct 2024 13:57:15 +0200 Subject: [PATCH 18/20] early return --- lib/workers/repository/update/branch/reuse.ts | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index eeddf8346cb63b..9de4d366cc02b7 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -122,30 +122,32 @@ async function determineRebaseWhenValue( result: BranchConfig, keepUpdated: boolean, ): Promise { - if (result.rebaseWhen === 'auto' || result.rebaseWhen === 'automerging') { - let reason; - - let newValue = 'behind-base-branch'; - if (result.automerge === true) { - reason = 'automerge=true'; - } else if ( - result.rebaseWhen !== 'automerging' && - (await platform.getBranchForceRebase?.(result.baseBranch)) - ) { - reason = 'platform is configured to require up-to-date branches'; - } else if (keepUpdated) { - reason = 'keep-updated label is set'; - } else if (result.rebaseWhen === 'automerging') { - newValue = 'never'; - reason = 'no keep-updated label and automerging is set'; - } else { - newValue = 'conflicted'; - reason = 'no rule for behind-base-branch applies'; - } + if (result.rebaseWhen !== 'auto' && result.rebaseWhen !== 'automerging') { + return; + } - logger.debug( - `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${newValue} because ${reason}`, - ); - result.rebaseWhen = newValue; + let reason; + let newValue = 'behind-base-branch'; + + if (result.automerge === true) { + reason = 'automerge=true'; + } else if ( + result.rebaseWhen !== 'automerging' && + (await platform.getBranchForceRebase?.(result.baseBranch)) + ) { + reason = 'platform is configured to require up-to-date branches'; + } else if (keepUpdated) { + reason = 'keep-updated label is set'; + } else if (result.rebaseWhen === 'automerging') { + newValue = 'never'; + reason = 'no keep-updated label and automerging is set'; + } else { + newValue = 'conflicted'; + reason = 'no rule for behind-base-branch applies'; } + + logger.debug( + `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${newValue} because ${reason}`, + ); + result.rebaseWhen = newValue; } From 3ecb7eabeab47bf724f1b036dc1daf6d25e7b600 Mon Sep 17 00:00:00 2001 From: philipabed Date: Wed, 30 Oct 2024 14:11:30 +0200 Subject: [PATCH 19/20] early return remove --- lib/workers/repository/update/branch/reuse.ts | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index 9de4d366cc02b7..eeddf8346cb63b 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -122,32 +122,30 @@ async function determineRebaseWhenValue( result: BranchConfig, keepUpdated: boolean, ): Promise { - if (result.rebaseWhen !== 'auto' && result.rebaseWhen !== 'automerging') { - return; - } + if (result.rebaseWhen === 'auto' || result.rebaseWhen === 'automerging') { + let reason; + + let newValue = 'behind-base-branch'; + if (result.automerge === true) { + reason = 'automerge=true'; + } else if ( + result.rebaseWhen !== 'automerging' && + (await platform.getBranchForceRebase?.(result.baseBranch)) + ) { + reason = 'platform is configured to require up-to-date branches'; + } else if (keepUpdated) { + reason = 'keep-updated label is set'; + } else if (result.rebaseWhen === 'automerging') { + newValue = 'never'; + reason = 'no keep-updated label and automerging is set'; + } else { + newValue = 'conflicted'; + reason = 'no rule for behind-base-branch applies'; + } - let reason; - let newValue = 'behind-base-branch'; - - if (result.automerge === true) { - reason = 'automerge=true'; - } else if ( - result.rebaseWhen !== 'automerging' && - (await platform.getBranchForceRebase?.(result.baseBranch)) - ) { - reason = 'platform is configured to require up-to-date branches'; - } else if (keepUpdated) { - reason = 'keep-updated label is set'; - } else if (result.rebaseWhen === 'automerging') { - newValue = 'never'; - reason = 'no keep-updated label and automerging is set'; - } else { - newValue = 'conflicted'; - reason = 'no rule for behind-base-branch applies'; + logger.debug( + `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${newValue} because ${reason}`, + ); + result.rebaseWhen = newValue; } - - logger.debug( - `Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${newValue} because ${reason}`, - ); - result.rebaseWhen = newValue; } From e69fc5c32c47e2f72695fbdc9af95e69e3be3380 Mon Sep 17 00:00:00 2001 From: philipabed Date: Wed, 30 Oct 2024 17:44:58 +0200 Subject: [PATCH 20/20] fix the condition order --- lib/workers/repository/update/branch/reuse.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/workers/repository/update/branch/reuse.ts b/lib/workers/repository/update/branch/reuse.ts index eeddf8346cb63b..b779727d2b0936 100644 --- a/lib/workers/repository/update/branch/reuse.ts +++ b/lib/workers/repository/update/branch/reuse.ts @@ -124,20 +124,16 @@ async function determineRebaseWhenValue( ): Promise { if (result.rebaseWhen === 'auto' || result.rebaseWhen === 'automerging') { let reason; - let newValue = 'behind-base-branch'; if (result.automerge === true) { reason = 'automerge=true'; - } else if ( - result.rebaseWhen !== 'automerging' && - (await platform.getBranchForceRebase?.(result.baseBranch)) - ) { - reason = 'platform is configured to require up-to-date branches'; } else if (keepUpdated) { reason = 'keep-updated label is set'; } else if (result.rebaseWhen === 'automerging') { newValue = 'never'; reason = 'no keep-updated label and automerging is set'; + } else if (await platform.getBranchForceRebase?.(result.baseBranch)) { + reason = 'platform is configured to require up-to-date branches'; } else { newValue = 'conflicted'; reason = 'no rule for behind-base-branch applies';