##// END OF EJS Templates
pyramid: ported pyramid routing for events
marcink -
r2016:062a6c2d stable
parent child
Show More
@@ -16,9 +16,11
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 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 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 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 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 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
@@ -201,6 +201,10 class RepoVCSEvent(RepoEvent):
201 if self.extras.get('server_url'):
201 if self.extras.get('server_url'):
202 return self.extras['server_url']
202 return self.extras['server_url']
203
203
204 @property
205 def request(self):
206 return self.extras.get('request') or self.get_request()
207
204
208
205 class RepoPrePullEvent(RepoVCSEvent):
209 class RepoPrePullEvent(RepoVCSEvent):
206 """
210 """
@@ -511,6 +511,36 class BaseController(WSGIController):
511 return WSGIController.__call__(self, environ, start_response)
511 return WSGIController.__call__(self, environ, start_response)
512
512
513
513
514 def add_events_routes(config):
515 """
516 Adds routing that can be used in events. Because some events are triggered
517 outside of pyramid context, we need to bootstrap request with some
518 routing registered
519 """
520 config.add_route(name='home', pattern='/')
521
522 config.add_route(name='repo_summary', pattern='/{repo_name}')
523 config.add_route(name='repo_summary_explicit', pattern='/{repo_name}/summary')
524 config.add_route(name='repo_group_home', pattern='/{repo_group_name}')
525
526 config.add_route(name='pullrequest_show',
527 pattern='/{repo_name}/pull-request/{pull_request_id}')
528 config.add_route(name='pull_requests_global',
529 pattern='/pull-request/{pull_request_id}')
530
531 config.add_route(name='repo_commit',
532 pattern='/{repo_name}/changeset/{commit_id}')
533 config.add_route(name='repo_files',
534 pattern='/{repo_name}/files/{commit_id}/{f_path}')
535
536
537 def bootstrap_request():
538 import pyramid.testing
539 request = pyramid.testing.DummyRequest()
540 config = pyramid.testing.setUp(request=request)
541 add_events_routes(config)
542
543
514 class BaseRepoController(BaseController):
544 class BaseRepoController(BaseController):
515 """
545 """
516 Base class for controllers responsible for loading all needed data for
546 Base class for controllers responsible for loading all needed data for
@@ -29,9 +29,11 import pylons
29 import rhodecode
29 import rhodecode
30
30
31 from rhodecode.model import meta
31 from rhodecode.model import meta
32 from rhodecode.lib.base import bootstrap_request
32 from rhodecode.lib import hooks_base
33 from rhodecode.lib import hooks_base
33 from rhodecode.lib.utils2 import (
34 from rhodecode.lib.utils2 import (
34 AttributeDict, safe_str, get_routes_generator_for_server_url)
35 AttributeDict, safe_str, get_routes_generator_for_server_url)
36 from rhodecode.lib.utils2 import AttributeDict
35
37
36
38
37 log = logging.getLogger(__name__)
39 log = logging.getLogger(__name__)
@@ -188,29 +190,30 class Hooks(object):
188 """
190 """
189
191
190 def repo_size(self, extras):
192 def repo_size(self, extras):
191 log.debug("Called repo_size of Hooks object")
193 log.debug("Called repo_size of %s object", self)
192 return self._call_hook(hooks_base.repo_size, extras)
194 return self._call_hook(hooks_base.repo_size, extras)
193
195
194 def pre_pull(self, extras):
196 def pre_pull(self, extras):
195 log.debug("Called pre_pull of Hooks object")
197 log.debug("Called pre_pull of %s object", self)
196 return self._call_hook(hooks_base.pre_pull, extras)
198 return self._call_hook(hooks_base.pre_pull, extras)
197
199
198 def post_pull(self, extras):
200 def post_pull(self, extras):
199 log.debug("Called post_pull of Hooks object")
201 log.debug("Called post_pull of %s object", self)
200 return self._call_hook(hooks_base.post_pull, extras)
202 return self._call_hook(hooks_base.post_pull, extras)
201
203
202 def pre_push(self, extras):
204 def pre_push(self, extras):
203 log.debug("Called pre_push of Hooks object")
205 log.debug("Called pre_push of %s object", self)
204 return self._call_hook(hooks_base.pre_push, extras)
206 return self._call_hook(hooks_base.pre_push, extras)
205
207
206 def post_push(self, extras):
208 def post_push(self, extras):
207 log.debug("Called post_push of Hooks object")
209 log.debug("Called post_push of %s object", self)
208 return self._call_hook(hooks_base.post_push, extras)
210 return self._call_hook(hooks_base.post_push, extras)
209
211
210 def _call_hook(self, hook, extras):
212 def _call_hook(self, hook, extras):
211 extras = AttributeDict(extras)
213 extras = AttributeDict(extras)
212 pylons_router = get_routes_generator_for_server_url(extras.server_url)
214 pylons_router = get_routes_generator_for_server_url(extras.server_url)
213 pylons.url._push_object(pylons_router)
215 pylons.url._push_object(pylons_router)
216 extras.request = bootstrap_request()
214
217
215 try:
218 try:
216 result = hook(extras)
219 result = hook(extras)
General Comments 0
You need to be logged in to leave comments. Login now