Show More
@@ -16,9 +16,11 b'' | |||
|
16 | 16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
17 | 17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
18 | 18 | import logging |
|
19 | import datetime | |
|
19 | 20 | |
|
20 | from datetime import datetime | |
|
21 | from zope.cachedescriptors.property import Lazy as LazyProperty | |
|
21 | 22 | from pyramid.threadlocal import get_current_request |
|
23 | ||
|
22 | 24 | from rhodecode.lib.utils2 import AttributeDict |
|
23 | 25 | |
|
24 | 26 | |
@@ -36,10 +38,20 b' class RhodecodeEvent(object):' | |||
|
36 | 38 | Base event class for all RhodeCode events |
|
37 | 39 | """ |
|
38 | 40 | name = "RhodeCodeEvent" |
|
41 | no_url_set = '<no server_url available>' | |
|
39 | 42 | |
|
40 | 43 | def __init__(self, request=None): |
|
41 |
self.request = request |
|
|
42 | self.utc_timestamp = datetime.utcnow() | |
|
44 | self._request = request | |
|
45 | self.utc_timestamp = datetime.datetime.utcnow() | |
|
46 | ||
|
47 | def get_request(self): | |
|
48 | if self._request: | |
|
49 | return self._request | |
|
50 | return get_current_request() | |
|
51 | ||
|
52 | @LazyProperty | |
|
53 | def request(self): | |
|
54 | return self.get_request() | |
|
43 | 55 | |
|
44 | 56 | @property |
|
45 | 57 | def auth_user(self): |
@@ -57,7 +69,6 b' class RhodecodeEvent(object):' | |||
|
57 | 69 | @property |
|
58 | 70 | def actor(self): |
|
59 | 71 | auth_user = self.auth_user |
|
60 | ||
|
61 | 72 | if auth_user: |
|
62 | 73 | instance = auth_user.get_instance() |
|
63 | 74 | if not instance: |
@@ -78,15 +89,14 b' class RhodecodeEvent(object):' | |||
|
78 | 89 | |
|
79 | 90 | @property |
|
80 | 91 | def server_url(self): |
|
81 | default = '<no server_url available>' | |
|
82 | 92 | if self.request: |
|
83 | 93 | try: |
|
84 | 94 | return self.request.route_url('home') |
|
85 | 95 | except Exception: |
|
86 | 96 | log.exception('Failed to fetch URL for server') |
|
87 |
return |
|
|
97 | return self.no_url_set | |
|
88 | 98 | |
|
89 |
return |
|
|
99 | return self.no_url_set | |
|
90 | 100 | |
|
91 | 101 | def as_dict(self): |
|
92 | 102 | data = { |
@@ -52,9 +52,10 b' class PullRequestEvent(RepoEvent):' | |||
|
52 | 52 | 'title': self.pullrequest.title, |
|
53 | 53 | 'issues': issues, |
|
54 | 54 | 'pull_request_id': self.pullrequest.pull_request_id, |
|
55 |
'url': PullRequestModel().get_url( |
|
|
55 | 'url': PullRequestModel().get_url( | |
|
56 | self.pullrequest, request=self.request), | |
|
56 | 57 | 'permalink_url': PullRequestModel().get_url( |
|
57 | self.pullrequest, permalink=True), | |
|
58 | self.pullrequest, request=self.request, permalink=True), | |
|
58 | 59 | 'status': self.pullrequest.calculated_review_status(), |
|
59 | 60 | 'commits': commits, |
|
60 | 61 | } |
@@ -134,9 +135,10 b' class PullRequestCommentEvent(PullReques' | |||
|
134 | 135 | 'type': self.comment.comment_type, |
|
135 | 136 | 'file': self.comment.f_path, |
|
136 | 137 | 'line': self.comment.line_no, |
|
137 |
'url': CommentsModel().get_url( |
|
|
138 | 'url': CommentsModel().get_url( | |
|
139 | self.comment, request=self.request), | |
|
138 | 140 | 'permalink_url': CommentsModel().get_url( |
|
139 | self.comment, permalink=True), | |
|
141 | self.comment, request=self.request, permalink=True), | |
|
140 | 142 | } |
|
141 | 143 | }) |
|
142 | 144 | return data |
@@ -120,6 +120,7 b' class RepoEvent(RhodecodeEvent):' | |||
|
120 | 120 | def as_dict(self): |
|
121 | 121 | from rhodecode.model.repo import RepoModel |
|
122 | 122 | data = super(RepoEvent, self).as_dict() |
|
123 | ||
|
123 | 124 | extra_fields = collections.OrderedDict() |
|
124 | 125 | for field in self.repo.extra_fields: |
|
125 | 126 | extra_fields[field.field_key] = field.field_value |
@@ -201,6 +202,10 b' class RepoVCSEvent(RepoEvent):' | |||
|
201 | 202 | if self.extras.get('server_url'): |
|
202 | 203 | return self.extras['server_url'] |
|
203 | 204 | |
|
205 | @property | |
|
206 | def request(self): | |
|
207 | return self.extras.get('request') or self.get_request() | |
|
208 | ||
|
204 | 209 | |
|
205 | 210 | class RepoPrePullEvent(RepoVCSEvent): |
|
206 | 211 | """ |
@@ -582,6 +582,36 b' def h_filter(s):' | |||
|
582 | 582 | return markupsafe.escape(s) |
|
583 | 583 | |
|
584 | 584 | |
|
585 | def add_events_routes(config): | |
|
586 | """ | |
|
587 | Adds routing that can be used in events. Because some events are triggered | |
|
588 | outside of pyramid context, we need to bootstrap request with some | |
|
589 | routing registered | |
|
590 | """ | |
|
591 | config.add_route(name='home', pattern='/') | |
|
592 | ||
|
593 | config.add_route(name='repo_summary', pattern='/{repo_name}') | |
|
594 | config.add_route(name='repo_summary_explicit', pattern='/{repo_name}/summary') | |
|
595 | config.add_route(name='repo_group_home', pattern='/{repo_group_name}') | |
|
596 | ||
|
597 | config.add_route(name='pullrequest_show', | |
|
598 | pattern='/{repo_name}/pull-request/{pull_request_id}') | |
|
599 | config.add_route(name='pull_requests_global', | |
|
600 | pattern='/pull-request/{pull_request_id}') | |
|
601 | ||
|
602 | config.add_route(name='repo_commit', | |
|
603 | pattern='/{repo_name}/changeset/{commit_id}') | |
|
604 | config.add_route(name='repo_files', | |
|
605 | pattern='/{repo_name}/files/{commit_id}/{f_path}') | |
|
606 | ||
|
607 | ||
|
608 | def bootstrap_request(): | |
|
609 | import pyramid.testing | |
|
610 | request = pyramid.testing.DummyRequest() | |
|
611 | config = pyramid.testing.setUp(request=request) | |
|
612 | add_events_routes(config) | |
|
613 | ||
|
614 | ||
|
585 | 615 | class BaseRepoController(BaseController): |
|
586 | 616 | """ |
|
587 | 617 | Base class for controllers responsible for loading all needed data for |
@@ -25,13 +25,11 b' import threading' | |||
|
25 | 25 | from BaseHTTPServer import BaseHTTPRequestHandler |
|
26 | 26 | from SocketServer import TCPServer |
|
27 | 27 | |
|
28 | import pylons | |
|
29 | 28 | import rhodecode |
|
30 | ||
|
31 | 29 | from rhodecode.model import meta |
|
30 | from rhodecode.lib.base import bootstrap_request | |
|
32 | 31 | from rhodecode.lib import hooks_base |
|
33 |
from rhodecode.lib.utils2 import |
|
|
34 | AttributeDict, safe_str, get_routes_generator_for_server_url) | |
|
32 | from rhodecode.lib.utils2 import AttributeDict | |
|
35 | 33 | |
|
36 | 34 | |
|
37 | 35 | log = logging.getLogger(__name__) |
@@ -188,29 +186,28 b' class Hooks(object):' | |||
|
188 | 186 | """ |
|
189 | 187 | |
|
190 | 188 | def repo_size(self, extras): |
|
191 |
log.debug("Called repo_size of |
|
|
189 | log.debug("Called repo_size of %s object", self) | |
|
192 | 190 | return self._call_hook(hooks_base.repo_size, extras) |
|
193 | 191 | |
|
194 | 192 | def pre_pull(self, extras): |
|
195 |
log.debug("Called pre_pull of |
|
|
193 | log.debug("Called pre_pull of %s object", self) | |
|
196 | 194 | return self._call_hook(hooks_base.pre_pull, extras) |
|
197 | 195 | |
|
198 | 196 | def post_pull(self, extras): |
|
199 |
log.debug("Called post_pull of |
|
|
197 | log.debug("Called post_pull of %s object", self) | |
|
200 | 198 | return self._call_hook(hooks_base.post_pull, extras) |
|
201 | 199 | |
|
202 | 200 | def pre_push(self, extras): |
|
203 |
log.debug("Called pre_push of |
|
|
201 | log.debug("Called pre_push of %s object", self) | |
|
204 | 202 | return self._call_hook(hooks_base.pre_push, extras) |
|
205 | 203 | |
|
206 | 204 | def post_push(self, extras): |
|
207 |
log.debug("Called post_push of |
|
|
205 | log.debug("Called post_push of %s object", self) | |
|
208 | 206 | return self._call_hook(hooks_base.post_push, extras) |
|
209 | 207 | |
|
210 | 208 | def _call_hook(self, hook, extras): |
|
211 | 209 | extras = AttributeDict(extras) |
|
212 | pylons_router = get_routes_generator_for_server_url(extras.server_url) | |
|
213 | pylons.url._push_object(pylons_router) | |
|
210 | extras.request = bootstrap_request() | |
|
214 | 211 | |
|
215 | 212 | try: |
|
216 | 213 | result = hook(extras) |
@@ -226,7 +223,6 b' class Hooks(object):' | |||
|
226 | 223 | 'exception_args': error_args, |
|
227 | 224 | } |
|
228 | 225 | finally: |
|
229 | pylons.url._pop_object() | |
|
230 | 226 | meta.Session.remove() |
|
231 | 227 | |
|
232 | 228 | return { |
General Comments 0
You need to be logged in to leave comments.
Login now