← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~cjwatson/launchpad/git-ssh-url-username into lp:launchpad

 

Colin Watson has proposed merging lp:~cjwatson/launchpad/git-ssh-url-username into lp:launchpad.

Commit message:
Include the appropriate username in git+ssh:// URLs in the UI.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #1600055 in Launchpad itself: "launchpad git config instructions should include your username"
  https://bugs.launchpad.net/launchpad/+bug/1600055

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/git-ssh-url-username/+merge/353748
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~cjwatson/launchpad/git-ssh-url-username into lp:launchpad.
=== modified file 'lib/lp/code/browser/gitref.py'
--- lib/lp/code/browser/gitref.py	2018-08-23 14:52:46 +0000
+++ lib/lp/code/browser/gitref.py	2018-08-25 16:24:20 +0000
@@ -12,9 +12,13 @@
     ]
 
 import json
-from urllib import quote_plus
 
 from lazr.restful.interface import copy_field
+from six.moves.urllib_parse import (
+    quote_plus,
+    urlsplit,
+    urlunsplit,
+    )
 from zope.component import getUtility
 from zope.formlib.widgets import TextAreaWidget
 from zope.interface import Interface
@@ -106,6 +110,14 @@
         return self.context.display_name
 
     @property
+    def git_ssh_url(self):
+        """The git+ssh:// URL for this branch, adjusted for this user."""
+        base_url = urlsplit(self.context.repository.git_ssh_url)
+        url = list(base_url)
+        url[1] = "{}@{}".format(self.user.name, base_url.hostname)
+        return urlunsplit(url)
+
+    @property
     def user_can_push(self):
         """Whether the user can push to this branch."""
         return (

=== modified file 'lib/lp/code/browser/gitrepository.py'
--- lib/lp/code/browser/gitrepository.py	2018-06-22 10:01:34 +0000
+++ lib/lp/code/browser/gitrepository.py	2018-08-25 16:24:20 +0000
@@ -1,4 +1,4 @@
-# Copyright 2015-2016 Canonical Ltd.  This software is licensed under the
+# Copyright 2015-2018 Canonical Ltd.  This software is licensed under the
 # GNU Affero General Public License version 3 (see the file LICENSE).
 
 """Git repository views."""
@@ -26,6 +26,10 @@
     copy_field,
     use_template,
     )
+from six.moves.urllib_parse import (
+    urlsplit,
+    urlunsplit,
+    )
 from zope.component import getUtility
 from zope.event import notify
 from zope.formlib import form
@@ -324,6 +328,14 @@
                 self.request, "launchpad.LimitedView", authorised_people)
 
     @property
+    def git_ssh_url(self):
+        """The git+ssh:// URL for this repository, adjusted for this user."""
+        base_url = urlsplit(self.context.git_ssh_url)
+        url = list(base_url)
+        url[1] = "{}@{}".format(self.user.name, base_url.hostname)
+        return urlunsplit(url)
+
+    @property
     def user_can_push(self):
         """Whether the user can push to this branch."""
         return (

=== modified file 'lib/lp/code/browser/tests/test_gitref.py'
--- lib/lp/code/browser/tests/test_gitref.py	2018-08-23 14:52:46 +0000
+++ lib/lp/code/browser/tests/test_gitref.py	2018-08-25 16:24:20 +0000
@@ -134,11 +134,12 @@
 
     def test_clone_instructions(self):
         [ref] = self.factory.makeGitRefs(paths=["refs/heads/branch"])
+        username = ref.owner.name
         text = self.getMainText(ref, "+index", user=ref.owner)
         self.assertTextMatchesExpressionIgnoreWhitespace(r"""
-            git clone -b branch https://.*
-            git clone -b branch git\+ssh://.*
-            """, text)
+            git clone -b branch https://git.launchpad.dev/.*
+            git clone -b branch git\+ssh://{username}@git.launchpad.dev/.*
+            """.format(username=username), text)
 
     def makeCommitLog(self):
         authors = [self.factory.makePerson() for _ in range(5)]

=== modified file 'lib/lp/code/browser/tests/test_gitrepository.py'
--- lib/lp/code/browser/tests/test_gitrepository.py	2018-08-23 09:30:24 +0000
+++ lib/lp/code/browser/tests/test_gitrepository.py	2018-08-25 16:24:20 +0000
@@ -93,11 +93,12 @@
 
     def test_clone_instructions(self):
         repository = self.factory.makeGitRepository()
+        username = repository.owner.name
         text = self.getMainText(repository, "+index", user=repository.owner)
         self.assertTextMatchesExpressionIgnoreWhitespace(r"""
-            git clone https://.*
-            git clone git\+ssh://.*
-            """, text)
+            git clone https://git.launchpad.dev/.*
+            git clone git\+ssh://{username}@git.launchpad.dev/.*
+            """.format(username=username), text)
 
     def test_user_can_push(self):
         # A user can push if they have edit permissions.
@@ -164,13 +165,15 @@
         # explain how to do so.
         self.factory.makeSSHKey(person=self.user, send_notification=False)
         repository = self.factory.makeGitRepository(owner=self.user)
+        username = self.user.name
         browser = self.getViewBrowser(repository)
         directions = find_tag_by_id(browser.contents, "push-directions")
         login_person(self.user)
         self.assertThat(extract_text(directions), DocTestMatches(dedent("""
             Update this repository:
-            git push git+ssh://git.launchpad.dev/{repository.shortened_path}
-            """).format(repository=repository),
+            git push
+            git+ssh://{username}@git.launchpad.dev/{repository.shortened_path}
+            """).format(username=username, repository=repository),
             flags=doctest.NORMALIZE_WHITESPACE))
 
     def test_push_directions_logged_in_can_push_no_sshkeys(self):

=== modified file 'lib/lp/code/templates/git-macros.pt'
--- lib/lp/code/templates/git-macros.pt	2016-11-17 23:16:12 +0000
+++ lib/lp/code/templates/git-macros.pt	2018-08-25 16:24:20 +0000
@@ -30,7 +30,7 @@
         <tt class="command">
           git clone
           <tal:branch condition="branch_name|nothing" replace="string:-b ${branch_name}" />
-          <span class="ssh-url" tal:content="repository/git_ssh_url" />
+          <span class="ssh-url" tal:content="view/git_ssh_url" />
         </tt>
       </tal:ssh>
     </dd>
@@ -63,7 +63,7 @@
           <dd>
             <tt class="command">
             git push
-            <span class="ssh-url" tal:content="repository/git_ssh_url" />
+            <span class="ssh-url" tal:content="view/git_ssh_url" />
             <tal:branch condition="branch_name|nothing" replace="branch_name" />
             </tt>
           </dd>


Follow ups