/
autodiscover.ts
82 lines (80 loc) · 2.65 KB
/
autodiscover.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import is from '@sindresorhus/is';
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 {
return String(is.string(value) ? value : value.repository).toLowerCase();
}
export async function autodiscoverRepositories(
config: AllConfig
): Promise<AllConfig> {
if (!config.autodiscover) {
if (!config.repositories?.length) {
logger.warn(
'No repositories found - did you want to run with flag --autodiscover?'
);
}
return config;
}
// Autodiscover list of repositories
let discovered = await platform.getRepos();
if (!discovered?.length) {
// Soft fail (no error thrown) if no accessible repositories
logger.debug(
'The account associated with your token does not have access to any repos'
);
return config;
}
if (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');
return config;
}
}
logger.info(
{ length: discovered.length, repositories: discovered },
`Autodiscovered repositories`
);
// istanbul ignore if
if (config.repositories?.length) {
logger.debug(
'Checking autodiscovered repositories against configured repositories'
);
for (const configuredRepo of config.repositories) {
const repository = repoName(configuredRepo);
let found = false;
for (let i = discovered.length - 1; i > -1; i -= 1) {
if (repository === repoName(discovered[i])) {
found = true;
logger.debug({ repository }, 'Using configured repository settings');
// TODO: fix typings
discovered[i] = configuredRepo as never;
}
}
if (!found) {
logger.warn(
{ repository },
'Configured repository is in not in autodiscover list'
);
}
}
}
return { ...config, repositories: discovered };
}