events: re-organizate events handling.

- pass in request from event to url functions to skip request lookup
- added some logging
- prepared for pyramid migration
This commit is contained in:
Marcin Kuzminski 2017-06-08 21:37:34 +02:00
parent 47f9f32ad9
commit 7e11e618ca
8 changed files with 39 additions and 18 deletions

View file

@ -22,7 +22,7 @@ from rhodecode.lib import helpers as h
from rhodecode.lib.utils2 import safe_int
def reviewer_as_json(user, reasons, mandatory):
def reviewer_as_json(user, reasons=None, mandatory=False):
"""
Returns json struct of a reviewer for frontend
@ -33,7 +33,7 @@ def reviewer_as_json(user, reasons, mandatory):
return {
'user_id': user.user_id,
'reasons': reasons,
'reasons': reasons or [],
'mandatory': mandatory,
'username': user.username,
'firstname': user.firstname,

View file

@ -318,6 +318,12 @@ def includeme(config):
config.add_subscriber(write_metadata_if_needed, ApplicationCreated)
config.add_subscriber(write_js_routes_if_enabled, ApplicationCreated)
# events
# TODO(marcink): this should be done when pyramid migration is finished
# config.add_subscriber(
# 'rhodecode.integrations.integrations_event_handler',
# 'rhodecode.events.RhodecodeEvent')
# Set the authorization policy.
authz_policy = ACLAuthorizationPolicy()
config.set_authorization_policy(authz_policy)

View file

@ -18,6 +18,8 @@
import logging
from pyramid.threadlocal import get_current_registry
from rhodecode.events.base import RhodecodeEvent
log = logging.getLogger(__name__)
@ -32,20 +34,21 @@ def trigger(event, registry=None):
# passing the registry as an argument to get rid of it.
registry = registry or get_current_registry()
registry.notify(event)
log.debug('event %s triggered', event)
log.debug('event %s triggered using registry %s', event, registry)
# Until we can work around the problem that VCS operations do not have a
# pyramid context to work with, we send the events to integrations directly
# Later it will be possible to use regular pyramid subscribers ie:
# config.add_subscriber(integrations_event_handler, RhodecodeEvent)
# config.add_subscriber(
# 'rhodecode.integrations.integrations_event_handler',
# 'rhodecode.events.RhodecodeEvent')
# trigger(event, request.registry)
from rhodecode.integrations import integrations_event_handler
if isinstance(event, RhodecodeEvent):
integrations_event_handler(event)
from rhodecode.events.base import RhodecodeEvent
from rhodecode.events.user import ( # noqa
UserPreCreate,
UserPostCreate,

View file

@ -33,12 +33,12 @@ log = logging.getLogger(__name__)
class RhodecodeEvent(object):
"""
Base event class for all Rhodecode events
Base event class for all RhodeCode events
"""
name = "RhodeCodeEvent"
def __init__(self):
self.request = get_current_request()
def __init__(self, request=None):
self.request = request or get_current_request()
self.utc_timestamp = datetime.utcnow()
@property
@ -80,9 +80,8 @@ class RhodecodeEvent(object):
def server_url(self):
default = '<no server_url available>'
if self.request:
from rhodecode.lib import helpers as h
try:
return h.route_url('home')
return self.request.route_url('home')
except Exception:
log.exception('Failed to fetch URL for server')
return default

View file

@ -41,6 +41,7 @@ class PullRequestEvent(RepoEvent):
data = super(PullRequestEvent, self).as_dict()
commits = _commits_as_dict(
self,
commit_ids=self.pullrequest.revisions,
repos=[self.pullrequest.source_repo]
)

View file

@ -27,10 +27,11 @@ from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
log = logging.getLogger(__name__)
def _commits_as_dict(commit_ids, repos):
def _commits_as_dict(event, commit_ids, repos):
"""
Helper function to serialize commit_ids
:param event: class calling this method
:param commit_ids: commits to get
:param repos: list of repos to check
"""
@ -69,9 +70,9 @@ def _commits_as_dict(commit_ids, repos):
cs_data['mentions'] = extract_mentioned_users(cs_data['message'])
cs_data['reviewers'] = reviewers
cs_data['url'] = RepoModel().get_commit_url(
repo, cs_data['raw_id'])
repo, cs_data['raw_id'], request=event.request)
cs_data['permalink_url'] = RepoModel().get_commit_url(
repo, cs_data['raw_id'], permalink=True)
repo, cs_data['raw_id'], request=event.request, permalink=True)
urlified_message, issues_data = process_patterns(
cs_data['message'], repo.repo_name)
cs_data['issues'] = issues_data
@ -128,8 +129,10 @@ class RepoEvent(RhodecodeEvent):
'repo_id': self.repo.repo_id,
'repo_name': self.repo.repo_name,
'repo_type': self.repo.repo_type,
'url': RepoModel().get_url(self.repo),
'permalink_url': RepoModel().get_url(self.repo, permalink=True),
'url': RepoModel().get_url(
self.repo, request=self.request),
'permalink_url': RepoModel().get_url(
self.repo, request=self.request, permalink=True),
'extra_fields': extra_fields
}
})
@ -248,7 +251,7 @@ class RepoPushEvent(RepoVCSEvent):
data['repo']['url'], branch_name)
commits = _commits_as_dict(
commit_ids=self.pushed_commit_ids, repos=[self.repo])
self, commit_ids=self.pushed_commit_ids, repos=[self.repo])
last_branch = None
for commit in reversed(commits):

View file

@ -96,6 +96,9 @@ class IntegrationModel(BaseModel):
""" Send an event to an integration """
handler = self.get_integration_handler(integration)
if handler:
log.debug(
'events: sending event %s on integration %s using handler %s',
event, integration, handler)
handler.send_event(event)
def get_integrations(self, scope, IntegrationType=None):

View file

@ -159,6 +159,9 @@ class RepoModel(BaseModel):
if not request:
request = get_current_request()
if not request:
return
if permalink:
return request.route_url(
'repo_summary', repo_name=safe_str(repo.repo_id))
@ -170,6 +173,9 @@ class RepoModel(BaseModel):
if not request:
request = get_current_request()
if not request:
return
if permalink:
return request.route_url(
'repo_commit', repo_name=safe_str(repo.repo_id),