2017-06-22 07:03:36 +00:00
|
|
|
const logger = require('../helpers/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-02 20:06:15 +00:00
|
|
|
const githubAppHelper = require('../helpers/github-app');
|
|
|
|
|
2017-01-20 13:03:18 +00:00
|
|
|
module.exports = {
|
|
|
|
parseConfigs,
|
2017-06-27 11:44:03 +00:00
|
|
|
filterConfig,
|
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');
|
|
|
|
}
|
|
|
|
config.repositories = await githubAppHelper.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-06-27 11:44:03 +00:00
|
|
|
function filterConfig(inputConfig, filterLevel) {
|
|
|
|
const outputConfig = Object.assign({}, inputConfig);
|
|
|
|
const levelScores = {
|
|
|
|
package: 10,
|
|
|
|
depType: 20,
|
|
|
|
packageFile: 30,
|
|
|
|
repository: 40,
|
|
|
|
global: 50,
|
|
|
|
};
|
|
|
|
const threshold = levelScores[filterLevel];
|
2017-06-26 11:08:57 +00:00
|
|
|
for (const option of definitions.getOptions()) {
|
2017-06-27 11:44:03 +00:00
|
|
|
const optionScore = levelScores[option.level] || 0;
|
|
|
|
if (optionScore > threshold) {
|
|
|
|
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
|
|
|
}
|