2017-06-20 15:57:04 +00:00
|
|
|
const logger = require('../logger');
|
2017-01-18 13:14:07 +00:00
|
|
|
const stringify = require('json-stringify-pretty-compact');
|
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-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-15 15:12:56 +00:00
|
|
|
let config = null;
|
2017-01-14 13:39:26 +00:00
|
|
|
|
2017-01-20 13:03:18 +00:00
|
|
|
module.exports = {
|
|
|
|
parseConfigs,
|
|
|
|
getCascadedConfig,
|
2017-01-22 13:40:14 +00:00
|
|
|
getRepositories,
|
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-02-07 20:45:35 +00:00
|
|
|
config = Object.assign({}, defaultConfig, fileConfig, envConfig, cliConfig);
|
|
|
|
|
|
|
|
// 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-01-18 13:14:07 +00:00
|
|
|
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-18 00:48:17 +00:00
|
|
|
|
2017-01-15 15:12:56 +00:00
|
|
|
// Get global config
|
2017-01-18 13:14:07 +00:00
|
|
|
logger.debug(`raw config=${redact(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-02 20:06:15 +00:00
|
|
|
delete config.githubAppKey;
|
|
|
|
logger.debug(`GitHub App config: ${JSON.stringify(config)}`);
|
|
|
|
} 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-04-21 05:00:26 +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
|
|
|
}
|
2017-02-07 20:45:35 +00:00
|
|
|
|
|
|
|
// Configure each repository
|
2017-04-21 08:12:41 +00:00
|
|
|
config.repositories = config.repositories.map(item => {
|
2017-02-07 20:45:35 +00:00
|
|
|
// 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) {
|
2017-02-07 20:45:35 +00:00
|
|
|
repo.packageFiles = config.packageFiles;
|
2017-01-15 15:12:56 +00:00
|
|
|
}
|
2017-02-07 20:45:35 +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;
|
|
|
|
});
|
2017-02-07 20:45:35 +00:00
|
|
|
|
|
|
|
return repo;
|
2017-01-15 15:12:56 +00:00
|
|
|
});
|
2017-02-07 20:45:35 +00:00
|
|
|
|
2017-01-15 15:12:56 +00:00
|
|
|
// Print config
|
2017-06-20 06:02:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-18 13:14:07 +00:00
|
|
|
function redact(inputConfig) {
|
2017-06-02 20:06:15 +00:00
|
|
|
const redactedConfig = Object.assign({}, inputConfig);
|
|
|
|
if (redactedConfig.token) {
|
2017-06-02 20:40:00 +00:00
|
|
|
redactedConfig.token = `${redactedConfig.token.substr(0, 4)}${new Array(
|
|
|
|
redactedConfig.token.length - 3
|
|
|
|
).join('*')}`;
|
2017-06-02 20:06:15 +00:00
|
|
|
}
|
|
|
|
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);
|
2017-06-02 20:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 13:14:07 +00:00
|
|
|
}
|
|
|
|
return stringify(redactedConfig);
|
|
|
|
}
|