diff --git a/lib/util/got/auth.ts b/lib/util/got/auth.ts deleted file mode 100644 index 0a5f3f87e5e17b..00000000000000 --- a/lib/util/got/auth.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { - PLATFORM_TYPE_GITEA, - PLATFORM_TYPE_GITHUB, - PLATFORM_TYPE_GITLAB, -} from '../../constants/platforms'; -import { logger } from '../../logger'; -import { create } from './util'; - -export default create({ - options: {}, - handler: (options, next) => { - if (options.auth || options.headers.authorization) { - return next(options); - } - if (options.token) { - logger.trace( - { hostname: options.hostname }, - 'Converting token to Bearer auth' - ); - if ( - options.hostType === PLATFORM_TYPE_GITHUB || - options.hostType === PLATFORM_TYPE_GITEA - ) { - options.headers.authorization = `token ${options.token}`; // eslint-disable-line no-param-reassign - } else if (options.hostType === PLATFORM_TYPE_GITLAB) { - options.headers['Private-token'] = options.token; // eslint-disable-line no-param-reassign - } else { - options.headers.authorization = `Bearer ${options.token}`; // eslint-disable-line no-param-reassign - } - delete options.token; // eslint-disable-line no-param-reassign - } - return next(options); - }, -}); diff --git a/lib/util/got/index.ts b/lib/util/got/index.ts index 8360e53336db1a..4db3c23d40bfa3 100644 --- a/lib/util/got/index.ts +++ b/lib/util/got/index.ts @@ -1,14 +1,15 @@ import got from 'got'; -import auth from './auth'; -import { mergeInstances } from './util'; +import { create, mergeInstances } from './util'; export * from './common'; -/* - * This is the default got instance for Renovate. - * - Cache all GET requests for the lifetime of the repo - * - */ -export const api = mergeInstances(got, auth); +const dummy = create({ + options: {}, + handler: (options, next) => { + return next(options); + }, +}); + +export const api = mergeInstances(got, dummy); export default api; diff --git a/lib/util/http/auth.ts b/lib/util/http/auth.ts new file mode 100644 index 00000000000000..f9bdc50e1e3950 --- /dev/null +++ b/lib/util/http/auth.ts @@ -0,0 +1,26 @@ +import { + PLATFORM_TYPE_GITEA, + PLATFORM_TYPE_GITHUB, + PLATFORM_TYPE_GITLAB, +} from '../../constants/platforms'; + +export function applyAuthorization(inOptions: any): any { + const options = { ...inOptions }; + if (options.auth || options.headers?.authorization) { + return options; + } + if (options.token) { + if ( + options.hostType === PLATFORM_TYPE_GITHUB || + options.hostType === PLATFORM_TYPE_GITEA + ) { + options.headers.authorization = `token ${options.token}`; + } else if (options.hostType === PLATFORM_TYPE_GITLAB) { + options.headers['Private-token'] = options.token; + } else { + options.headers.authorization = `Bearer ${options.token}`; + } + delete options.token; + } + return options; +} diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts index 2b8cd7d6774d9e..bff118fdd9040d 100644 --- a/lib/util/http/index.ts +++ b/lib/util/http/index.ts @@ -4,6 +4,7 @@ import { GotPromise } from 'got'; import * as runCache from '../cache/run'; import { clone } from '../clone'; import got from '../got'; +import { applyAuthorization } from './auth'; import { applyHostRules } from './host-rules'; interface OutgoingHttpHeaders { @@ -90,6 +91,7 @@ export class Http { }; options = applyHostRules(url, options); + options = applyAuthorization(options); // Cache GET requests unless useCache=false let promisedRes: GotPromise;