##// END OF EJS Templates
events: fixed missing construcors on user based events.
marcink -
r1422:5294110a default
parent child Browse files
Show More
@@ -1,263 +1,265 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 import logging
20
20
21 from rhodecode.translation import lazy_ugettext
21 from rhodecode.translation import lazy_ugettext
22 from rhodecode.model.db import User, Repository, Session
22 from rhodecode.model.db import User, Repository, Session
23 from rhodecode.events.base import RhodecodeEvent
23 from rhodecode.events.base import RhodecodeEvent
24 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
24 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
25
25
26 log = logging.getLogger(__name__)
26 log = logging.getLogger(__name__)
27
27
28
28 def _commits_as_dict(commit_ids, repos):
29 def _commits_as_dict(commit_ids, repos):
29 """
30 """
30 Helper function to serialize commit_ids
31 Helper function to serialize commit_ids
31
32
32 :param commit_ids: commits to get
33 :param commit_ids: commits to get
33 :param repos: list of repos to check
34 :param repos: list of repos to check
34 """
35 """
35 from rhodecode.lib.utils2 import extract_mentioned_users
36 from rhodecode.lib.utils2 import extract_mentioned_users
36 from rhodecode.model.db import Repository
37 from rhodecode.model.db import Repository
37 from rhodecode.lib import helpers as h
38 from rhodecode.lib import helpers as h
38 from rhodecode.lib.helpers import process_patterns
39 from rhodecode.lib.helpers import process_patterns
39 from rhodecode.lib.helpers import urlify_commit_message
40 from rhodecode.lib.helpers import urlify_commit_message
40
41
41 if not repos:
42 if not repos:
42 raise Exception('no repo defined')
43 raise Exception('no repo defined')
43
44
44 if not isinstance(repos, (tuple, list)):
45 if not isinstance(repos, (tuple, list)):
45 repos = [repos]
46 repos = [repos]
46
47
47 if not commit_ids:
48 if not commit_ids:
48 return []
49 return []
49
50
50 needed_commits = list(commit_ids)
51 needed_commits = list(commit_ids)
51
52
52 commits = []
53 commits = []
53 reviewers = []
54 reviewers = []
54 for repo in repos:
55 for repo in repos:
55 if not needed_commits:
56 if not needed_commits:
56 return commits # return early if we have the commits we need
57 return commits # return early if we have the commits we need
57
58
58 vcs_repo = repo.scm_instance(cache=False)
59 vcs_repo = repo.scm_instance(cache=False)
59 try:
60 try:
60 # use copy of needed_commits since we modify it while iterating
61 # use copy of needed_commits since we modify it while iterating
61 for commit_id in list(needed_commits):
62 for commit_id in list(needed_commits):
62 try:
63 try:
63 cs = vcs_repo.get_changeset(commit_id)
64 cs = vcs_repo.get_changeset(commit_id)
64 except CommitDoesNotExistError:
65 except CommitDoesNotExistError:
65 continue # maybe its in next repo
66 continue # maybe its in next repo
66
67
67 cs_data = cs.__json__()
68 cs_data = cs.__json__()
68 cs_data['mentions'] = extract_mentioned_users(cs_data['message'])
69 cs_data['mentions'] = extract_mentioned_users(cs_data['message'])
69 cs_data['reviewers'] = reviewers
70 cs_data['reviewers'] = reviewers
70 cs_data['url'] = h.url('changeset_home',
71 cs_data['url'] = h.url('changeset_home',
71 repo_name=repo.repo_name,
72 repo_name=repo.repo_name,
72 revision=cs_data['raw_id'],
73 revision=cs_data['raw_id'],
73 qualified=True
74 qualified=True
74 )
75 )
75 urlified_message, issues_data = process_patterns(
76 urlified_message, issues_data = process_patterns(
76 cs_data['message'], repo.repo_name)
77 cs_data['message'], repo.repo_name)
77 cs_data['issues'] = issues_data
78 cs_data['issues'] = issues_data
78 cs_data['message_html'] = urlify_commit_message(cs_data['message'],
79 cs_data['message_html'] = urlify_commit_message(cs_data['message'],
79 repo.repo_name)
80 repo.repo_name)
80 commits.append(cs_data)
81 commits.append(cs_data)
81
82
82 needed_commits.remove(commit_id)
83 needed_commits.remove(commit_id)
83
84
84 except Exception as e:
85 except Exception as e:
85 log.exception(e)
86 log.exception(e)
86 # we don't send any commits when crash happens, only full list matters
87 # we don't send any commits when crash happens, only full list matters
87 # we short circuit then.
88 # we short circuit then.
88 return []
89 return []
89
90
90 missing_commits = set(commit_ids) - set(c['raw_id'] for c in commits)
91 missing_commits = set(commit_ids) - set(c['raw_id'] for c in commits)
91 if missing_commits:
92 if missing_commits:
92 log.error('missing commits: %s' % ', '.join(missing_commits))
93 log.error('missing commits: %s' % ', '.join(missing_commits))
93
94
94 return commits
95 return commits
95
96
96
97
97 def _issues_as_dict(commits):
98 def _issues_as_dict(commits):
98 """ Helper function to serialize issues from commits """
99 """ Helper function to serialize issues from commits """
99 issues = {}
100 issues = {}
100 for commit in commits:
101 for commit in commits:
101 for issue in commit['issues']:
102 for issue in commit['issues']:
102 issues[issue['id']] = issue
103 issues[issue['id']] = issue
103 return issues
104 return issues
104
105
106
105 class RepoEvent(RhodecodeEvent):
107 class RepoEvent(RhodecodeEvent):
106 """
108 """
107 Base class for events acting on a repository.
109 Base class for events acting on a repository.
108
110
109 :param repo: a :class:`Repository` instance
111 :param repo: a :class:`Repository` instance
110 """
112 """
111
113
112 def __init__(self, repo):
114 def __init__(self, repo):
113 super(RepoEvent, self).__init__()
115 super(RepoEvent, self).__init__()
114 self.repo = repo
116 self.repo = repo
115
117
116 def as_dict(self):
118 def as_dict(self):
117 from rhodecode.model.repo import RepoModel
119 from rhodecode.model.repo import RepoModel
118 data = super(RepoEvent, self).as_dict()
120 data = super(RepoEvent, self).as_dict()
119 data.update({
121 data.update({
120 'repo': {
122 'repo': {
121 'repo_id': self.repo.repo_id,
123 'repo_id': self.repo.repo_id,
122 'repo_name': self.repo.repo_name,
124 'repo_name': self.repo.repo_name,
123 'repo_type': self.repo.repo_type,
125 'repo_type': self.repo.repo_type,
124 'url': RepoModel().get_url(self.repo)
126 'url': RepoModel().get_url(self.repo)
125 }
127 }
126 })
128 })
127 return data
129 return data
128
130
129
131
130 class RepoPreCreateEvent(RepoEvent):
132 class RepoPreCreateEvent(RepoEvent):
131 """
133 """
132 An instance of this class is emitted as an :term:`event` before a repo is
134 An instance of this class is emitted as an :term:`event` before a repo is
133 created.
135 created.
134 """
136 """
135 name = 'repo-pre-create'
137 name = 'repo-pre-create'
136 display_name = lazy_ugettext('repository pre create')
138 display_name = lazy_ugettext('repository pre create')
137
139
138
140
139 class RepoCreateEvent(RepoEvent):
141 class RepoCreateEvent(RepoEvent):
140 """
142 """
141 An instance of this class is emitted as an :term:`event` whenever a repo is
143 An instance of this class is emitted as an :term:`event` whenever a repo is
142 created.
144 created.
143 """
145 """
144 name = 'repo-create'
146 name = 'repo-create'
145 display_name = lazy_ugettext('repository created')
147 display_name = lazy_ugettext('repository created')
146
148
147
149
148 class RepoPreDeleteEvent(RepoEvent):
150 class RepoPreDeleteEvent(RepoEvent):
149 """
151 """
150 An instance of this class is emitted as an :term:`event` whenever a repo is
152 An instance of this class is emitted as an :term:`event` whenever a repo is
151 created.
153 created.
152 """
154 """
153 name = 'repo-pre-delete'
155 name = 'repo-pre-delete'
154 display_name = lazy_ugettext('repository pre delete')
156 display_name = lazy_ugettext('repository pre delete')
155
157
156
158
157 class RepoDeleteEvent(RepoEvent):
159 class RepoDeleteEvent(RepoEvent):
158 """
160 """
159 An instance of this class is emitted as an :term:`event` whenever a repo is
161 An instance of this class is emitted as an :term:`event` whenever a repo is
160 created.
162 created.
161 """
163 """
162 name = 'repo-delete'
164 name = 'repo-delete'
163 display_name = lazy_ugettext('repository deleted')
165 display_name = lazy_ugettext('repository deleted')
164
166
165
167
166 class RepoVCSEvent(RepoEvent):
168 class RepoVCSEvent(RepoEvent):
167 """
169 """
168 Base class for events triggered by the VCS
170 Base class for events triggered by the VCS
169 """
171 """
170 def __init__(self, repo_name, extras):
172 def __init__(self, repo_name, extras):
171 self.repo = Repository.get_by_repo_name(repo_name)
173 self.repo = Repository.get_by_repo_name(repo_name)
172 if not self.repo:
174 if not self.repo:
173 raise Exception('repo by this name %s does not exist' % repo_name)
175 raise Exception('repo by this name %s does not exist' % repo_name)
174 self.extras = extras
176 self.extras = extras
175 super(RepoVCSEvent, self).__init__(self.repo)
177 super(RepoVCSEvent, self).__init__(self.repo)
176
178
177 @property
179 @property
178 def actor(self):
180 def actor(self):
179 if self.extras.get('username'):
181 if self.extras.get('username'):
180 return User.get_by_username(self.extras['username'])
182 return User.get_by_username(self.extras['username'])
181
183
182 @property
184 @property
183 def actor_ip(self):
185 def actor_ip(self):
184 if self.extras.get('ip'):
186 if self.extras.get('ip'):
185 return self.extras['ip']
187 return self.extras['ip']
186
188
187 @property
189 @property
188 def server_url(self):
190 def server_url(self):
189 if self.extras.get('server_url'):
191 if self.extras.get('server_url'):
190 return self.extras['server_url']
192 return self.extras['server_url']
191
193
192
194
193 class RepoPrePullEvent(RepoVCSEvent):
195 class RepoPrePullEvent(RepoVCSEvent):
194 """
196 """
195 An instance of this class is emitted as an :term:`event` before commits
197 An instance of this class is emitted as an :term:`event` before commits
196 are pulled from a repo.
198 are pulled from a repo.
197 """
199 """
198 name = 'repo-pre-pull'
200 name = 'repo-pre-pull'
199 display_name = lazy_ugettext('repository pre pull')
201 display_name = lazy_ugettext('repository pre pull')
200
202
201
203
202 class RepoPullEvent(RepoVCSEvent):
204 class RepoPullEvent(RepoVCSEvent):
203 """
205 """
204 An instance of this class is emitted as an :term:`event` after commits
206 An instance of this class is emitted as an :term:`event` after commits
205 are pulled from a repo.
207 are pulled from a repo.
206 """
208 """
207 name = 'repo-pull'
209 name = 'repo-pull'
208 display_name = lazy_ugettext('repository pull')
210 display_name = lazy_ugettext('repository pull')
209
211
210
212
211 class RepoPrePushEvent(RepoVCSEvent):
213 class RepoPrePushEvent(RepoVCSEvent):
212 """
214 """
213 An instance of this class is emitted as an :term:`event` before commits
215 An instance of this class is emitted as an :term:`event` before commits
214 are pushed to a repo.
216 are pushed to a repo.
215 """
217 """
216 name = 'repo-pre-push'
218 name = 'repo-pre-push'
217 display_name = lazy_ugettext('repository pre push')
219 display_name = lazy_ugettext('repository pre push')
218
220
219
221
220 class RepoPushEvent(RepoVCSEvent):
222 class RepoPushEvent(RepoVCSEvent):
221 """
223 """
222 An instance of this class is emitted as an :term:`event` after commits
224 An instance of this class is emitted as an :term:`event` after commits
223 are pushed to a repo.
225 are pushed to a repo.
224
226
225 :param extras: (optional) dict of data from proxied VCS actions
227 :param extras: (optional) dict of data from proxied VCS actions
226 """
228 """
227 name = 'repo-push'
229 name = 'repo-push'
228 display_name = lazy_ugettext('repository push')
230 display_name = lazy_ugettext('repository push')
229
231
230 def __init__(self, repo_name, pushed_commit_ids, extras):
232 def __init__(self, repo_name, pushed_commit_ids, extras):
231 super(RepoPushEvent, self).__init__(repo_name, extras)
233 super(RepoPushEvent, self).__init__(repo_name, extras)
232 self.pushed_commit_ids = pushed_commit_ids
234 self.pushed_commit_ids = pushed_commit_ids
233
235
234 def as_dict(self):
236 def as_dict(self):
235 data = super(RepoPushEvent, self).as_dict()
237 data = super(RepoPushEvent, self).as_dict()
236 branch_url = repo_url = data['repo']['url']
238 branch_url = repo_url = data['repo']['url']
237
239
238 commits = _commits_as_dict(
240 commits = _commits_as_dict(
239 commit_ids=self.pushed_commit_ids, repos=[self.repo])
241 commit_ids=self.pushed_commit_ids, repos=[self.repo])
240
242
241 last_branch = None
243 last_branch = None
242 for commit in reversed(commits):
244 for commit in reversed(commits):
243 commit['branch'] = commit['branch'] or last_branch
245 commit['branch'] = commit['branch'] or last_branch
244 last_branch = commit['branch']
246 last_branch = commit['branch']
245 issues = _issues_as_dict(commits)
247 issues = _issues_as_dict(commits)
246
248
247 branches = set(
249 branches = set(
248 commit['branch'] for commit in commits if commit['branch'])
250 commit['branch'] for commit in commits if commit['branch'])
249 branches = [
251 branches = [
250 {
252 {
251 'name': branch,
253 'name': branch,
252 'url': '{}/changelog?branch={}'.format(
254 'url': '{}/changelog?branch={}'.format(
253 data['repo']['url'], branch)
255 data['repo']['url'], branch)
254 }
256 }
255 for branch in branches
257 for branch in branches
256 ]
258 ]
257
259
258 data['push'] = {
260 data['push'] = {
259 'commits': commits,
261 'commits': commits,
260 'issues': issues,
262 'issues': issues,
261 'branches': branches,
263 'branches': branches,
262 }
264 }
263 return data
265 return data
@@ -1,78 +1,82 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 from zope.interface import implementer
19 from zope.interface import implementer
20
20
21 from rhodecode.translation import lazy_ugettext
21 from rhodecode.translation import lazy_ugettext
22 from rhodecode.events.base import RhodecodeEvent
22 from rhodecode.events.base import RhodecodeEvent
23 from rhodecode.events.interfaces import (
23 from rhodecode.events.interfaces import (
24 IUserRegistered, IUserPreCreate, IUserPreUpdate)
24 IUserRegistered, IUserPreCreate, IUserPreUpdate)
25
25
26
26
27 @implementer(IUserRegistered)
27 @implementer(IUserRegistered)
28 class UserRegistered(RhodecodeEvent):
28 class UserRegistered(RhodecodeEvent):
29 """
29 """
30 An instance of this class is emitted as an :term:`event` whenever a user
30 An instance of this class is emitted as an :term:`event` whenever a user
31 account is registered.
31 account is registered.
32 """
32 """
33 name = 'user-register'
33 name = 'user-register'
34 display_name = lazy_ugettext('user registered')
34 display_name = lazy_ugettext('user registered')
35
35
36 def __init__(self, user, session):
36 def __init__(self, user, session):
37 super(UserRegistered, self).__init__()
37 self.user = user
38 self.user = user
38 self.session = session
39 self.session = session
39
40
40
41
41 @implementer(IUserPreCreate)
42 @implementer(IUserPreCreate)
42 class UserPreCreate(RhodecodeEvent):
43 class UserPreCreate(RhodecodeEvent):
43 """
44 """
44 An instance of this class is emitted as an :term:`event` before a new user
45 An instance of this class is emitted as an :term:`event` before a new user
45 object is created.
46 object is created.
46 """
47 """
47 name = 'user-pre-create'
48 name = 'user-pre-create'
48 display_name = lazy_ugettext('user pre create')
49 display_name = lazy_ugettext('user pre create')
49
50
50 def __init__(self, user_data):
51 def __init__(self, user_data):
52 super(UserPreCreate, self).__init__()
51 self.user_data = user_data
53 self.user_data = user_data
52
54
53
55
54 @implementer(IUserPreCreate)
56 @implementer(IUserPreCreate)
55 class UserPostCreate(RhodecodeEvent):
57 class UserPostCreate(RhodecodeEvent):
56 """
58 """
57 An instance of this class is emitted as an :term:`event` after a new user
59 An instance of this class is emitted as an :term:`event` after a new user
58 object is created.
60 object is created.
59 """
61 """
60 name = 'user-post-create'
62 name = 'user-post-create'
61 display_name = lazy_ugettext('user post create')
63 display_name = lazy_ugettext('user post create')
62
64
63 def __init__(self, user_data):
65 def __init__(self, user_data):
66 super(UserPostCreate, self).__init__()
64 self.user_data = user_data
67 self.user_data = user_data
65
68
66
69
67 @implementer(IUserPreUpdate)
70 @implementer(IUserPreUpdate)
68 class UserPreUpdate(RhodecodeEvent):
71 class UserPreUpdate(RhodecodeEvent):
69 """
72 """
70 An instance of this class is emitted as an :term:`event` before a user
73 An instance of this class is emitted as an :term:`event` before a user
71 object is updated.
74 object is updated.
72 """
75 """
73 name = 'user-pre-update'
76 name = 'user-pre-update'
74 display_name = lazy_ugettext('user pre update')
77 display_name = lazy_ugettext('user pre update')
75
78
76 def __init__(self, user, user_data):
79 def __init__(self, user, user_data):
80 super(UserPreUpdate, self).__init__()
77 self.user = user
81 self.user = user
78 self.user_data = user_data
82 self.user_data = user_data
General Comments 0
You need to be logged in to leave comments. Login now