Merge lp:~abentley/launchpad/branch-url into lp:launchpad

Proposed by Aaron Bentley on 2010-02-16
Status: Merged
Approved by: Michael Hudson-Doyle on 2010-02-17
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~abentley/launchpad/branch-url
Merge into: lp:launchpad
Prerequisite: lp:~jtv/launchpad/branch-url
Diff against target: 108 lines (+20/-26)
2 files modified
lib/lp/code/model/branch.py (+18/-14)
lib/lp/code/xmlrpc/branch.py (+2/-12)
To merge this branch: bzr merge lp:~abentley/launchpad/branch-url
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle Approve on 2010-02-17
Jeroen T. Vermeulen 2010-02-16 Pending
Review via email: mp+19441@code.launchpad.net
To post a comment you must log in.
Aaron Bentley (abentley) wrote :

This branch refactors code.model.branch.Branch.composePublicUrl and code.xmlrpc.branch.PublicCodehostingAPI to use common code for generating branch URLs.

Because the latter generates URLs using a unique name if no branch exists, I extracted most of Branch.composePublicUrl into a function, compose_public_url, that takes unique_name as a parameter.

There are no new tests, but the code passes existing tests for composePublicUrl and PublicCodehostingAPI.

Michael Hudson-Doyle (mwhudson) wrote :

Makes sense 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/model/branch.py'
2--- lib/lp/code/model/branch.py 2010-02-16 20:47:17 +0000
3+++ lib/lp/code/model/branch.py 2010-02-16 20:47:18 +0000
4@@ -7,9 +7,11 @@
5 __all__ = [
6 'Branch',
7 'BranchSet',
8+ 'compose_public_url',
9 ]
10
11 from datetime import datetime
12+import os.path
13
14 from bzrlib.branch import Branch as BzrBranch
15 from bzrlib.revision import NULL_REVISION
16@@ -481,24 +483,11 @@
17
18 def composePublicURL(self, scheme='http'):
19 """See `IBranch`."""
20- # Avoid circular imports.
21- from lp.code.xmlrpc.branch import PublicCodehostingAPI
22-
23- # Accept sftp as a legacy protocol.
24- accepted_schemes = set(PublicCodehostingAPI.supported_schemes)
25- accepted_schemes.add('sftp')
26-
27 # Not all protocols work for private branches.
28 public_schemes = ['http']
29-
30- assert scheme in accepted_schemes, "Unknown scheme: %s" % scheme
31 assert not (self.private and scheme in public_schemes), (
32 "Private branch %s has no public URL." % self.unique_name)
33-
34- host = URI(config.codehosting.supermirror_root).host
35- path = '/' + self.unique_name
36-
37- return str(URI(scheme=scheme, host=host, path=path))
38+ return compose_public_url(scheme, self.unique_name)
39
40 @property
41 def warehouse_url(self):
42@@ -1309,3 +1298,18 @@
43 """
44 update_trigger_modified_fields(branch)
45 send_branch_modified_notifications(branch, event)
46+
47+
48+def compose_public_url(scheme, unique_name, suffix=None):
49+ # Avoid circular imports.
50+ from lp.code.xmlrpc.branch import PublicCodehostingAPI
51+
52+ # Accept sftp as a legacy protocol.
53+ accepted_schemes = set(PublicCodehostingAPI.supported_schemes)
54+ accepted_schemes.add('sftp')
55+ assert scheme in accepted_schemes, "Unknown scheme: %s" % scheme
56+ host = URI(config.codehosting.supermirror_root).host
57+ path = '/' + unique_name
58+ if suffix is not None:
59+ path = os.path.join(path, suffix)
60+ return str(URI(scheme=scheme, host=host, path=path))
61
62=== modified file 'lib/lp/code/xmlrpc/branch.py'
63--- lib/lp/code/xmlrpc/branch.py 2009-07-17 00:26:05 +0000
64+++ lib/lp/code/xmlrpc/branch.py 2010-02-16 20:47:18 +0000
65@@ -11,13 +11,10 @@
66 'BranchSetAPI', 'IBranchSetAPI', 'IPublicCodehostingAPI',
67 'PublicCodehostingAPI']
68
69-import os
70
71 from zope.component import getUtility
72 from zope.interface import Interface, implements
73
74-from lazr.uri import URI
75-
76 from canonical.config import config
77 from lp.bugs.interfaces.bug import IBugSet
78 from canonical.launchpad.webapp.interfaces import ILaunchBag, NotFoundError
79@@ -185,10 +182,6 @@
80
81 supported_schemes = 'bzr+ssh', 'http'
82
83- def _getBazaarHost(self):
84- """Return the hostname for the codehosting server."""
85- return URI(config.codehosting.supermirror_root).host
86-
87 def _getResultDict(self, branch, suffix=None, supported_schemes=None):
88 """Return a result dict with a list of URLs for the given branch.
89
90@@ -207,16 +200,13 @@
91
92 def _getUniqueNameResultDict(self, unique_name, suffix=None,
93 supported_schemes=None):
94+ from lp.code.model.branch import compose_public_url
95 if supported_schemes is None:
96 supported_schemes = self.supported_schemes
97 result = dict(urls=[])
98- host = self._getBazaarHost()
99- path = '/' + unique_name
100- if suffix is not None:
101- path = os.path.join(path, suffix)
102 for scheme in supported_schemes:
103 result['urls'].append(
104- str(URI(host=host, scheme=scheme, path=path)))
105+ compose_public_url(scheme, unique_name, suffix))
106 return result
107
108 @return_fault