diff --git a/lib/config/index.js b/lib/config/index.js index 3ef2348832384525d01f3d5fe466f2518611ac69..450aa58e82cc9b9d48b79739f5749f2692bcd582 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -1,5 +1,7 @@ const logger = require('winston'); const stringify = require('json-stringify-pretty-compact'); +const githubApi = require('../api/github'); +const gitlabApi = require('../api/gitlab'); const defaultsParser = require('./defaults'); const fileParser = require('./file'); @@ -36,6 +38,21 @@ function parseConfigs(env, argv) { // Get global config logger.debug(`raw config=${redact(config)}`); + // Check platforms and tokens + if (config.platform === 'github') { + if (!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}.`); + } + // We need at least one repository defined if (!config.repositories || config.repositories.length === 0) { throw new Error('At least one repository must be configured'); diff --git a/lib/worker.js b/lib/worker.js index 818d48748ba12fea0661162bce617d59c385da72..b63d39cddcf45bbe10839595917e9c85e4d68452 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -1,7 +1,5 @@ const logger = require('winston'); const stringify = require('json-stringify-pretty-compact'); -const githubApi = require('./api/github'); -const gitlabApi = require('./api/gitlab'); const handlebars = require('handlebars'); const versionsHelper = require('./helpers/versions'); const packageJson = require('./helpers/package-json'); @@ -26,14 +24,6 @@ async function processPackageFile(repoName, packageFile, packageConfig) { config = Object.assign({}, packageConfig); config.packageFile = packageFile; - // Set API - if (packageConfig.platform === 'github') { - config.api = githubApi; - } - if (packageConfig.platform === 'gitlab') { - config.api = gitlabApi; - } - logger.info(`Processing ${repoName} ${packageFile}`); const packageContent = await config.api.getFileJson(packageFile); diff --git a/test/config/index.spec.js b/test/config/index.spec.js index 722fbeb600e0ece873cff5a3e989f42cc29bb8f7..ff4c40cab0a0e1479868657486f29e3d661d8d2a 100644 --- a/test/config/index.spec.js +++ b/test/config/index.spec.js @@ -10,18 +10,57 @@ describe('config/index', () => { configParser = require('../../lib/config/index.js'); defaultArgv = argv(); }); - it('throws for no token', () => { + it('throws for invalid platform', () => { const env = {}; - configParser.parseConfigs.bind(configParser, env, defaultArgv).should.throw('At least one repository must be configured'); + defaultArgv.push('--platform=foo'); + let err; + try { + configParser.parseConfigs(env, defaultArgv); + } catch (e) { + err = e; + } + expect(err.message).toBe('Unsupported platform: foo.'); + }); + it('throws for no GitHub token', () => { + const env = {}; + let err; + try { + configParser.parseConfigs(env, defaultArgv); + } catch (e) { + err = e; + } + expect(err.message).toBe('You need to supply a GitHub token.'); + }); + it('throws for no GitLab token', () => { + const env = { RENOVATE_PLATFORM: 'gitlab' }; + let err; + try { + configParser.parseConfigs(env, defaultArgv); + } catch (e) { + err = e; + } + expect(err.message).toBe('You need to supply a GitLab token.'); }); it('supports token in env', () => { const env = { GITHUB_TOKEN: 'abc' }; - configParser.parseConfigs.bind(configParser, env, defaultArgv).should.throw('At least one repository must be configured'); + let err; + try { + configParser.parseConfigs(env, defaultArgv); + } catch (e) { + err = e; + } + expect(err.message).toBe('At least one repository must be configured'); }); it('supports token in CLI options', () => { - const env = {}; defaultArgv = defaultArgv.concat(['--token=abc']); - configParser.parseConfigs.bind(configParser, env, defaultArgv).should.throw('At least one repository must be configured'); + const env = {}; + let err; + try { + configParser.parseConfigs(env, defaultArgv); + } catch (e) { + err = e; + } + expect(err.message).toBe('At least one repository must be configured'); }); it('supports repositories in CLI', () => { const env = {};