Skip to content

Commit

Permalink
feat(autodiscover): allow usage of regex patterns (#13243)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 18, 2022
1 parent 0eecd38 commit e406cd1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
22 changes: 20 additions & 2 deletions docs/usage/self-hosted-configuration.md
Expand Up @@ -79,16 +79,34 @@ If you want Renovate to run on only a subset of those, use the `autodiscoverFilt
## autodiscoverFilter

You can use this option to filter the list of repositories that the Renovate bot account can access through `autodiscover`.
It takes a [minimatch](https://www.npmjs.com/package/minimatch) glob-style pattern.
It takes a [minimatch](https://www.npmjs.com/package/minimatch) glob-style or regex pattern.

e.g.
**Minimatch**:

```json
{
"autodiscoverFilter": "project/*"
}
```

**Regex**:

All text inside the start and end `/` will be treated as a regular expression.

```json
{
"autodiscoverFilter": "/project/.*/"
}
```

You can negate the regex by putting a `!` in front:

```json
{
"autodiscoverFilter": "!/project/.*/"
}
```

## baseDir

By default Renovate uses a temporary directory like `/tmp/renovate` to store its data.
Expand Down
38 changes: 38 additions & 0 deletions lib/workers/global/autodiscover.spec.ts
Expand Up @@ -72,4 +72,42 @@ describe('workers/global/autodiscover', () => {
const res = await autodiscoverRepositories(config);
expect(res).toEqual(config);
});
it('filters autodiscovered github repos with regex', async () => {
config.autodiscover = true;
config.autodiscoverFilter = '/project/re*./';
config.platform = PlatformId.Github;
hostRules.find = jest.fn(() => ({
token: 'abc',
}));
ghApi.getRepos = jest.fn(() =>
Promise.resolve(['project/repo', 'project/another-repo'])
);
const res = await autodiscoverRepositories(config);
expect(res.repositories).toEqual(['project/repo']);
});
it('filters autodiscovered github repos with regex negation', async () => {
config.autodiscover = true;
config.autodiscoverFilter = '!/project/re*./';
config.platform = PlatformId.Github;
hostRules.find = jest.fn(() => ({
token: 'abc',
}));
ghApi.getRepos = jest.fn(() =>
Promise.resolve(['project/repo', 'project/another-repo'])
);
const res = await autodiscoverRepositories(config);
expect(res.repositories).toEqual(['project/another-repo']);
});
it('fail if regex pattern is not valid', async () => {
config.autodiscover = true;
config.autodiscoverFilter = '/project/re**./';
config.platform = PlatformId.Github;
hostRules.find = jest.fn(() => ({
token: 'abc',
}));
ghApi.getRepos = jest.fn(() =>
Promise.resolve(['project/repo', 'project/another-repo'])
);
await expect(autodiscoverRepositories(config)).rejects.toThrow();
});
});
15 changes: 14 additions & 1 deletion lib/workers/global/autodiscover.ts
Expand Up @@ -3,6 +3,7 @@ import minimatch from 'minimatch';
import type { AllConfig } from '../../config/types';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { configRegexPredicate, isConfigRegex } from '../../util/regex';

// istanbul ignore next
function repoName(value: string | { repository: string }): string {
Expand Down Expand Up @@ -30,7 +31,19 @@ export async function autodiscoverRepositories(
return config;
}
if (config.autodiscoverFilter) {
discovered = discovered.filter(minimatch.filter(config.autodiscoverFilter));
if (isConfigRegex(config.autodiscoverFilter)) {
const autodiscoveryPred = configRegexPredicate(config.autodiscoverFilter);
if (!autodiscoveryPred) {
throw new Error(
`Failed to parse regex pattern "${config.autodiscoverFilter}"`
);
}
discovered = discovered.filter(autodiscoveryPred);
} else {
discovered = discovered.filter(
minimatch.filter(config.autodiscoverFilter)
);
}
if (!discovered.length) {
// Soft fail (no error thrown) if no accessible repositories match the filter
logger.debug('None of the discovered repositories matched the filter');
Expand Down

0 comments on commit e406cd1

Please sign in to comment.