changeset 2716:4c71667160e5 beta

use os.environ as a fallback for getting special info from hooks, this will allow calling RhodeCode hooks from outside the system eg. via SSH - also verify repo if it's a correct VCS throw 404 error otherwise
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 15 Aug 2012 00:22:53 +0200
parents 298bac3757a7
children dd240b2b7a12
files rhodecode/lib/hooks.py rhodecode/lib/middleware/simplegit.py rhodecode/lib/middleware/simplehg.py rhodecode/lib/utils.py
diffstat 4 files changed, 49 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/hooks.py	Tue Aug 14 01:16:29 2012 +0200
+++ b/rhodecode/lib/hooks.py	Wed Aug 15 00:22:53 2012 +0200
@@ -33,6 +33,7 @@
 from rhodecode.lib import helpers as h
 from rhodecode.lib.utils import action_logger
 from rhodecode.lib.vcs.backends.base import EmptyChangeset
+from rhodecode.lib.compat import json
 
 
 def _get_scm_size(alias, root_path):
@@ -90,12 +91,23 @@
     :param ui:
     :param repo:
     """
+    try:
+        rc_extras = json.loads(os.environ.get('RC_SCM_DATA', "{}"))
+    except:
+        rc_extras = {}
     extras = dict(repo.ui.configitems('rhodecode_extras'))
-    username = extras['username']
-    repository = extras['repository']
-    scm = extras['scm']
+    if 'username' in extras:
+        username = extras['username']
+        repository = extras['repository']
+        scm = extras['scm']
+    elif 'username' in rc_extras:
+        username = rc_extras['username']
+        repository = rc_extras['repository']
+        scm = rc_extras['scm']
+    else:
+        raise Exception('Missing data in repo.ui and os.environ')
+
     action = 'pull'
-
     action_logger(username, action, repository, extras['ip'], commit=True)
     # extension hook call
     from rhodecode import EXTENSIONS
@@ -116,11 +128,24 @@
     :param repo: repo object containing the `ui` object
     """
 
+    try:
+        rc_extras = json.loads(os.environ.get('RC_SCM_DATA', "{}"))
+    except:
+        rc_extras = {}
+
     extras = dict(repo.ui.configitems('rhodecode_extras'))
-    username = extras['username']
-    repository = extras['repository']
-    action = extras['action'] + ':%s'
-    scm = extras['scm']
+    if 'username' in extras:
+        username = extras['username']
+        repository = extras['repository']
+        scm = extras['scm']
+    elif 'username' in rc_extras:
+        username = rc_extras['username']
+        repository = rc_extras['repository']
+        scm = rc_extras['scm']
+    else:
+        raise Exception('Missing data in repo.ui and os.environ')
+
+    action = 'push' + ':%s'
 
     if scm == 'hg':
         node = kwargs['node']
--- a/rhodecode/lib/middleware/simplegit.py	Tue Aug 14 01:16:29 2012 +0200
+++ b/rhodecode/lib/middleware/simplegit.py	Wed Aug 15 00:22:53 2012 +0200
@@ -81,6 +81,7 @@
 from rhodecode.lib.base import BaseVCSController
 from rhodecode.lib.auth import get_container_username
 from rhodecode.lib.utils import is_valid_repo, make_ui
+from rhodecode.lib.compat import json
 from rhodecode.model.db import User, RhodeCodeUi
 
 log = logging.getLogger(__name__)
@@ -122,7 +123,7 @@
             return HTTPInternalServerError()(environ, start_response)
 
         # quick check if that dir exists...
-        if is_valid_repo(repo_name, self.basepath) is False:
+        if is_valid_repo(repo_name, self.basepath, 'git') is False:
             return HTTPNotFound()(environ, start_response)
 
         #======================================================================
@@ -190,7 +191,8 @@
             'repository': repo_name,
             'scm': 'git',
         }
-
+        # set the environ variables for this request
+        os.environ['RC_SCM_DATA'] = json.dumps(extras)
         #===================================================================
         # GIT REQUEST HANDLING
         #===================================================================
--- a/rhodecode/lib/middleware/simplehg.py	Tue Aug 14 01:16:29 2012 +0200
+++ b/rhodecode/lib/middleware/simplehg.py	Wed Aug 15 00:22:53 2012 +0200
@@ -40,6 +40,7 @@
 from rhodecode.lib.base import BaseVCSController
 from rhodecode.lib.auth import get_container_username
 from rhodecode.lib.utils import make_ui, is_valid_repo, ui_sections
+from rhodecode.lib.compat import json
 from rhodecode.model.db import User
 
 
@@ -87,7 +88,7 @@
             return HTTPInternalServerError()(environ, start_response)
 
         # quick check if that dir exists...
-        if is_valid_repo(repo_name, self.basepath) is False:
+        if is_valid_repo(repo_name, self.basepath, 'hg') is False:
             return HTTPNotFound()(environ, start_response)
 
         #======================================================================
@@ -157,7 +158,8 @@
             'repository': repo_name,
             'scm': 'hg',
         }
-
+        # set the environ variables for this request
+        os.environ['RC_SCM_DATA'] = json.dumps(extras)
         #======================================================================
         # MERCURIAL REQUEST HANDLING
         #======================================================================
--- a/rhodecode/lib/utils.py	Tue Aug 14 01:16:29 2012 +0200
+++ b/rhodecode/lib/utils.py	Wed Aug 15 00:22:53 2012 +0200
@@ -203,19 +203,24 @@
     return _get_repos(path)
 
 
-def is_valid_repo(repo_name, base_path):
+def is_valid_repo(repo_name, base_path, scm=None):
     """
-    Returns True if given path is a valid repository False otherwise
+    Returns True if given path is a valid repository False otherwise.
+    If scm param is given also compare if given scm is the same as expected 
+    from scm parameter
 
     :param repo_name:
     :param base_path:
+    :param scm:
 
     :return True: if given path is a valid repository
     """
     full_path = os.path.join(safe_str(base_path), safe_str(repo_name))
 
     try:
-        get_scm(full_path)
+        scm_ = get_scm(full_path)
+        if scm:
+            return scm_[0] == scm
         return True
     except VCSError:
         return False