renovate/lib/config/index.js

183 lines
5.6 KiB
JavaScript
Raw Normal View History

const logger = require('../logger');
const stringify = require('json-stringify-pretty-compact');
const githubApi = require('../api/github');
const gitlabApi = require('../api/gitlab');
2017-01-14 13:39:26 +00:00
const defaultsParser = require('./defaults');
const fileParser = require('./file');
const cliParser = require('./cli');
const envParser = require('./env');
const githubAppHelper = require('../helpers/github-app');
2017-01-15 15:12:56 +00:00
let config = null;
2017-01-14 13:39:26 +00:00
module.exports = {
parseConfigs,
getCascadedConfig,
2017-01-22 13:40:14 +00:00
getRepositories,
};
async function parseConfigs(env, argv) {
2017-01-15 15:12:56 +00:00
logger.debug('Parsing configs');
2017-01-14 13:39:26 +00:00
2017-01-15 15:12:56 +00:00
// Get configs
const defaultConfig = defaultsParser.getConfig();
const fileConfig = fileParser.getConfig(env);
const cliConfig = cliParser.getConfig(argv);
const envConfig = envParser.getConfig(env);
2017-01-14 13:39:26 +00:00
config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
// Set log level
logger.levels('stdout', config.logLevel);
2017-06-20 19:34:25 +00:00
// Add file logger
if (config.logFile) {
logger.debug(
`Enabling ${config.logFileLevel} logging to ${config.logFile}`
);
logger.addStream({
name: 'logfile',
path: config.logFile,
level: config.logFileLevel,
});
}
logger.debug(`Default config = ${redact(defaultConfig)}`);
logger.debug(`File config = ${redact(fileConfig)}`);
logger.debug(`CLI config: ${redact(cliConfig)}`);
logger.debug(`Env config: ${redact(envConfig)}`);
2017-01-15 15:12:56 +00:00
// Get global config
logger.debug(`raw config=${redact(config)}`);
2017-01-14 13:39:26 +00:00
// Check platforms and tokens
if (config.platform === 'github') {
if (!config.githubAppId && !config.token && !env.GITHUB_TOKEN) {
throw new Error('You need to supply a GitHub token.');
}
config.api = githubApi;
} else if (config.platform === 'gitlab') {
if (!config.token && !env.GITLAB_TOKEN) {
throw new Error('You need to supply a GitLab token.');
}
config.api = gitlabApi;
} else {
throw new Error(`Unsupported platform: ${config.platform}.`);
}
if (config.githubAppId) {
logger.info('Initialising GitHub App mode');
if (!config.githubAppKey) {
throw new Error('A GitHub App Private Key must be provided');
}
config.repositories = await githubAppHelper.getRepositories(config);
logger.info(`Found ${config.repositories.length} repositories installed`);
delete config.githubAppKey;
logger.debug(`GitHub App config: ${JSON.stringify(config)}`);
} else if (config.autodiscover) {
// Autodiscover list of repositories
if (config.platform === 'github') {
logger.info('Autodiscovering GitHub repositories');
2017-04-21 08:12:41 +00:00
config.repositories = await githubApi.getRepos(
config.token,
2017-04-21 08:25:49 +00:00
config.endpoint
2017-04-21 08:12:41 +00:00
);
} else if (config.platform === 'gitlab') {
logger.info('Autodiscovering GitLab repositories');
2017-04-21 08:12:41 +00:00
config.repositories = await gitlabApi.getRepos(
config.token,
2017-04-21 08:25:49 +00:00
config.endpoint
2017-04-21 08:12:41 +00:00
);
}
if (!config.repositories || config.repositories.length === 0) {
// Soft fail (no error thrown) if no accessible repositories
2017-04-21 08:12:41 +00:00
logger.info(
2017-04-21 08:25:49 +00:00
'The account associated with your token does not have access to any repos'
2017-04-21 08:12:41 +00:00
);
return;
}
} else if (!config.repositories || config.repositories.length === 0) {
// We need at least one repository defined
2017-04-21 08:12:41 +00:00
throw new Error(
2017-04-21 08:25:49 +00:00
'At least one repository must be configured, or use --autodiscover'
2017-04-21 08:12:41 +00:00
);
2017-01-14 13:39:26 +00:00
}
// Configure each repository
2017-04-21 08:12:41 +00:00
config.repositories = config.repositories.map(item => {
// Convert any repository strings to objects
const repo = typeof item === 'string' ? { repository: item } : item;
// copy across some fields from the base config if not present
repo.token = repo.token || config.token;
repo.platform = repo.platform || config.platform;
repo.onboarding = repo.onboarding || config.onboarding;
repo.endpoint = repo.endpoint || config.endpoint;
// Set default packageFiles
2017-01-15 15:12:56 +00:00
if (!repo.packageFiles || !repo.packageFiles.length) {
repo.packageFiles = config.packageFiles;
2017-01-15 15:12:56 +00:00
}
// Expand packageFile format
2017-04-21 08:12:41 +00:00
repo.packageFiles = repo.packageFiles.map(packageFile => {
2017-01-15 15:12:56 +00:00
if (typeof packageFile === 'string') {
return { fileName: packageFile };
}
return packageFile;
});
return repo;
2017-01-15 15:12:56 +00:00
});
2017-01-15 15:12:56 +00:00
// Print config
logger.debug(`config=${redact(config)}`);
2017-06-20 19:34:25 +00:00
// Remove log file entries
delete config.logFile;
delete config.logFileLevel;
2017-01-15 15:12:56 +00:00
}
2017-01-14 13:39:26 +00:00
function getCascadedConfig(repo, packageFile) {
const cascadedConfig = Object.assign({}, config, repo, packageFile);
// Remove unnecessary fields
delete cascadedConfig.repositories;
delete cascadedConfig.repository;
delete cascadedConfig.fileName;
return cascadedConfig;
}
2017-01-22 13:40:14 +00:00
function getRepositories() {
return config.repositories;
2017-01-14 13:39:26 +00:00
}
function redact(inputConfig) {
const redactedConfig = Object.assign({}, inputConfig);
if (redactedConfig.token) {
redactedConfig.token = `${redactedConfig.token.substr(0, 4)}${new Array(
redactedConfig.token.length - 3
).join('*')}`;
}
if (redactedConfig.githubAppKey) {
redactedConfig.githubAppKey = '***REDACTED***';
}
if (inputConfig.repositories) {
redactedConfig.repositories = [];
for (const repository of inputConfig.repositories) {
2017-06-20 19:34:08 +00:00
if (typeof repository !== 'string') {
const redactedRepo = Object.assign({}, repository);
if (redactedRepo.token) {
redactedRepo.token = `${redactedRepo.token.substr(0, 4)}${new Array(
redactedRepo.token.length - 3
).join('*')}`;
}
redactedConfig.repositories.push(redactedRepo);
} else {
redactedConfig.repositories.push(repository);
}
}
}
return stringify(redactedConfig);
}