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