changeset 7311:02e0d2d469bf stable

utils: move clone URI validator function to more generic utils.is_valid_repo_uri No changes to the functionality, even though the API and implementation could use some clean-up ...
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 07 May 2018 11:38:13 +0200
parents d64cf8f33f6f
children fa3365c94064
files kallithea/lib/utils.py kallithea/model/validators.py
diffstat 2 files changed, 32 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/utils.py	Mon May 07 11:37:55 2018 +0200
+++ b/kallithea/lib/utils.py	Mon May 07 11:38:13 2018 +0200
@@ -265,6 +265,36 @@
         dirs[:] = recurse_dirs
 
 
+def is_valid_repo_uri(repo_type, url, ui):
+    """Check if the url seems like a valid remote repo location - raise an Exception if any problems"""
+    if repo_type == 'hg':
+        from kallithea.lib.vcs.backends.hg.repository import MercurialRepository
+        if url.startswith('http') or url.startswith('ssh'):
+            # initially check if it's at least the proper URL
+            # or does it pass basic auth
+            MercurialRepository._check_url(url, ui)
+        elif url.startswith('svn+http'):
+            from hgsubversion.svnrepo import svnremoterepo
+            svnremoterepo(ui, url).svn.uuid
+        elif url.startswith('git+http'):
+            raise NotImplementedError()
+        else:
+            raise Exception('URI %s not allowed' % (url,))
+
+    elif repo_type == 'git':
+        from kallithea.lib.vcs.backends.git.repository import GitRepository
+        if url.startswith('http') or url.startswith('git'):
+            # initially check if it's at least the proper URL
+            # or does it pass basic auth
+            GitRepository._check_url(url)
+        elif url.startswith('svn+http'):
+            raise NotImplementedError()
+        elif url.startswith('hg+http'):
+            raise NotImplementedError()
+        else:
+            raise Exception('URI %s not allowed' % (url))
+
+
 def is_valid_repo(repo_name, base_path, scm=None):
     """
     Returns True if given path is a valid repository False otherwise.
--- a/kallithea/model/validators.py	Mon May 07 11:37:55 2018 +0200
+++ b/kallithea/model/validators.py	Mon May 07 11:38:13 2018 +0200
@@ -30,7 +30,7 @@
 )
 from kallithea.lib.compat import OrderedSet
 from kallithea.lib import ipaddr
-from kallithea.lib.utils import repo_name_slug
+from kallithea.lib.utils import repo_name_slug, is_valid_repo_uri
 from kallithea.lib.utils2 import str2bool, aslist
 from kallithea.model.db import RepoGroup, Repository, UserGroup, User
 from kallithea.lib.exceptions import LdapImportError
@@ -432,34 +432,6 @@
 def ValidCloneUri():
     from kallithea.lib.utils import make_ui
 
-    def url_handler(repo_type, url, ui):
-        if repo_type == 'hg':
-            from kallithea.lib.vcs.backends.hg.repository import MercurialRepository
-            if url.startswith('http') or url.startswith('ssh'):
-                # initially check if it's at least the proper URL
-                # or does it pass basic auth
-                MercurialRepository._check_url(url, ui)
-            elif url.startswith('svn+http'):
-                from hgsubversion.svnrepo import svnremoterepo
-                svnremoterepo(ui, url).svn.uuid
-            elif url.startswith('git+http'):
-                raise NotImplementedError()
-            else:
-                raise Exception('clone from URI %s not allowed' % (url,))
-
-        elif repo_type == 'git':
-            from kallithea.lib.vcs.backends.git.repository import GitRepository
-            if url.startswith('http') or url.startswith('git'):
-                # initially check if it's at least the proper URL
-                # or does it pass basic auth
-                GitRepository._check_url(url)
-            elif url.startswith('svn+http'):
-                raise NotImplementedError()
-            elif url.startswith('hg+http'):
-                raise NotImplementedError()
-            else:
-                raise Exception('clone from URI %s not allowed' % (url))
-
     class _validator(formencode.validators.FancyValidator):
         messages = {
             'clone_uri': _('Invalid repository URL'),
@@ -473,7 +445,7 @@
 
             if url and url != value.get('clone_uri_hidden'):
                 try:
-                    url_handler(repo_type, url, make_ui('db', clear_session=False))
+                    is_valid_repo_uri(repo_type, url, make_ui('db', clear_session=False))
                 except Exception:
                     log.exception('URL validation failed')
                     msg = M(self, 'clone_uri')