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