Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rebaseWhen): small refactor for rebaseWhen value setter #32175

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 39 additions & 32 deletions lib/workers/repository/update/branch/reuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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<void> {
// Helper function for logging and assigning
const setRebaseWhen = (value: string, reason: string): void => {
PhilipAbed marked this conversation as resolved.
Show resolved Hide resolved
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');
}
}
}