Commits

Marcin Kuzminski committed 4c71667 Draft

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

Comments (0)

Files changed (4)

rhodecode/lib/hooks.py

 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):
     :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
     :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']

rhodecode/lib/middleware/simplegit.py

 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__)
             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)
 
         #======================================================================
             'repository': repo_name,
             'scm': 'git',
         }
-
+        # set the environ variables for this request
+        os.environ['RC_SCM_DATA'] = json.dumps(extras)
         #===================================================================
         # GIT REQUEST HANDLING
         #===================================================================

rhodecode/lib/middleware/simplehg.py

 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
 
 
             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)
 
         #======================================================================
             'repository': repo_name,
             'scm': 'hg',
         }
-
+        # set the environ variables for this request
+        os.environ['RC_SCM_DATA'] = json.dumps(extras)
         #======================================================================
         # MERCURIAL REQUEST HANDLING
         #======================================================================

rhodecode/lib/utils.py

     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