Changeset - 7ba73396e5ba
[Not reviewed]
default
0 4 0
Mads Kiilerich - 7 years ago 2019-01-10 03:43:14
mads@kiilerich.com
hooks: move _handle_rc_scm_extras to utils2 as set_hook_environment and get_hook_environment

This is also a step away from using the name 'extras' ... but still a lot of
traces left behind ...
4 files changed with 35 insertions and 26 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/base.py
Show inline comments
 
@@ -49,7 +49,7 @@ from kallithea import __version__, BACKE
 

	
 
from kallithea.config.routing import url
 
from kallithea.lib.utils2 import str2bool, safe_unicode, AttributeDict, \
 
    safe_str, safe_int
 
    safe_str, safe_int, set_hook_environment
 
from kallithea.lib import auth_modules
 
from kallithea.lib.auth import AuthUser, HasPermissionAnyMiddleware
 
from kallithea.lib.compat import json
 
@@ -333,7 +333,7 @@ class BaseVCSController(object):
 
            #======================================================================
 
            # REQUEST HANDLING
 
            #======================================================================
 
            ScmModel()._handle_rc_scm_extras(user.username, ip_addr,
 
            set_hook_environment(user.username, ip_addr,
 
                parsed_request.repo_name, self.scm_alias, parsed_request.action)
 

	
 
            try:
kallithea/lib/hooks.py
Show inline comments
 
@@ -35,7 +35,7 @@ from kallithea.lib.utils import action_l
 
from kallithea.lib.vcs.backends.base import EmptyChangeset
 
from kallithea.lib.exceptions import UserCreationError
 
from kallithea.lib.utils import make_ui, setup_cache_regions
 
from kallithea.lib.utils2 import safe_str, safe_unicode, _extract_extras
 
from kallithea.lib.utils2 import safe_str, safe_unicode, get_hook_environment
 
from kallithea.model.db import Repository, User, Ui
 

	
 

	
 
@@ -85,7 +85,7 @@ def log_pull_action(ui, repo, **kwargs):
 

	
 
    Does *not* use the action from the hook environment but is always 'pull'.
 
    """
 
    ex = _extract_extras()
 
    ex = get_hook_environment()
 

	
 
    user = User.get_by_username(ex.username)
 
    action = 'pull'
 
@@ -124,7 +124,7 @@ def process_pushed_raw_ids(revs):
 
    or from the Git post-receive hook calling handle_git_post_receive ...
 
    or from scm _handle_push.
 
    """
 
    ex = _extract_extras()
 
    ex = get_hook_environment()
 

	
 
    action = '%s:%s' % (ex.action, ','.join(revs))
 
    action_logger(ex.username, action, ex.repository, ex.ip, commit=True)
 
@@ -308,7 +308,7 @@ def _hook_environment(repo_path):
 
    from kallithea.config.environment import load_environment
 
    from kallithea.model.base import init_model
 

	
 
    extras = _extract_extras()
 
    extras = get_hook_environment()
 
    ini_file_path = extras['config']
 
    #logging.config.fileConfig(ini_file_path) # Note: we are in a different process - don't use configured logging
 
    app_conf = appconfig('config:%s' % ini_file_path)
kallithea/lib/utils2.py
Show inline comments
 
@@ -518,11 +518,16 @@ def obfuscate_url_pw(engine):
 
    return str(_url)
 

	
 

	
 
def _extract_extras():
 
def get_hook_environment():
 
    """
 
    Extracts the Kallithea extras data from os.environ, and wraps it into named
 
    AttributeDict object
 
    Get hook context by deserializing the global KALLITHEA_EXTRAS environment
 
    variable.
 

	
 
    Called early in Git out-of-process hooks to get .ini config path so the
 
    basic environment can be configured properly. Also used in all hooks to get
 
    information about the action that triggered it.
 
    """
 

	
 
    try:
 
        extras = json.loads(os.environ['KALLITHEA_EXTRAS'])
 
    except KeyError:
 
@@ -537,7 +542,24 @@ def _extract_extras():
 
    return AttributeDict(extras)
 

	
 

	
 
def _set_extras(extras):
 
def set_hook_environment(username, ip_addr, repo_name, repo_alias, action=None):
 
    """Prepare global context for running hooks by serializing data in the
 
    global KALLITHEA_EXTRAS environment variable.
 

	
 
    Most importantly, this allow Git hooks to do proper logging and updating of
 
    caches after pushes.
 

	
 
    Must always be called before anything with hooks are invoked.
 
    """
 
    from kallithea import CONFIG
 
    extras = {
 
        'ip': ip_addr, # used in log_push/pull_action action_logger
 
        'username': username,
 
        'action': action or 'push_local', # used in log_push_action_raw_ids action_logger
 
        'repository': repo_name,
 
        'scm': repo_alias, # used to pick hack in log_push_action_raw_ids
 
        'config': CONFIG['__file__'], # used by git hook to read config
 
    }
 
    os.environ['KALLITHEA_EXTRAS'] = json.dumps(extras)
 

	
 

	
kallithea/model/scm.py
Show inline comments
 
@@ -47,7 +47,7 @@ from kallithea.lib.vcs.backends.base imp
 

	
 
from kallithea import BACKENDS
 
from kallithea.lib import helpers as h
 
from kallithea.lib.utils2 import safe_str, safe_unicode, _set_extras
 
from kallithea.lib.utils2 import safe_str, safe_unicode, set_hook_environment
 
from kallithea.lib.auth import HasRepoPermissionLevel, HasRepoGroupPermissionLevel, \
 
    HasUserGroupPermissionLevel, HasPermissionAny, HasPermissionAny
 
from kallithea.lib.utils import get_filesystem_repos, make_ui, \
 
@@ -327,19 +327,6 @@ class ScmModel(object):
 
        repo.fork = fork
 
        return repo
 

	
 
    def _handle_rc_scm_extras(self, username, ip_addr, repo_name, repo_alias,
 
                              action=None):
 
        from kallithea import CONFIG
 
        extras = {
 
            'ip': ip_addr,
 
            'username': username,
 
            'action': action or 'push_local',
 
            'repository': repo_name,
 
            'scm': repo_alias,
 
            'config': CONFIG['__file__'],
 
        }
 
        _set_extras(extras)
 

	
 
    def _handle_push(self, repo, username, ip_addr, action, repo_name, revisions):
 
        """
 
        Handle that the repository has changed.
 
@@ -351,7 +338,7 @@ class ScmModel(object):
 
        :param repo_name: name of repo
 
        :param revisions: list of revisions that we pushed
 
        """
 
        self._handle_rc_scm_extras(username, ip_addr, repo_name, repo_alias=repo.alias, action=action)
 
        set_hook_environment(username, ip_addr, repo_name, repo_alias=repo.alias, action=action)
 
        process_pushed_raw_ids(revisions) # also calls mark_for_invalidation
 

	
 
    def _get_IMC_module(self, scm_type):
 
@@ -396,7 +383,7 @@ class ScmModel(object):
 
                                  repo_name=repo_name,
 
                                  revisions=[])
 
            else:
 
                self._handle_rc_scm_extras(username, ip_addr, dbrepo.repo_name,
 
                set_hook_environment(username, ip_addr, dbrepo.repo_name,
 
                                           repo.alias, action='push_remote')
 
                repo.pull(clone_uri)
 

	
0 comments (0 inline, 0 general)