Skip to content
Snippets Groups Projects
Commit 4ddd0738 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

feat: set github app preview header when in app mode (#949)

From https://developer.github.com/v3/apps/available-endpoints/:
> In order to access the API with your GitHub App, you must provide a custom media type in the `Accept` Header for your requests.
```
application/vnd.github.machine-man-preview+json
```
parent 3c141584
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,18 @@ function sleep(ms) { ...@@ -9,6 +9,18 @@ function sleep(ms) {
async function ghGotRetry(path, opts, retries = 5) { async function ghGotRetry(path, opts, retries = 5) {
try { try {
if (appMode) {
// eslint-disable-next-line no-param-reassign
opts = opts || {};
// eslint-disable-next-line no-param-reassign
opts.headers = Object.assign(
{
accept: 'application/vnd.github.machine-man-preview+json',
'user-agent': 'https://github.com/singapore/renovate',
},
opts.headers
);
}
const res = await ghGot(path, opts); const res = await ghGot(path, opts);
return res; return res;
} catch (err) { } catch (err) {
...@@ -78,4 +90,9 @@ for (const x of helpers) { ...@@ -78,4 +90,9 @@ for (const x of helpers) {
}; };
} }
let appMode = false;
ghGotRetry.setAppMode = function setAppMode(val) {
appMode = val;
};
module.exports = ghGotRetry; module.exports = ghGotRetry;
...@@ -1063,6 +1063,7 @@ Array [ ...@@ -1063,6 +1063,7 @@ Array [
"headers": Object { "headers": Object {
"accept": "application/vnd.github.machine-man-preview+json", "accept": "application/vnd.github.machine-man-preview+json",
"authorization": "Bearer sometoken", "authorization": "Bearer sometoken",
"user-agent": "https://github.com/singapore/renovate",
}, },
}, },
], ],
......
...@@ -3,6 +3,7 @@ const logger = require('../_fixtures/logger'); ...@@ -3,6 +3,7 @@ const logger = require('../_fixtures/logger');
describe('api/github', () => { describe('api/github', () => {
let github; let github;
let ghGot; let ghGot;
let ghGotRetry;
beforeEach(() => { beforeEach(() => {
// clean up env // clean up env
delete process.env.GITHUB_TOKEN; delete process.env.GITHUB_TOKEN;
...@@ -11,6 +12,7 @@ describe('api/github', () => { ...@@ -11,6 +12,7 @@ describe('api/github', () => {
// reset module // reset module
jest.resetModules(); jest.resetModules();
jest.mock('gh-got'); jest.mock('gh-got');
ghGotRetry = require('../../lib/api/gh-got-retry');
github = require('../../lib/api/github'); github = require('../../lib/api/github');
ghGot = require('gh-got'); ghGot = require('gh-got');
}); });
...@@ -20,6 +22,7 @@ describe('api/github', () => { ...@@ -20,6 +22,7 @@ describe('api/github', () => {
ghGot.mockImplementationOnce(() => ({ ghGot.mockImplementationOnce(() => ({
body: ['a', 'b'], body: ['a', 'b'],
})); }));
ghGotRetry.setAppMode(true);
const installations = await github.getInstallations('sometoken'); const installations = await github.getInstallations('sometoken');
expect(ghGot.mock.calls).toMatchSnapshot(); expect(ghGot.mock.calls).toMatchSnapshot();
expect(installations).toMatchSnapshot(); expect(installations).toMatchSnapshot();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment