diff --git a/lib/config/validation.js b/lib/config/validation.js index 33ea6adfa3f6bb129db322163b8bc38cc12fe7f3..02a7f0ca5ee71b2c911de0cddfdf18bf1437b601 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 bceffefff4a0f44f368d9f92dd4e717d388a9a74..0bab777969cfb1cf07450ee93e4077108b2ab770 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 3d72ca850698e8072369071fa544fd3ef9268312..f418e356bc015017984f09cb2fff19cbbf129c06 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 b1dae82aff7cb90ab2e3548165a2845b7125d525..7e94562d77d980e25fb73a77b12be57710d07abc 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 6a3fcb6b1e346183af4a9570930f98c251e5d8aa..95d90c65a9b9ff9f5161753fe13b66e726fd35b5 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 0000000000000000000000000000000000000000..bb757516aad9f12cbfbea5eb928afd417184ed8a --- /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 6f148dd343d450b131375992fa9d103289266710..c326e4a8e11b297b4222298f9be122cfe58ae4b0 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: {