renovate/lib/config/index.js

177 lines
5 KiB
JavaScript
Raw Normal View History

const logger = require('../logger');
const githubApi = require('../api/github');
const gitlabApi = require('../api/gitlab');
2017-01-14 13:39:26 +00:00
const definitions = require('./definitions');
const defaultsParser = require('./defaults');
const fileParser = require('./file');
const cliParser = require('./cli');
const envParser = require('./env');
const githubApp = require('./github-app');
module.exports = {
parseConfigs,
mergeChildConfig,
filterConfig,
getOnboardingConfig,
};
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
const 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,
});
}
Refactor repository worker (#344) * Move to subdir * Downgrade eslint to 3 * Refactor api and config usage * Refactor mergeRenovateJson * Test mergeRenovateJson * getOnboardingStatus tests * Refactor repository structure * Refactor config.logger * Revert "Refactor config.logger" This reverts commit 6d7f81af6ee284d01aab811dab7eb05c2274edf3. * Refactor repository logging * Refactor try/catch * Refactor platform and onboarding * Refactor setNpmrc * Fix github logger * npm api use config.logger * Refactor repo worker logger * Refactor repo worker * Refactor branched upgrades * Repository refactoring * Move some debug logging to trace * Deprecate fileName * Refactor upgrades * Refactor repository logs * More repository log refactoring * Refactor repository location * Revert "Refactor repository location" This reverts commit faecbf29516737a2752de54103c0228b9112a51c. * Fix tests * mergeRenovateJson * Recombine repository worker * Add initApis tests * add detectPackageFiles tests * Add determineRepoUpgrades tests * start groupUpgradesByBranch tests * add test * add test * Finish groupUpgradesByBranch coverage * Test updateBranchesSequentially * Finish repo coverage * Finish branch worker coverage * Finish workers coverage * Fix isPin * Complete workers coverage * Finish helpers coverage * Add gitlab api tests * getBranchStatus tests * test createPr * start getPr testing * getPr * update and merge PR tests * getFile * getFileContent tests * getFileJson tests * createFile * updateFile * createBranch * commitFilesToBranch * update yarn * Update yarn
2017-06-25 05:36:13 +00:00
logger.trace({ config: defaultConfig }, 'Default config');
logger.debug({ config: fileConfig }, 'File config');
logger.debug({ config: cliConfig }, 'CLI config');
logger.debug({ config: envConfig }, 'Env config');
2017-01-15 15:12:56 +00:00
// Get global config
Refactor repository worker (#344) * Move to subdir * Downgrade eslint to 3 * Refactor api and config usage * Refactor mergeRenovateJson * Test mergeRenovateJson * getOnboardingStatus tests * Refactor repository structure * Refactor config.logger * Revert "Refactor config.logger" This reverts commit 6d7f81af6ee284d01aab811dab7eb05c2274edf3. * Refactor repository logging * Refactor try/catch * Refactor platform and onboarding * Refactor setNpmrc * Fix github logger * npm api use config.logger * Refactor repo worker logger * Refactor repo worker * Refactor branched upgrades * Repository refactoring * Move some debug logging to trace * Deprecate fileName * Refactor upgrades * Refactor repository logs * More repository log refactoring * Refactor repository location * Revert "Refactor repository location" This reverts commit faecbf29516737a2752de54103c0228b9112a51c. * Fix tests * mergeRenovateJson * Recombine repository worker * Add initApis tests * add detectPackageFiles tests * Add determineRepoUpgrades tests * start groupUpgradesByBranch tests * add test * add test * Finish groupUpgradesByBranch coverage * Test updateBranchesSequentially * Finish repo coverage * Finish branch worker coverage * Finish workers coverage * Fix isPin * Complete workers coverage * Finish helpers coverage * Add gitlab api tests * getBranchStatus tests * test createPr * start getPr testing * getPr * update and merge PR tests * getFile * getFileContent tests * getFileJson tests * createFile * updateFile * createBranch * commitFilesToBranch * update yarn * Update yarn
2017-06-25 05:36:13 +00:00
logger.trace({ config }, 'Raw 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 githubApp.getRepositories(config);
logger.info(`Found ${config.repositories.length} repositories installed`);
logger.debug({ config }, 'GitHub App 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 config;
}
} 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
}
2017-01-15 15:12:56 +00:00
// Print config
Refactor repository worker (#344) * Move to subdir * Downgrade eslint to 3 * Refactor api and config usage * Refactor mergeRenovateJson * Test mergeRenovateJson * getOnboardingStatus tests * Refactor repository structure * Refactor config.logger * Revert "Refactor config.logger" This reverts commit 6d7f81af6ee284d01aab811dab7eb05c2274edf3. * Refactor repository logging * Refactor try/catch * Refactor platform and onboarding * Refactor setNpmrc * Fix github logger * npm api use config.logger * Refactor repo worker logger * Refactor repo worker * Refactor branched upgrades * Repository refactoring * Move some debug logging to trace * Deprecate fileName * Refactor upgrades * Refactor repository logs * More repository log refactoring * Refactor repository location * Revert "Refactor repository location" This reverts commit faecbf29516737a2752de54103c0228b9112a51c. * Fix tests * mergeRenovateJson * Recombine repository worker * Add initApis tests * add detectPackageFiles tests * Add determineRepoUpgrades tests * start groupUpgradesByBranch tests * add test * add test * Finish groupUpgradesByBranch coverage * Test updateBranchesSequentially * Finish repo coverage * Finish branch worker coverage * Finish workers coverage * Fix isPin * Complete workers coverage * Finish helpers coverage * Add gitlab api tests * getBranchStatus tests * test createPr * start getPr testing * getPr * update and merge PR tests * getFile * getFileContent tests * getFileJson tests * createFile * updateFile * createBranch * commitFilesToBranch * update yarn * Update yarn
2017-06-25 05:36:13 +00:00
logger.trace({ config }, 'Global config');
2017-06-20 19:34:25 +00:00
// Remove log file entries
delete config.logFile;
delete config.logFileLevel;
return config;
2017-01-15 15:12:56 +00:00
}
2017-01-14 13:39:26 +00:00
function mergeChildConfig(parentConfig, childConfig, additional) {
const config = Object.assign({}, parentConfig, childConfig);
for (const option of definitions.getOptions()) {
if (option.mergeable && childConfig[option.name]) {
logger.debug(`mergeable option: ${option.name}`);
// TODO: handle arrays
config[option.name] = Object.assign(
{},
parentConfig[option.name],
childConfig[option.name]
);
logger.debug(
`config.${option.name}=${JSON.stringify(config[option.name])}`
);
}
}
if (additional) {
Object.assign(config, additional);
}
return config;
}
function filterConfig(inputConfig, targetPhase) {
logger.trace({ config: inputConfig }, `filterConfig('${targetPhase}')`);
const outputConfig = Object.assign({}, inputConfig);
const phases = [
'global',
'repository',
'packageFile',
'depType',
'package',
'branch',
'pr',
];
const targetIndex = phases.indexOf(targetPhase);
for (const option of definitions.getOptions()) {
const optionIndex = phases.indexOf(option.phase);
if (optionIndex !== -1 && optionIndex < targetIndex) {
delete outputConfig[option.name];
}
}
return outputConfig;
}
function getOnboardingConfig(repoConfig) {
const config = {};
for (const option of definitions.getOptions()) {
if (option.phase !== 'global' && option.onboarding !== false) {
config[option.name] = repoConfig[option.name];
}
}
if (repoConfig.detectedPackageFiles) {
config.packageFiles = [];
}
return config;
}