Skip to content

Commit 5028c90

Browse files
authoredOct 28, 2024··
refactor(rebaseWhen): small refactor for rebaseWhen value setter (#32175)
1 parent 7527f13 commit 5028c90

File tree

1 file changed

+37
-32
lines changed
  • lib/workers/repository/update/branch

1 file changed

+37
-32
lines changed
 

‎lib/workers/repository/update/branch/reuse.ts

+37-32
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,10 @@ export async function shouldReuseExistingBranch(
3737
return result;
3838
}
3939
logger.debug(`Branch already exists`);
40-
if (result.rebaseWhen === 'auto') {
41-
if (result.automerge === true) {
42-
logger.debug(
43-
'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because automerge=true',
44-
);
45-
result.rebaseWhen = 'behind-base-branch';
46-
} else if (await platform.getBranchForceRebase?.(result.baseBranch)) {
47-
logger.debug(
48-
'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because platform is configured to require up-to-date branches',
49-
);
50-
result.rebaseWhen = 'behind-base-branch';
51-
} else if (await shouldKeepUpdated(result, baseBranch, branchName)) {
52-
logger.debug(
53-
'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because keep-updated label is set',
54-
);
55-
result.rebaseWhen = 'behind-base-branch';
56-
}
57-
}
58-
if (result.rebaseWhen === 'auto') {
59-
logger.debug(
60-
'Converting rebaseWhen=auto to rebaseWhen=conflicted because no rule for converting to rebaseWhen=behind-base-branch applies',
61-
);
62-
result.rebaseWhen = 'conflicted';
63-
}
64-
if (
65-
result.rebaseWhen === 'behind-base-branch' ||
66-
(await shouldKeepUpdated(result, baseBranch, branchName))
67-
) {
40+
const keepUpdated = await shouldKeepUpdated(result, baseBranch, branchName);
41+
await determineRebaseWhenValue(result, keepUpdated);
42+
43+
if (result.rebaseWhen === 'behind-base-branch' || keepUpdated) {
6844
if (await scm.isBranchBehindBase(branchName, baseBranch)) {
6945
logger.debug(`Branch is behind base branch and needs rebasing`);
7046
// We can rebase the branch only if no PR or PR can be rebased
@@ -91,10 +67,7 @@ export async function shouldReuseExistingBranch(
9167

9268
if ((await scm.isBranchModified(branchName, baseBranch)) === false) {
9369
logger.debug(`Branch is not mergeable and needs rebasing`);
94-
if (
95-
result.rebaseWhen === 'never' &&
96-
!(await shouldKeepUpdated(result, baseBranch, branchName))
97-
) {
70+
if (result.rebaseWhen === 'never' && !keepUpdated) {
9871
logger.debug('Rebasing disabled by config');
9972
result.reuseExistingBranch = true;
10073
result.isModified = false;
@@ -136,3 +109,35 @@ export async function shouldReuseExistingBranch(
136109
result.isModified = false;
137110
return result;
138111
}
112+
113+
/**
114+
* This method updates rebaseWhen value when it's set to auto(default)
115+
*
116+
* @param result BranchConfig
117+
* @param keepUpdated boolean
118+
*/
119+
async function determineRebaseWhenValue(
120+
result: BranchConfig,
121+
keepUpdated: boolean,
122+
): Promise<void> {
123+
if (result.rebaseWhen === 'auto') {
124+
let reason;
125+
126+
let newValue = 'behind-base-branch';
127+
if (result.automerge === true) {
128+
reason = 'automerge=true';
129+
} else if (await platform.getBranchForceRebase?.(result.baseBranch)) {
130+
reason = 'platform is configured to require up-to-date branches';
131+
} else if (keepUpdated) {
132+
reason = 'keep-updated label is set';
133+
} else {
134+
newValue = 'conflicted';
135+
reason = 'no rule for behind-base-branch applies';
136+
}
137+
138+
logger.debug(
139+
`Converting rebaseWhen=${result.rebaseWhen} to rebaseWhen=${newValue} because ${reason}`,
140+
);
141+
result.rebaseWhen = newValue;
142+
}
143+
}

0 commit comments

Comments
 (0)
Please sign in to comment.