From 4ddd073863d34efd14072508ae4749b33d5d73b5 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Mon, 16 Oct 2017 09:45:31 +0200
Subject: [PATCH] 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
```
---
 lib/api/gh-got-retry.js                    | 17 +++++++++++++++++
 test/api/__snapshots__/github.spec.js.snap |  1 +
 test/api/github.spec.js                    |  3 +++
 3 files changed, 21 insertions(+)

diff --git a/lib/api/gh-got-retry.js b/lib/api/gh-got-retry.js
index f2c8997fcc..6534ba74d8 100644
--- a/lib/api/gh-got-retry.js
+++ b/lib/api/gh-got-retry.js
@@ -9,6 +9,18 @@ function sleep(ms) {
 
 async function ghGotRetry(path, opts, retries = 5) {
   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);
     return res;
   } catch (err) {
@@ -78,4 +90,9 @@ for (const x of helpers) {
   };
 }
 
+let appMode = false;
+ghGotRetry.setAppMode = function setAppMode(val) {
+  appMode = val;
+};
+
 module.exports = ghGotRetry;
diff --git a/test/api/__snapshots__/github.spec.js.snap b/test/api/__snapshots__/github.spec.js.snap
index d291793c82..0cc62aac05 100644
--- a/test/api/__snapshots__/github.spec.js.snap
+++ b/test/api/__snapshots__/github.spec.js.snap
@@ -1063,6 +1063,7 @@ Array [
       "headers": Object {
         "accept": "application/vnd.github.machine-man-preview+json",
         "authorization": "Bearer sometoken",
+        "user-agent": "https://github.com/singapore/renovate",
       },
     },
   ],
diff --git a/test/api/github.spec.js b/test/api/github.spec.js
index 8bbd55995e..bc110a81a5 100644
--- a/test/api/github.spec.js
+++ b/test/api/github.spec.js
@@ -3,6 +3,7 @@ const logger = require('../_fixtures/logger');
 describe('api/github', () => {
   let github;
   let ghGot;
+  let ghGotRetry;
   beforeEach(() => {
     // clean up env
     delete process.env.GITHUB_TOKEN;
@@ -11,6 +12,7 @@ describe('api/github', () => {
     // reset module
     jest.resetModules();
     jest.mock('gh-got');
+    ghGotRetry = require('../../lib/api/gh-got-retry');
     github = require('../../lib/api/github');
     ghGot = require('gh-got');
   });
@@ -20,6 +22,7 @@ describe('api/github', () => {
       ghGot.mockImplementationOnce(() => ({
         body: ['a', 'b'],
       }));
+      ghGotRetry.setAppMode(true);
       const installations = await github.getInstallations('sometoken');
       expect(ghGot.mock.calls).toMatchSnapshot();
       expect(installations).toMatchSnapshot();
-- 
GitLab