From bbb700c54d2145cf68f63d671f660f8ec0bb3f9a Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Tue, 12 Dec 2017 14:54:09 +0100 Subject: [PATCH] feat: add github writeToken capability in github wrapper --- lib/config/validation.js | 2 +- lib/logger/config-serializer.js | 1 + lib/platform/github/gh-got-wrapper.js | 14 +++++++++++--- lib/platform/github/index.js | 4 ++-- lib/workers/repository/init/apis.js | 3 ++- .../__snapshots__/gh-got-wrapper.spec.js.snap | 14 ++++++++++++++ test/platform/github/gh-got-wrapper.spec.js | 6 ++++++ 7 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 test/platform/github/__snapshots__/gh-got-wrapper.spec.js.snap diff --git a/lib/config/validation.js b/lib/config/validation.js index 33ea6adfa3..02a7f0ca5e 100644 --- a/lib/config/validation.js +++ b/lib/config/validation.js @@ -18,7 +18,7 @@ function validateConfig(config) { let warnings = []; function isIgnored(key) { - const ignoredNodes = ['depType', 'npmToken', 'packageFile']; + const ignoredNodes = ['depType', 'npmToken', 'packageFile', 'writeToken']; return ignoredNodes.indexOf(key) !== -1; } diff --git a/lib/logger/config-serializer.js b/lib/logger/config-serializer.js index bceffefff4..0bab777969 100644 --- a/lib/logger/config-serializer.js +++ b/lib/logger/config-serializer.js @@ -11,6 +11,7 @@ function configSerializer(config) { 'yarnrc', 'privateKey', 'gitPrivateKey', + 'writeToken', ]; const templateFields = ['commitMessage', 'prTitle', 'prBody']; // eslint-disable-next-line array-callback-return diff --git a/lib/platform/github/gh-got-wrapper.js b/lib/platform/github/gh-got-wrapper.js index 3d72ca8506..f418e356bc 100644 --- a/lib/platform/github/gh-got-wrapper.js +++ b/lib/platform/github/gh-got-wrapper.js @@ -1,6 +1,7 @@ const ghGot = require('gh-got'); const parseLinkHeader = require('parse-link-header'); +let writeToken; let cache = {}; // istanbul ignore next @@ -116,8 +117,14 @@ async function get(path, opts, retries = 5) { const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete']; for (const x of helpers) { - get[x] = (url, opts) => - get(url, Object.assign({}, opts, { method: x.toUpperCase() })); + // eslint-disable-next-line no-loop-func + get[x] = (url, opts) => { + const options = Object.assign({}, opts, { method: x.toUpperCase() }); + if (writeToken && ['post', 'put', 'patch', 'delete'].includes(x)) { + options.token = writeToken; + } + return get(url, options); + }; } let appMode = false; @@ -125,8 +132,9 @@ get.setAppMode = function setAppMode(val) { appMode = val; }; -get.reset = function reset() { +get.reset = function reset(token) { cache = {}; + writeToken = token; }; module.exports = get; diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js index b1dae82aff..7e94562d77 100644 --- a/lib/platform/github/index.js +++ b/lib/platform/github/index.js @@ -67,7 +67,7 @@ async function getRepos(token, endpoint) { } // Initialize GitHub by getting base branch and SHA -async function initRepo(repoName, token, endpoint, forkMode = false) { +async function initRepo(repoName, token, endpoint, forkMode, writeToken) { logger.debug(`initRepo("${repoName}")`); if (token) { process.env.GITHUB_TOKEN = token; @@ -78,7 +78,7 @@ async function initRepo(repoName, token, endpoint, forkMode = false) { process.env.GITHUB_ENDPOINT = endpoint; } config = {}; - get.reset(); + get.reset(writeToken); config.repoName = repoName; const platformConfig = {}; let res; diff --git a/lib/workers/repository/init/apis.js b/lib/workers/repository/init/apis.js index 6a3fcb6b1e..95d90c65a9 100644 --- a/lib/workers/repository/init/apis.js +++ b/lib/workers/repository/init/apis.js @@ -12,7 +12,8 @@ async function getPlatformConfig(config) { config.repository, config.token, config.endpoint, - config.forkMode + config.forkMode, + config.writeToken ); return { ...config, diff --git a/test/platform/github/__snapshots__/gh-got-wrapper.spec.js.snap b/test/platform/github/__snapshots__/gh-got-wrapper.spec.js.snap new file mode 100644 index 0000000000..bb757516aa --- /dev/null +++ b/test/platform/github/__snapshots__/gh-got-wrapper.spec.js.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`platform/gh-got-wrapper uses writeToken 1`] = ` +Array [ + Array [ + "some-url", + Object { + "body": Object {}, + "method": "PUT", + "token": "some-write-token", + }, + ], +] +`; diff --git a/test/platform/github/gh-got-wrapper.spec.js b/test/platform/github/gh-got-wrapper.spec.js index 6f148dd343..c326e4a8e1 100644 --- a/test/platform/github/gh-got-wrapper.spec.js +++ b/test/platform/github/gh-got-wrapper.spec.js @@ -7,6 +7,7 @@ describe('platform/gh-got-wrapper', () => { const body = ['a', 'b']; beforeEach(() => { jest.resetAllMocks(); + get.reset(); get.setAppMode(false); }); it('supports app mode', async () => { @@ -16,6 +17,11 @@ describe('platform/gh-got-wrapper', () => { 'application/vnd.github.machine-man-preview+json, some-accept' ); }); + it('uses writeToken', async () => { + get.reset('some-write-token'); + await get.put('some-url', { body: {} }); + expect(ghGot.mock.calls).toMatchSnapshot(); + }); it('paginates', async () => { ghGot.mockReturnValueOnce({ headers: { -- GitLab