From e00210d396971650af03a9c45ab444df2a2c6d46 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Fri, 16 Jun 2023 22:06:23 +0200 Subject: [PATCH] feat: baseBranches $default (#22824) --- docs/usage/configuration-options.md | 8 ++++++++ lib/workers/repository/process/index.spec.ts | 15 +++++++++++++++ lib/workers/repository/process/index.ts | 16 ++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 563c043e92..8c2cf43b7a 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -300,6 +300,14 @@ With a negation, all branches except those matching the regex will be added to t } ``` +You can also use the special `"$default"` string to denote the repository's default branch, which is useful if you have it in an org preset, e.g.: + +```json +{ + "baseBranches": ["$default", "/^release\\/.*/"] +} +``` + <!-- prettier-ignore --> !!! note Do _not_ use the `baseBranches` config option when you've set a `forkToken`. diff --git a/lib/workers/repository/process/index.spec.ts b/lib/workers/repository/process/index.spec.ts index 0a7a53999f..1d9c3546a4 100644 --- a/lib/workers/repository/process/index.spec.ts +++ b/lib/workers/repository/process/index.spec.ts @@ -150,5 +150,20 @@ describe('workers/repository/process/index', () => { expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'dev' }); expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'some-other' }); }); + + it('maps $default to defaultBranch', async () => { + extract.mockResolvedValue({} as never); + config.baseBranches = ['$default']; + config.defaultBranch = 'master'; + git.getBranchList.mockReturnValue(['dev', 'master']); + scm.branchExists.mockResolvedValue(true); + const res = await extractDependencies(config); + expect(res).toStrictEqual({ + branchList: [undefined], + branches: [undefined], + packageFiles: undefined, + }); + expect(addMeta).toHaveBeenCalledWith({ baseBranch: 'master' }); + }); }); }); diff --git a/lib/workers/repository/process/index.ts b/lib/workers/repository/process/index.ts index cced4e1702..d9cce0b776 100644 --- a/lib/workers/repository/process/index.ts +++ b/lib/workers/repository/process/index.ts @@ -82,7 +82,10 @@ async function getBaseBranchConfig( return baseBranchConfig; } -function unfoldBaseBranches(baseBranches: string[]): string[] { +function unfoldBaseBranches( + defaultBranch: string, + baseBranches: string[] +): string[] { const unfoldedList: string[] = []; const allBranches = getBranchList(); @@ -90,7 +93,13 @@ function unfoldBaseBranches(baseBranches: string[]): string[] { const isAllowedPred = configRegexPredicate(baseBranch); if (isAllowedPred) { const matchingBranches = allBranches.filter(isAllowedPred); + logger.debug( + `baseBranches regex "${baseBranch}" matches [${matchingBranches.join()}]` + ); unfoldedList.push(...matchingBranches); + } else if (baseBranch === '$default') { + logger.debug(`baseBranches "$default" matches "${defaultBranch}"`); + unfoldedList.push(defaultBranch); } else { unfoldedList.push(baseBranch); } @@ -109,7 +118,10 @@ export async function extractDependencies( packageFiles: null!, }; if (GlobalConfig.get('platform') !== 'local' && config.baseBranches?.length) { - config.baseBranches = unfoldBaseBranches(config.baseBranches); + config.baseBranches = unfoldBaseBranches( + config.defaultBranch!, + config.baseBranches + ); logger.debug({ baseBranches: config.baseBranches }, 'baseBranches'); const extracted: Record<string, Record<string, PackageFile[]>> = {}; for (const baseBranch of config.baseBranches) { -- GitLab