##// END OF EJS Templates
pyramid: ported pyramid routing for events
marcink -
r2016:062a6c2d stable
parent child Browse files
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 or get_current_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 default
97 return self.no_url_set
88 98
89 return default
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(self.pullrequest),
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(self.comment),
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
@@ -201,6 +201,10 b' class RepoVCSEvent(RepoEvent):'
201 201 if self.extras.get('server_url'):
202 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 209 class RepoPrePullEvent(RepoVCSEvent):
206 210 """
@@ -511,6 +511,36 b' class BaseController(WSGIController):'
511 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 544 class BaseRepoController(BaseController):
515 545 """
516 546 Base class for controllers responsible for loading all needed data for
@@ -29,9 +29,11 b' import pylons'
29 29 import rhodecode
30 30
31 31 from rhodecode.model import meta
32 from rhodecode.lib.base import bootstrap_request
32 33 from rhodecode.lib import hooks_base
33 34 from rhodecode.lib.utils2 import (
34 35 AttributeDict, safe_str, get_routes_generator_for_server_url)
36 from rhodecode.lib.utils2 import AttributeDict
35 37
36 38
37 39 log = logging.getLogger(__name__)
@@ -188,29 +190,30 b' class Hooks(object):'
188 190 """
189 191
190 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 194 return self._call_hook(hooks_base.repo_size, extras)
193 195
194 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 198 return self._call_hook(hooks_base.pre_pull, extras)
197 199
198 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 202 return self._call_hook(hooks_base.post_pull, extras)
201 203
202 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 206 return self._call_hook(hooks_base.pre_push, extras)
205 207
206 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 210 return self._call_hook(hooks_base.post_push, extras)
209 211
210 212 def _call_hook(self, hook, extras):
211 213 extras = AttributeDict(extras)
212 214 pylons_router = get_routes_generator_for_server_url(extras.server_url)
213 215 pylons.url._push_object(pylons_router)
216 extras.request = bootstrap_request()
214 217
215 218 try:
216 219 result = hook(extras)
General Comments 0
You need to be logged in to leave comments. Login now