From 2c8b817b4d229817323bcc1a4105e79ecaf4b3dc Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 12 Jul 2018 14:10:19 +0200
Subject: [PATCH] fix(config): return config.endpoint from env parsing

This should address the GITHUB_ENDPOINT problem in #2249
---
 lib/config/env.js                          |  9 ++++
 test/config/__snapshots__/env.spec.js.snap | 49 ++++++++++++++++++----
 test/config/env.spec.js                    | 37 ++++++++++++----
 3 files changed, 77 insertions(+), 18 deletions(-)

diff --git a/lib/config/env.js b/lib/config/env.js
index ce4e9eb6ea..bfde0ea98d 100644
--- a/lib/config/env.js
+++ b/lib/config/env.js
@@ -70,5 +70,14 @@ function getConfig(env) {
     });
   }
 
+  if (config.platform === 'gitlab') {
+    config.endpoint = env.GITLAB_ENDPOINT;
+  } else if (config.platform === 'vsts') {
+    config.endpoint = 'vsts';
+  } else if (env.GITHUB_ENDPOINT) {
+    // GitHub is default
+    config.endpoint = env.GITHUB_ENDPOINT;
+  }
+
   return config;
 }
diff --git a/test/config/__snapshots__/env.spec.js.snap b/test/config/__snapshots__/env.spec.js.snap
index bc5ac148e3..b1ad17ae56 100644
--- a/test/config/__snapshots__/env.spec.js.snap
+++ b/test/config/__snapshots__/env.spec.js.snap
@@ -2,22 +2,47 @@
 
 exports[`config/env .getConfig(env) supports GitHub custom endpoint 1`] = `
 Object {
+  "endpoint": "a ghe endpoint",
   "endpoints": Array [],
 }
 `;
 
 exports[`config/env .getConfig(env) supports GitHub custom endpoint and github.com 1`] = `
 Object {
+  "endpoint": "a ghe endpoint",
   "endpoints": Array [
     Object {
       "platform": "github",
-      "token": "public",
+      "token": "a github.com token",
     },
     Object {
       "default": true,
-      "endpoint": "endpoint",
+      "endpoint": "a ghe endpoint",
       "platform": "github",
-      "token": "token",
+      "token": "a ghe token",
+    },
+  ],
+}
+`;
+
+exports[`config/env .getConfig(env) supports GitHub custom endpoint and github.com and gitlab.com 1`] = `
+Object {
+  "endpoint": "a ghe endpoint",
+  "endpoints": Array [
+    Object {
+      "platform": "github",
+      "token": "a github.com token",
+    },
+    Object {
+      "default": true,
+      "endpoint": "a ghe endpoint",
+      "platform": "github",
+      "token": "a ghe token",
+    },
+    Object {
+      "endpoint": undefined,
+      "platform": "gitlab",
+      "token": "a gitlab token",
     },
   ],
 }
@@ -30,7 +55,7 @@ Object {
       "default": true,
       "endpoint": undefined,
       "platform": "github",
-      "token": "token",
+      "token": "github.com token",
     },
   ],
 }
@@ -38,36 +63,42 @@ Object {
 
 exports[`config/env .getConfig(env) supports GitLab custom endpoint 1`] = `
 Object {
+  "endpoint": "a gitlab endpoint",
   "endpoints": Array [
     Object {
-      "endpoint": "endpoint",
+      "endpoint": "a gitlab endpoint",
       "platform": "gitlab",
-      "token": "token",
+      "token": "a gitlab token",
     },
   ],
+  "platform": "gitlab",
 }
 `;
 
 exports[`config/env .getConfig(env) supports GitLab token 1`] = `
 Object {
+  "endpoint": undefined,
   "endpoints": Array [
     Object {
       "endpoint": undefined,
       "platform": "gitlab",
-      "token": "token",
+      "token": "a gitlab.com token",
     },
   ],
+  "platform": "gitlab",
 }
 `;
 
 exports[`config/env .getConfig(env) supports VSTS 1`] = `
 Object {
+  "endpoint": "vsts",
   "endpoints": Array [
     Object {
-      "endpoint": "endpoint",
+      "endpoint": "a vsts endpoint",
       "platform": "vsts",
-      "token": undefined,
+      "token": "a vsts token",
     },
   ],
+  "platform": "vsts",
 }
 `;
diff --git a/test/config/env.spec.js b/test/config/env.spec.js
index c68cfac495..e37bc6c67e 100644
--- a/test/config/env.spec.js
+++ b/test/config/env.spec.js
@@ -35,32 +35,51 @@ describe('config/env', () => {
       expect(env.getConfig(envParam).lockFileMaintenance).toEqual({});
     });
     it('supports GitHub token', () => {
-      const envParam = { GITHUB_TOKEN: 'token' };
+      const envParam = { GITHUB_TOKEN: 'github.com token' };
       expect(env.getConfig(envParam)).toMatchSnapshot();
     });
     it('supports GitHub custom endpoint', () => {
-      const envParam = { GITHUB_ENDPOINT: 'endpoint' };
+      const envParam = { GITHUB_ENDPOINT: 'a ghe endpoint' };
       expect(env.getConfig(envParam)).toMatchSnapshot();
     });
-
     it('supports GitHub custom endpoint and github.com', () => {
       const envParam = {
-        GITHUB_COM_TOKEN: 'public',
-        GITHUB_ENDPOINT: 'endpoint',
-        GITHUB_TOKEN: 'token',
+        GITHUB_COM_TOKEN: 'a github.com token',
+        GITHUB_ENDPOINT: 'a ghe endpoint',
+        GITHUB_TOKEN: 'a ghe token',
+      };
+      expect(env.getConfig(envParam)).toMatchSnapshot();
+    });
+    it('supports GitHub custom endpoint and github.com and gitlab.com', () => {
+      const envParam = {
+        GITHUB_COM_TOKEN: 'a github.com token',
+        GITHUB_ENDPOINT: 'a ghe endpoint',
+        GITHUB_TOKEN: 'a ghe token',
+        GITLAB_TOKEN: 'a gitlab token',
       };
       expect(env.getConfig(envParam)).toMatchSnapshot();
     });
     it('supports GitLab token', () => {
-      const envParam = { GITLAB_TOKEN: 'token' };
+      const envParam = {
+        RENOVATE_PLATFORM: 'gitlab',
+        GITLAB_TOKEN: 'a gitlab.com token',
+      };
       expect(env.getConfig(envParam)).toMatchSnapshot();
     });
     it('supports GitLab custom endpoint', () => {
-      const envParam = { GITLAB_TOKEN: 'token', GITLAB_ENDPOINT: 'endpoint' };
+      const envParam = {
+        RENOVATE_PLATFORM: 'gitlab',
+        GITLAB_TOKEN: 'a gitlab token',
+        GITLAB_ENDPOINT: 'a gitlab endpoint',
+      };
       expect(env.getConfig(envParam)).toMatchSnapshot();
     });
     it('supports VSTS', () => {
-      const envParam = { VSTS_TOKEN: 'token', VSTS_ENDPOINT: 'endpoint' };
+      const envParam = {
+        RENOVATE_PLATFORM: 'vsts',
+        VSTS_TOKEN: 'a vsts token',
+        VSTS_ENDPOINT: 'a vsts endpoint',
+      };
       expect(env.getConfig(envParam)).toMatchSnapshot();
     });
   });
-- 
GitLab