diff --git a/lib/datasource/docker/index.js b/lib/datasource/docker/index.js index e5ccc6acc6cae271b9b6ff7eb82e4052131969df..810b80937f9c00cbc67ed048eb80f1d96438f8cc 100644 --- a/lib/datasource/docker/index.js +++ b/lib/datasource/docker/index.js @@ -51,7 +51,8 @@ async function getAuthHeaders(registry, repository) { ); const { host } = URL.parse(registry); - const opts = hostRules.find({ hostType: 'docker', host }, { json: true }); + const opts = hostRules.find({ hostType: 'docker', host }); + opts.json = true; if (opts.username && opts.password) { const auth = Buffer.from(`${opts.username}:${opts.password}`).toString( 'base64' diff --git a/lib/datasource/packagist/index.js b/lib/datasource/packagist/index.js index a6a3a980691922aef8708b9f17c51dc7f1fea139..85e26a15b87079966e8b92ddc9ed583d20460c24 100644 --- a/lib/datasource/packagist/index.js +++ b/lib/datasource/packagist/index.js @@ -13,7 +13,8 @@ module.exports = { function getHostOpts(url) { const { host } = URL.parse(url); - const opts = hostRules.find({ hostType: 'packagist', host }, { json: true }); + const opts = hostRules.find({ hostType: 'packagist', host }); + opts.json = true; if (opts && opts.username && opts.password) { const auth = Buffer.from(`${opts.username}:${opts.password}`).toString( 'base64' diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index 86427874b1184eab5b628c70e59193cad1357e2c..575d3a959f94031f50cd999145b6a60c598caca6 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -55,57 +55,49 @@ export function update(params: IPlatformConfig) { return true; } -export function find( - { - hostType, - host, - endpoint, - }: { hostType: string; host?: string; endpoint?: string }, - overrides?: IPlatformConfig -) { +function copy(config: object) { + return JSON.parse(JSON.stringify(config || null)); +} + +export function find({ + hostType, + host, + endpoint, +}: { + hostType: string; + host?: string; + endpoint?: string; +}) { const massagedHost = host || (endpoint ? URL.parse(endpoint).host : undefined); if (!hostTypes[hostType]) { if (massagedHost && hostsOnly[massagedHost]) { - return merge(hostsOnly[massagedHost], overrides); + return copy(hostsOnly[massagedHost]); } - return merge(null, overrides); + return null; } // istanbul ignore if if (hostType === 'docker') { if (hostTypes.docker.hostType === 'docker') { - return merge(hostTypes.docker, overrides); + return copy(hostTypes.docker); } - return merge(hostTypes.docker[massagedHost!], overrides); + return copy(hostTypes.docker[massagedHost!]); } if (massagedHost) { - return merge(hostTypes[hostType][massagedHost], overrides); + return copy(hostTypes[hostType][massagedHost]); } const configs = Object.values(hostTypes[hostType]); let config; if (configs.length === 1) { [config] = configs; } - return merge(config, overrides); + return copy(config); } export function hosts({ hostType }: { hostType: string }) { return Object.keys({ ...hostTypes[hostType] }); } -function merge(config: IPlatformConfig | null, overrides?: IPlatformConfig) { - 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 }; -} - export function clear() { Object.keys(hostTypes).forEach(key => delete hostTypes[key]); } diff --git a/test/datasource/__snapshots__/docker.spec.js.snap b/test/datasource/__snapshots__/docker.spec.js.snap index ee55a9b17504f645093b2a28acfd2f00c5ead7cb..583e6a0bb6ae7a8151e470a2c515fb708a55d694 100644 --- a/test/datasource/__snapshots__/docker.spec.js.snap +++ b/test/datasource/__snapshots__/docker.spec.js.snap @@ -15,6 +15,7 @@ exports[`api/docker getPkgReleases adds library/ prefix for Docker Hub (explicit "headers": Object { "Authorization": "Basic c29tZS11c2VybmFtZTpzb21lLXBhc3N3b3Jk", }, + "json": true, "password": "some-password", "username": "some-username", }, @@ -106,6 +107,7 @@ exports[`api/docker getPkgReleases adds library/ prefix for Docker Hub (implicit "headers": Object { "Authorization": "Basic c29tZS11c2VybmFtZTpzb21lLXBhc3N3b3Jk", }, + "json": true, "password": "some-password", "username": "some-username", }, @@ -197,6 +199,7 @@ exports[`api/docker getPkgReleases adds no library/ prefix for other registries "headers": Object { "Authorization": "Basic c29tZS11c2VybmFtZTpzb21lLXBhc3N3b3Jk", }, + "json": true, "password": "some-password", "username": "some-username", }, diff --git a/test/util/__snapshots__/host-rules.spec.js.snap b/test/util/__snapshots__/host-rules.spec.js.snap index 89d0a5b35e3835a8eaec33fff98893538e1627f3..cde1560fbceb7c463687e08a7c1a09daf6716da8 100644 --- a/test/util/__snapshots__/host-rules.spec.js.snap +++ b/test/util/__snapshots__/host-rules.spec.js.snap @@ -1,26 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`util/host-rules find() allows overrides 1`] = ` -Object { - "endpoint": "endpoint/", - "hostType": "github", - "other": "data", - "token": "secret", -} -`; - -exports[`util/host-rules find() allows overrides 2`] = ` -Object { - "token": "secret", -} -`; - -exports[`util/host-rules find() allows overrides 3`] = ` -Object { - "token": "secret", -} -`; - exports[`util/host-rules find() matches on endpoint 1`] = ` Object { "endpoint": "https://nuget.local/api/", @@ -47,10 +26,20 @@ Object { } `; -exports[`util/host-rules find() needs exact host matches 2`] = `null`; +exports[`util/host-rules find() needs exact host matches 2`] = ` +Object { + "endpoint": "endpoint/", + "host": "nuget.org", + "hostType": "nuget", + "password": "p4$$w0rd", + "username": "root", +} +`; exports[`util/host-rules find() needs exact host matches 3`] = `null`; +exports[`util/host-rules find() needs exact host matches 4`] = `null`; + exports[`util/host-rules update() supports endpoint-only 1`] = ` Object { "endpoint": "https://some.endpoint", diff --git a/test/util/host-rules.spec.js b/test/util/host-rules.spec.js index 8fd2c6cba6b38cf84e972e332b2f869b14e6c5c7..2b53997d2485cb48033e28f59d004f8c334ed6d1 100644 --- a/test/util/host-rules.spec.js +++ b/test/util/host-rules.spec.js @@ -33,26 +33,6 @@ describe('util/host-rules', () => { }); }); describe('find()', () => { - it('allows overrides', () => { - update({ - hostType: 'github', - endpoint: 'endpoint', - token: 'token', - other: 'data', - }); - const overrides = { - token: 'secret', - other: null, - foo: undefined, - }; - expect(find({ hostType: 'github' }, overrides)).toMatchSnapshot(); - expect( - find({ hostType: 'github', host: 'api.github.com' }, overrides) - ).toMatchSnapshot(); - expect( - find({ hostType: 'github', host: 'example.com' }, overrides) - ).toMatchSnapshot(); - }); it('needs exact host matches', () => { update({ hostType: 'nuget', @@ -61,6 +41,7 @@ describe('util/host-rules', () => { username: 'root', password: 'p4$$w0rd', }); + expect(find({ hostType: 'nuget' })).toMatchSnapshot(); expect(find({ hostType: 'nuget', host: 'nuget.org' })).toMatchSnapshot(); expect( find({ hostType: 'nuget', host: 'not.nuget.org' })