2017-06-29 05:29:41 +00:00
|
|
|
const logger = require('../logger');
|
2017-04-21 04:43:26 +00:00
|
|
|
const githubApi = require('../api/github');
|
|
|
|
const gitlabApi = require('../api/gitlab');
|
2017-01-14 13:39:26 +00:00
|
|
|
|
2017-06-26 11:08:57 +00:00
|
|
|
const definitions = require('./definitions');
|
|
|
|
|
2017-01-20 13:03:18 +00:00
|
|
|
const defaultsParser = require('./defaults');
|
|
|
|
const fileParser = require('./file');
|
|
|
|
const cliParser = require('./cli');
|
|
|
|
const envParser = require('./env');
|
|
|
|
|
2017-06-29 05:29:41 +00:00
|
|
|
const githubApp = require('./github-app');
|
2017-06-02 20:06:15 +00:00
|
|
|
|
2017-01-20 13:03:18 +00:00
|
|
|
module.exports = {
|
|
|
|
parseConfigs,
|
2017-07-01 04:44:41 +00:00
|
|
|
mergeChildConfig,
|
2017-06-27 11:44:03 +00:00
|
|
|
filterConfig,
|
2017-06-29 13:38:18 +00:00
|
|
|
getOnboardingConfig,
|
2017-01-20 13:03:18 +00:00
|
|
|
};
|
|
|
|
|
2017-04-21 05:00:26 +00:00
|
|
|
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
|
2017-01-20 13:03:18 +00:00
|
|
|
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
|
|
|
|
2017-06-22 07:03:36 +00:00
|
|
|
const config = Object.assign(
|
|
|
|
{},
|
|
|
|
defaultConfig,
|
|
|
|
fileConfig,
|
|
|
|
envConfig,
|
|
|
|
cliConfig
|
|
|
|
);
|
2017-02-07 20:45:35 +00:00
|
|
|
|
|
|
|
// Set log level
|
2017-06-20 15:57:04 +00:00
|
|
|
logger.levels('stdout', config.logLevel);
|
2017-02-07 20:45:35 +00:00
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-25 05:36:13 +00:00
|
|
|
logger.trace({ config: defaultConfig }, 'Default config');
|
2017-06-22 07:03:36 +00:00
|
|
|
logger.debug({ config: fileConfig }, 'File config');
|
|
|
|
logger.debug({ config: cliConfig }, 'CLI config');
|
|
|
|
logger.debug({ config: envConfig }, 'Env config');
|
2017-01-18 00:48:17 +00:00
|
|
|
|
2017-01-15 15:12:56 +00:00
|
|
|
// Get global config
|
2017-06-25 05:36:13 +00:00
|
|
|
logger.trace({ config }, 'Raw config');
|
2017-01-14 13:39:26 +00:00
|
|
|
|
2017-04-21 04:43:26 +00:00
|
|
|
// Check platforms and tokens
|
|
|
|
if (config.platform === 'github') {
|
2017-06-02 20:06:15 +00:00
|
|
|
if (!config.githubAppId && !config.token && !env.GITHUB_TOKEN) {
|
2017-04-21 04:43:26 +00:00
|
|
|
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}.`);
|
|
|
|
}
|
|
|
|
|
2017-06-02 20:06:15 +00:00
|
|
|
if (config.githubAppId) {
|
|
|
|
logger.info('Initialising GitHub App mode');
|
|
|
|
if (!config.githubAppKey) {
|
|
|
|
throw new Error('A GitHub App Private Key must be provided');
|
|
|
|
}
|
2017-06-29 05:29:41 +00:00
|
|
|
config.repositories = await githubApp.getRepositories(config);
|
2017-06-20 06:02:17 +00:00
|
|
|
logger.info(`Found ${config.repositories.length} repositories installed`);
|
2017-06-22 07:03:36 +00:00
|
|
|
logger.debug({ config }, 'GitHub App config');
|
2017-06-02 20:06:15 +00:00
|
|
|
} else if (config.autodiscover) {
|
|
|
|
// Autodiscover list of repositories
|
2017-04-21 05:00:26 +00:00
|
|
|
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
|
|
|
);
|
2017-04-21 05:00:26 +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
|
|
|
);
|
2017-04-21 05:00:26 +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
|
|
|
);
|
2017-06-22 07:03:36 +00:00
|
|
|
return config;
|
2017-04-21 05:00:26 +00:00
|
|
|
}
|
|
|
|
} 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-02-07 20:45:35 +00:00
|
|
|
|
2017-01-15 15:12:56 +00:00
|
|
|
// Print config
|
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;
|
2017-06-22 07:03:36 +00:00
|
|
|
return config;
|
2017-01-15 15:12:56 +00:00
|
|
|
}
|
2017-01-14 13:39:26 +00:00
|
|
|
|
2017-07-02 05:50:46 +00:00
|
|
|
function mergeChildConfig(parentConfig, childConfig, additional) {
|
2017-07-01 04:44:41 +00:00
|
|
|
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])}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-07-02 05:50:46 +00:00
|
|
|
if (additional) {
|
|
|
|
Object.assign(config, additional);
|
|
|
|
}
|
2017-07-01 04:44:41 +00:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2017-07-02 07:15:33 +00:00
|
|
|
function filterConfig(inputConfig, targetPhase) {
|
|
|
|
logger.trace({ config: inputConfig }, `filterConfig('${targetPhase}')`);
|
2017-06-27 11:44:03 +00:00
|
|
|
const outputConfig = Object.assign({}, inputConfig);
|
2017-07-02 07:15:33 +00:00
|
|
|
const phases = [
|
|
|
|
'global',
|
|
|
|
'repository',
|
|
|
|
'packageFile',
|
|
|
|
'depType',
|
|
|
|
'package',
|
|
|
|
'branch',
|
|
|
|
'pr',
|
|
|
|
];
|
|
|
|
const targetIndex = phases.indexOf(targetPhase);
|
2017-06-26 11:08:57 +00:00
|
|
|
for (const option of definitions.getOptions()) {
|
2017-07-02 07:15:33 +00:00
|
|
|
const optionIndex = phases.indexOf(option.phase);
|
|
|
|
if (optionIndex !== -1 && optionIndex < targetIndex) {
|
2017-06-27 11:44:03 +00:00
|
|
|
delete outputConfig[option.name];
|
2017-06-26 11:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-27 11:44:03 +00:00
|
|
|
return outputConfig;
|
2017-01-18 13:14:07 +00:00
|
|
|
}
|
2017-06-29 13:38:18 +00:00
|
|
|
|
|
|
|
function getOnboardingConfig(repoConfig) {
|
|
|
|
const config = {};
|
|
|
|
for (const option of definitions.getOptions()) {
|
2017-07-02 07:15:33 +00:00
|
|
|
if (option.phase !== 'global' && option.onboarding !== false) {
|
2017-06-29 13:38:18 +00:00
|
|
|
config[option.name] = repoConfig[option.name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (repoConfig.detectedPackageFiles) {
|
|
|
|
config.packageFiles = [];
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|