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
=== modified file 'breezy/plugins/propose/github.py'
--- breezy/plugins/propose/github.py 2019-02-14 04:35:05 +0000
+++ breezy/plugins/propose/github.py 2019-02-15 02:29:22 +0000
@@ -132,8 +132,7 @@
132 self._pr.edit(state='closed')132 self._pr.edit(state='closed')
133133
134134
135def parse_github_url(branch):135def parse_github_url(url):
136 url = urlutils.split_segment_parameters(branch.user_url)[0]
137 (scheme, user, password, host, port, path) = urlutils.parse_url(136 (scheme, user, password, host, port, path) = urlutils.parse_url(
138 url)137 url)
139 if host != 'github.com':138 if host != 'github.com':
@@ -141,6 +140,12 @@
141 (owner, repo_name) = path.strip('/').split('/')140 (owner, repo_name) = path.strip('/').split('/')
142 if repo_name.endswith('.git'):141 if repo_name.endswith('.git'):
143 repo_name = repo_name[:-4]142 repo_name = repo_name[:-4]
143 return owner, repo_name
144
145
146def parse_github_branch_url(branch):
147 url = urlutils.split_segment_parameters(branch.user_url)[0]
148 owner, repo_name = parse_github_url(url)
144 return owner, repo_name, branch.name149 return owner, repo_name, branch.name
145150
146151
@@ -186,7 +191,7 @@
186 owner=None, revision_id=None, overwrite=False,191 owner=None, revision_id=None, overwrite=False,
187 allow_lossy=True):192 allow_lossy=True):
188 import github193 import github
189 base_owner, base_project, base_branch_name = parse_github_url(base_branch)194 base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
190 base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))195 base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
191 if owner is None:196 if owner is None:
192 owner = self.gh.get_user().login197 owner = self.gh.get_user().login
@@ -222,14 +227,14 @@
222227
223 @convert_github_error228 @convert_github_error
224 def get_push_url(self, branch):229 def get_push_url(self, branch):
225 owner, project, branch_name = parse_github_url(branch)230 owner, project, branch_name = parse_github_branch_url(branch)
226 repo = self.gh.get_repo('%s/%s' % (owner, project))231 repo = self.gh.get_repo('%s/%s' % (owner, project))
227 return github_url_to_bzr_url(repo.ssh_url, branch_name)232 return github_url_to_bzr_url(repo.ssh_url, branch_name)
228233
229 @convert_github_error234 @convert_github_error
230 def get_derived_branch(self, base_branch, name, project=None, owner=None):235 def get_derived_branch(self, base_branch, name, project=None, owner=None):
231 import github236 import github
232 base_owner, base_project, base_branch_name = parse_github_url(base_branch)237 base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
233 base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))238 base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
234 if owner is None:239 if owner is None:
235 owner = self.gh.get_user().login240 owner = self.gh.get_user().login
@@ -249,9 +254,9 @@
249 @convert_github_error254 @convert_github_error
250 def iter_proposals(self, source_branch, target_branch, status='open'):255 def iter_proposals(self, source_branch, target_branch, status='open'):
251 (source_owner, source_repo_name, source_branch_name) = (256 (source_owner, source_repo_name, source_branch_name) = (
252 parse_github_url(source_branch))257 parse_github_branch_url(source_branch))
253 (target_owner, target_repo_name, target_branch_name) = (258 (target_owner, target_repo_name, target_branch_name) = (
254 parse_github_url(target_branch))259 parse_github_branch_url(target_branch))
255 target_repo = self.gh.get_repo(260 target_repo = self.gh.get_repo(
256 "%s/%s" % (target_owner, target_repo_name))261 "%s/%s" % (target_owner, target_repo_name))
257 state = {262 state = {
@@ -277,18 +282,18 @@
277282
278 def hosts(self, branch):283 def hosts(self, branch):
279 try:284 try:
280 parse_github_url(branch)285 parse_github_branch_url(branch)
281 except NotGitHubUrl:286 except NotGitHubUrl:
282 return False287 return False
283 else:288 else:
284 return True289 return True
285290
286 @classmethod291 @classmethod
287 def probe(cls, branch):292 def probe_from_url(cls, url):
288 try:293 try:
289 parse_github_url(branch)294 parse_github_url(url)
290 except NotGitHubUrl:295 except NotGitHubUrl:
291 raise UnsupportedHoster(branch)296 raise UnsupportedHoster(url)
292 return cls()297 return cls()
293298
294 @classmethod299 @classmethod
@@ -319,9 +324,9 @@
319 self.source_branch = source_branch324 self.source_branch = source_branch
320 self.target_branch = target_branch325 self.target_branch = target_branch
321 (self.target_owner, self.target_repo_name, self.target_branch_name) = (326 (self.target_owner, self.target_repo_name, self.target_branch_name) = (
322 parse_github_url(self.target_branch))327 parse_github_branch_url(self.target_branch))
323 (self.source_owner, self.source_repo_name, self.source_branch_name) = (328 (self.source_owner, self.source_repo_name, self.source_branch_name) = (
324 parse_github_url(self.source_branch))329 parse_github_branch_url(self.source_branch))
325330
326 def get_infotext(self):331 def get_infotext(self):
327 """Determine the initial comment for the merge proposal."""332 """Determine the initial comment for the merge proposal."""
328333
=== modified file 'breezy/plugins/propose/gitlabs.py'
--- breezy/plugins/propose/gitlabs.py 2019-02-12 23:38:25 +0000
+++ breezy/plugins/propose/gitlabs.py 2019-02-15 02:29:22 +0000
@@ -110,17 +110,22 @@
110 raise GitLabLoginMissing()110 raise GitLabLoginMissing()
111111
112112
113def parse_gitlab_url(branch):113def parse_gitlab_url(url):
114 url = urlutils.split_segment_parameters(branch.user_url)[0]
115 (scheme, user, password, host, port, path) = urlutils.parse_url(114 (scheme, user, password, host, port, path) = urlutils.parse_url(
116 url)115 url)
117 if scheme not in ('git+ssh', 'https', 'http'):116 if scheme not in ('git+ssh', 'https', 'http'):
118 raise NotGitLabUrl(branch.user_url)117 raise NotGitLabUrl(url)
119 if not host:118 if not host:
120 raise NotGitLabUrl(branch.user_url)119 raise NotGitLabUrl(url)
121 path = path.strip('/')120 path = path.strip('/')
122 if path.endswith('.git'):121 if path.endswith('.git'):
123 path = path[:-4]122 path = path[:-4]
123 return host, path
124
125
126def parse_gitlab_branch_url(branch):
127 url = urlutils.split_segment_parameters(branch.user_url)[0]
128 host, path = parse_gitlab_url(url)
124 return host, path, branch.name129 return host, path, branch.name
125130
126131
@@ -183,7 +188,7 @@
183 self.gl = gl188 self.gl = gl
184189
185 def get_push_url(self, branch):190 def get_push_url(self, branch):
186 (host, project_name, branch_name) = parse_gitlab_url(branch)191 (host, project_name, branch_name) = parse_gitlab_branch_url(branch)
187 project = self.gl.projects.get(project_name)192 project = self.gl.projects.get(project_name)
188 return gitlab_url_to_bzr_url(193 return gitlab_url_to_bzr_url(
189 project.ssh_url_to_repo, branch_name)194 project.ssh_url_to_repo, branch_name)
@@ -192,7 +197,7 @@
192 owner=None, revision_id=None, overwrite=False,197 owner=None, revision_id=None, overwrite=False,
193 allow_lossy=True):198 allow_lossy=True):
194 import gitlab199 import gitlab
195 (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)200 (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
196 self.gl.auth()201 self.gl.auth()
197 try:202 try:
198 base_project = self.gl.projects.get(base_project)203 base_project = self.gl.projects.get(base_project)
@@ -230,7 +235,7 @@
230235
231 def get_derived_branch(self, base_branch, name, project=None, owner=None):236 def get_derived_branch(self, base_branch, name, project=None, owner=None):
232 import gitlab237 import gitlab
233 (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)238 (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
234 self.gl.auth()239 self.gl.auth()
235 try:240 try:
236 base_project = self.gl.projects.get(base_project)241 base_project = self.gl.projects.get(base_project)
@@ -258,9 +263,9 @@
258 def iter_proposals(self, source_branch, target_branch, status):263 def iter_proposals(self, source_branch, target_branch, status):
259 import gitlab264 import gitlab
260 (source_host, source_project_name, source_branch_name) = (265 (source_host, source_project_name, source_branch_name) = (
261 parse_gitlab_url(source_branch))266 parse_gitlab_branch_url(source_branch))
262 (target_host, target_project_name, target_branch_name) = (267 (target_host, target_project_name, target_branch_name) = (
263 parse_gitlab_url(target_branch))268 parse_gitlab_branch_url(target_branch))
264 if source_host != target_host:269 if source_host != target_host:
265 raise DifferentGitLabInstances(source_host, target_host)270 raise DifferentGitLabInstances(source_host, target_host)
266 self.gl.auth()271 self.gl.auth()
@@ -281,17 +286,17 @@
281286
282 def hosts(self, branch):287 def hosts(self, branch):
283 try:288 try:
284 (host, project, branch_name) = parse_gitlab_url(branch)289 (host, project, branch_name) = parse_gitlab_branch_url(branch)
285 except NotGitLabUrl:290 except NotGitLabUrl:
286 return False291 return False
287 return (self.gl.url == ('https://%s' % host))292 return (self.gl.url == ('https://%s' % host))
288293
289 @classmethod294 @classmethod
290 def probe(cls, branch):295 def probe_from_url(cls, url):
291 try:296 try:
292 (host, project, branch_name) = parse_gitlab_url(branch)297 (host, project) = parse_gitlab_url(url)
293 except NotGitLabUrl:298 except NotGitLabUrl:
294 raise UnsupportedHoster(branch)299 raise UnsupportedHoster(url)
295 import gitlab300 import gitlab
296 import requests.exceptions301 import requests.exceptions
297 try:302 try:
@@ -299,12 +304,12 @@
299 gl.auth()304 gl.auth()
300 except requests.exceptions.SSLError:305 except requests.exceptions.SSLError:
301 # Well, I guess it could be..306 # Well, I guess it could be..
302 raise UnsupportedHoster(branch)307 raise UnsupportedHoster(url)
303 except gitlab.GitlabGetError:308 except gitlab.GitlabGetError:
304 raise UnsupportedHoster(branch)309 raise UnsupportedHoster(url)
305 except gitlab.GitlabHttpError as e:310 except gitlab.GitlabHttpError as e:
306 if e.response_code in (404, 405, 503):311 if e.response_code in (404, 405, 503):
307 raise UnsupportedHoster(branch)312 raise UnsupportedHoster(url)
308 else:313 else:
309 raise314 raise
310 return cls(gl)315 return cls(gl)
@@ -332,10 +337,10 @@
332 self.gl = gl337 self.gl = gl
333 self.source_branch = source_branch338 self.source_branch = source_branch
334 (self.source_host, self.source_project_name, self.source_branch_name) = (339 (self.source_host, self.source_project_name, self.source_branch_name) = (
335 parse_gitlab_url(source_branch))340 parse_gitlab_branch_url(source_branch))
336 self.target_branch = target_branch341 self.target_branch = target_branch
337 (self.target_host, self.target_project_name, self.target_branch_name) = (342 (self.target_host, self.target_project_name, self.target_branch_name) = (
338 parse_gitlab_url(target_branch))343 parse_gitlab_branch_url(target_branch))
339 if self.source_host != self.target_host:344 if self.source_host != self.target_host:
340 raise DifferentGitLabInstances(self.source_host, self.target_host)345 raise DifferentGitLabInstances(self.source_host, self.target_host)
341346
342347
=== modified file 'breezy/plugins/propose/launchpad.py'
--- breezy/plugins/propose/launchpad.py 2019-02-02 17:36:19 +0000
+++ breezy/plugins/propose/launchpad.py 2019-02-15 02:29:22 +0000
@@ -174,10 +174,10 @@
174 return plausible_launchpad_url(branch.user_url)174 return plausible_launchpad_url(branch.user_url)
175175
176 @classmethod176 @classmethod
177 def probe(cls, branch):177 def probe_from_url(cls, url):
178 if plausible_launchpad_url(branch.user_url):178 if plausible_launchpad_url(url):
179 return Launchpad()179 return Launchpad()
180 raise UnsupportedHoster(branch)180 raise UnsupportedHoster(url)
181181
182 def _get_lp_git_ref_from_branch(self, branch):182 def _get_lp_git_ref_from_branch(self, branch):
183 url, params = urlutils.split_segment_parameters(branch.user_url)183 url, params = urlutils.split_segment_parameters(branch.user_url)
184184
=== modified file 'breezy/plugins/propose/propose.py'
--- breezy/plugins/propose/propose.py 2019-02-10 00:54:14 +0000
+++ breezy/plugins/propose/propose.py 2019-02-15 02:29:22 +0000
@@ -22,6 +22,7 @@
22 errors,22 errors,
23 hooks,23 hooks,
24 registry,24 registry,
25 urlutils,
25 )26 )
2627
2728
@@ -225,9 +226,15 @@
225 raise NotImplementedError(self.hosts)226 raise NotImplementedError(self.hosts)
226227
227 @classmethod228 @classmethod
228 def probe(cls, branch):229 def probe_from_branch(cls, branch):
229 """Create a Hoster object if this hoster knows about a branch."""230 """Create a Hoster object if this hoster knows about a branch."""
230 raise NotImplementedError(cls.probe)231 url = urlutils.split_segment_parameters(branch.user_url)[0]
232 return cls.probe_from_url(url)
233
234 @classmethod
235 def probe_from_url(cls, url):
236 """Create a Hoster object if this hoster knows about a URL."""
237 raise NotImplementedError(cls.probe_from_url)
231238
232 # TODO(jelmer): Some way of cleaning up old branch proposals/branches239 # TODO(jelmer): Some way of cleaning up old branch proposals/branches
233240
@@ -259,7 +266,7 @@
259 return hoster266 return hoster
260 for name, hoster_cls in hosters.items():267 for name, hoster_cls in hosters.items():
261 try:268 try:
262 hoster = hoster_cls.probe(branch)269 hoster = hoster_cls.probe_from_branch(branch)
263 except UnsupportedHoster:270 except UnsupportedHoster:
264 pass271 pass
265 else:272 else:

Subscribers

People subscribed via source and target branches