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

Proposed by Colin Watson
Status: Merged
Merged at revision: 18760
Proposed branch: lp:~cjwatson/launchpad/git-ssh-url-username
Merge into: lp:launchpad
Diff against target: 149 lines (+40/-12)
5 files modified
lib/lp/code/browser/gitref.py (+13/-1)
lib/lp/code/browser/gitrepository.py (+13/-1)
lib/lp/code/browser/tests/test_gitref.py (+4/-3)
lib/lp/code/browser/tests/test_gitrepository.py (+8/-5)
lib/lp/code/templates/git-macros.pt (+2/-2)
To merge this branch: bzr merge lp:~cjwatson/launchpad/git-ssh-url-username
Reviewer Review Type Date Requested Status
Maximiliano Bertacchini (community) Approve
Launchpad code reviewers Pending
Review via email: mp+353748@code.launchpad.net

Commit message

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

To post a comment you must log in.
Revision history for this message
Maximiliano Bertacchini (maxiberta) wrote :

Looks good to me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/browser/gitref.py'
2--- lib/lp/code/browser/gitref.py 2018-08-23 14:52:46 +0000
3+++ lib/lp/code/browser/gitref.py 2018-08-25 16:24:20 +0000
4@@ -12,9 +12,13 @@
5 ]
6
7 import json
8-from urllib import quote_plus
9
10 from lazr.restful.interface import copy_field
11+from six.moves.urllib_parse import (
12+ quote_plus,
13+ urlsplit,
14+ urlunsplit,
15+ )
16 from zope.component import getUtility
17 from zope.formlib.widgets import TextAreaWidget
18 from zope.interface import Interface
19@@ -106,6 +110,14 @@
20 return self.context.display_name
21
22 @property
23+ def git_ssh_url(self):
24+ """The git+ssh:// URL for this branch, adjusted for this user."""
25+ base_url = urlsplit(self.context.repository.git_ssh_url)
26+ url = list(base_url)
27+ url[1] = "{}@{}".format(self.user.name, base_url.hostname)
28+ return urlunsplit(url)
29+
30+ @property
31 def user_can_push(self):
32 """Whether the user can push to this branch."""
33 return (
34
35=== modified file 'lib/lp/code/browser/gitrepository.py'
36--- lib/lp/code/browser/gitrepository.py 2018-06-22 10:01:34 +0000
37+++ lib/lp/code/browser/gitrepository.py 2018-08-25 16:24:20 +0000
38@@ -1,4 +1,4 @@
39-# Copyright 2015-2016 Canonical Ltd. This software is licensed under the
40+# Copyright 2015-2018 Canonical Ltd. This software is licensed under the
41 # GNU Affero General Public License version 3 (see the file LICENSE).
42
43 """Git repository views."""
44@@ -26,6 +26,10 @@
45 copy_field,
46 use_template,
47 )
48+from six.moves.urllib_parse import (
49+ urlsplit,
50+ urlunsplit,
51+ )
52 from zope.component import getUtility
53 from zope.event import notify
54 from zope.formlib import form
55@@ -324,6 +328,14 @@
56 self.request, "launchpad.LimitedView", authorised_people)
57
58 @property
59+ def git_ssh_url(self):
60+ """The git+ssh:// URL for this repository, adjusted for this user."""
61+ base_url = urlsplit(self.context.git_ssh_url)
62+ url = list(base_url)
63+ url[1] = "{}@{}".format(self.user.name, base_url.hostname)
64+ return urlunsplit(url)
65+
66+ @property
67 def user_can_push(self):
68 """Whether the user can push to this branch."""
69 return (
70
71=== modified file 'lib/lp/code/browser/tests/test_gitref.py'
72--- lib/lp/code/browser/tests/test_gitref.py 2018-08-23 14:52:46 +0000
73+++ lib/lp/code/browser/tests/test_gitref.py 2018-08-25 16:24:20 +0000
74@@ -134,11 +134,12 @@
75
76 def test_clone_instructions(self):
77 [ref] = self.factory.makeGitRefs(paths=["refs/heads/branch"])
78+ username = ref.owner.name
79 text = self.getMainText(ref, "+index", user=ref.owner)
80 self.assertTextMatchesExpressionIgnoreWhitespace(r"""
81- git clone -b branch https://.*
82- git clone -b branch git\+ssh://.*
83- """, text)
84+ git clone -b branch https://git.launchpad.dev/.*
85+ git clone -b branch git\+ssh://{username}@git.launchpad.dev/.*
86+ """.format(username=username), text)
87
88 def makeCommitLog(self):
89 authors = [self.factory.makePerson() for _ in range(5)]
90
91=== modified file 'lib/lp/code/browser/tests/test_gitrepository.py'
92--- lib/lp/code/browser/tests/test_gitrepository.py 2018-08-23 09:30:24 +0000
93+++ lib/lp/code/browser/tests/test_gitrepository.py 2018-08-25 16:24:20 +0000
94@@ -93,11 +93,12 @@
95
96 def test_clone_instructions(self):
97 repository = self.factory.makeGitRepository()
98+ username = repository.owner.name
99 text = self.getMainText(repository, "+index", user=repository.owner)
100 self.assertTextMatchesExpressionIgnoreWhitespace(r"""
101- git clone https://.*
102- git clone git\+ssh://.*
103- """, text)
104+ git clone https://git.launchpad.dev/.*
105+ git clone git\+ssh://{username}@git.launchpad.dev/.*
106+ """.format(username=username), text)
107
108 def test_user_can_push(self):
109 # A user can push if they have edit permissions.
110@@ -164,13 +165,15 @@
111 # explain how to do so.
112 self.factory.makeSSHKey(person=self.user, send_notification=False)
113 repository = self.factory.makeGitRepository(owner=self.user)
114+ username = self.user.name
115 browser = self.getViewBrowser(repository)
116 directions = find_tag_by_id(browser.contents, "push-directions")
117 login_person(self.user)
118 self.assertThat(extract_text(directions), DocTestMatches(dedent("""
119 Update this repository:
120- git push git+ssh://git.launchpad.dev/{repository.shortened_path}
121- """).format(repository=repository),
122+ git push
123+ git+ssh://{username}@git.launchpad.dev/{repository.shortened_path}
124+ """).format(username=username, repository=repository),
125 flags=doctest.NORMALIZE_WHITESPACE))
126
127 def test_push_directions_logged_in_can_push_no_sshkeys(self):
128
129=== modified file 'lib/lp/code/templates/git-macros.pt'
130--- lib/lp/code/templates/git-macros.pt 2016-11-17 23:16:12 +0000
131+++ lib/lp/code/templates/git-macros.pt 2018-08-25 16:24:20 +0000
132@@ -30,7 +30,7 @@
133 <tt class="command">
134 git clone
135 <tal:branch condition="branch_name|nothing" replace="string:-b ${branch_name}" />
136- <span class="ssh-url" tal:content="repository/git_ssh_url" />
137+ <span class="ssh-url" tal:content="view/git_ssh_url" />
138 </tt>
139 </tal:ssh>
140 </dd>
141@@ -63,7 +63,7 @@
142 <dd>
143 <tt class="command">
144 git push
145- <span class="ssh-url" tal:content="repository/git_ssh_url" />
146+ <span class="ssh-url" tal:content="view/git_ssh_url" />
147 <tal:branch condition="branch_name|nothing" replace="branch_name" />
148 </tt>
149 </dd>