Merge "Add support for github enterprise" into feature/zuulv3
diff --git a/doc/source/connections.rst b/doc/source/connections.rst
index 7b302e9..120d529 100644
--- a/doc/source/connections.rst
+++ b/doc/source/connections.rst
@@ -87,6 +87,11 @@
   Path to SSH key to use when cloning github repositories.
   ``sshkey=/home/zuul/.ssh/id_rsa``
 
+**git_host**
+  Optional: Hostname of the github install (such as a GitHub Enterprise)
+  If not specified, defaults to ``github.com``
+  ``git_host=github.myenterprise.com``
+
 SMTP
 ----
 
diff --git a/tests/fixtures/zuul-github-driver.conf b/tests/fixtures/zuul-github-driver.conf
index 58c7cd5..ab34619 100644
--- a/tests/fixtures/zuul-github-driver.conf
+++ b/tests/fixtures/zuul-github-driver.conf
@@ -20,3 +20,8 @@
 [connection github_ssh]
 driver=github
 sshkey=/home/zuul/.ssh/id_rsa
+
+[connection github_ent]
+driver=github
+sshkey=/home/zuul/.ssh/id_rsa
+git_host=github.enterprise.io
diff --git a/tests/unit/test_github_driver.py b/tests/unit/test_github_driver.py
index e8abf79..a1a05e0 100644
--- a/tests/unit/test_github_driver.py
+++ b/tests/unit/test_github_driver.py
@@ -230,6 +230,12 @@
         url = self.fake_github_ssh.real_getGitUrl('org/project')
         self.assertEqual('ssh://git@github.com/org/project.git', url)
 
+    @simple_layout('layouts/basic-github.yaml', driver='github')
+    def test_git_enterprise_url(self):
+        """Test that git_url option gives git url with proper host"""
+        url = self.fake_github_ent.real_getGitUrl('org/project')
+        self.assertEqual('ssh://git@github.enterprise.io/org/project.git', url)
+
     @simple_layout('layouts/reporting-github.yaml', driver='github')
     def test_reporting(self):
         # pipeline reports pull status both on start and success
diff --git a/zuul/driver/github/githubconnection.py b/zuul/driver/github/githubconnection.py
index 9b505ba..d715222 100644
--- a/zuul/driver/github/githubconnection.py
+++ b/zuul/driver/github/githubconnection.py
@@ -239,18 +239,18 @@
     log = logging.getLogger("connection.github")
     payload_path = 'payload'
     git_user = 'git'
-    git_host = 'github.com'
 
     def __init__(self, driver, connection_name, connection_config):
         super(GithubConnection, self).__init__(
             driver, connection_name, connection_config)
         self.github = None
-        self.canonical_hostname = self.connection_config.get(
-            'canonical_hostname', 'github.com')
         self._change_cache = {}
         self.projects = {}
-        self.source = driver.getSource(self)
         self._git_ssh = bool(self.connection_config.get('sshkey', None))
+        self.git_host = self.connection_config.get('git_host', 'github.com')
+        self.canonical_hostname = self.connection_config.get(
+            'canonical_hostname', self.git_host)
+        self.source = driver.getSource(self)
 
     def onLoad(self):
         webhook_listener = GithubWebhookListener(self)
@@ -264,7 +264,11 @@
     def _authenticateGithubAPI(self):
         token = self.connection_config.get('api_token', None)
         if token is not None:
-            self.github = github3.login(token=token)
+            if self.git_host != 'github.com':
+                url = 'https://%s/' % self.git_host
+                self.github = github3.enterprise_login(token=token, url=url)
+            else:
+                self.github = github3.login(token=token)
             self.log.info("Github API Authentication successful.")
         else:
             self.github = None