From 73f953ca947ede32cd553e8f0848e5af3d3deff9 Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Mon, 27 May 2019 09:21:56 +0200 Subject: [PATCH] refactor(gitlab): use util/got/auth --- lib/platform/gitlab/gl-got-wrapper.js | 19 +++++-------------- lib/util/got/auth.js | 10 ++++++---- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/platform/gitlab/gl-got-wrapper.js b/lib/platform/gitlab/gl-got-wrapper.js index 7f303a5aee..b8eb1d2467 100644 --- a/lib/platform/gitlab/gl-got-wrapper.js +++ b/lib/platform/gitlab/gl-got-wrapper.js @@ -1,27 +1,18 @@ -const URL = require('url'); const parseLinkHeader = require('parse-link-header'); -const hostRules = require('../../util/host-rules'); const got = require('../../util/got'); +const hostType = 'gitlab'; let baseUrl = 'https://gitlab.com/api/v4/'; async function get(path, options) { - const url = URL.resolve(baseUrl, path); const opts = { - ...options, - ...hostRules.find({ hostType: 'gitlab', url }), + hostType, + baseUrl, json: true, + ...options, }; - if (opts.token) { - opts.headers = { - ...opts.headers, - 'PRIVATE-TOKEN': opts.token, - }; - delete opts.token; - } - delete opts.endpoint; - const res = await got(url, opts); + const res = await got(path, opts); if (opts.paginate) { // Check if result is paginated try { diff --git a/lib/util/got/auth.js b/lib/util/got/auth.js index f2840e0048..ad2b833a3e 100644 --- a/lib/util/got/auth.js +++ b/lib/util/got/auth.js @@ -4,7 +4,7 @@ const got = require('got'); module.exports = got.create({ options: {}, handler: (options, next) => { - if (options.headers.authorization || options.auth) { + if (options.auth || options.headers.authorization) { return next(options); } if (options.token) { @@ -12,11 +12,13 @@ module.exports = got.create({ { hostname: options.hostname }, 'Converting token to Bearer auth' ); - let authHeader = `Bearer ${options.token}`; if (options.hostType === 'github') { - authHeader = `token ${options.token}`; // it seems to work with Bearer too, but docs say to use token + options.headers.authorization = `token ${options.token}`; // eslint-disable-line no-param-reassign + } else if (options.hostType === '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 } - options.headers.authorization = authHeader; // eslint-disable-line no-param-reassign delete options.token; // eslint-disable-line no-param-reassign } return next(options); -- GitLab