##// END OF EJS Templates
events: add repo_type to repo events
dan -
r445:75c0e57d default
parent child Browse files
Show More
@@ -1,219 +1,220 b''
1 # Copyright (C) 2016-2016 RhodeCode GmbH
1 # Copyright (C) 2016-2016 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
24
25 log = logging.getLogger()
25 log = logging.getLogger()
26
26
27
27
28 class RepoEvent(RhodecodeEvent):
28 class RepoEvent(RhodecodeEvent):
29 """
29 """
30 Base class for events acting on a repository.
30 Base class for events acting on a repository.
31
31
32 :param repo: a :class:`Repository` instance
32 :param repo: a :class:`Repository` instance
33 """
33 """
34
34
35 def __init__(self, repo):
35 def __init__(self, repo):
36 super(RepoEvent, self).__init__()
36 super(RepoEvent, self).__init__()
37 self.repo = repo
37 self.repo = repo
38
38
39 def as_dict(self):
39 def as_dict(self):
40 from rhodecode.model.repo import RepoModel
40 from rhodecode.model.repo import RepoModel
41 data = super(RepoEvent, self).as_dict()
41 data = super(RepoEvent, self).as_dict()
42 data.update({
42 data.update({
43 'repo': {
43 'repo': {
44 'repo_id': self.repo.repo_id,
44 'repo_id': self.repo.repo_id,
45 'repo_name': self.repo.repo_name,
45 'repo_name': self.repo.repo_name,
46 'repo_type': self.repo.repo_type,
46 'url': RepoModel().get_url(self.repo)
47 'url': RepoModel().get_url(self.repo)
47 }
48 }
48 })
49 })
49 return data
50 return data
50
51
51 def _commits_as_dict(self, commit_ids):
52 def _commits_as_dict(self, commit_ids):
52 """ Helper function to serialize commit_ids """
53 """ Helper function to serialize commit_ids """
53
54
54 from rhodecode.lib.utils2 import extract_mentioned_users
55 from rhodecode.lib.utils2 import extract_mentioned_users
55 from rhodecode.model.db import Repository
56 from rhodecode.model.db import Repository
56 from rhodecode.lib import helpers as h
57 from rhodecode.lib import helpers as h
57 from rhodecode.lib.helpers import process_patterns
58 from rhodecode.lib.helpers import process_patterns
58 from rhodecode.lib.helpers import urlify_commit_message
59 from rhodecode.lib.helpers import urlify_commit_message
59 if not commit_ids:
60 if not commit_ids:
60 return []
61 return []
61 commits = []
62 commits = []
62 reviewers = []
63 reviewers = []
63 vcs_repo = self.repo.scm_instance(cache=False)
64 vcs_repo = self.repo.scm_instance(cache=False)
64 try:
65 try:
65 for commit_id in commit_ids:
66 for commit_id in commit_ids:
66 cs = vcs_repo.get_changeset(commit_id)
67 cs = vcs_repo.get_changeset(commit_id)
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=self.repo.repo_name,
72 repo_name=self.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'], self.repo.repo_name)
77 cs_data['message'], self.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 self.repo.repo_name)
80 self.repo.repo_name)
80 commits.append(cs_data)
81 commits.append(cs_data)
81 except Exception as e:
82 except Exception as e:
82 log.exception(e)
83 log.exception(e)
83 # we don't send any commits when crash happens, only full list matters
84 # we don't send any commits when crash happens, only full list matters
84 # we short circuit then.
85 # we short circuit then.
85 return []
86 return []
86 return commits
87 return commits
87
88
88 def _issues_as_dict(self, commits):
89 def _issues_as_dict(self, commits):
89 """ Helper function to serialize issues from commits """
90 """ Helper function to serialize issues from commits """
90 issues = {}
91 issues = {}
91 for commit in commits:
92 for commit in commits:
92 for issue in commit['issues']:
93 for issue in commit['issues']:
93 issues[issue['id']] = issue
94 issues[issue['id']] = issue
94 return issues
95 return issues
95
96
96
97
97 class RepoPreCreateEvent(RepoEvent):
98 class RepoPreCreateEvent(RepoEvent):
98 """
99 """
99 An instance of this class is emitted as an :term:`event` before a repo is
100 An instance of this class is emitted as an :term:`event` before a repo is
100 created.
101 created.
101 """
102 """
102 name = 'repo-pre-create'
103 name = 'repo-pre-create'
103 display_name = lazy_ugettext('repository pre create')
104 display_name = lazy_ugettext('repository pre create')
104
105
105
106
106 class RepoCreateEvent(RepoEvent):
107 class RepoCreateEvent(RepoEvent):
107 """
108 """
108 An instance of this class is emitted as an :term:`event` whenever a repo is
109 An instance of this class is emitted as an :term:`event` whenever a repo is
109 created.
110 created.
110 """
111 """
111 name = 'repo-create'
112 name = 'repo-create'
112 display_name = lazy_ugettext('repository created')
113 display_name = lazy_ugettext('repository created')
113
114
114
115
115 class RepoPreDeleteEvent(RepoEvent):
116 class RepoPreDeleteEvent(RepoEvent):
116 """
117 """
117 An instance of this class is emitted as an :term:`event` whenever a repo is
118 An instance of this class is emitted as an :term:`event` whenever a repo is
118 created.
119 created.
119 """
120 """
120 name = 'repo-pre-delete'
121 name = 'repo-pre-delete'
121 display_name = lazy_ugettext('repository pre delete')
122 display_name = lazy_ugettext('repository pre delete')
122
123
123
124
124 class RepoDeleteEvent(RepoEvent):
125 class RepoDeleteEvent(RepoEvent):
125 """
126 """
126 An instance of this class is emitted as an :term:`event` whenever a repo is
127 An instance of this class is emitted as an :term:`event` whenever a repo is
127 created.
128 created.
128 """
129 """
129 name = 'repo-delete'
130 name = 'repo-delete'
130 display_name = lazy_ugettext('repository deleted')
131 display_name = lazy_ugettext('repository deleted')
131
132
132
133
133 class RepoVCSEvent(RepoEvent):
134 class RepoVCSEvent(RepoEvent):
134 """
135 """
135 Base class for events triggered by the VCS
136 Base class for events triggered by the VCS
136 """
137 """
137 def __init__(self, repo_name, extras):
138 def __init__(self, repo_name, extras):
138 self.repo = Repository.get_by_repo_name(repo_name)
139 self.repo = Repository.get_by_repo_name(repo_name)
139 if not self.repo:
140 if not self.repo:
140 raise Exception('repo by this name %s does not exist' % repo_name)
141 raise Exception('repo by this name %s does not exist' % repo_name)
141 self.extras = extras
142 self.extras = extras
142 super(RepoVCSEvent, self).__init__(self.repo)
143 super(RepoVCSEvent, self).__init__(self.repo)
143
144
144 @property
145 @property
145 def actor(self):
146 def actor(self):
146 if self.extras.get('username'):
147 if self.extras.get('username'):
147 return User.get_by_username(self.extras['username'])
148 return User.get_by_username(self.extras['username'])
148
149
149 @property
150 @property
150 def actor_ip(self):
151 def actor_ip(self):
151 if self.extras.get('ip'):
152 if self.extras.get('ip'):
152 return self.extras['ip']
153 return self.extras['ip']
153
154
154
155
155 class RepoPrePullEvent(RepoVCSEvent):
156 class RepoPrePullEvent(RepoVCSEvent):
156 """
157 """
157 An instance of this class is emitted as an :term:`event` before commits
158 An instance of this class is emitted as an :term:`event` before commits
158 are pulled from a repo.
159 are pulled from a repo.
159 """
160 """
160 name = 'repo-pre-pull'
161 name = 'repo-pre-pull'
161 display_name = lazy_ugettext('repository pre pull')
162 display_name = lazy_ugettext('repository pre pull')
162
163
163
164
164 class RepoPullEvent(RepoVCSEvent):
165 class RepoPullEvent(RepoVCSEvent):
165 """
166 """
166 An instance of this class is emitted as an :term:`event` after commits
167 An instance of this class is emitted as an :term:`event` after commits
167 are pulled from a repo.
168 are pulled from a repo.
168 """
169 """
169 name = 'repo-pull'
170 name = 'repo-pull'
170 display_name = lazy_ugettext('repository pull')
171 display_name = lazy_ugettext('repository pull')
171
172
172
173
173 class RepoPrePushEvent(RepoVCSEvent):
174 class RepoPrePushEvent(RepoVCSEvent):
174 """
175 """
175 An instance of this class is emitted as an :term:`event` before commits
176 An instance of this class is emitted as an :term:`event` before commits
176 are pushed to a repo.
177 are pushed to a repo.
177 """
178 """
178 name = 'repo-pre-push'
179 name = 'repo-pre-push'
179 display_name = lazy_ugettext('repository pre push')
180 display_name = lazy_ugettext('repository pre push')
180
181
181
182
182 class RepoPushEvent(RepoVCSEvent):
183 class RepoPushEvent(RepoVCSEvent):
183 """
184 """
184 An instance of this class is emitted as an :term:`event` after commits
185 An instance of this class is emitted as an :term:`event` after commits
185 are pushed to a repo.
186 are pushed to a repo.
186
187
187 :param extras: (optional) dict of data from proxied VCS actions
188 :param extras: (optional) dict of data from proxied VCS actions
188 """
189 """
189 name = 'repo-push'
190 name = 'repo-push'
190 display_name = lazy_ugettext('repository push')
191 display_name = lazy_ugettext('repository push')
191
192
192 def __init__(self, repo_name, pushed_commit_ids, extras):
193 def __init__(self, repo_name, pushed_commit_ids, extras):
193 super(RepoPushEvent, self).__init__(repo_name, extras)
194 super(RepoPushEvent, self).__init__(repo_name, extras)
194 self.pushed_commit_ids = pushed_commit_ids
195 self.pushed_commit_ids = pushed_commit_ids
195
196
196 def as_dict(self):
197 def as_dict(self):
197 data = super(RepoPushEvent, self).as_dict()
198 data = super(RepoPushEvent, self).as_dict()
198 branch_url = repo_url = data['repo']['url']
199 branch_url = repo_url = data['repo']['url']
199
200
200 commits = self._commits_as_dict(self.pushed_commit_ids)
201 commits = self._commits_as_dict(self.pushed_commit_ids)
201 issues = self._issues_as_dict(commits)
202 issues = self._issues_as_dict(commits)
202
203
203 branches = set(
204 branches = set(
204 commit['branch'] for commit in commits if commit['branch'])
205 commit['branch'] for commit in commits if commit['branch'])
205 branches = [
206 branches = [
206 {
207 {
207 'name': branch,
208 'name': branch,
208 'url': '{}/changelog?branch={}'.format(
209 'url': '{}/changelog?branch={}'.format(
209 data['repo']['url'], branch)
210 data['repo']['url'], branch)
210 }
211 }
211 for branch in branches
212 for branch in branches
212 ]
213 ]
213
214
214 data['push'] = {
215 data['push'] = {
215 'commits': commits,
216 'commits': commits,
216 'issues': issues,
217 'issues': issues,
217 'branches': branches,
218 'branches': branches,
218 }
219 }
219 return data No newline at end of file
220 return data
General Comments 0
You need to be logged in to leave comments. Login now