Origin: commit, https://code.launchpad.net/~jelmer/brz/probe-url, revision: 7271
Author: Jelmer Vernooĳ <jelmer@jelmer.uk>
Last-Update: 2019-02-15
Applied-Upstream: no
X-Bzr-Revision-Id: jelmer@jelmer.uk-20190215022910-d6rtcihh2bth34k0

=== modified file 'breezy/plugins/propose/github.py'
--- old/breezy/plugins/propose/github.py	2019-02-14 04:35:05 +0000
+++ new/breezy/plugins/propose/github.py	2019-02-14 19:33:19 +0000
@@ -132,8 +132,7 @@
         self._pr.edit(state='closed')
 
 
-def parse_github_url(branch):
-    url = urlutils.split_segment_parameters(branch.user_url)[0]
+def parse_github_url(url):
     (scheme, user, password, host, port, path) = urlutils.parse_url(
         url)
     if host != 'github.com':
@@ -141,6 +140,12 @@
     (owner, repo_name) = path.strip('/').split('/')
     if repo_name.endswith('.git'):
         repo_name = repo_name[:-4]
+    return owner, repo_name
+
+
+def parse_github_branch_url(branch):
+    url = urlutils.split_segment_parameters(branch.user_url)[0]
+    owner, repo_name = parse_github_url(url)
     return owner, repo_name, branch.name
 
 
@@ -186,7 +191,7 @@
                         owner=None, revision_id=None, overwrite=False,
                         allow_lossy=True):
         import github
-        base_owner, base_project, base_branch_name = parse_github_url(base_branch)
+        base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
         base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
         if owner is None:
             owner = self.gh.get_user().login
@@ -222,14 +227,14 @@
 
     @convert_github_error
     def get_push_url(self, branch):
-        owner, project, branch_name = parse_github_url(branch)
+        owner, project, branch_name = parse_github_branch_url(branch)
         repo = self.gh.get_repo('%s/%s' % (owner, project))
         return github_url_to_bzr_url(repo.ssh_url, branch_name)
 
     @convert_github_error
     def get_derived_branch(self, base_branch, name, project=None, owner=None):
         import github
-        base_owner, base_project, base_branch_name = parse_github_url(base_branch)
+        base_owner, base_project, base_branch_name = parse_github_branch_url(base_branch)
         base_repo = self.gh.get_repo('%s/%s' % (base_owner, base_project))
         if owner is None:
             owner = self.gh.get_user().login
@@ -249,9 +254,9 @@
     @convert_github_error
     def iter_proposals(self, source_branch, target_branch, status='open'):
         (source_owner, source_repo_name, source_branch_name) = (
-            parse_github_url(source_branch))
+            parse_github_branch_url(source_branch))
         (target_owner, target_repo_name, target_branch_name) = (
-            parse_github_url(target_branch))
+            parse_github_branch_url(target_branch))
         target_repo = self.gh.get_repo(
             "%s/%s" % (target_owner, target_repo_name))
         state = {
@@ -277,18 +282,18 @@
 
     def hosts(self, branch):
         try:
-            parse_github_url(branch)
+            parse_github_branch_url(branch)
         except NotGitHubUrl:
             return False
         else:
             return True
 
     @classmethod
-    def probe(cls, branch):
+    def probe_from_url(cls, url):
         try:
-            parse_github_url(branch)
+            parse_github_url(url)
         except NotGitHubUrl:
-            raise UnsupportedHoster(branch)
+            raise UnsupportedHoster(url)
         return cls()
 
     @classmethod
@@ -319,9 +324,9 @@
         self.source_branch = source_branch
         self.target_branch = target_branch
         (self.target_owner, self.target_repo_name, self.target_branch_name) = (
-            parse_github_url(self.target_branch))
+            parse_github_branch_url(self.target_branch))
         (self.source_owner, self.source_repo_name, self.source_branch_name) = (
-            parse_github_url(self.source_branch))
+            parse_github_branch_url(self.source_branch))
 
     def get_infotext(self):
         """Determine the initial comment for the merge proposal."""

=== modified file 'breezy/plugins/propose/gitlabs.py'
--- old/breezy/plugins/propose/gitlabs.py	2019-02-12 23:38:25 +0000
+++ new/breezy/plugins/propose/gitlabs.py	2019-02-14 19:33:19 +0000
@@ -110,17 +110,22 @@
             raise GitLabLoginMissing()
 
 
-def parse_gitlab_url(branch):
-    url = urlutils.split_segment_parameters(branch.user_url)[0]
+def parse_gitlab_url(url):
     (scheme, user, password, host, port, path) = urlutils.parse_url(
         url)
     if scheme not in ('git+ssh', 'https', 'http'):
-        raise NotGitLabUrl(branch.user_url)
+        raise NotGitLabUrl(url)
     if not host:
-        raise NotGitLabUrl(branch.user_url)
+        raise NotGitLabUrl(url)
     path = path.strip('/')
     if path.endswith('.git'):
         path = path[:-4]
+    return host, path
+
+
+def parse_gitlab_branch_url(branch):
+    url = urlutils.split_segment_parameters(branch.user_url)[0]
+    host, path = parse_gitlab_url(url)
     return host, path, branch.name
 
 
@@ -183,7 +188,7 @@
         self.gl = gl
 
     def get_push_url(self, branch):
