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