##// END OF EJS Templates
events: make the System URL extraction safer....
marcink -
r1424:5c92e9f4 default
parent child Browse files
Show More
@@ -1,84 +1,93 b''
1 1 # Copyright (C) 2016-2017 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 import logging
18 19
19 20 from datetime import datetime
20 21 from pyramid.threadlocal import get_current_request
21 22 from rhodecode.lib.utils2 import AttributeDict
22 23
23 24
24 25 # this is a user object to be used for events caused by the system (eg. shell)
25 26 SYSTEM_USER = AttributeDict(dict(
26 27 username='__SYSTEM__'
27 28 ))
28 29
30 log = logging.getLogger(__name__)
31
29 32
30 33 class RhodecodeEvent(object):
31 34 """
32 35 Base event class for all Rhodecode events
33 36 """
34 37 name = "RhodeCodeEvent"
35 38
36 39 def __init__(self):
37 40 self.request = get_current_request()
38 41 self.utc_timestamp = datetime.utcnow()
39 42
40 43 @property
41 44 def auth_user(self):
42 45 if not self.request:
43 46 return
44 47
45 48 user = getattr(self.request, 'user', None)
46 49 if user:
47 50 return user
48 51
49 52 api_user = getattr(self.request, 'rpc_user', None)
50 53 if api_user:
51 54 return api_user
52 55
53 56 @property
54 57 def actor(self):
55 58 auth_user = self.auth_user
56 59 if auth_user:
57 60 return auth_user.get_instance()
58 61 return SYSTEM_USER
59 62
60 63 @property
61 64 def actor_ip(self):
62 65 auth_user = self.auth_user
63 66 if auth_user:
64 67 return auth_user.ip_addr
65 68 return '<no ip available>'
66 69
67 70 @property
68 71 def server_url(self):
72 default = '<no server_url available>'
69 73 if self.request:
70 74 from rhodecode.lib import helpers as h
71 return h.url('home', qualified=True)
72 return '<no server_url available>'
75 try:
76 return h.url('home', qualified=True)
77 except Exception:
78 log.exception('Failed to fetch URL for server')
79 return default
80
81 return default
73 82
74 83 def as_dict(self):
75 84 data = {
76 85 'name': self.name,
77 86 'utc_timestamp': self.utc_timestamp,
78 87 'actor_ip': self.actor_ip,
79 88 'actor': {
80 89 'username': self.actor.username
81 90 },
82 91 'server_url': self.server_url
83 92 }
84 93 return data
@@ -1,131 +1,134 b''
1 1 # Copyright (C) 2016-2017 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
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
19 import logging
19 20
20 21 from rhodecode.translation import lazy_ugettext
21 22 from rhodecode.events.repo import (
22 23 RepoEvent, _commits_as_dict, _issues_as_dict)
23 24
25 log = logging.getLogger(__name__)
26
24 27
25 28 class PullRequestEvent(RepoEvent):
26 29 """
27 30 Base class for pull request events.
28 31
29 32 :param pullrequest: a :class:`PullRequest` instance
30 33 """
31 34
32 35 def __init__(self, pullrequest):
33 36 super(PullRequestEvent, self).__init__(pullrequest.target_repo)
34 37 self.pullrequest = pullrequest
35 38
36 39 def as_dict(self):
37 40 from rhodecode.model.pull_request import PullRequestModel
38 41 data = super(PullRequestEvent, self).as_dict()
39 42
40 43 commits = _commits_as_dict(
41 44 commit_ids=self.pullrequest.revisions,
42 45 repos=[self.pullrequest.source_repo]
43 46 )
44 47 issues = _issues_as_dict(commits)
45 48
46 49 data.update({
47 50 'pullrequest': {
48 51 'title': self.pullrequest.title,
49 52 'issues': issues,
50 53 'pull_request_id': self.pullrequest.pull_request_id,
51 54 'url': PullRequestModel().get_url(self.pullrequest),
52 55 'status': self.pullrequest.calculated_review_status(),
53 56 'commits': commits,
54 57 }
55 58 })
56 59 return data
57 60
58 61
59 62 class PullRequestCreateEvent(PullRequestEvent):
60 63 """
61 64 An instance of this class is emitted as an :term:`event` after a pull
62 65 request is created.
63 66 """
64 67 name = 'pullrequest-create'
65 68 display_name = lazy_ugettext('pullrequest created')
66 69
67 70
68 71 class PullRequestCloseEvent(PullRequestEvent):
69 72 """
70 73 An instance of this class is emitted as an :term:`event` after a pull
71 74 request is closed.
72 75 """
73 76 name = 'pullrequest-close'
74 77 display_name = lazy_ugettext('pullrequest closed')
75 78
76 79
77 80 class PullRequestUpdateEvent(PullRequestEvent):
78 81 """
79 82 An instance of this class is emitted as an :term:`event` after a pull
80 83 request's commits have been updated.
81 84 """
82 85 name = 'pullrequest-update'
83 86 display_name = lazy_ugettext('pullrequest commits updated')
84 87
85 88
86 89 class PullRequestReviewEvent(PullRequestEvent):
87 90 """
88 91 An instance of this class is emitted as an :term:`event` after a pull
89 92 request review has changed.
90 93 """
91 94 name = 'pullrequest-review'
92 95 display_name = lazy_ugettext('pullrequest review changed')
93 96
94 97
95 98 class PullRequestMergeEvent(PullRequestEvent):
96 99 """
97 100 An instance of this class is emitted as an :term:`event` after a pull
98 101 request is merged.
99 102 """
100 103 name = 'pullrequest-merge'
101 104 display_name = lazy_ugettext('pullrequest merged')
102 105
103 106
104 107 class PullRequestCommentEvent(PullRequestEvent):
105 108 """
106 109 An instance of this class is emitted as an :term:`event` after a pull
107 110 request comment is created.
108 111 """
109 112 name = 'pullrequest-comment'
110 113 display_name = lazy_ugettext('pullrequest commented')
111 114
112 115 def __init__(self, pullrequest, comment):
113 116 super(PullRequestCommentEvent, self).__init__(pullrequest)
114 117 self.comment = comment
115 118
116 119 def as_dict(self):
117 120 from rhodecode.model.comment import CommentsModel
118 121 data = super(PullRequestCommentEvent, self).as_dict()
119 122
120 123 status = None
121 124 if self.comment.status_change:
122 125 status = self.comment.status_change[0].status
123 126
124 127 data.update({
125 128 'comment': {
126 129 'status': status,
127 130 'text': self.comment.text,
128 131 'url': CommentsModel().get_url(self.comment)
129 132 }
130 133 })
131 134 return data
@@ -1,82 +1,85 b''
1 1 # Copyright (C) 2016-2017 RhodeCode GmbH
2 2 #
3 3 # This program is free software: you can redistribute it and/or modify
4 4 # it under the terms of the GNU Affero General Public License, version 3
5 5 # (only), as published by the Free Software Foundation.
6 6 #
7 7 # This program is distributed in the hope that it will be useful,
8 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 # GNU General Public License for more details.
11 11 #
12 12 # You should have received a copy of the GNU Affero General Public License
13 13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 #
15 15 # This program is dual-licensed. If you wish to learn more about the
16 16 # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 # and proprietary license terms, please see https://rhodecode.com/licenses/
18 import logging
18 19
19 20 from zope.interface import implementer
20 21
21 22 from rhodecode.translation import lazy_ugettext
22 23 from rhodecode.events.base import RhodecodeEvent
23 24 from rhodecode.events.interfaces import (
24 25 IUserRegistered, IUserPreCreate, IUserPreUpdate)
25 26
27 log = logging.getLogger(__name__)
28
26 29
27 30 @implementer(IUserRegistered)
28 31 class UserRegistered(RhodecodeEvent):
29 32 """
30 33 An instance of this class is emitted as an :term:`event` whenever a user
31 34 account is registered.
32 35 """
33 36 name = 'user-register'
34 37 display_name = lazy_ugettext('user registered')
35 38
36 39 def __init__(self, user, session):
37 40 super(UserRegistered, self).__init__()
38 41 self.user = user
39 42 self.session = session
40 43
41 44
42 45 @implementer(IUserPreCreate)
43 46 class UserPreCreate(RhodecodeEvent):
44 47 """
45 48 An instance of this class is emitted as an :term:`event` before a new user
46 49 object is created.
47 50 """
48 51 name = 'user-pre-create'
49 52 display_name = lazy_ugettext('user pre create')
50 53
51 54 def __init__(self, user_data):
52 55 super(UserPreCreate, self).__init__()
53 56 self.user_data = user_data
54 57
55 58
56 59 @implementer(IUserPreCreate)
57 60 class UserPostCreate(RhodecodeEvent):
58 61 """
59 62 An instance of this class is emitted as an :term:`event` after a new user
60 63 object is created.
61 64 """
62 65 name = 'user-post-create'
63 66 display_name = lazy_ugettext('user post create')
64 67
65 68 def __init__(self, user_data):
66 69 super(UserPostCreate, self).__init__()
67 70 self.user_data = user_data
68 71
69 72
70 73 @implementer(IUserPreUpdate)
71 74 class UserPreUpdate(RhodecodeEvent):
72 75 """
73 76 An instance of this class is emitted as an :term:`event` before a user
74 77 object is updated.
75 78 """
76 79 name = 'user-pre-update'
77 80 display_name = lazy_ugettext('user pre update')
78 81
79 82 def __init__(self, user, user_data):
80 83 super(UserPreUpdate, self).__init__()
81 84 self.user = user
82 85 self.user_data = user_data
General Comments 0
You need to be logged in to leave comments. Login now