# HG changeset patch # User Mads Kiilerich # Date 1547088194 -3600 # Node ID 7ba73396e5ba6d76ea522f0c0f209716cd1b7d61 # Parent 1901954df11df0e89bd2968d72ccad65e82008d4 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 ... diff -r 1901954df11d -r 7ba73396e5ba kallithea/lib/base.py --- a/kallithea/lib/base.py Thu Jan 10 03:35:01 2019 +0100 +++ b/kallithea/lib/base.py Thu Jan 10 03:43:14 2019 +0100 @@ -49,7 +49,7 @@ 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 @@ #====================================================================== # 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: diff -r 1901954df11d -r 7ba73396e5ba kallithea/lib/hooks.py --- a/kallithea/lib/hooks.py Thu Jan 10 03:35:01 2019 +0100 +++ b/kallithea/lib/hooks.py Thu Jan 10 03:43:14 2019 +0100 @@ -35,7 +35,7 @@ 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 @@ 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 @@ 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 @@ 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) diff -r 1901954df11d -r 7ba73396e5ba kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py Thu Jan 10 03:35:01 2019 +0100 +++ b/kallithea/lib/utils2.py Thu Jan 10 03:43:14 2019 +0100 @@ -518,11 +518,16 @@ 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 @@ 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) diff -r 1901954df11d -r 7ba73396e5ba kallithea/model/scm.py --- a/kallithea/model/scm.py Thu Jan 10 03:35:01 2019 +0100 +++ b/kallithea/model/scm.py Thu Jan 10 03:43:14 2019 +0100 @@ -47,7 +47,7 @@ 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 @@ 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 @@ :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 @@ 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)