Skip to content

Commit

Permalink
feat: baseBranches $default (#22824)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jun 16, 2023
1 parent 8131b43 commit e00210d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
8 changes: 8 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
15 changes: 15 additions & 0 deletions lib/workers/repository/process/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
});
});
});
16 changes: 14 additions & 2 deletions lib/workers/repository/process/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,24 @@ async function getBaseBranchConfig(
return baseBranchConfig;
}

function unfoldBaseBranches(baseBranches: string[]): string[] {
function unfoldBaseBranches(
defaultBranch: string,
baseBranches: string[]
): string[] {
const unfoldedList: string[] = [];

const allBranches = getBranchList();
for (const baseBranch of baseBranches) {
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);
}
Expand All @@ -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) {
Expand Down

0 comments on commit e00210d

Please sign in to comment.