2019-07-17 08:14:56 +00:00
|
|
|
import is from '@sindresorhus/is';
|
2019-11-26 13:05:25 +00:00
|
|
|
import minimatch from 'minimatch';
|
2021-02-05 09:49:34 +00:00
|
|
|
import { GlobalConfig } from '../../config';
|
2020-05-01 16:03:48 +00:00
|
|
|
import { logger } from '../../logger';
|
|
|
|
import { platform } from '../../platform';
|
2019-07-17 08:14:56 +00:00
|
|
|
|
2019-11-26 13:05:25 +00:00
|
|
|
// istanbul ignore next
|
|
|
|
function repoName(value: string | { repository: string }): string {
|
|
|
|
return String(is.string(value) ? value : value.repository).toLowerCase();
|
|
|
|
}
|
2019-01-22 17:17:59 +00:00
|
|
|
|
2019-11-26 13:05:25 +00:00
|
|
|
export async function autodiscoverRepositories(
|
2021-02-05 09:49:34 +00:00
|
|
|
config: GlobalConfig
|
|
|
|
): Promise<GlobalConfig> {
|
2019-01-22 17:17:59 +00:00
|
|
|
if (!config.autodiscover) {
|
2020-08-10 14:18:08 +00:00
|
|
|
if (!config.repositories?.length) {
|
2020-01-11 06:28:42 +00:00
|
|
|
logger.warn(
|
|
|
|
'No repositories found - did you want to run with flag --autodiscover?'
|
|
|
|
);
|
|
|
|
}
|
2019-01-22 17:17:59 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
// Autodiscover list of repositories
|
2019-05-20 08:59:30 +00:00
|
|
|
let discovered = await platform.getRepos();
|
2020-07-18 06:42:32 +00:00
|
|
|
if (!discovered?.length) {
|
2019-01-22 17:17:59 +00:00
|
|
|
// Soft fail (no error thrown) if no accessible repositories
|
2020-02-24 07:43:01 +00:00
|
|
|
logger.debug(
|
2019-01-22 17:17:59 +00:00
|
|
|
'The account associated with your token does not have access to any repos'
|
|
|
|
);
|
|
|
|
return config;
|
|
|
|
}
|
2019-03-17 05:49:39 +00:00
|
|
|
if (config.autodiscoverFilter) {
|
|
|
|
discovered = discovered.filter(minimatch.filter(config.autodiscoverFilter));
|
|
|
|
if (!discovered.length) {
|
|
|
|
// Soft fail (no error thrown) if no accessible repositories match the filter
|
2020-02-24 07:43:01 +00:00
|
|
|
logger.debug('None of the discovered repositories matched the filter');
|
2019-03-17 05:49:39 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 12:47:49 +00:00
|
|
|
logger.info(
|
|
|
|
{ length: discovered.length, repositories: discovered },
|
|
|
|
`Autodiscovered repositories`
|
|
|
|
);
|
2019-01-22 17:17:59 +00:00
|
|
|
// istanbul ignore if
|
2020-08-10 14:18:08 +00:00
|
|
|
if (config.repositories?.length) {
|
2019-01-22 17:17:59 +00:00
|
|
|
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');
|
2019-11-26 13:05:25 +00:00
|
|
|
// TODO: fix typings
|
|
|
|
discovered[i] = configuredRepo as never;
|
2019-01-22 17:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
logger.warn(
|
|
|
|
{ repository },
|
|
|
|
'Configured repository is in not in autodiscover list'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { ...config, repositories: discovered };
|
|
|
|
}
|