# HG changeset patch # User RhodeCode Admin # Date 2024-03-11 13:55:48 # Node ID d43cbc34e272a1ba782db3f0535855e52bdb7696 # Parent 0de02c1c9a7f4dbaeacbf38d9559da1981e9a385 fix(events): fixed celery based events where it was missing request object diff --git a/rhodecode/events/base.py b/rhodecode/events/base.py --- a/rhodecode/events/base.py +++ b/rhodecode/events/base.py @@ -15,13 +15,14 @@ # This program is dual-licensed. If you wish to learn more about the # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ + import logging import datetime -import typing from zope.cachedescriptors.property import Lazy as LazyProperty -from pyramid.threadlocal import get_current_request +from rhodecode.lib.pyramid_utils import get_current_request +from rhodecode.lib.auth import AuthUser from rhodecode.lib.utils2 import AttributeDict @@ -41,8 +42,9 @@ class RhodecodeEvent(object): name = "RhodeCodeEvent" no_url_set = '' - def __init__(self, request=None): + def __init__(self, request=None, actor=None): self._request = request + self._actor = actor self.utc_timestamp = datetime.datetime.utcnow() def __repr__(self): @@ -72,16 +74,22 @@ class RhodecodeEvent(object): @property def actor(self): + # if an explicit actor is specified, use this + if self._actor: + return self._actor + auth_user = self.auth_user - if auth_user: + log.debug('Got integration actor: %s', auth_user) + if isinstance(auth_user, AuthUser): instance = auth_user.get_instance() + # we can't find this DB user... if not instance: return AttributeDict(dict( username=auth_user.username, user_id=auth_user.user_id, )) - return instance - + elif auth_user: + return auth_user return SYSTEM_USER @property @@ -129,3 +137,4 @@ class FtsBuild(RhodecodeEvent): """ name = 'fts-build' display_name = 'Start FTS Build' + diff --git a/rhodecode/events/repo.py b/rhodecode/events/repo.py --- a/rhodecode/events/repo.py +++ b/rhodecode/events/repo.py @@ -156,11 +156,11 @@ class RepoEvent(RhodeCodeIntegrationEven Base class for events acting on a repository. """ - def __init__(self, repo): + def __init__(self, repo, actor=None): """ :param repo: a :class:`Repository` instance """ - super().__init__() + super().__init__(actor=actor) self.repo = repo def as_dict(self): diff --git a/rhodecode/lib/auth.py b/rhodecode/lib/auth.py --- a/rhodecode/lib/auth.py +++ b/rhodecode/lib/auth.py @@ -1688,7 +1688,7 @@ def get_csrf_token(session, force_new=Fa def get_request(perm_class_instance): - from pyramid.threadlocal import get_current_request + from rhodecode.lib.pyramid_utils import get_current_request pyramid_request = get_current_request() return pyramid_request diff --git a/rhodecode/lib/celerylib/tasks.py b/rhodecode/lib/celerylib/tasks.py --- a/rhodecode/lib/celerylib/tasks.py +++ b/rhodecode/lib/celerylib/tasks.py @@ -193,6 +193,7 @@ def create_repo(form_data, cur_user): enable_downloads=enable_downloads, state=state ) + Session().commit() # now create this repo on Filesystem diff --git a/rhodecode/lib/hooks_utils.py b/rhodecode/lib/hooks_utils.py --- a/rhodecode/lib/hooks_utils.py +++ b/rhodecode/lib/hooks_utils.py @@ -17,7 +17,6 @@ # and proprietary license terms, please see https://rhodecode.com/licenses/ import webob -from pyramid.threadlocal import get_current_request from rhodecode import events from rhodecode.lib import hooks_base @@ -33,6 +32,7 @@ def _supports_repo_type(repo_type): def _get_vcs_operation_context(username, repo_name, repo_type, action): # NOTE(dan): import loop from rhodecode.lib.base import vcs_operation_context + from rhodecode.lib.pyramid_utils import get_current_request check_locking = action in ('pull', 'push') diff --git a/rhodecode/lib/pyramid_utils.py b/rhodecode/lib/pyramid_utils.py --- a/rhodecode/lib/pyramid_utils.py +++ b/rhodecode/lib/pyramid_utils.py @@ -23,6 +23,7 @@ import configparser from rhodecode.lib.config_utils import get_config from pyramid.paster import bootstrap as pyramid_bootstrap, setup_logging # pragma: no cover +from pyramid.threadlocal import get_current_request as pyramid_current_request def bootstrap(config_uri, options=None, env=None): @@ -46,3 +47,11 @@ def bootstrap(config_uri, options=None, 'ip_addr': '127.0.0.1'}) return pyramid_bootstrap(config_uri, request=request, options=options) + +def get_current_request(): + pyramid_req = pyramid_current_request() + if not pyramid_req: + # maybe we're in celery context and need to get the PYRAMID_REQUEST + from rhodecode.lib.celerylib.loader import celery_app + pyramid_req = celery_app.conf['PYRAMID_REQUEST'] + return pyramid_req diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -608,7 +608,7 @@ class RepoModel(BaseModel): # we need to flush here, in order to check if database won't # throw any exceptions, create filesystem dirs at the very end self.sa.flush() - events.trigger(events.RepoCreateEvent(new_repo)) + events.trigger(events.RepoCreateEvent(new_repo, actor=owner)) return new_repo except Exception: