##// END OF EJS Templates
events: ported pylons part to pyramid....
marcink -
r1959:b0de121b default
parent child Browse files
Show More
@@ -16,9 +16,11 b''
16 # RhodeCode Enterprise Edition, including its added features, Support services,
16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 # and proprietary license terms, please see https://rhodecode.com/licenses/
17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 import logging
18 import logging
19 import datetime
19
20
20 from datetime import datetime
21 from zope.cachedescriptors.property import Lazy as LazyProperty
21 from pyramid.threadlocal import get_current_request
22 from pyramid.threadlocal import get_current_request
23
22 from rhodecode.lib.utils2 import AttributeDict
24 from rhodecode.lib.utils2 import AttributeDict
23
25
24
26
@@ -36,10 +38,20 b' class RhodecodeEvent(object):'
36 Base event class for all RhodeCode events
38 Base event class for all RhodeCode events
37 """
39 """
38 name = "RhodeCodeEvent"
40 name = "RhodeCodeEvent"
41 no_url_set = '<no server_url available>'
39
42
40 def __init__(self, request=None):
43 def __init__(self, request=None):
41 self.request = request or get_current_request()
44 self._request = request
42 self.utc_timestamp = datetime.utcnow()
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 @property
56 @property
45 def auth_user(self):
57 def auth_user(self):
@@ -57,7 +69,6 b' class RhodecodeEvent(object):'
57 @property
69 @property
58 def actor(self):
70 def actor(self):
59 auth_user = self.auth_user
71 auth_user = self.auth_user
60
61 if auth_user:
72 if auth_user:
62 instance = auth_user.get_instance()
73 instance = auth_user.get_instance()
63 if not instance:
74 if not instance:
@@ -78,15 +89,14 b' class RhodecodeEvent(object):'
78
89
79 @property
90 @property
80 def server_url(self):
91 def server_url(self):
81 default = '<no server_url available>'
82 if self.request:
92 if self.request:
83 try:
93 try:
84 return self.request.route_url('home')
94 return self.request.route_url('home')
85 except Exception:
95 except Exception:
86 log.exception('Failed to fetch URL for server')
96 log.exception('Failed to fetch URL for server')
87 return default
97 return self.no_url_set
88
98
89 return default
99 return self.no_url_set
90
100
91 def as_dict(self):
101 def as_dict(self):
92 data = {
102 data = {
@@ -52,9 +52,10 b' class PullRequestEvent(RepoEvent):'
52 'title': self.pullrequest.title,
52 'title': self.pullrequest.title,
53 'issues': issues,
53 'issues': issues,
54 'pull_request_id': self.pullrequest.pull_request_id,
54 'pull_request_id': self.pullrequest.pull_request_id,
55 'url': PullRequestModel().get_url(self.pullrequest),
55 'url': PullRequestModel().get_url(
56 self.pullrequest, request=self.request),
56 'permalink_url': PullRequestModel().get_url(
57 'permalink_url': PullRequestModel().get_url(
57 self.pullrequest, permalink=True),
58 self.pullrequest, request=self.request, permalink=True),
58 'status': self.pullrequest.calculated_review_status(),
59 'status': self.pullrequest.calculated_review_status(),
59 'commits': commits,
60 'commits': commits,
60 }
61 }
@@ -134,9 +135,10 b' class PullRequestCommentEvent(PullReques'
134 'type': self.comment.comment_type,
135 'type': self.comment.comment_type,
135 'file': self.comment.f_path,
136 'file': self.comment.f_path,
136 'line': self.comment.line_no,
137 'line': self.comment.line_no,
137 'url': CommentsModel().get_url(self.comment),
138 'url': CommentsModel().get_url(
139 self.comment, request=self.request),
138 'permalink_url': CommentsModel().get_url(
140 'permalink_url': CommentsModel().get_url(
139 self.comment, permalink=True),
141 self.comment, request=self.request, permalink=True),
140 }
142 }
141 })
143 })
142 return data
144 return data
@@ -120,6 +120,7 b' class RepoEvent(RhodecodeEvent):'
120 def as_dict(self):
120 def as_dict(self):
121 from rhodecode.model.repo import RepoModel
121 from rhodecode.model.repo import RepoModel
122 data = super(RepoEvent, self).as_dict()
122 data = super(RepoEvent, self).as_dict()
123
123 extra_fields = collections.OrderedDict()
124 extra_fields = collections.OrderedDict()
124 for field in self.repo.extra_fields:
125 for field in self.repo.extra_fields:
125 extra_fields[field.field_key] = field.field_value
126 extra_fields[field.field_key] = field.field_value
@@ -201,6 +202,10 b' class RepoVCSEvent(RepoEvent):'
201 if self.extras.get('server_url'):
202 if self.extras.get('server_url'):
202 return self.extras['server_url']
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 class RepoPrePullEvent(RepoVCSEvent):
210 class RepoPrePullEvent(RepoVCSEvent):
206 """
211 """
@@ -582,6 +582,36 b' def h_filter(s):'
582 return markupsafe.escape(s)
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 class BaseRepoController(BaseController):
615 class BaseRepoController(BaseController):
586 """
616 """
587 Base class for controllers responsible for loading all needed data for
617 Base class for controllers responsible for loading all needed data for
@@ -25,13 +25,11 b' import threading'
25 from BaseHTTPServer import BaseHTTPRequestHandler
25 from BaseHTTPServer import BaseHTTPRequestHandler
26 from SocketServer import TCPServer
26 from SocketServer import TCPServer
27
27
28 import pylons
29 import rhodecode
28 import rhodecode
30
31 from rhodecode.model import meta
29 from rhodecode.model import meta
30 from rhodecode.lib.base import bootstrap_request
32 from rhodecode.lib import hooks_base
31 from rhodecode.lib import hooks_base
33 from rhodecode.lib.utils2 import (
32 from rhodecode.lib.utils2 import AttributeDict
34 AttributeDict, safe_str, get_routes_generator_for_server_url)
35
33
36
34
37 log = logging.getLogger(__name__)
35 log = logging.getLogger(__name__)
@@ -188,29 +186,28 b' class Hooks(object):'
188 """
186 """
189
187
190 def repo_size(self, extras):
188 def repo_size(self, extras):
191 log.debug("Called repo_size of Hooks object")
189 log.debug("Called repo_size of %s object", self)
192 return self._call_hook(hooks_base.repo_size, extras)
190 return self._call_hook(hooks_base.repo_size, extras)
193
191
194 def pre_pull(self, extras):
192 def pre_pull(self, extras):
195 log.debug("Called pre_pull of Hooks object")
193 log.debug("Called pre_pull of %s object", self)
196 return self._call_hook(hooks_base.pre_pull, extras)
194 return self._call_hook(hooks_base.pre_pull, extras)
197
195
198 def post_pull(self, extras):
196 def post_pull(self, extras):
199 log.debug("Called post_pull of Hooks object")
197 log.debug("Called post_pull of %s object", self)
200 return self._call_hook(hooks_base.post_pull, extras)
198 return self._call_hook(hooks_base.post_pull, extras)
201
199
202 def pre_push(self, extras):
200 def pre_push(self, extras):
203 log.debug("Called pre_push of Hooks object")
201 log.debug("Called pre_push of %s object", self)
204 return self._call_hook(hooks_base.pre_push, extras)
202 return self._call_hook(hooks_base.pre_push, extras)
205
203
206 def post_push(self, extras):
204 def post_push(self, extras):
207 log.debug("Called post_push of Hooks object")
205 log.debug("Called post_push of %s object", self)
208 return self._call_hook(hooks_base.post_push, extras)
206 return self._call_hook(hooks_base.post_push, extras)
209
207
210 def _call_hook(self, hook, extras):
208 def _call_hook(self, hook, extras):
211 extras = AttributeDict(extras)
209 extras = AttributeDict(extras)
212 pylons_router = get_routes_generator_for_server_url(extras.server_url)
210 extras.request = bootstrap_request()
213 pylons.url._push_object(pylons_router)
214
211
215 try:
212 try:
216 result = hook(extras)
213 result = hook(extras)
@@ -226,7 +223,6 b' class Hooks(object):'
226 'exception_args': error_args,
223 'exception_args': error_args,
227 }
224 }
228 finally:
225 finally:
229 pylons.url._pop_object()
230 meta.Session.remove()
226 meta.Session.remove()
231
227
232 return {
228 return {
General Comments 0
You need to be logged in to leave comments. Login now