-        (host, project_name, branch_name) = parse_gitlab_url(branch)
+        (host, project_name, branch_name) = parse_gitlab_branch_url(branch)
         project = self.gl.projects.get(project_name)
         return gitlab_url_to_bzr_url(
             project.ssh_url_to_repo, branch_name)
@@ -192,7 +197,7 @@
                         owner=None, revision_id=None, overwrite=False,
                         allow_lossy=True):
         import gitlab
-        (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)
+        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
         self.gl.auth()
         try:
             base_project = self.gl.projects.get(base_project)
@@ -230,7 +235,7 @@
 
     def get_derived_branch(self, base_branch, name, project=None, owner=None):
         import gitlab
-        (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)
+        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
         self.gl.auth()
         try:
             base_project = self.gl.projects.get(base_project)
@@ -258,9 +263,9 @@
     def iter_proposals(self, source_branch, target_branch, status):
         import gitlab
         (source_host, source_project_name, source_branch_name) = (
-            parse_gitlab_url(source_branch))
+            parse_gitlab_branch_url(source_branch))
         (target_host, target_project_name, target_branch_name) = (
-            parse_gitlab_url(target_branch))
+            parse_gitlab_branch_url(target_branch))
         if source_host != target_host:
             raise DifferentGitLabInstances(source_host, target_host)
         self.gl.auth()
@@ -281,17 +286,17 @@
 
     def hosts(self, branch):
         try:
-            (host, project, branch_name) = parse_gitlab_url(branch)
+            (host, project, branch_name) = parse_gitlab_branch_url(branch)
         except NotGitLabUrl:
             return False
         return (self.gl.url == ('https://%s' % host))
 
     @classmethod
-    def probe(cls, branch):
+    def probe_from_url(cls, url):
         try:
-            (host, project, branch_name) = parse_gitlab_url(branch)
+            (host, project) = parse_gitlab_url(url)
         except NotGitLabUrl:
-            raise UnsupportedHoster(branch)
+            raise UnsupportedHoster(url)
         import gitlab
         import requests.exceptions
         try:
@@ -299,12 +304,12 @@
             gl.auth()
         except requests.exceptions.SSLError:
             # Well, I guess it could be..
-            raise UnsupportedHoster(branch)
+            raise UnsupportedHoster(url)
         except gitlab.GitlabGetError:
-            raise UnsupportedHoster(branch)
+            raise UnsupportedHoster(url)
         except gitlab.GitlabHttpError as e:
             if e.response_code in (404, 405, 503):
-                raise UnsupportedHoster(branch)
+                raise UnsupportedHoster(url)
             else:
                 raise
         return cls(gl)
@@ -332,10 +337,10 @@
         self.gl = gl
         self.source_branch = source_branch
         (self.source_host, self.source_project_name, self.source_branch_name) = (
-            parse_gitlab_url(source_branch))
+            parse_gitlab_branch_url(source_branch))
         self.target_branch = target_branch
         (self.target_host, self.target_project_name, self.target_branch_name) = (
-            parse_gitlab_url(target_branch))
+            parse_gitlab_branch_url(target_branch))
         if self.source_host != self.target_host:
             raise DifferentGitLabInstances(self.source_host, self.target_host)
 

=== modified file 'breezy/plugins/propose/launchpad.py'
--- old/breezy/plugins/propose/launchpad.py	2019-02-02 17:36:19 +0000
+++ new/breezy/plugins/propose/launchpad.py	2019-02-10 18:37:58 +0000
@@ -174,10 +174,10 @@
         return plausible_launchpad_url(branch.user_url)
 
     @classmethod
-    def probe(cls, branch):
-        if plausible_launchpad_url(branch.user_url):
+    def probe_from_url(cls, url):
+        if plausible_launchpad_url(url):
             return Launchpad()
-        raise UnsupportedHoster(branch)
+        raise UnsupportedHoster(url)
 
     def _get_lp_git_ref_from_branch(self, branch):
         url, params = urlutils.split_segment_parameters(branch.user_url)

=== modified file 'breezy/plugins/propose/propose.py'
--- old/breezy/plugins/propose/propose.py	2019-02-10 00:54:14 +0000
+++ new/breezy/plugins/propose/propose.py	2019-02-15 02:29:10 +0000
@@ -22,6 +22,7 @@
     errors,
     hooks,
     registry,
+    urlutils,
     )
 
 
@@ -225,9 +226,15 @@
         raise NotImplementedError(self.hosts)
 
     @classmethod
-    def probe(cls, branch):
+    def probe_from_branch(cls, branch):
         """Create a Hoster object if this hoster knows about a branch."""
-        raise NotImplementedError(cls.probe)
+        url = urlutils.split_segment_parameters(branch.user_url)[0]
+        return cls.probe_from_url(url)
+
+    @classmethod
+    def probe_from_url(cls, url):
+        """Create a Hoster object if this hoster knows about a URL."""
+        raise NotImplementedError(cls.probe_from_url)
 
     # TODO(jelmer): Some way of cleaning up old branch proposals/branches
 
@@ -259,7 +266,7 @@
                 return hoster
     for name, hoster_cls in hosters.items():
         try:
-            hoster = hoster_cls.probe(branch)
+            hoster = hoster_cls.probe_from_branch(branch)
         except UnsupportedHoster:
             pass
         else:

