Skip to content

Commit 18bcf81

Browse files
authored
fix(repository/finalize): avoid pruning crash when base branches are not resolved yet (renovatebot#41928)
1 parent 484c2ec commit 18bcf81

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/workers/repository/finalize/prune.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@ describe('workers/repository/finalize/prune', () => {
113113
);
114114
});
115115

116+
it('uses defaultBranch when baseBranchPatterns exist but baseBranches are not computed yet', async () => {
117+
config.branchList = [];
118+
config.baseBranchPatterns = ['/^release\\/.*/'];
119+
config.defaultBranch = 'main';
120+
git.getBranchList.mockReturnValueOnce([
121+
'renovate/release/1.x-dependency',
122+
]);
123+
platform.findPr.mockResolvedValueOnce(null);
124+
125+
await expect(
126+
cleanup.pruneStaleBranches(config, config.branchList),
127+
).resolves.not.toThrow();
128+
129+
expect(platform.findPr).toHaveBeenCalledExactlyOnceWith({
130+
branchName: 'renovate/release/1.x-dependency',
131+
state: 'open',
132+
targetBranch: 'main',
133+
});
134+
expect(scm.isBranchModified).toHaveBeenCalledExactlyOnceWith(
135+
'renovate/release/1.x-dependency',
136+
'main',
137+
);
138+
expect(scm.deleteBranch).toHaveBeenCalledExactlyOnceWith(
139+
'renovate/release/1.x-dependency',
140+
);
141+
});
142+
116143
it('does nothing on dryRun', async () => {
117144
config.branchList = ['renovate/a', 'renovate/b'];
118145
GlobalConfig.set({ dryRun: 'full' });

lib/workers/repository/finalize/prune.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async function cleanUpBranches(
120120
* @param config Renovate configuration
121121
*/
122122
function calculateBaseBranchRegex(config: RenovateConfig): RegExp | null {
123-
if (!config.baseBranchPatterns?.length) {
123+
if (!config.baseBranchPatterns?.length || !config.baseBranches?.length) {
124124
return null;
125125
}
126126

@@ -130,9 +130,7 @@ function calculateBaseBranchRegex(config: RenovateConfig): RegExp | null {
130130
.filter(uniqueStrings)
131131
.map(escapeRegExp);
132132

133-
// calculate possible base branches and escape for regex
134-
// if baseBranchPatterns is configured, baseBranches will be defined too
135-
const baseBranches = config.baseBranches!.map(escapeRegExp);
133+
const baseBranches = config.baseBranches.map(escapeRegExp);
136134

137135
// create regex to extract base branche from branch name
138136
const baseBranchRe = regEx(

0 commit comments

Comments
 (0)