renovate/lib/workers/global/autodiscover.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-07-17 08:14:56 +00:00
import is from '@sindresorhus/is';
import minimatch from 'minimatch';
import { RenovateConfig } 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
// istanbul ignore next
function repoName(value: string | { repository: string }): string {
return String(is.string(value) ? value : value.repository).toLowerCase();
}
export async function autodiscoverRepositories(
config: RenovateConfig
): Promise<RenovateConfig> {
if (!config.autodiscover) {
2020-01-11 06:28:42 +00:00
if (!(config.repositories && 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 && 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) {
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.debug(`Discovered ${discovered.length} repositories`);
// istanbul ignore if
if (config.repositories && 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 };
}