From 068ff16ec41f76a37a7f245e53bc0986a58ede52 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Mon, 2 Jul 2018 13:18:37 +0200 Subject: [PATCH] fix: Revert "feat: endpoint credentials handling (#2146)" This reverts commit 04e0ca1f80bd40e46a69e4fa954a997e4877824e. --- lib/config/env.js | 32 +------ lib/config/index.js | 44 ++++------ lib/datasource/github.js | 20 +++++ lib/datasource/npm.js | 13 +-- lib/platform/github/gh-got-wrapper.js | 12 +-- lib/platform/github/index.js | 28 +++--- lib/platform/gitlab/gl-got-wrapper.js | 13 +-- lib/platform/gitlab/index.js | 22 +++-- lib/platform/vsts/index.js | 7 +- lib/platform/vsts/vsts-got-wrapper.js | 15 ++-- lib/platform/vsts/vsts-helper.js | 21 +++++ lib/util/endpoints.js | 85 ------------------- lib/workers/global/index.js | 7 -- lib/workers/pr/changelog/index.js | 35 +++++++- lib/workers/pr/changelog/source-github.js | 30 +++---- test/config/__snapshots__/env.spec.js.snap | 71 ---------------- test/config/env.spec.js | 45 ++-------- test/config/index.spec.js | 1 - test/platform/github/index.spec.js | 6 ++ test/platform/gitlab/index.spec.js | 9 +- test/platform/vsts/index.spec.js | 20 +---- test/platform/vsts/vsts-got-wrapper.spec.js | 38 ++++++--- test/platform/vsts/vsts-helper.spec.js | 44 ++++++++++ .../util/__snapshots__/endpoints.spec.js.snap | 43 ---------- test/util/endpoints.spec.js | 62 -------------- test/workers/pr/changelog.spec.js | 37 ++++---- test/workers/repository/init/apis.spec.js | 2 +- 27 files changed, 275 insertions(+), 487 deletions(-) delete mode 100644 lib/util/endpoints.js delete mode 100644 test/config/__snapshots__/env.spec.js.snap delete mode 100644 test/util/__snapshots__/endpoints.spec.js.snap delete mode 100644 test/util/endpoints.spec.js diff --git a/lib/config/env.js b/lib/config/env.js index c298e080f5..68e0dea667 100644 --- a/lib/config/env.js +++ b/lib/config/env.js @@ -19,7 +19,7 @@ function getEnvName(option) { function getConfig(env) { const options = configDefinitions.getOptions(); - const config = { endpoints: [] }; + const config = {}; const coersions = { boolean: val => val === 'true', @@ -39,35 +39,5 @@ function getConfig(env) { } }); - if (env.GITHUB_COM_TOKEN) { - config.endpoints.push({ - platform: 'github', - token: env.GITHUB_COM_TOKEN, - }); - } - if (env.GITHUB_TOKEN) { - config.endpoints.push({ - platform: 'github', - endpoint: env.GITHUB_ENDPOINT, - token: env.GITHUB_TOKEN, - }); - } - - if (env.GITLAB_TOKEN) { - config.endpoints.push({ - platform: 'gitlab', - endpoint: env.GITLAB_ENDPOINT, - token: env.GITLAB_TOKEN, - }); - } - - if (env.VSTS_ENDPOINT || env.VSTS_TOKEN) { - config.endpoints.push({ - platform: 'vsts', - endpoint: env.VSTS_ENDPOINT, - token: env.GITLAB_TOKEN, - }); - } - return config; } diff --git a/lib/config/index.js b/lib/config/index.js index f1c23508bf..8dca350bdd 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -12,8 +12,6 @@ const { getPlatformApi } = require('../platform'); const { resolveConfigPresets } = require('./presets'); const { get, getLanguageList, getManagerList } = require('../manager'); -const endpoints = require('../util/endpoints'); - exports.parseConfigs = parseConfigs; exports.mergeChildConfig = mergeChildConfig; exports.filterConfig = filterConfig; @@ -80,39 +78,27 @@ async function parseConfigs(env, argv) { logger.trace({ config }, 'Raw config'); // Check platforms and tokens - const { platform, endpoint, token } = config; - const platformInfo = endpoints.defaults[platform]; - if (!platformInfo) { - throw new Error(`Unsupported platform: ${config.platform}.`); - } - config.endpoints.forEach(endpoints.update); - delete config.endpoints; - delete config.token; - - const credentials = endpoints.find( - { platform }, - { - platform, - endpoint: endpoint || platformInfo.endpoint, - token, + if (config.platform === 'github') { + if (!config.token && !env.GITHUB_TOKEN) { + throw new Error('You need to supply a GitHub token.'); } - ); - - // we don't need to check endpoint, endpoints.update({}) will do that - if (!credentials.token) { - throw new Error(`You need to supply a ${platformInfo.name} token.`); + } else if (config.platform === 'gitlab') { + if (!config.token && !env.GITLAB_TOKEN) { + throw new Error('You need to supply a GitLab token.'); + } + } else if (config.platform === 'vsts') { + if (!config.token && !env.VSTS_TOKEN) { + throw new Error('You need to supply a VSTS token.'); + } + } else { + throw new Error(`Unsupported platform: ${config.platform}.`); } - endpoints.update({ - ...credentials, - default: true, - }); - if (config.autodiscover) { // Autodiscover list of repositories const discovered = await getPlatformApi(config.platform).getRepos( - credentials.token, - credentials.endpoint + config.token, + config.endpoint ); if (!(discovered && discovered.length)) { // Soft fail (no error thrown) if no accessible repositories diff --git a/lib/datasource/github.js b/lib/datasource/github.js index 365470b61f..4418198c11 100644 --- a/lib/datasource/github.js +++ b/lib/datasource/github.js @@ -9,6 +9,19 @@ async function getDependency(purl, config) { const { versionScheme } = config || {}; const { fullname: repo, qualifiers: options } = purl; let versions; + let endpoint; + let token; + // istanbul ignore if + if ( + process.env.GITHUB_ENDPOINT && + !process.env.GITHUB_ENDPOINT.startsWith('https://api.github.com') + ) { + logger.debug('Removing GHE token before retrieving node releases'); + endpoint = process.env.GITHUB_ENDPOINT; + delete process.env.GITHUB_ENDPOINT; + token = process.env.GITHUB_TOKEN; + process.env.GITHUB_TOKEN = process.env.GITHUB_COM_TOKEN; + } try { if (options.ref === 'release') { const url = `repos/${repo}/releases?per_page=100`; @@ -25,6 +38,13 @@ async function getDependency(purl, config) { { repo, err, message: err.message }, 'Error retrieving from github' ); + } finally { + // istanbul ignore if + if (endpoint) { + logger.debug('Restoring GHE token and endpoint'); + process.env.GITHUB_TOKEN = token; + process.env.GITHUB_ENDPOINT = endpoint; + } } if (!versions) { return null; diff --git a/lib/datasource/npm.js b/lib/datasource/npm.js index 1a9161cc95..609351de2e 100644 --- a/lib/datasource/npm.js +++ b/lib/datasource/npm.js @@ -9,7 +9,6 @@ const registryAuthToken = require('registry-auth-token'); const parse = require('github-url-from-git'); const { isBase64 } = require('validator'); const { isVersion, sortVersions } = require('../versioning')('semver'); -const endpoints = require('../util/endpoints'); module.exports = { maskToken, @@ -163,10 +162,14 @@ async function getDependencyInner(name, retries = 5) { if (res.repository && res.repository.url) { const extraBaseUrls = []; - // istanbul ignore next - endpoints.hosts({ platform: 'github' }).forEach(host => { - extraBaseUrls.push(host, `gist.${host}`); - }); + // istanbul ignore if + if (process.env.GITHUB_ENDPOINT) { + const parsedEndpoint = url.parse(process.env.GITHUB_ENDPOINT); + extraBaseUrls.push( + parsedEndpoint.hostname, + `gist.${parsedEndpoint.hostname}` + ); + } // Massage www out of github URL res.repository.url = res.repository.url.replace( 'www.github.com', diff --git a/lib/platform/github/gh-got-wrapper.js b/lib/platform/github/gh-got-wrapper.js index b984aa7855..85a43f74b0 100644 --- a/lib/platform/github/gh-got-wrapper.js +++ b/lib/platform/github/gh-got-wrapper.js @@ -2,17 +2,13 @@ const URL = require('url'); const ghGot = require('gh-got'); const delay = require('delay'); const parseLinkHeader = require('parse-link-header'); -const endpoints = require('../../util/endpoints'); let cache = {}; -async function get(path, options, retries = 5) { - const { host } = URL.parse(path); - const opts = { - ...endpoints.find({ platform: 'github', host }), - ...options, - }; - const method = opts.method || 'get'; +async function get(path, opts, retries = 5) { + /* eslint-disable no-param-reassign */ + opts = Object.assign({}, opts); + const method = opts.method ? opts.method : 'get'; if (method === 'get' && cache[path]) { logger.trace({ path }, 'Returning cached result'); return cache[path]; diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index 75244296a6..e1b12ac81d 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -5,7 +5,6 @@ const moment = require('moment'); const openpgp = require('openpgp'); const delay = require('delay'); const path = require('path'); -const endpoints = require('../../util/endpoints'); let config = {}; @@ -55,11 +54,15 @@ module.exports = { // Get all repositories that the user has access to async function getRepos(token, endpoint) { logger.info('Autodiscovering GitHub repositories'); - const opts = endpoints.find({ platform: 'github' }, { token, endpoint }); - if (!opts.token) { + logger.debug('getRepos(token, endpoint)'); + if (token) { + process.env.GITHUB_TOKEN = token; + } else if (!process.env.GITHUB_TOKEN) { throw new Error('No token found for getRepos'); } - endpoints.update({ ...opts, platform: 'github', default: true }); + if (endpoint) { + process.env.GITHUB_ENDPOINT = endpoint; + } try { const res = await get('user/repos', { paginate: true }); return res.body.map(repo => repo.full_name); @@ -97,11 +100,16 @@ async function initRepo({ gitPrivateKey, }) { logger.debug(`initRepo("${repository}")`); - const opts = endpoints.find({ platform: 'github' }, { token, endpoint }); - if (!opts.token) { + if (token) { + logger.debug('Setting token in env for use by gh-got'); + process.env.GITHUB_TOKEN = token; + } else if (!process.env.GITHUB_TOKEN) { throw new Error(`No token found for GitHub repository ${repository}`); } - endpoints.update({ ...opts, platform: 'github', default: true }); + if (endpoint) { + logger.debug('Setting endpoint in env for use by gh-got'); + process.env.GITHUB_ENDPOINT = endpoint; + } logger.debug('Resetting platform config'); // config is used by the platform api itself, not necessary for the app layer to know cleanRepo(); @@ -200,11 +208,11 @@ async function initRepo({ config.repository = null; // Get list of existing repos const existingRepos = (await get('user/repos?per_page=100', { - token: forkToken || opts.token, + token: forkToken || process.env.GITHUB_TOKEN, paginate: true, })).body.map(r => r.full_name); config.repository = (await get.post(`repos/${repository}/forks`, { - token: forkToken || opts.token, + token: forkToken || process.env.GITHUB_TOKEN, })).body.full_name; if (existingRepos.includes(config.repository)) { logger.info( @@ -224,7 +232,7 @@ async function initRepo({ body: { sha: config.parentSha, }, - token: forkToken || opts.token, + token: forkToken || process.env.GITHUB_TOKEN, } ); } else { diff --git a/lib/platform/gitlab/gl-got-wrapper.js b/lib/platform/gitlab/gl-got-wrapper.js index d10d2226c6..e517af745b 100644 --- a/lib/platform/gitlab/gl-got-wrapper.js +++ b/lib/platform/gitlab/gl-got-wrapper.js @@ -1,24 +1,17 @@ -const URL = require('url'); const glGot = require('gl-got'); const parseLinkHeader = require('parse-link-header'); -const endpoints = require('../../util/endpoints'); let cache = {}; -async function get(path, options, retries = 5) { - const { host } = URL.parse(path); - const opts = { - ...endpoints.find({ platform: 'gitlab', host }), - ...options, - }; - const method = opts.method || 'get'; +async function get(path, opts, retries = 5) { + const method = opts && opts.method ? opts.method : 'get'; if (method === 'get' && cache[path]) { logger.debug({ path }, 'Returning cached result'); return cache[path]; } logger.debug({ path }, method.toUpperCase()); const res = await glGot(path, opts); - if (opts.paginate) { + if (opts && opts.paginate) { // Check if result is paginated try { const linkHeader = parseLinkHeader(res.headers.link); diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js index a8eda38938..9811dcab30 100644 --- a/lib/platform/gitlab/index.js +++ b/lib/platform/gitlab/index.js @@ -1,7 +1,6 @@ const is = require('@sindresorhus/is'); const get = require('./gl-got-wrapper'); const addrs = require('email-addresses'); -const endpoints = require('../../util/endpoints'); let config = {}; @@ -51,11 +50,14 @@ module.exports = { async function getRepos(token, endpoint) { logger.info('Autodiscovering GitLab repositories'); logger.debug('getRepos(token, endpoint)'); - const opts = endpoints.find({ platform: 'gitlab' }, { token, endpoint }); - if (!opts.token) { + if (token) { + process.env.GITLAB_TOKEN = token; + } else if (!process.env.GITLAB_TOKEN) { throw new Error('No token found for getRepos'); } - endpoints.update({ ...opts, platform: 'gitlab', default: true }); + if (endpoint) { + process.env.GITLAB_ENDPOINT = endpoint; + } try { const url = `projects?membership=true&per_page=100`; const res = await get(url, { paginate: true }); @@ -73,11 +75,17 @@ function urlEscape(str) { // Initialize GitLab by getting base branch async function initRepo({ repository, token, endpoint, gitAuthor }) { - const opts = endpoints.find({ platform: 'gitlab' }, { token, endpoint }); - if (!opts.token) { + if (token) { + process.env.GITLAB_TOKEN = token; + } else if (!process.env.GITLAB_TOKEN) { throw new Error(`No token found for GitLab repository ${repository}`); } - endpoints.update({ ...opts, platform: 'gitlab', default: true }); + if (token) { + process.env.GITLAB_TOKEN = token; + } + if (endpoint) { + process.env.GITLAB_ENDPOINT = endpoint; + } config = {}; get.reset(); config.repository = urlEscape(repository); diff --git a/lib/platform/vsts/index.js b/lib/platform/vsts/index.js index d0be6f6f7d..f16c69959c 100644 --- a/lib/platform/vsts/index.js +++ b/lib/platform/vsts/index.js @@ -1,7 +1,6 @@ // @ts-nocheck //because of logger, we can't ts-check const vstsHelper = require('./vsts-helper'); const vstsApi = require('./vsts-got-wrapper'); -const endpoints = require('../../util/endpoints'); const config = {}; @@ -51,8 +50,7 @@ module.exports = { async function getRepos(token, endpoint) { logger.info('Autodiscovering vsts repositories'); logger.debug('getRepos(token, endpoint)'); - const opts = endpoints.find({ platform: 'vsts' }, { token, endpoint }); - endpoints.update({ ...opts, platform: 'vsts', default: true }); + vstsHelper.setTokenAndEndpoint(token, endpoint); const vstsApiGit = await vstsApi.gitApi(); const repos = await vstsApiGit.getRepositories(); return repos.map(repo => `${repo.project.name}/${repo.name}`); @@ -60,8 +58,7 @@ async function getRepos(token, endpoint) { async function initRepo({ repository, token, endpoint }) { logger.debug(`initRepo("${repository}")`); - const opts = endpoints.find({ platform: 'vsts' }, { token, endpoint }); - endpoints.update({ ...opts, platform: 'vsts', default: true }); + vstsHelper.setTokenAndEndpoint(token, endpoint); config.repository = repository; config.fileList = null; config.prList = null; diff --git a/lib/platform/vsts/vsts-got-wrapper.js b/lib/platform/vsts/vsts-got-wrapper.js index bb49e3c5aa..0deb855996 100644 --- a/lib/platform/vsts/vsts-got-wrapper.js +++ b/lib/platform/vsts/vsts-got-wrapper.js @@ -1,5 +1,4 @@ const vsts = require('vso-node-api'); -const endpoints = require('../../util/endpoints'); module.exports = { vstsObj, @@ -8,12 +7,18 @@ module.exports = { }; function vstsObj() { - const config = endpoints.find({ platform: 'vsts' }, {}); - if (!config.token) { + if (!process.env.VSTS_TOKEN) { throw new Error(`No token found for vsts`); } - const authHandler = vsts.getPersonalAccessTokenHandler(config.token); - return new vsts.WebApi(config.endpoint, authHandler); + if (!process.env.VSTS_ENDPOINT) { + throw new Error( + `You need an endpoint with vsts. Something like this: https://{instance}.VisualStudio.com/{collection} (https://fabrikam.visualstudio.com/DefaultCollection)` + ); + } + const authHandler = vsts.getPersonalAccessTokenHandler( + process.env.VSTS_TOKEN + ); + return new vsts.WebApi(process.env.VSTS_ENDPOINT, authHandler); } function gitApi() { diff --git a/lib/platform/vsts/vsts-helper.js b/lib/platform/vsts/vsts-helper.js index 5d83064593..90f06992d9 100644 --- a/lib/platform/vsts/vsts-helper.js +++ b/lib/platform/vsts/vsts-helper.js @@ -3,6 +3,7 @@ const vstsApi = require('./vsts-got-wrapper'); module.exports = { + setTokenAndEndpoint, getBranchNameWithoutRefsheadsPrefix, getRefs, getVSTSBranchObj, @@ -15,6 +16,26 @@ module.exports = { getProjectAndRepo, }; +/** + * + * @param {string} token + * @param {string} endpoint + */ +function setTokenAndEndpoint(token, endpoint) { + if (token) { + process.env.VSTS_TOKEN = token; + } else if (!process.env.VSTS_TOKEN) { + throw new Error(`No token found for vsts`); + } + if (endpoint) { + process.env.VSTS_ENDPOINT = endpoint; + } else { + throw new Error( + `You need an endpoint with vsts. Something like this: https://{instance}.VisualStudio.com/{collection} (https://fabrikam.visualstudio.com/DefaultCollection)` + ); + } +} + /** * * @param {string} branchName diff --git a/lib/util/endpoints.js b/lib/util/endpoints.js deleted file mode 100644 index b70c731374..0000000000 --- a/lib/util/endpoints.js +++ /dev/null @@ -1,85 +0,0 @@ -const URL = require('url'); - -const defaults = { - github: { name: 'GitHub', endpoint: 'https://api.github.com/' }, - gitlab: { name: 'GitLab', endpoint: 'https://gitlab.com/api/v4/' }, - vsts: { name: 'VSTS' }, -}; - -module.exports = { - update, - find, - clear, - defaults, - hosts, -}; - -const platforms = {}; - -function update(params) { - const { platform } = params; - if (!platform) { - throw new Error('Failed to set configuration: no platform specified'); - } - const config = { ...defaults[platform], ...params }; - const { endpoint } = config; - if (!endpoint) { - throw new Error( - `Failed to configure platform '${platform}': no endpoint defined` - ); - } - let { host } = config; - // extract host from endpoint - host = host || (endpoint && URL.parse(endpoint).host); - // endpoint is in the format host/path (protocol missing) - host = host || (endpoint && URL.parse('http://' + endpoint).host); - if (!host) { - throw new Error( - `Failed to configure platform '${platform}': no host for endpoint '${endpoint}'` - ); - } - platforms[platform] = { ...platforms[platform] }; - if (config.default) { - for (const conf of Object.values(platforms[platform])) { - delete conf.default; - } - } - platforms[platform][host] = { ...platforms[platform][host], ...config }; - return true; -} - -function find({ platform, host }, overrides) { - if (!platforms[platform]) { - return merge(null, overrides); - } - if (host) { - return merge(platforms[platform][host], overrides); - } - const configs = Object.values(platforms[platform]); - let config = configs.find(c => c.default); - if (!config && configs.length === 1) { - [config] = configs; - } - return merge(config, overrides); -} - -function hosts({ platform }) { - return Object.keys({ ...platforms[platform] }); -} - -function merge(config, overrides) { - if (!overrides) { - return config || null; - } - const locals = { ...overrides }; - Object.keys(locals).forEach(key => { - if (locals[key] === undefined || locals[key] === null) { - delete locals[key]; - } - }); - return { ...config, ...locals }; -} - -function clear() { - Object.keys(platforms).forEach(key => delete platforms[key]); -} diff --git a/lib/workers/global/index.js b/lib/workers/global/index.js index 86eb4d9d03..8c5eef62ac 100644 --- a/lib/workers/global/index.js +++ b/lib/workers/global/index.js @@ -12,13 +12,6 @@ async function start() { initLogger(); try { const config = await configParser.parseConfigs(process.env, process.argv); - delete process.env.GITHUB_TOKEN; - delete process.env.GITHUB_ENDPOINT; - delete process.env.GITHUB_COM_TOKEN; - delete process.env.GITLAB_TOKEN; - delete process.env.GITLAB_ENDPOINT; - delete process.env.VSTS_TOKEN; - delete process.env.VSTS_ENDPOINT; if (config.repositories.length === 0) { logger.warn( 'No repositories found - did you want to run with flag --autodiscover?' diff --git a/lib/workers/pr/changelog/index.js b/lib/workers/pr/changelog/index.js index 11a6953ef6..4815ac7b51 100644 --- a/lib/workers/pr/changelog/index.js +++ b/lib/workers/pr/changelog/index.js @@ -1,3 +1,5 @@ +const url = require('url'); + const versioning = require('../../../versioning'); const { addReleaseNotes } = require('../release-notes'); @@ -25,9 +27,32 @@ async function getChangeLogJSON(args) { logger.debug('Returning cached changelog'); return cachedResult; } + let token; + let endpoint; + let gheBaseURL; + let githubBaseURL = 'https://github.com/'; + if ( + process.env.GITHUB_ENDPOINT && + !process.env.GITHUB_ENDPOINT.startsWith('https://api.github.com') + ) { + const parsedEndpoint = url.parse(process.env.GITHUB_ENDPOINT); + gheBaseURL = `${parsedEndpoint.protocol}//${parsedEndpoint.hostname}/`; + if (repositoryUrl.startsWith(gheBaseURL)) { + githubBaseURL = gheBaseURL; + } else { + // Switch tokens + token = process.env.GITHUB_TOKEN; + endpoint = process.env.GITHUB_ENDPOINT; + delete process.env.GITHUB_ENDPOINT; + process.env.GITHUB_TOKEN = process.env.GITHUB_COM_TOKEN; + } + } try { - const res = await sourceGithub.getChangeLogJSON({ ...args }); + const res = await sourceGithub.getChangeLogJSON({ + ...args, + githubBaseURL, + }); const output = await addReleaseNotes(res); await sourceCache.setChangeLogJSON(args, output); return output; @@ -37,5 +62,13 @@ async function getChangeLogJSON(args) { 'getChangeLogJSON error' ); return null; + } finally { + // wrap everything in a try/finally to ensure process.env.GITHUB_TOKEN is restored no matter if + // getChangeLogJSON and addReleaseNotes succed or fails + if (token) { + logger.debug('Restoring GHE token and endpoint'); + process.env.GITHUB_TOKEN = token; + process.env.GITHUB_ENDPOINT = endpoint; + } } } diff --git a/lib/workers/pr/changelog/source-github.js b/lib/workers/pr/changelog/source-github.js index 6bf4ec9334..afdddf38c0 100644 --- a/lib/workers/pr/changelog/source-github.js +++ b/lib/workers/pr/changelog/source-github.js @@ -1,5 +1,3 @@ -const URL = require('url'); -const endpoints = require('../../../util/endpoints'); const versioning = require('../../../versioning'); const ghGot = require('../../../platform/github/gh-got-wrapper'); @@ -7,11 +5,10 @@ module.exports = { getChangeLogJSON, }; -async function getTags(endpoint, versionScheme, repository) { +async function getTags(versionScheme, repository) { const { isVersion } = versioning(versionScheme); try { const res = await ghGot(`repos/${repository}/tags?per_page=100`, { - endpoint, paginate: true, }); @@ -38,15 +35,13 @@ async function getTags(endpoint, versionScheme, repository) { } } -async function getDateRef(endpoint, repository, timestamp) { +async function getDateRef(repository, timestamp) { if (!timestamp) { return null; } logger.trace({ repository, timestamp }, 'Looking for commit SHA by date'); try { - const res = await ghGot(`repos/${repository}/commits/@{${timestamp}}`, { - endpoint, - }); + const res = await ghGot(`repos/${repository}/commits/@{${timestamp}}`); const commit = res && res.body; return commit && commit.sha; } catch (err) { @@ -57,6 +52,7 @@ async function getDateRef(endpoint, repository, timestamp) { async function getChangeLogJSON({ versionScheme, + githubBaseURL, fromVersion, toVersion, repositoryUrl, @@ -65,17 +61,13 @@ async function getChangeLogJSON({ const { isVersion, equals, isGreaterThan, sortVersions } = versioning( versionScheme ); - const { protocol, host, pathname } = URL.parse(repositoryUrl); - const githubBaseURL = `${protocol}//${host}/`; - const config = endpoints.find({ - platform: 'github', - host: host === 'github.com' ? 'api.github.com' : host, - }); - if (!config) { - logger.debug('Repository URL does not match any hnown hosts'); + if (!(repositoryUrl && repositoryUrl.startsWith(githubBaseURL))) { + logger.debug('Repository URL does not match base URL'); return null; } - const repository = pathname.slice(1); + const repository = repositoryUrl + .replace(githubBaseURL, '') + .replace(/#.*/, ''); if (repository.split('/').length !== 2) { logger.info('Invalid github URL found'); return null; @@ -94,7 +86,7 @@ async function getChangeLogJSON({ return null; } - const tags = await getTags(config.endpoint, versionScheme, repository); + const tags = await getTags(versionScheme, repository); function getRef(release) { const tagName = tags.find(tag => equals(tag, release.version)); @@ -104,7 +96,7 @@ async function getChangeLogJSON({ if (release.gitRef) { return release.gitRef; } - return getDateRef(config.endpoint, repository, release.releaseTimestamp); + return getDateRef(repository, release.releaseTimestamp); } const changelogReleases = []; diff --git a/test/config/__snapshots__/env.spec.js.snap b/test/config/__snapshots__/env.spec.js.snap deleted file mode 100644 index 4a9aa46987..0000000000 --- a/test/config/__snapshots__/env.spec.js.snap +++ /dev/null @@ -1,71 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`config/env .getConfig(env) supports GitHub custom endpoint 1`] = ` -Object { - "endpoints": Array [], -} -`; - -exports[`config/env .getConfig(env) supports GitHub custom endpoint and github.com 1`] = ` -Object { - "endpoints": Array [ - Object { - "platform": "github", - "token": "public", - }, - Object { - "endpoint": "endpoint", - "platform": "github", - "token": "token", - }, - ], -} -`; - -exports[`config/env .getConfig(env) supports GitHub token 1`] = ` -Object { - "endpoints": Array [ - Object { - "endpoint": undefined, - "platform": "github", - "token": "token", - }, - ], -} -`; - -exports[`config/env .getConfig(env) supports GitLab custom endpoint 1`] = ` -Object { - "endpoints": Array [ - Object { - "endpoint": "endpoint", - "platform": "gitlab", - "token": "token", - }, - ], -} -`; - -exports[`config/env .getConfig(env) supports GitLab token 1`] = ` -Object { - "endpoints": Array [ - Object { - "endpoint": undefined, - "platform": "gitlab", - "token": "token", - }, - ], -} -`; - -exports[`config/env .getConfig(env) supports VSTS 1`] = ` -Object { - "endpoints": Array [ - Object { - "endpoint": "endpoint", - "platform": "vsts", - "token": undefined, - }, - ], -} -`; diff --git a/test/config/env.spec.js b/test/config/env.spec.js index c68cfac495..31e04ea905 100644 --- a/test/config/env.spec.js +++ b/test/config/env.spec.js @@ -3,65 +3,36 @@ const env = require('../../lib/config/env.js'); describe('config/env', () => { describe('.getConfig(env)', () => { it('returns empty env', () => { - expect(env.getConfig({})).toEqual({ endpoints: [] }); + env.getConfig({}).should.eql({}); }); it('supports boolean true', () => { const envParam = { RENOVATE_RECREATE_CLOSED: 'true' }; - expect(env.getConfig(envParam).recreateClosed).toBe(true); + env.getConfig(envParam).should.eql({ recreateClosed: true }); }); it('supports boolean false', () => { const envParam = { RENOVATE_RECREATE_CLOSED: 'false' }; - expect(env.getConfig(envParam).recreateClosed).toBe(false); + env.getConfig(envParam).should.eql({ recreateClosed: false }); }); it('supports boolean nonsense as false', () => { const envParam = { RENOVATE_RECREATE_CLOSED: 'foo' }; - expect(env.getConfig(envParam).recreateClosed).toBe(false); + env.getConfig(envParam).should.eql({ recreateClosed: false }); }); delete process.env.RENOVATE_RECREATE_CLOSED; it('supports list single', () => { const envParam = { RENOVATE_LABELS: 'a' }; - expect(env.getConfig(envParam).labels).toEqual(['a']); + env.getConfig(envParam).should.eql({ labels: ['a'] }); }); it('supports list multiple', () => { const envParam = { RENOVATE_LABELS: 'a,b,c' }; - expect(env.getConfig(envParam).labels).toEqual(['a', 'b', 'c']); + env.getConfig(envParam).should.eql({ labels: ['a', 'b', 'c'] }); }); it('supports string', () => { const envParam = { RENOVATE_TOKEN: 'a' }; - expect(env.getConfig(envParam).token).toBe('a'); + env.getConfig(envParam).should.eql({ token: 'a' }); }); it('supports json', () => { const envParam = { RENOVATE_LOCK_FILE_MAINTENANCE: '{}' }; - expect(env.getConfig(envParam).lockFileMaintenance).toEqual({}); - }); - it('supports GitHub token', () => { - const envParam = { GITHUB_TOKEN: 'token' }; - expect(env.getConfig(envParam)).toMatchSnapshot(); - }); - it('supports GitHub custom endpoint', () => { - const envParam = { GITHUB_ENDPOINT: 'endpoint' }; - expect(env.getConfig(envParam)).toMatchSnapshot(); - }); - - it('supports GitHub custom endpoint and github.com', () => { - const envParam = { - GITHUB_COM_TOKEN: 'public', - GITHUB_ENDPOINT: 'endpoint', - GITHUB_TOKEN: 'token', - }; - expect(env.getConfig(envParam)).toMatchSnapshot(); - }); - it('supports GitLab token', () => { - const envParam = { GITLAB_TOKEN: 'token' }; - expect(env.getConfig(envParam)).toMatchSnapshot(); - }); - it('supports GitLab custom endpoint', () => { - const envParam = { GITLAB_TOKEN: 'token', GITLAB_ENDPOINT: 'endpoint' }; - expect(env.getConfig(envParam)).toMatchSnapshot(); - }); - it('supports VSTS', () => { - const envParam = { VSTS_TOKEN: 'token', VSTS_ENDPOINT: 'endpoint' }; - expect(env.getConfig(envParam)).toMatchSnapshot(); + expect(env.getConfig(envParam)).toEqual({ lockFileMaintenance: {} }); }); }); describe('.getEnvName(definition)', () => { diff --git a/test/config/index.spec.js b/test/config/index.spec.js index 24b2d7b560..1127dcc489 100644 --- a/test/config/index.spec.js +++ b/test/config/index.spec.js @@ -134,7 +134,6 @@ describe('config/index', () => { defaultArgv = defaultArgv.concat([ '--autodiscover', '--platform=vsts', - '--endpoint=endpoint', '--token=abc', ]); vstsHelper.getFile.mockImplementationOnce(() => `Hello Renovate!`); diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js index 6beecd9ef1..888442d477 100644 --- a/test/platform/github/index.spec.js +++ b/test/platform/github/index.spec.js @@ -2,6 +2,10 @@ describe('platform/github', () => { let github; let get; beforeEach(() => { + // clean up env + delete process.env.GITHUB_TOKEN; + delete process.env.GITHUB_ENDPOINT; + // reset module jest.resetModules(); jest.mock('delay'); @@ -99,6 +103,8 @@ describe('platform/github', () => { }); expect(get.mock.calls).toMatchSnapshot(); expect(config).toMatchSnapshot(); + expect(process.env.GITHUB_TOKEN).toBe(token); + expect(process.env.GITHUB_ENDPOINT).toBe(endpoint); }); }); it('should throw an error if no token is provided', async () => { diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js index 8b255dae51..55aa5adb51 100644 --- a/test/platform/gitlab/index.spec.js +++ b/test/platform/gitlab/index.spec.js @@ -1,11 +1,10 @@ -const endpoints = require('../../../lib/util/endpoints'); - describe('platform/gitlab', () => { let gitlab; let get; beforeEach(() => { - // clean up endpoints - endpoints.clear(); + // clean up env + delete process.env.GITLAB_TOKEN; + delete process.env.GITLAB_ENDPOINT; // reset module jest.resetModules(); @@ -112,6 +111,8 @@ describe('platform/gitlab', () => { }); expect(get.mock.calls).toMatchSnapshot(); expect(config).toMatchSnapshot(); + expect(process.env.GITLAB_TOKEN).toBe(token); + expect(process.env.GITLAB_ENDPOINT).toBe(endpoint); }); }); it(`should escape all forward slashes in project names`, async () => { diff --git a/test/platform/vsts/index.spec.js b/test/platform/vsts/index.spec.js index 681c9acddb..42bae97619 100644 --- a/test/platform/vsts/index.spec.js +++ b/test/platform/vsts/index.spec.js @@ -1,12 +1,11 @@ -const endpoints = require('../../../lib/util/endpoints'); - describe('platform/vsts', () => { let vsts; let vstsApi; let vstsHelper; beforeEach(() => { - // clean up endpoints - endpoints.clear(); + // clean up env + delete process.env.VSTS_TOKEN; + delete process.env.VSTS_ENDPOINT; // reset module jest.resetModules(); @@ -81,18 +80,7 @@ describe('platform/vsts', () => { repo: 'some-repo', })); - if (typeof args[0] === 'string') { - return vsts.initRepo({ - repository: args[0], - token: args[1], - endpoint: 'https://my.custom.endpoint/', - }); - } - - return vsts.initRepo({ - endpoint: 'https://my.custom.endpoint/', - ...args[0], - }); + return vsts.initRepo(...args); } describe('initRepo', () => { diff --git a/test/platform/vsts/vsts-got-wrapper.spec.js b/test/platform/vsts/vsts-got-wrapper.spec.js index f8d4abbf20..5237465d55 100644 --- a/test/platform/vsts/vsts-got-wrapper.spec.js +++ b/test/platform/vsts/vsts-got-wrapper.spec.js @@ -1,28 +1,46 @@ describe('platform/vsts/vsts-got-wrapper', () => { - let endpoints; let vsts; beforeEach(() => { + // clean up env + delete process.env.VSTS_TOKEN; + delete process.env.VSTS_ENDPOINT; + // reset module jest.resetModules(); - endpoints = require('../../../lib/util/endpoints'); vsts = require('../../../lib/platform/vsts/vsts-got-wrapper'); }); describe('gitApi', () => { - it('should throw an error if no token is provided', () => { - expect(vsts.gitApi).toThrow('No token found for vsts'); - expect(vsts.getCoreApi).toThrow('No token found for vsts'); + it('should throw an error if no token is provided', async () => { + let err; + try { + await vsts.gitApi(); + } catch (e) { + err = e; + } + expect(err.message).toBe('No token found for vsts'); + }); + it('should throw an error if no endpoint is provided', async () => { + let err; + try { + process.env.VSTS_TOKEN = 'myToken'; + await vsts.getCoreApi(); + } catch (e) { + err = e; + } + expect(err.message).toBe( + `You need an endpoint with vsts. Something like this: https://{instance}.VisualStudio.com/{collection} (https://fabrikam.visualstudio.com/DefaultCollection)` + ); }); it('should set token and endpoint', async () => { - endpoints.update({ - platform: 'vsts', - token: 'myToken', - endpoint: 'myEndpoint', - }); + process.env.VSTS_TOKEN = 'myToken'; + process.env.VSTS_ENDPOINT = 'myEndpoint'; const res = await vsts.vstsObj(); // We will track if the lib vso-node-api change expect(res).toMatchSnapshot(); + expect(process.env.VSTS_TOKEN).toBe(`myToken`); + expect(process.env.VSTS_ENDPOINT).toBe(`myEndpoint`); }); }); }); diff --git a/test/platform/vsts/vsts-helper.spec.js b/test/platform/vsts/vsts-helper.spec.js index 1aa6848db1..f11d28ea3f 100644 --- a/test/platform/vsts/vsts-helper.spec.js +++ b/test/platform/vsts/vsts-helper.spec.js @@ -5,6 +5,10 @@ describe('platform/vsts/helpers', () => { let vstsApi; beforeEach(() => { + // clean up env + delete process.env.VSTS_TOKEN; + delete process.env.VSTS_ENDPOINT; + // reset module jest.resetModules(); jest.mock('../../../lib/platform/vsts/vsts-got-wrapper'); @@ -12,6 +16,46 @@ describe('platform/vsts/helpers', () => { vstsApi = require('../../../lib/platform/vsts/vsts-got-wrapper'); }); + describe('getRepos', () => { + it('should throw an error if no token is provided', async () => { + let err; + try { + await vstsHelper.setTokenAndEndpoint(); + } catch (e) { + err = e; + } + expect(err.message).toBe('No token found for vsts'); + }); + it('should throw an error if no endpoint provided (with env variable on token)', async () => { + let err; + process.env.VSTS_TOKEN = 'token123'; + try { + await vstsHelper.setTokenAndEndpoint(); + } catch (e) { + err = e; + } + expect(err.message).toBe( + 'You need an endpoint with vsts. Something like this: https://{instance}.VisualStudio.com/{collection} (https://fabrikam.visualstudio.com/DefaultCollection)' + ); + }); + it('should throw an error if no endpoint is provided', async () => { + let err; + try { + await vstsHelper.setTokenAndEndpoint('myToken'); + } catch (e) { + err = e; + } + expect(err.message).toBe( + `You need an endpoint with vsts. Something like this: https://{instance}.VisualStudio.com/{collection} (https://fabrikam.visualstudio.com/DefaultCollection)` + ); + }); + it('should set token and endpoint', async () => { + await vstsHelper.setTokenAndEndpoint('myToken', 'myEndpoint'); + expect(process.env.VSTS_TOKEN).toBe(`myToken`); + expect(process.env.VSTS_ENDPOINT).toBe(`myEndpoint`); + }); + }); + describe('getNewBranchName', () => { it('should add refs/heads', () => { const res = vstsHelper.getNewBranchName('testBB'); diff --git a/test/util/__snapshots__/endpoints.spec.js.snap b/test/util/__snapshots__/endpoints.spec.js.snap deleted file mode 100644 index 96c7b38024..0000000000 --- a/test/util/__snapshots__/endpoints.spec.js.snap +++ /dev/null @@ -1,43 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`util/endpoints find() allows overrides 1`] = ` -Object { - "endpoint": "endpoint", - "name": "GitHub", - "other": "data", - "platform": "github", - "token": "secret", -} -`; - -exports[`util/endpoints find() allows overrides 2`] = ` -Object { - "token": "secret", -} -`; - -exports[`util/endpoints find() allows overrides 3`] = ` -Object { - "token": "secret", -} -`; - -exports[`util/endpoints update() uses default endpoint 1`] = ` -Object { - "endpoint": "https://api.github.com/", - "name": "GitHub", - "other": "data", - "platform": "github", - "token": "token", -} -`; - -exports[`util/endpoints update() uses default endpoint 2`] = ` -Object { - "endpoint": "https://api.github.com/", - "name": "GitHub", - "other": "data", - "platform": "github", - "token": "token", -} -`; diff --git a/test/util/endpoints.spec.js b/test/util/endpoints.spec.js deleted file mode 100644 index d7c4913e15..0000000000 --- a/test/util/endpoints.spec.js +++ /dev/null @@ -1,62 +0,0 @@ -const { update, find, clear } = require('../../lib/util/endpoints'); - -describe('util/endpoints', () => { - beforeEach(() => { - clear(); - }); - describe('update()', () => { - it('throws if no platform ', () => { - expect(() => update({})).toThrow( - 'Failed to set configuration: no platform specified' - ); - }); - it('throws if no endpoint ', () => { - expect(() => update({ platform: 'vsts' })).toThrow( - `Failed to configure platform 'vsts': no endpoint defined` - ); - }); - - it('throws if invalid endpoint ', () => { - expect(() => - update({ platform: 'vsts', endpoint: '/some/path' }) - ).toThrow( - `Failed to configure platform 'vsts': no host for endpoint '/some/path'` - ); - }); - - it('uses default endpoint', () => { - update({ - platform: 'github', - token: 'token', - other: 'data', - }); - expect(find({ platform: 'github' })).toMatchSnapshot(); - expect( - find({ platform: 'github', host: 'api.github.com' }) - ).toMatchSnapshot(); - expect(find({ platform: 'github', host: 'example.com' })).toBe(null); - }); - }); - describe('find()', () => { - it('allows overrides', () => { - update({ - platform: 'github', - endpoint: 'endpoint', - token: 'token', - other: 'data', - }); - const overrides = { - token: 'secret', - other: null, - foo: undefined, - }; - expect(find({ platform: 'github' }, overrides)).toMatchSnapshot(); - expect( - find({ platform: 'github', host: 'api.github.com' }, overrides) - ).toMatchSnapshot(); - expect( - find({ platform: 'github', host: 'example.com' }, overrides) - ).toMatchSnapshot(); - }); - }); -}); diff --git a/test/workers/pr/changelog.spec.js b/test/workers/pr/changelog.spec.js index 257181a379..07f1152e03 100644 --- a/test/workers/pr/changelog.spec.js +++ b/test/workers/pr/changelog.spec.js @@ -2,7 +2,6 @@ jest.mock('../../../lib/platform/github/gh-got-wrapper'); jest.mock('../../../lib/datasource/npm'); jest.mock('got'); -const endpoints = require('../../../lib/util/endpoints'); const ghGot = require('../../../lib/platform/github/gh-got-wrapper'); const { getChangeLogJSON } = require('../../../lib/workers/pr/changelog'); @@ -34,11 +33,7 @@ describe('workers/pr/changelog', () => { describe('getChangeLogJSON', () => { beforeEach(async () => { ghGot.mockClear(); - endpoints.clear(); - endpoints.update({ - platform: 'github', - endpoint: 'https://api.github.com/', - }); + await rmAllCache(); }); it('returns null if no fromVersion', async () => { @@ -171,36 +166,37 @@ describe('workers/pr/changelog', () => { ).toBe(null); }); it('supports github enterprise and github.com changelog', async () => { - endpoints.update({ - platform: 'github', - token: 'super_secret', - endpoint: 'https://github-enterprise.example.com/', - }); + const token = process.env.GITHUB_TOKEN; + const endpoint = process.env.GITHUB_ENDPOINT; + process.env.GITHUB_TOKEN = 'super_secret'; + process.env.GITHUB_ENDPOINT = 'https://github-enterprise.example.com/'; + const oldenv = { ...process.env }; expect( await getChangeLogJSON({ ...upgrade, }) ).toMatchSnapshot(); + // check that process env was restored + expect(process.env).toEqual(oldenv); + process.env.GITHUB_TOKEN = token; + process.env.GITHUB_ENDPOINT = endpoint; }); it('supports github enterprise and github enterprise changelog', async () => { - endpoints.update({ - platform: 'github', - endpoint: 'https://github-enterprise.example.com/', - }); - process.env.GITHUB_ENDPOINT = ''; + const endpoint = process.env.GITHUB_ENDPOINT; + process.env.GITHUB_ENDPOINT = 'https://github-enterprise.example.com/'; expect( await getChangeLogJSON({ ...upgrade, repositoryUrl: 'https://github-enterprise.example.com/chalk/chalk', }) ).toMatchSnapshot(); + + process.env.GITHUB_ENDPOINT = endpoint; }); it('supports github enterprise alwo when retrieving data from cache', async () => { - endpoints.update({ - platform: 'github', - endpoint: 'https://github-enterprise.example.com/', - }); + const endpoint = process.env.GITHUB_ENDPOINT; + process.env.GITHUB_ENDPOINT = 'https://github-enterprise.example.com/'; expect( await getChangeLogJSON({ ...upgrade, @@ -214,6 +210,7 @@ describe('workers/pr/changelog', () => { repositoryUrl: 'https://github-enterprise.example.com/chalk/chalk', }) ).toMatchSnapshot(); + process.env.GITHUB_ENDPOINT = endpoint; }); }); }); diff --git a/test/workers/repository/init/apis.spec.js b/test/workers/repository/init/apis.spec.js index 4857b03f42..7624bafe5e 100644 --- a/test/workers/repository/init/apis.spec.js +++ b/test/workers/repository/init/apis.spec.js @@ -33,7 +33,7 @@ describe('workers/repository/init/apis', () => { await initApis(config); } catch (error) { expect(error.message).toBe( - `Failed to configure platform 'vsts': no endpoint defined` + 'You need an endpoint with vsts. Something like this: https://{instance}.VisualStudio.com/{collection} (https://fabrikam.visualstudio.com/DefaultCollection)' ); } }); -- GitLab