Show More
@@ -1,140 +1,141 b'' | |||
|
1 | 1 | # Copyright (C) 2016-2023 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 | 19 | import logging |
|
20 | 20 | import datetime |
|
21 | 21 | |
|
22 | 22 | from zope.cachedescriptors.property import Lazy as LazyProperty |
|
23 | 23 | |
|
24 | 24 | from rhodecode.lib.pyramid_utils import get_current_request |
|
25 | from rhodecode.lib.auth import AuthUser | |
|
26 | 25 | from rhodecode.lib.utils2 import AttributeDict |
|
27 | 26 | |
|
28 | 27 | |
|
29 | 28 | # this is a user object to be used for events caused by the system (eg. shell) |
|
30 | 29 | SYSTEM_USER = AttributeDict(dict( |
|
31 | 30 | username='__SYSTEM__', |
|
32 | 31 | user_id='__SYSTEM_ID__' |
|
33 | 32 | )) |
|
34 | 33 | |
|
35 | 34 | log = logging.getLogger(__name__) |
|
36 | 35 | |
|
37 | 36 | |
|
38 | 37 | class RhodecodeEvent(object): |
|
39 | 38 | """ |
|
40 | 39 | Base event class for all RhodeCode events |
|
41 | 40 | """ |
|
42 | 41 | name = "RhodeCodeEvent" |
|
43 | 42 | no_url_set = '<no server_url available>' |
|
44 | 43 | |
|
45 | 44 | def __init__(self, request=None, actor=None): |
|
46 | 45 | self._request = request |
|
47 | 46 | self._actor = actor |
|
48 | 47 | self.utc_timestamp = datetime.datetime.utcnow() |
|
49 | 48 | |
|
50 | 49 | def __repr__(self): |
|
51 | 50 | return '<{}:({})>'.format(self.__class__.__name__, self.name) |
|
52 | 51 | |
|
53 | 52 | def get_request(self): |
|
54 | 53 | if self._request: |
|
55 | 54 | return self._request |
|
56 | 55 | return get_current_request() |
|
57 | 56 | |
|
58 | 57 | @LazyProperty |
|
59 | 58 | def request(self): |
|
60 | 59 | return self.get_request() |
|
61 | 60 | |
|
62 | 61 | @property |
|
63 | 62 | def auth_user(self): |
|
64 | 63 | if not self.request: |
|
65 | 64 | return |
|
66 | 65 | |
|
67 | 66 | user = getattr(self.request, 'user', None) |
|
68 | 67 | if user: |
|
69 | 68 | return user |
|
70 | 69 | |
|
71 | 70 | api_user = getattr(self.request, 'rpc_user', None) |
|
72 | 71 | if api_user: |
|
73 | 72 | return api_user |
|
74 | 73 | |
|
75 | 74 | @property |
|
76 | 75 | def actor(self): |
|
76 | from rhodecode.lib.auth import AuthUser | |
|
77 | ||
|
77 | 78 | # if an explicit actor is specified, use this |
|
78 | 79 | if self._actor: |
|
79 | 80 | return self._actor |
|
80 | 81 | |
|
81 | 82 | auth_user = self.auth_user |
|
82 | 83 | log.debug('Got integration actor: %s', auth_user) |
|
83 | 84 | if isinstance(auth_user, AuthUser): |
|
84 | 85 | instance = auth_user.get_instance() |
|
85 | 86 | # we can't find this DB user... |
|
86 | 87 | if not instance: |
|
87 | 88 | return AttributeDict(dict( |
|
88 | 89 | username=auth_user.username, |
|
89 | 90 | user_id=auth_user.user_id, |
|
90 | 91 | )) |
|
91 | 92 | elif auth_user: |
|
92 | 93 | return auth_user |
|
93 | 94 | return SYSTEM_USER |
|
94 | 95 | |
|
95 | 96 | @property |
|
96 | 97 | def actor_ip(self): |
|
97 | 98 | auth_user = self.auth_user |
|
98 | 99 | if auth_user: |
|
99 | 100 | return auth_user.ip_addr |
|
100 | 101 | return '<no ip available>' |
|
101 | 102 | |
|
102 | 103 | @property |
|
103 | 104 | def server_url(self): |
|
104 | 105 | if self.request: |
|
105 | 106 | try: |
|
106 | 107 | return self.request.route_url('home') |
|
107 | 108 | except Exception: |
|
108 | 109 | log.exception('Failed to fetch URL for server') |
|
109 | 110 | return self.no_url_set |
|
110 | 111 | |
|
111 | 112 | return self.no_url_set |
|
112 | 113 | |
|
113 | 114 | def as_dict(self): |
|
114 | 115 | data = { |
|
115 | 116 | 'name': self.name, |
|
116 | 117 | 'utc_timestamp': self.utc_timestamp, |
|
117 | 118 | 'actor_ip': self.actor_ip, |
|
118 | 119 | 'actor': { |
|
119 | 120 | 'username': self.actor.username, |
|
120 | 121 | 'user_id': self.actor.user_id |
|
121 | 122 | }, |
|
122 | 123 | 'server_url': self.server_url |
|
123 | 124 | } |
|
124 | 125 | return data |
|
125 | 126 | |
|
126 | 127 | |
|
127 | 128 | class RhodeCodeIntegrationEvent(RhodecodeEvent): |
|
128 | 129 | """ |
|
129 | 130 | Special subclass for Integration events |
|
130 | 131 | """ |
|
131 | 132 | description = '' |
|
132 | 133 | |
|
133 | 134 | |
|
134 | 135 | class FtsBuild(RhodecodeEvent): |
|
135 | 136 | """ |
|
136 | 137 | This event will be triggered when FTS Build is triggered |
|
137 | 138 | """ |
|
138 | 139 | name = 'fts-build' |
|
139 | 140 | display_name = 'Start FTS Build' |
|
140 | 141 |
General Comments 0
You need to be logged in to leave comments.
Login now