Changeset - 4c71667160e5
[Not reviewed]
beta
0 4 0
Marcin Kuzminski - 13 years ago 2012-08-15 00:22:53
marcin@python-works.com
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
4 files changed with 49 insertions and 15 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/hooks.py
Show inline comments
 
@@ -33,6 +33,7 @@ from mercurial.node import nullrev
 
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 @@ def log_pull_action(ui, repo, **kwargs):
 
    :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 @@ def log_push_action(ui, repo, **kwargs):
 
    :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
Show inline comments
 
@@ -81,6 +81,7 @@ from rhodecode.lib.utils2 import safe_st
 
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 @@ class SimpleGit(BaseVCSController):
 
            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 @@ class SimpleGit(BaseVCSController):
 
            '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
Show inline comments
 
@@ -40,6 +40,7 @@ from rhodecode.lib.utils2 import safe_st
 
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 @@ class SimpleHg(BaseVCSController):
 
            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 @@ class SimpleHg(BaseVCSController):
 
            '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
Show inline comments
 
@@ -203,19 +203,24 @@ def get_repos(path, recursive=False):
 
    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
0 comments (0 inline, 0 general)