Merge lp:~jelmer/brz/probe-url into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/probe-url
Merge into: lp:brz
Diff against target: 277 lines (+54/-37)
4 files modified
breezy/plugins/propose/github.py (+18/-13)
breezy/plugins/propose/gitlabs.py (+23/-18)
breezy/plugins/propose/launchpad.py (+3/-3)
breezy/plugins/propose/propose.py (+10/-3)
To merge this branch: bzr merge lp:~jelmer/brz/probe-url
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+362951@code.launchpad.net

Commit message

Split out a 'probe_from_url' method on Hoster.

Description of the change

Split out a 'probe_from_url' method on Hoster.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

Thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/plugins/propose/github.py'
2--- breezy/plugins/propose/github.py 2019-02-14 04:35:05 +0000
3+++ breezy/plugins/propose/github.py 2019-02-15 02:29:22 +0000
4@@ -132,8 +132,7 @@
5 self._pr.edit(state='closed')
6
7
8-def parse_github_url(branch):
9- url = urlutils.split_segment_parameters(branch.user_url)[0]
10+def parse_github_url(url):
11 (scheme, user, password, host, port, path) = urlutils.parse_url(
12 url)
13 if host != 'github.com':
14@@ -141,6 +140,12 @@
15 (owner, repo_name) = path.strip('/').split('/')
16 if repo_name.endswith('.git'):
17 repo_name = repo_name[:-4]
18+ return owner, repo_name
19+
20+
21+def parse_github_branch_url(branch):
22+ url = urlutils.split_segment_parameters(branch.user_url)[0]
23+ owner, repo_name = parse_github_url(url)
24 return owner, repo_name, branch.name
25
26
27@@ -186,7 +191,7 @@
28 owner=None, revision_id=None, overwrite=False,
29 allow_lossy=True):
30 import github
31- base_owner, base_project, base_branch_name = parse_github_url(base_branch)
32+ base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
33 base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
34 if owner is None:
35 owner = self.gh.get_user().login
36@@ -222,14 +227,14 @@
37
38 @convert_github_error
39 def get_push_url(self, branch):
40- owner, project, branch_name = parse_github_url(branch)
41+ owner, project, branch_name = parse_github_branch_url(branch)
42 repo = self.gh.get_repo('%s/%s' % (owner, project))
43 return github_url_to_bzr_url(repo.ssh_url, branch_name)
44
45 @convert_github_error
46 def get_derived_branch(self, base_branch, name, project=None, owner=None):
47 import github
48- base_owner, base_project, base_branch_name = parse_github_url(base_branch)
49+ base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
50 base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
51 if owner is None:
52 owner = self.gh.get_user().login
53@@ -249,9 +254,9 @@
54 @convert_github_error
55 def iter_proposals(self, source_branch, target_branch, status='open'):
56 (source_owner, source_repo_name, source_branch_name) = (
57- parse_github_url(source_branch))
58+ parse_github_branch_url(source_branch))
59 (target_owner, target_repo_name, target_branch_name) = (
60- parse_github_url(target_branch))
61+ parse_github_branch_url(target_branch))
62 target_repo = self.gh.get_repo(
63 "%s/%s" % (target_owner, target_repo_name))
64 state = {
65@@ -277,18 +282,18 @@
66
67 def hosts(self, branch):
68 try:
69- parse_github_url(branch)
70+ parse_github_branch_url(branch)
71 except NotGitHubUrl:
72 return False
73 else:
74 return True
75
76 @classmethod
77- def probe(cls, branch):
78+ def probe_from_url(cls, url):
79 try:
80- parse_github_url(branch)
81+ parse_github_url(url)
82 except NotGitHubUrl:
83- raise UnsupportedHoster(branch)
84+ raise UnsupportedHoster(url)
85 return cls()
86
87 @classmethod
88@@ -319,9 +324,9 @@
89 self.source_branch = source_branch
90 self.target_branch = target_branch
91 (self.target_owner, self.target_repo_name, self.target_branch_name) = (
92- parse_github_url(self.target_branch))
93+ parse_github_branch_url(self.target_branch))
94 (self.source_owner, self.source_repo_name, self.source_branch_name) = (
95- parse_github_url(self.source_branch))
96+ parse_github_branch_url(self.source_branch))
97
98 def get_infotext(self):
99 """Determine the initial comment for the merge proposal."""
100
101=== modified file 'breezy/plugins/propose/gitlabs.py'
102--- breezy/plugins/propose/gitlabs.py 2019-02-12 23:38:25 +0000
103+++ breezy/plugins/propose/gitlabs.py 2019-02-15 02:29:22 +0000
104@@ -110,17 +110,22 @@
105 raise GitLabLoginMissing()
106
107
108-def parse_gitlab_url(branch):
109- url = urlutils.split_segment_parameters(branch.user_url)[0]
110+def parse_gitlab_url(url):
111 (scheme, user, password, host, port, path) = urlutils.parse_url(
112 url)
113 if scheme not in ('git+ssh', 'https', 'http'):
114- raise NotGitLabUrl(branch.user_url)
115+ raise NotGitLabUrl(url)
116 if not host:
117- raise NotGitLabUrl(branch.user_url)
118+ raise NotGitLabUrl(url)
119 path = path.strip('/')
120 if path.endswith('.git'):
121 path = path[:-4]
122+ return host, path
123+
124+
125+def parse_gitlab_branch_url(branch):
126+ url = urlutils.split_segment_parameters(branch.user_url)[0]
127+ host, path = parse_gitlab_url(url)
128 return host, path, branch.name
129
130
131@@ -183,7 +188,7 @@
132 self.gl = gl
133
134 def get_push_url(self, branch):
135- (host, project_name, branch_name) = parse_gitlab_url(branch)
136+ (host, project_name, branch_name) = parse_gitlab_branch_url(branch)
137 project = self.gl.projects.get(project_name)
138 return gitlab_url_to_bzr_url(
139 project.ssh_url_to_repo, branch_name)
140@@ -192,7 +197,7 @@
141 owner=None, revision_id=None, overwrite=False,
142 allow_lossy=True):
143 import gitlab
144- (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)
145+ (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
146 self.gl.auth()
147 try:
148 base_project = self.gl.projects.get(base_project)
149@@ -230,7 +235,7 @@
150
151 def get_derived_branch(self, base_branch, name, project=None, owner=None):
152 import gitlab
153- (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)
154+ (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
155 self.gl.auth()
156 try:
157 base_project = self.gl.projects.get(base_project)
158@@ -258,9 +263,9 @@
159 def iter_proposals(self, source_branch, target_branch, status):
160 import gitlab
161 (source_host, source_project_name, source_branch_name) = (
162- parse_gitlab_url(source_branch))
163+ parse_gitlab_branch_url(source_branch))
164 (target_host, target_project_name, target_branch_name) = (
165- parse_gitlab_url(target_branch))
166+ parse_gitlab_branch_url(target_branch))
167 if source_host != target_host:
168 raise DifferentGitLabInstances(source_host, target_host)
169 self.gl.auth()
170@@ -281,17 +286,17 @@
171
172 def hosts(self, branch):
173 try:
174- (host, project, branch_name) = parse_gitlab_url(branch)
175+ (host, project, branch_name) = parse_gitlab_branch_url(branch)
176 except NotGitLabUrl:
177 return False
178 return (self.gl.url == ('https://%s' % host))
179
180 @classmethod
181- def probe(cls, branch):
182+ def probe_from_url(cls, url):
183 try:
184- (host, project, branch_name) = parse_gitlab_url(branch)
185+ (host, project) = parse_gitlab_url(url)
186 except NotGitLabUrl:
187- raise UnsupportedHoster(branch)
188+ raise UnsupportedHoster(url)
189 import gitlab
190 import requests.exceptions
191 try:
192@@ -299,12 +304,12 @@
193 gl.auth()
194 except requests.exceptions.SSLError:
195 # Well, I guess it could be..
196- raise UnsupportedHoster(branch)
197+ raise UnsupportedHoster(url)
198 except gitlab.GitlabGetError:
199- raise UnsupportedHoster(branch)
200+ raise UnsupportedHoster(url)
201 except gitlab.GitlabHttpError as e:
202 if e.response_code in (404, 405, 503):
203- raise UnsupportedHoster(branch)
204+ raise UnsupportedHoster(url)
205 else:
206 raise
207 return cls(gl)
208@@ -332,10 +337,10 @@
209 self.gl = gl
210 self.source_branch = source_branch
211 (self.source_host, self.source_project_name, self.source_branch_name) = (
212- parse_gitlab_url(source_branch))
213+ parse_gitlab_branch_url(source_branch))
214 self.target_branch = target_branch
215 (self.target_host, self.target_project_name, self.target_branch_name) = (
216- parse_gitlab_url(target_branch))
217+ parse_gitlab_branch_url(target_branch))
218 if self.source_host != self.target_host:
219 raise DifferentGitLabInstances(self.source_host, self.target_host)
220
221
222=== modified file 'breezy/plugins/propose/launchpad.py'
223--- breezy/plugins/propose/launchpad.py 2019-02-02 17:36:19 +0000
224+++ breezy/plugins/propose/launchpad.py 2019-02-15 02:29:22 +0000
225@@ -174,10 +174,10 @@
226 return plausible_launchpad_url(branch.user_url)
227
228 @classmethod
229- def probe(cls, branch):
230- if plausible_launchpad_url(branch.user_url):
231+ def probe_from_url(cls, url):
232+ if plausible_launchpad_url(url):
233 return Launchpad()
234- raise UnsupportedHoster(branch)
235+ raise UnsupportedHoster(url)
236
237 def _get_lp_git_ref_from_branch(self, branch):
238 url, params = urlutils.split_segment_parameters(branch.user_url)
239
240=== modified file 'breezy/plugins/propose/propose.py'
241--- breezy/plugins/propose/propose.py 2019-02-10 00:54:14 +0000
242+++ breezy/plugins/propose/propose.py 2019-02-15 02:29:22 +0000
243@@ -22,6 +22,7 @@
244 errors,
245 hooks,
246 registry,
247+ urlutils,
248 )
249
250
251@@ -225,9 +226,15 @@
252 raise NotImplementedError(self.hosts)
253
254 @classmethod
255- def probe(cls, branch):
256+ def probe_from_branch(cls, branch):
257 """Create a Hoster object if this hoster knows about a branch."""
258- raise NotImplementedError(cls.probe)
259+ url = urlutils.split_segment_parameters(branch.user_url)[0]
260+ return cls.probe_from_url(url)
261+
262+ @classmethod
263+ def probe_from_url(cls, url):
264+ """Create a Hoster object if this hoster knows about a URL."""
265+ raise NotImplementedError(cls.probe_from_url)
266
267 # TODO(jelmer): Some way of cleaning up old branch proposals/branches
268
269@@ -259,7 +266,7 @@
270 return hoster
271 for name, hoster_cls in hosters.items():
272 try:
273- hoster = hoster_cls.probe(branch)
274+ hoster = hoster_cls.probe_from_branch(branch)
275 except UnsupportedHoster:
276 pass
277 else:

Subscribers

People subscribed via source and target branches