Merge "Configurable SSH access to GitHub" into feature/zuulv3
diff --git a/doc/source/connections.rst b/doc/source/connections.rst
index 521e4ed..7b302e9 100644
--- a/doc/source/connections.rst
+++ b/doc/source/connections.rst
@@ -83,6 +83,10 @@
   See `Securing your webhooks
   <https://developer.github.com/webhooks/securing/>`_.
 
+**sshkey**
+  Path to SSH key to use when cloning github repositories.
+  ``sshkey=/home/zuul/.ssh/id_rsa``
+
 SMTP
 ----
 
diff --git a/tests/base.py b/tests/base.py
index 1092c6e..56b1269 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -736,6 +736,9 @@
     def getGitUrl(self, project):
         return os.path.join(self.upstream_root, str(project))
 
+    def real_getGitUrl(self, project):
+        return super(FakeGithubConnection, self).getGitUrl(project)
+
     def getProjectBranches(self, project):
         """Masks getProjectBranches since we don't have a real github"""
 
diff --git a/tests/fixtures/zuul-github-driver.conf b/tests/fixtures/zuul-github-driver.conf
index b979a3f..b4a85f7 100644
--- a/tests/fixtures/zuul-github-driver.conf
+++ b/tests/fixtures/zuul-github-driver.conf
@@ -15,3 +15,7 @@
 
 [connection github]
 driver=github
+
+[connection github_ssh]
+driver=github
+sshkey=/home/zuul/.ssh/id_rsa
diff --git a/tests/unit/test_github_driver.py b/tests/unit/test_github_driver.py
index c9847b2..1e5f6a6 100644
--- a/tests/unit/test_github_driver.py
+++ b/tests/unit/test_github_driver.py
@@ -110,3 +110,15 @@
 
         self.assertEqual('SUCCESS',
                          self.getJobFromHistory('project-post').result)
+
+    @simple_layout('layouts/basic-github.yaml', driver='github')
+    def test_git_https_url(self):
+        """Test that git_ssh option gives git url with ssh"""
+        url = self.fake_github.real_getGitUrl('org/project')
+        self.assertEqual('https://github.com/org/project', url)
+
+    @simple_layout('layouts/basic-github.yaml', driver='github')
+    def test_git_ssh_url(self):
+        """Test that git_ssh option gives git url with ssh"""
+        url = self.fake_github_ssh.real_getGitUrl('org/project')
+        self.assertEqual('ssh://git@github.com/org/project.git', url)
diff --git a/zuul/driver/github/githubconnection.py b/zuul/driver/github/githubconnection.py
index 18903af..7085bf6 100644
--- a/zuul/driver/github/githubconnection.py
+++ b/zuul/driver/github/githubconnection.py
@@ -182,6 +182,8 @@
     driver_name = 'github'
     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__(
@@ -192,6 +194,7 @@
         self._change_cache = {}
         self.projects = {}
         self.source = driver.getSource(self)
+        self._git_ssh = bool(self.connection_config.get('sshkey', None))
 
     def onLoad(self):
         webhook_listener = GithubWebhookListener(self)
@@ -242,11 +245,15 @@
         return change
 
     def getGitUrl(self, project):
-        url = 'https://%s/%s' % ("github.com", project)
+        if self._git_ssh:
+            url = 'ssh://%s@%s/%s.git' % \
+                (self.git_user, self.git_host, project)
+        else:
+            url = 'https://%s/%s' % (self.git_host, project)
         return url
 
     def getGitwebUrl(self, project, sha=None):
-        url = 'https://%s/%s' % ("github.com", project)
+        url = 'https://%s/%s' % (self.git_host, project)
         if sha is not None:
             url += '/commit/%s' % sha
         return url