##// END OF EJS Templates
pull-requests: make the renderer stored and saved for each pull requests....
marcink -
r2903:6d16d1cd default
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -0,0 +1,40 b''
1 import logging
2
3 from sqlalchemy import *
4
5 from rhodecode.model import meta
6 from rhodecode.lib.dbmigrate.versions import _reset_base, notify
7
8 log = logging.getLogger(__name__)
9
10
11 def upgrade(migrate_engine):
12 """
13 Upgrade operations go here.
14 Don't create your own engine; bind migrate_engine to your metadata
15 """
16 _reset_base(migrate_engine)
17 from rhodecode.lib.dbmigrate.schema import db_4_11_0_0 as db
18
19 pull_request_table = db.PullRequest.__table__
20 pull_request_version_table = db.PullRequestVersion.__table__
21
22 renderer = Column('description_renderer', Unicode(64), nullable=True)
23 renderer.create(table=pull_request_table)
24
25 renderer_ver = Column('description_renderer', Unicode(64), nullable=True)
26 renderer_ver.create(table=pull_request_version_table)
27
28 # issue fixups
29 fixups(db, meta.Session)
30
31
32 def downgrade(migrate_engine):
33 meta = MetaData()
34 meta.bind = migrate_engine
35
36
37 def fixups(models, _SESSION):
38 pass
39
40
@@ -1,63 +1,63 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22
23 23 RhodeCode, a web based repository management software
24 24 versioning implementation: http://www.python.org/dev/peps/pep-0386/
25 25 """
26 26
27 27 import os
28 28 import sys
29 29 import platform
30 30
31 31 VERSION = tuple(open(os.path.join(
32 32 os.path.dirname(__file__), 'VERSION')).read().split('.'))
33 33
34 34 BACKENDS = {
35 35 'hg': 'Mercurial repository',
36 36 'git': 'Git repository',
37 37 'svn': 'Subversion repository',
38 38 }
39 39
40 40 CELERY_ENABLED = False
41 41 CELERY_EAGER = False
42 42
43 43 # link to config for pyramid
44 44 CONFIG = {}
45 45
46 46 # Populated with the settings dictionary from application init in
47 47 # rhodecode.conf.environment.load_pyramid_environment
48 48 PYRAMID_SETTINGS = {}
49 49
50 50 # Linked module for extensions
51 51 EXTENSIONS = {}
52 52
53 53 __version__ = ('.'.join((str(each) for each in VERSION[:3])))
54 __dbversion__ = 86 # defines current db version for migrations
54 __dbversion__ = 87 # defines current db version for migrations
55 55 __platform__ = platform.system()
56 56 __license__ = 'AGPLv3, and Commercial License'
57 57 __author__ = 'RhodeCode GmbH'
58 58 __url__ = 'https://code.rhodecode.com'
59 59
60 60 is_windows = __platform__ in ['Windows']
61 61 is_unix = not is_windows
62 62 is_test = False
63 63 disable_error_handler = False
@@ -1,1203 +1,1206 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20 import mock
21 21 import pytest
22 22
23 23 import rhodecode
24 24 from rhodecode.lib.vcs.backends.base import MergeResponse, MergeFailureReason
25 25 from rhodecode.lib.vcs.nodes import FileNode
26 26 from rhodecode.lib import helpers as h
27 27 from rhodecode.model.changeset_status import ChangesetStatusModel
28 28 from rhodecode.model.db import (
29 29 PullRequest, ChangesetStatus, UserLog, Notification, ChangesetComment)
30 30 from rhodecode.model.meta import Session
31 31 from rhodecode.model.pull_request import PullRequestModel
32 32 from rhodecode.model.user import UserModel
33 33 from rhodecode.tests import (
34 34 assert_session_flash, TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN)
35 35 from rhodecode.tests.utils import AssertResponse
36 36
37 37
38 38 def route_path(name, params=None, **kwargs):
39 39 import urllib
40 40
41 41 base_url = {
42 42 'repo_changelog': '/{repo_name}/changelog',
43 43 'repo_changelog_file': '/{repo_name}/changelog/{commit_id}/{f_path}',
44 44 'pullrequest_show': '/{repo_name}/pull-request/{pull_request_id}',
45 45 'pullrequest_show_all': '/{repo_name}/pull-request',
46 46 'pullrequest_show_all_data': '/{repo_name}/pull-request-data',
47 47 'pullrequest_repo_refs': '/{repo_name}/pull-request/refs/{target_repo_name:.*?[^/]}',
48 48 'pullrequest_repo_destinations': '/{repo_name}/pull-request/repo-destinations',
49 49 'pullrequest_new': '/{repo_name}/pull-request/new',
50 50 'pullrequest_create': '/{repo_name}/pull-request/create',
51 51 'pullrequest_update': '/{repo_name}/pull-request/{pull_request_id}/update',
52 52 'pullrequest_merge': '/{repo_name}/pull-request/{pull_request_id}/merge',
53 53 'pullrequest_delete': '/{repo_name}/pull-request/{pull_request_id}/delete',
54 54 'pullrequest_comment_create': '/{repo_name}/pull-request/{pull_request_id}/comment',
55 55 'pullrequest_comment_delete': '/{repo_name}/pull-request/{pull_request_id}/comment/{comment_id}/delete',
56 56 }[name].format(**kwargs)
57 57
58 58 if params:
59 59 base_url = '{}?{}'.format(base_url, urllib.urlencode(params))
60 60 return base_url
61 61
62 62
63 63 @pytest.mark.usefixtures('app', 'autologin_user')
64 64 @pytest.mark.backends("git", "hg")
65 65 class TestPullrequestsView(object):
66 66
67 67 def test_index(self, backend):
68 68 self.app.get(route_path(
69 69 'pullrequest_new',
70 70 repo_name=backend.repo_name))
71 71
72 72 def test_option_menu_create_pull_request_exists(self, backend):
73 73 repo_name = backend.repo_name
74 74 response = self.app.get(h.route_path('repo_summary', repo_name=repo_name))
75 75
76 76 create_pr_link = '<a href="%s">Create Pull Request</a>' % route_path(
77 77 'pullrequest_new', repo_name=repo_name)
78 78 response.mustcontain(create_pr_link)
79 79
80 80 def test_create_pr_form_with_raw_commit_id(self, backend):
81 81 repo = backend.repo
82 82
83 83 self.app.get(
84 84 route_path('pullrequest_new',
85 85 repo_name=repo.repo_name,
86 86 commit=repo.get_commit().raw_id),
87 87 status=200)
88 88
89 89 @pytest.mark.parametrize('pr_merge_enabled', [True, False])
90 90 def test_show(self, pr_util, pr_merge_enabled):
91 91 pull_request = pr_util.create_pull_request(
92 92 mergeable=pr_merge_enabled, enable_notifications=False)
93 93
94 94 response = self.app.get(route_path(
95 95 'pullrequest_show',
96 96 repo_name=pull_request.target_repo.scm_instance().name,
97 97 pull_request_id=pull_request.pull_request_id))
98 98
99 99 for commit_id in pull_request.revisions:
100 100 response.mustcontain(commit_id)
101 101
102 102 assert pull_request.target_ref_parts.type in response
103 103 assert pull_request.target_ref_parts.name in response
104 104 target_clone_url = pull_request.target_repo.clone_url()
105 105 assert target_clone_url in response
106 106
107 107 assert 'class="pull-request-merge"' in response
108 108 assert (
109 109 'Server-side pull request merging is disabled.'
110 110 in response) != pr_merge_enabled
111 111
112 112 def test_close_status_visibility(self, pr_util, user_util, csrf_token):
113 113 # Logout
114 114 response = self.app.post(
115 115 h.route_path('logout'),
116 116 params={'csrf_token': csrf_token})
117 117 # Login as regular user
118 118 response = self.app.post(h.route_path('login'),
119 119 {'username': TEST_USER_REGULAR_LOGIN,
120 120 'password': 'test12'})
121 121
122 122 pull_request = pr_util.create_pull_request(
123 123 author=TEST_USER_REGULAR_LOGIN)
124 124
125 125 response = self.app.get(route_path(
126 126 'pullrequest_show',
127 127 repo_name=pull_request.target_repo.scm_instance().name,
128 128 pull_request_id=pull_request.pull_request_id))
129 129
130 130 response.mustcontain('Server-side pull request merging is disabled.')
131 131
132 132 assert_response = response.assert_response()
133 133 # for regular user without a merge permissions, we don't see it
134 134 assert_response.no_element_exists('#close-pull-request-action')
135 135
136 136 user_util.grant_user_permission_to_repo(
137 137 pull_request.target_repo,
138 138 UserModel().get_by_username(TEST_USER_REGULAR_LOGIN),
139 139 'repository.write')
140 140 response = self.app.get(route_path(
141 141 'pullrequest_show',
142 142 repo_name=pull_request.target_repo.scm_instance().name,
143 143 pull_request_id=pull_request.pull_request_id))
144 144
145 145 response.mustcontain('Server-side pull request merging is disabled.')
146 146
147 147 assert_response = response.assert_response()
148 148 # now regular user has a merge permissions, we have CLOSE button
149 149 assert_response.one_element_exists('#close-pull-request-action')
150 150
151 151 def test_show_invalid_commit_id(self, pr_util):
152 152 # Simulating invalid revisions which will cause a lookup error
153 153 pull_request = pr_util.create_pull_request()
154 154 pull_request.revisions = ['invalid']
155 155 Session().add(pull_request)
156 156 Session().commit()
157 157
158 158 response = self.app.get(route_path(
159 159 'pullrequest_show',
160 160 repo_name=pull_request.target_repo.scm_instance().name,
161 161 pull_request_id=pull_request.pull_request_id))
162 162
163 163 for commit_id in pull_request.revisions:
164 164 response.mustcontain(commit_id)
165 165
166 166 def test_show_invalid_source_reference(self, pr_util):
167 167 pull_request = pr_util.create_pull_request()
168 168 pull_request.source_ref = 'branch:b:invalid'
169 169 Session().add(pull_request)
170 170 Session().commit()
171 171
172 172 self.app.get(route_path(
173 173 'pullrequest_show',
174 174 repo_name=pull_request.target_repo.scm_instance().name,
175 175 pull_request_id=pull_request.pull_request_id))
176 176
177 177 def test_edit_title_description(self, pr_util, csrf_token):
178 178 pull_request = pr_util.create_pull_request()
179 179 pull_request_id = pull_request.pull_request_id
180 180
181 181 response = self.app.post(
182 182 route_path('pullrequest_update',
183 183 repo_name=pull_request.target_repo.repo_name,
184 184 pull_request_id=pull_request_id),
185 185 params={
186 186 'edit_pull_request': 'true',
187 187 'title': 'New title',
188 188 'description': 'New description',
189 189 'csrf_token': csrf_token})
190 190
191 191 assert_session_flash(
192 192 response, u'Pull request title & description updated.',
193 193 category='success')
194 194
195 195 pull_request = PullRequest.get(pull_request_id)
196 196 assert pull_request.title == 'New title'
197 197 assert pull_request.description == 'New description'
198 198
199 199 def test_edit_title_description_closed(self, pr_util, csrf_token):
200 200 pull_request = pr_util.create_pull_request()
201 201 pull_request_id = pull_request.pull_request_id
202 202 repo_name = pull_request.target_repo.repo_name
203 203 pr_util.close()
204 204
205 205 response = self.app.post(
206 206 route_path('pullrequest_update',
207 207 repo_name=repo_name, pull_request_id=pull_request_id),
208 208 params={
209 209 'edit_pull_request': 'true',
210 210 'title': 'New title',
211 211 'description': 'New description',
212 212 'csrf_token': csrf_token}, status=200)
213 213 assert_session_flash(
214 214 response, u'Cannot update closed pull requests.',
215 215 category='error')
216 216
217 217 def test_update_invalid_source_reference(self, pr_util, csrf_token):
218 218 from rhodecode.lib.vcs.backends.base import UpdateFailureReason
219 219
220 220 pull_request = pr_util.create_pull_request()
221 221 pull_request.source_ref = 'branch:invalid-branch:invalid-commit-id'
222 222 Session().add(pull_request)
223 223 Session().commit()
224 224
225 225 pull_request_id = pull_request.pull_request_id
226 226
227 227 response = self.app.post(
228 228 route_path('pullrequest_update',
229 229 repo_name=pull_request.target_repo.repo_name,
230 230 pull_request_id=pull_request_id),
231 231 params={'update_commits': 'true',
232 232 'csrf_token': csrf_token})
233 233
234 234 expected_msg = str(PullRequestModel.UPDATE_STATUS_MESSAGES[
235 235 UpdateFailureReason.MISSING_SOURCE_REF])
236 236 assert_session_flash(response, expected_msg, category='error')
237 237
238 238 def test_missing_target_reference(self, pr_util, csrf_token):
239 239 from rhodecode.lib.vcs.backends.base import MergeFailureReason
240 240 pull_request = pr_util.create_pull_request(
241 241 approved=True, mergeable=True)
242 242 pull_request.target_ref = 'branch:invalid-branch:invalid-commit-id'
243 243 Session().add(pull_request)
244 244 Session().commit()
245 245
246 246 pull_request_id = pull_request.pull_request_id
247 247 pull_request_url = route_path(
248 248 'pullrequest_show',
249 249 repo_name=pull_request.target_repo.repo_name,
250 250 pull_request_id=pull_request_id)
251 251
252 252 response = self.app.get(pull_request_url)
253 253
254 254 assertr = AssertResponse(response)
255 255 expected_msg = PullRequestModel.MERGE_STATUS_MESSAGES[
256 256 MergeFailureReason.MISSING_TARGET_REF]
257 257 assertr.element_contains(
258 258 'span[data-role="merge-message"]', str(expected_msg))
259 259
260 260 def test_comment_and_close_pull_request_custom_message_approved(
261 261 self, pr_util, csrf_token, xhr_header):
262 262
263 263 pull_request = pr_util.create_pull_request(approved=True)
264 264 pull_request_id = pull_request.pull_request_id
265 265 author = pull_request.user_id
266 266 repo = pull_request.target_repo.repo_id
267 267
268 268 self.app.post(
269 269 route_path('pullrequest_comment_create',
270 270 repo_name=pull_request.target_repo.scm_instance().name,
271 271 pull_request_id=pull_request_id),
272 272 params={
273 273 'close_pull_request': '1',
274 274 'text': 'Closing a PR',
275 275 'csrf_token': csrf_token},
276 276 extra_environ=xhr_header,)
277 277
278 278 journal = UserLog.query()\
279 279 .filter(UserLog.user_id == author)\
280 280 .filter(UserLog.repository_id == repo) \
281 281 .order_by('user_log_id') \
282 282 .all()
283 283 assert journal[-1].action == 'repo.pull_request.close'
284 284
285 285 pull_request = PullRequest.get(pull_request_id)
286 286 assert pull_request.is_closed()
287 287
288 288 status = ChangesetStatusModel().get_status(
289 289 pull_request.source_repo, pull_request=pull_request)
290 290 assert status == ChangesetStatus.STATUS_APPROVED
291 291 comments = ChangesetComment().query() \
292 292 .filter(ChangesetComment.pull_request == pull_request) \
293 293 .order_by(ChangesetComment.comment_id.asc())\
294 294 .all()
295 295 assert comments[-1].text == 'Closing a PR'
296 296
297 297 def test_comment_force_close_pull_request_rejected(
298 298 self, pr_util, csrf_token, xhr_header):
299 299 pull_request = pr_util.create_pull_request()
300 300 pull_request_id = pull_request.pull_request_id
301 301 PullRequestModel().update_reviewers(
302 302 pull_request_id, [(1, ['reason'], False, []), (2, ['reason2'], False, [])],
303 303 pull_request.author)
304 304 author = pull_request.user_id
305 305 repo = pull_request.target_repo.repo_id
306 306
307 307 self.app.post(
308 308 route_path('pullrequest_comment_create',
309 309 repo_name=pull_request.target_repo.scm_instance().name,
310 310 pull_request_id=pull_request_id),
311 311 params={
312 312 'close_pull_request': '1',
313 313 'csrf_token': csrf_token},
314 314 extra_environ=xhr_header)
315 315
316 316 pull_request = PullRequest.get(pull_request_id)
317 317
318 318 journal = UserLog.query()\
319 319 .filter(UserLog.user_id == author, UserLog.repository_id == repo) \
320 320 .order_by('user_log_id') \
321 321 .all()
322 322 assert journal[-1].action == 'repo.pull_request.close'
323 323
324 324 # check only the latest status, not the review status
325 325 status = ChangesetStatusModel().get_status(
326 326 pull_request.source_repo, pull_request=pull_request)
327 327 assert status == ChangesetStatus.STATUS_REJECTED
328 328
329 329 def test_comment_and_close_pull_request(
330 330 self, pr_util, csrf_token, xhr_header):
331 331 pull_request = pr_util.create_pull_request()
332 332 pull_request_id = pull_request.pull_request_id
333 333
334 334 response = self.app.post(
335 335 route_path('pullrequest_comment_create',
336 336 repo_name=pull_request.target_repo.scm_instance().name,
337 337 pull_request_id=pull_request.pull_request_id),
338 338 params={
339 339 'close_pull_request': 'true',
340 340 'csrf_token': csrf_token},
341 341 extra_environ=xhr_header)
342 342
343 343 assert response.json
344 344
345 345 pull_request = PullRequest.get(pull_request_id)
346 346 assert pull_request.is_closed()
347 347
348 348 # check only the latest status, not the review status
349 349 status = ChangesetStatusModel().get_status(
350 350 pull_request.source_repo, pull_request=pull_request)
351 351 assert status == ChangesetStatus.STATUS_REJECTED
352 352
353 353 def test_create_pull_request(self, backend, csrf_token):
354 354 commits = [
355 355 {'message': 'ancestor'},
356 356 {'message': 'change'},
357 357 {'message': 'change2'},
358 358 ]
359 359 commit_ids = backend.create_master_repo(commits)
360 360 target = backend.create_repo(heads=['ancestor'])
361 361 source = backend.create_repo(heads=['change2'])
362 362
363 363 response = self.app.post(
364 364 route_path('pullrequest_create', repo_name=source.repo_name),
365 365 [
366 366 ('source_repo', source.repo_name),
367 367 ('source_ref', 'branch:default:' + commit_ids['change2']),
368 368 ('target_repo', target.repo_name),
369 369 ('target_ref', 'branch:default:' + commit_ids['ancestor']),
370 370 ('common_ancestor', commit_ids['ancestor']),
371 ('pullrequest_title', 'Title'),
371 372 ('pullrequest_desc', 'Description'),
372 ('pullrequest_title', 'Title'),
373 ('description_renderer', 'markdown'),
373 374 ('__start__', 'review_members:sequence'),
374 375 ('__start__', 'reviewer:mapping'),
375 376 ('user_id', '1'),
376 377 ('__start__', 'reasons:sequence'),
377 378 ('reason', 'Some reason'),
378 379 ('__end__', 'reasons:sequence'),
379 380 ('__start__', 'rules:sequence'),
380 381 ('__end__', 'rules:sequence'),
381 382 ('mandatory', 'False'),
382 383 ('__end__', 'reviewer:mapping'),
383 384 ('__end__', 'review_members:sequence'),
384 385 ('__start__', 'revisions:sequence'),
385 386 ('revisions', commit_ids['change']),
386 387 ('revisions', commit_ids['change2']),
387 388 ('__end__', 'revisions:sequence'),
388 389 ('user', ''),
389 390 ('csrf_token', csrf_token),
390 391 ],
391 392 status=302)
392 393
393 394 location = response.headers['Location']
394 395 pull_request_id = location.rsplit('/', 1)[1]
395 396 assert pull_request_id != 'new'
396 397 pull_request = PullRequest.get(int(pull_request_id))
397 398
398 399 # check that we have now both revisions
399 400 assert pull_request.revisions == [commit_ids['change2'], commit_ids['change']]
400 401 assert pull_request.source_ref == 'branch:default:' + commit_ids['change2']
401 402 expected_target_ref = 'branch:default:' + commit_ids['ancestor']
402 403 assert pull_request.target_ref == expected_target_ref
403 404
404 405 def test_reviewer_notifications(self, backend, csrf_token):
405 406 # We have to use the app.post for this test so it will create the
406 407 # notifications properly with the new PR
407 408 commits = [
408 409 {'message': 'ancestor',
409 410 'added': [FileNode('file_A', content='content_of_ancestor')]},
410 411 {'message': 'change',
411 412 'added': [FileNode('file_a', content='content_of_change')]},
412 413 {'message': 'change-child'},
413 414 {'message': 'ancestor-child', 'parents': ['ancestor'],
414 415 'added': [
415 416 FileNode('file_B', content='content_of_ancestor_child')]},
416 417 {'message': 'ancestor-child-2'},
417 418 ]
418 419 commit_ids = backend.create_master_repo(commits)
419 420 target = backend.create_repo(heads=['ancestor-child'])
420 421 source = backend.create_repo(heads=['change'])
421 422
422 423 response = self.app.post(
423 424 route_path('pullrequest_create', repo_name=source.repo_name),
424 425 [
425 426 ('source_repo', source.repo_name),
426 427 ('source_ref', 'branch:default:' + commit_ids['change']),
427 428 ('target_repo', target.repo_name),
428 429 ('target_ref', 'branch:default:' + commit_ids['ancestor-child']),
429 430 ('common_ancestor', commit_ids['ancestor']),
431 ('pullrequest_title', 'Title'),
430 432 ('pullrequest_desc', 'Description'),
431 ('pullrequest_title', 'Title'),
433 ('description_renderer', 'markdown'),
432 434 ('__start__', 'review_members:sequence'),
433 435 ('__start__', 'reviewer:mapping'),
434 436 ('user_id', '2'),
435 437 ('__start__', 'reasons:sequence'),
436 438 ('reason', 'Some reason'),
437 439 ('__end__', 'reasons:sequence'),
438 440 ('__start__', 'rules:sequence'),
439 441 ('__end__', 'rules:sequence'),
440 442 ('mandatory', 'False'),
441 443 ('__end__', 'reviewer:mapping'),
442 444 ('__end__', 'review_members:sequence'),
443 445 ('__start__', 'revisions:sequence'),
444 446 ('revisions', commit_ids['change']),
445 447 ('__end__', 'revisions:sequence'),
446 448 ('user', ''),
447 449 ('csrf_token', csrf_token),
448 450 ],
449 451 status=302)
450 452
451 453 location = response.headers['Location']
452 454
453 455 pull_request_id = location.rsplit('/', 1)[1]
454 456 assert pull_request_id != 'new'
455 457 pull_request = PullRequest.get(int(pull_request_id))
456 458
457 459 # Check that a notification was made
458 460 notifications = Notification.query()\
459 461 .filter(Notification.created_by == pull_request.author.user_id,
460 462 Notification.type_ == Notification.TYPE_PULL_REQUEST,
461 463 Notification.subject.contains(
462 464 "wants you to review pull request #%s" % pull_request_id))
463 465 assert len(notifications.all()) == 1
464 466
465 467 # Change reviewers and check that a notification was made
466 468 PullRequestModel().update_reviewers(
467 469 pull_request.pull_request_id, [(1, [], False, [])],
468 470 pull_request.author)
469 471 assert len(notifications.all()) == 2
470 472
471 473 def test_create_pull_request_stores_ancestor_commit_id(self, backend,
472 474 csrf_token):
473 475 commits = [
474 476 {'message': 'ancestor',
475 477 'added': [FileNode('file_A', content='content_of_ancestor')]},
476 478 {'message': 'change',
477 479 'added': [FileNode('file_a', content='content_of_change')]},
478 480 {'message': 'change-child'},
479 481 {'message': 'ancestor-child', 'parents': ['ancestor'],
480 482 'added': [
481 483 FileNode('file_B', content='content_of_ancestor_child')]},
482 484 {'message': 'ancestor-child-2'},
483 485 ]
484 486 commit_ids = backend.create_master_repo(commits)
485 487 target = backend.create_repo(heads=['ancestor-child'])
486 488 source = backend.create_repo(heads=['change'])
487 489
488 490 response = self.app.post(
489 491 route_path('pullrequest_create', repo_name=source.repo_name),
490 492 [
491 493 ('source_repo', source.repo_name),
492 494 ('source_ref', 'branch:default:' + commit_ids['change']),
493 495 ('target_repo', target.repo_name),
494 496 ('target_ref', 'branch:default:' + commit_ids['ancestor-child']),
495 497 ('common_ancestor', commit_ids['ancestor']),
498 ('pullrequest_title', 'Title'),
496 499 ('pullrequest_desc', 'Description'),
497 ('pullrequest_title', 'Title'),
500 ('description_renderer', 'markdown'),
498 501 ('__start__', 'review_members:sequence'),
499 502 ('__start__', 'reviewer:mapping'),
500 503 ('user_id', '1'),
501 504 ('__start__', 'reasons:sequence'),
502 505 ('reason', 'Some reason'),
503 506 ('__end__', 'reasons:sequence'),
504 507 ('__start__', 'rules:sequence'),
505 508 ('__end__', 'rules:sequence'),
506 509 ('mandatory', 'False'),
507 510 ('__end__', 'reviewer:mapping'),
508 511 ('__end__', 'review_members:sequence'),
509 512 ('__start__', 'revisions:sequence'),
510 513 ('revisions', commit_ids['change']),
511 514 ('__end__', 'revisions:sequence'),
512 515 ('user', ''),
513 516 ('csrf_token', csrf_token),
514 517 ],
515 518 status=302)
516 519
517 520 location = response.headers['Location']
518 521
519 522 pull_request_id = location.rsplit('/', 1)[1]
520 523 assert pull_request_id != 'new'
521 524 pull_request = PullRequest.get(int(pull_request_id))
522 525
523 526 # target_ref has to point to the ancestor's commit_id in order to
524 527 # show the correct diff
525 528 expected_target_ref = 'branch:default:' + commit_ids['ancestor']
526 529 assert pull_request.target_ref == expected_target_ref
527 530
528 531 # Check generated diff contents
529 532 response = response.follow()
530 533 assert 'content_of_ancestor' not in response.body
531 534 assert 'content_of_ancestor-child' not in response.body
532 535 assert 'content_of_change' in response.body
533 536
534 537 def test_merge_pull_request_enabled(self, pr_util, csrf_token):
535 538 # Clear any previous calls to rcextensions
536 539 rhodecode.EXTENSIONS.calls.clear()
537 540
538 541 pull_request = pr_util.create_pull_request(
539 542 approved=True, mergeable=True)
540 543 pull_request_id = pull_request.pull_request_id
541 544 repo_name = pull_request.target_repo.scm_instance().name,
542 545
543 546 response = self.app.post(
544 547 route_path('pullrequest_merge',
545 548 repo_name=str(repo_name[0]),
546 549 pull_request_id=pull_request_id),
547 550 params={'csrf_token': csrf_token}).follow()
548 551
549 552 pull_request = PullRequest.get(pull_request_id)
550 553
551 554 assert response.status_int == 200
552 555 assert pull_request.is_closed()
553 556 assert_pull_request_status(
554 557 pull_request, ChangesetStatus.STATUS_APPROVED)
555 558
556 559 # Check the relevant log entries were added
557 560 user_logs = UserLog.query().order_by('-user_log_id').limit(3)
558 561 actions = [log.action for log in user_logs]
559 562 pr_commit_ids = PullRequestModel()._get_commit_ids(pull_request)
560 563 expected_actions = [
561 564 u'repo.pull_request.close',
562 565 u'repo.pull_request.merge',
563 566 u'repo.pull_request.comment.create'
564 567 ]
565 568 assert actions == expected_actions
566 569
567 570 user_logs = UserLog.query().order_by('-user_log_id').limit(4)
568 571 actions = [log for log in user_logs]
569 572 assert actions[-1].action == 'user.push'
570 573 assert actions[-1].action_data['commit_ids'] == pr_commit_ids
571 574
572 575 # Check post_push rcextension was really executed
573 576 push_calls = rhodecode.EXTENSIONS.calls['post_push']
574 577 assert len(push_calls) == 1
575 578 unused_last_call_args, last_call_kwargs = push_calls[0]
576 579 assert last_call_kwargs['action'] == 'push'
577 580 assert last_call_kwargs['pushed_revs'] == pr_commit_ids
578 581
579 582 def test_merge_pull_request_disabled(self, pr_util, csrf_token):
580 583 pull_request = pr_util.create_pull_request(mergeable=False)
581 584 pull_request_id = pull_request.pull_request_id
582 585 pull_request = PullRequest.get(pull_request_id)
583 586
584 587 response = self.app.post(
585 588 route_path('pullrequest_merge',
586 589 repo_name=pull_request.target_repo.scm_instance().name,
587 590 pull_request_id=pull_request.pull_request_id),
588 591 params={'csrf_token': csrf_token}).follow()
589 592
590 593 assert response.status_int == 200
591 594 response.mustcontain(
592 595 'Merge is not currently possible because of below failed checks.')
593 596 response.mustcontain('Server-side pull request merging is disabled.')
594 597
595 598 @pytest.mark.skip_backends('svn')
596 599 def test_merge_pull_request_not_approved(self, pr_util, csrf_token):
597 600 pull_request = pr_util.create_pull_request(mergeable=True)
598 601 pull_request_id = pull_request.pull_request_id
599 602 repo_name = pull_request.target_repo.scm_instance().name
600 603
601 604 response = self.app.post(
602 605 route_path('pullrequest_merge',
603 606 repo_name=repo_name,
604 607 pull_request_id=pull_request_id),
605 608 params={'csrf_token': csrf_token}).follow()
606 609
607 610 assert response.status_int == 200
608 611
609 612 response.mustcontain(
610 613 'Merge is not currently possible because of below failed checks.')
611 614 response.mustcontain('Pull request reviewer approval is pending.')
612 615
613 616 def test_merge_pull_request_renders_failure_reason(
614 617 self, user_regular, csrf_token, pr_util):
615 618 pull_request = pr_util.create_pull_request(mergeable=True, approved=True)
616 619 pull_request_id = pull_request.pull_request_id
617 620 repo_name = pull_request.target_repo.scm_instance().name
618 621
619 622 model_patcher = mock.patch.multiple(
620 623 PullRequestModel,
621 624 merge_repo=mock.Mock(return_value=MergeResponse(
622 625 True, False, 'STUB_COMMIT_ID', MergeFailureReason.PUSH_FAILED)),
623 626 merge_status=mock.Mock(return_value=(True, 'WRONG_MESSAGE')))
624 627
625 628 with model_patcher:
626 629 response = self.app.post(
627 630 route_path('pullrequest_merge',
628 631 repo_name=repo_name,
629 632 pull_request_id=pull_request_id),
630 633 params={'csrf_token': csrf_token}, status=302)
631 634
632 635 assert_session_flash(response, PullRequestModel.MERGE_STATUS_MESSAGES[
633 636 MergeFailureReason.PUSH_FAILED])
634 637
635 638 def test_update_source_revision(self, backend, csrf_token):
636 639 commits = [
637 640 {'message': 'ancestor'},
638 641 {'message': 'change'},
639 642 {'message': 'change-2'},
640 643 ]
641 644 commit_ids = backend.create_master_repo(commits)
642 645 target = backend.create_repo(heads=['ancestor'])
643 646 source = backend.create_repo(heads=['change'])
644 647
645 648 # create pr from a in source to A in target
646 649 pull_request = PullRequest()
647 650 pull_request.source_repo = source
648 651 # TODO: johbo: Make sure that we write the source ref this way!
649 652 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
650 653 branch=backend.default_branch_name, commit_id=commit_ids['change'])
651 654 pull_request.target_repo = target
652 655
653 656 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
654 657 branch=backend.default_branch_name,
655 658 commit_id=commit_ids['ancestor'])
656 659 pull_request.revisions = [commit_ids['change']]
657 660 pull_request.title = u"Test"
658 661 pull_request.description = u"Description"
659 662 pull_request.author = UserModel().get_by_username(
660 663 TEST_USER_ADMIN_LOGIN)
661 664 Session().add(pull_request)
662 665 Session().commit()
663 666 pull_request_id = pull_request.pull_request_id
664 667
665 668 # source has ancestor - change - change-2
666 669 backend.pull_heads(source, heads=['change-2'])
667 670
668 671 # update PR
669 672 self.app.post(
670 673 route_path('pullrequest_update',
671 674 repo_name=target.repo_name,
672 675 pull_request_id=pull_request_id),
673 676 params={'update_commits': 'true',
674 677 'csrf_token': csrf_token})
675 678
676 679 # check that we have now both revisions
677 680 pull_request = PullRequest.get(pull_request_id)
678 681 assert pull_request.revisions == [
679 682 commit_ids['change-2'], commit_ids['change']]
680 683
681 684 # TODO: johbo: this should be a test on its own
682 685 response = self.app.get(route_path(
683 686 'pullrequest_new',
684 687 repo_name=target.repo_name))
685 688 assert response.status_int == 200
686 689 assert 'Pull request updated to' in response.body
687 690 assert 'with 1 added, 0 removed commits.' in response.body
688 691
689 692 def test_update_target_revision(self, backend, csrf_token):
690 693 commits = [
691 694 {'message': 'ancestor'},
692 695 {'message': 'change'},
693 696 {'message': 'ancestor-new', 'parents': ['ancestor']},
694 697 {'message': 'change-rebased'},
695 698 ]
696 699 commit_ids = backend.create_master_repo(commits)
697 700 target = backend.create_repo(heads=['ancestor'])
698 701 source = backend.create_repo(heads=['change'])
699 702
700 703 # create pr from a in source to A in target
701 704 pull_request = PullRequest()
702 705 pull_request.source_repo = source
703 706 # TODO: johbo: Make sure that we write the source ref this way!
704 707 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
705 708 branch=backend.default_branch_name, commit_id=commit_ids['change'])
706 709 pull_request.target_repo = target
707 710 # TODO: johbo: Target ref should be branch based, since tip can jump
708 711 # from branch to branch
709 712 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
710 713 branch=backend.default_branch_name,
711 714 commit_id=commit_ids['ancestor'])
712 715 pull_request.revisions = [commit_ids['change']]
713 716 pull_request.title = u"Test"
714 717 pull_request.description = u"Description"
715 718 pull_request.author = UserModel().get_by_username(
716 719 TEST_USER_ADMIN_LOGIN)
717 720 Session().add(pull_request)
718 721 Session().commit()
719 722 pull_request_id = pull_request.pull_request_id
720 723
721 724 # target has ancestor - ancestor-new
722 725 # source has ancestor - ancestor-new - change-rebased
723 726 backend.pull_heads(target, heads=['ancestor-new'])
724 727 backend.pull_heads(source, heads=['change-rebased'])
725 728
726 729 # update PR
727 730 self.app.post(
728 731 route_path('pullrequest_update',
729 732 repo_name=target.repo_name,
730 733 pull_request_id=pull_request_id),
731 734 params={'update_commits': 'true',
732 735 'csrf_token': csrf_token},
733 736 status=200)
734 737
735 738 # check that we have now both revisions
736 739 pull_request = PullRequest.get(pull_request_id)
737 740 assert pull_request.revisions == [commit_ids['change-rebased']]
738 741 assert pull_request.target_ref == 'branch:{branch}:{commit_id}'.format(
739 742 branch=backend.default_branch_name,
740 743 commit_id=commit_ids['ancestor-new'])
741 744
742 745 # TODO: johbo: This should be a test on its own
743 746 response = self.app.get(route_path(
744 747 'pullrequest_new',
745 748 repo_name=target.repo_name))
746 749 assert response.status_int == 200
747 750 assert 'Pull request updated to' in response.body
748 751 assert 'with 1 added, 1 removed commits.' in response.body
749 752
750 753 def test_update_target_revision_with_removal_of_1_commit_git(self, backend_git, csrf_token):
751 754 backend = backend_git
752 755 commits = [
753 756 {'message': 'master-commit-1'},
754 757 {'message': 'master-commit-2-change-1'},
755 758 {'message': 'master-commit-3-change-2'},
756 759
757 760 {'message': 'feat-commit-1', 'parents': ['master-commit-1']},
758 761 {'message': 'feat-commit-2'},
759 762 ]
760 763 commit_ids = backend.create_master_repo(commits)
761 764 target = backend.create_repo(heads=['master-commit-3-change-2'])
762 765 source = backend.create_repo(heads=['feat-commit-2'])
763 766
764 767 # create pr from a in source to A in target
765 768 pull_request = PullRequest()
766 769 pull_request.source_repo = source
767 770 # TODO: johbo: Make sure that we write the source ref this way!
768 771 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
769 772 branch=backend.default_branch_name,
770 773 commit_id=commit_ids['master-commit-3-change-2'])
771 774
772 775 pull_request.target_repo = target
773 776 # TODO: johbo: Target ref should be branch based, since tip can jump
774 777 # from branch to branch
775 778 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
776 779 branch=backend.default_branch_name,
777 780 commit_id=commit_ids['feat-commit-2'])
778 781
779 782 pull_request.revisions = [
780 783 commit_ids['feat-commit-1'],
781 784 commit_ids['feat-commit-2']
782 785 ]
783 786 pull_request.title = u"Test"
784 787 pull_request.description = u"Description"
785 788 pull_request.author = UserModel().get_by_username(
786 789 TEST_USER_ADMIN_LOGIN)
787 790 Session().add(pull_request)
788 791 Session().commit()
789 792 pull_request_id = pull_request.pull_request_id
790 793
791 794 # PR is created, now we simulate a force-push into target,
792 795 # that drops a 2 last commits
793 796 vcsrepo = target.scm_instance()
794 797 vcsrepo.config.clear_section('hooks')
795 798 vcsrepo.run_git_command(['reset', '--soft', 'HEAD~2'])
796 799
797 800 # update PR
798 801 self.app.post(
799 802 route_path('pullrequest_update',
800 803 repo_name=target.repo_name,
801 804 pull_request_id=pull_request_id),
802 805 params={'update_commits': 'true',
803 806 'csrf_token': csrf_token},
804 807 status=200)
805 808
806 809 response = self.app.get(route_path(
807 810 'pullrequest_new',
808 811 repo_name=target.repo_name))
809 812 assert response.status_int == 200
810 813 response.mustcontain('Pull request updated to')
811 814 response.mustcontain('with 0 added, 0 removed commits.')
812 815
813 816 def test_update_of_ancestor_reference(self, backend, csrf_token):
814 817 commits = [
815 818 {'message': 'ancestor'},
816 819 {'message': 'change'},
817 820 {'message': 'change-2'},
818 821 {'message': 'ancestor-new', 'parents': ['ancestor']},
819 822 {'message': 'change-rebased'},
820 823 ]
821 824 commit_ids = backend.create_master_repo(commits)
822 825 target = backend.create_repo(heads=['ancestor'])
823 826 source = backend.create_repo(heads=['change'])
824 827
825 828 # create pr from a in source to A in target
826 829 pull_request = PullRequest()
827 830 pull_request.source_repo = source
828 831 # TODO: johbo: Make sure that we write the source ref this way!
829 832 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
830 833 branch=backend.default_branch_name,
831 834 commit_id=commit_ids['change'])
832 835 pull_request.target_repo = target
833 836 # TODO: johbo: Target ref should be branch based, since tip can jump
834 837 # from branch to branch
835 838 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
836 839 branch=backend.default_branch_name,
837 840 commit_id=commit_ids['ancestor'])
838 841 pull_request.revisions = [commit_ids['change']]
839 842 pull_request.title = u"Test"
840 843 pull_request.description = u"Description"
841 844 pull_request.author = UserModel().get_by_username(
842 845 TEST_USER_ADMIN_LOGIN)
843 846 Session().add(pull_request)
844 847 Session().commit()
845 848 pull_request_id = pull_request.pull_request_id
846 849
847 850 # target has ancestor - ancestor-new
848 851 # source has ancestor - ancestor-new - change-rebased
849 852 backend.pull_heads(target, heads=['ancestor-new'])
850 853 backend.pull_heads(source, heads=['change-rebased'])
851 854
852 855 # update PR
853 856 self.app.post(
854 857 route_path('pullrequest_update',
855 858 repo_name=target.repo_name,
856 859 pull_request_id=pull_request_id),
857 860 params={'update_commits': 'true',
858 861 'csrf_token': csrf_token},
859 862 status=200)
860 863
861 864 # Expect the target reference to be updated correctly
862 865 pull_request = PullRequest.get(pull_request_id)
863 866 assert pull_request.revisions == [commit_ids['change-rebased']]
864 867 expected_target_ref = 'branch:{branch}:{commit_id}'.format(
865 868 branch=backend.default_branch_name,
866 869 commit_id=commit_ids['ancestor-new'])
867 870 assert pull_request.target_ref == expected_target_ref
868 871
869 872 def test_remove_pull_request_branch(self, backend_git, csrf_token):
870 873 branch_name = 'development'
871 874 commits = [
872 875 {'message': 'initial-commit'},
873 876 {'message': 'old-feature'},
874 877 {'message': 'new-feature', 'branch': branch_name},
875 878 ]
876 879 repo = backend_git.create_repo(commits)
877 880 commit_ids = backend_git.commit_ids
878 881
879 882 pull_request = PullRequest()
880 883 pull_request.source_repo = repo
881 884 pull_request.target_repo = repo
882 885 pull_request.source_ref = 'branch:{branch}:{commit_id}'.format(
883 886 branch=branch_name, commit_id=commit_ids['new-feature'])
884 887 pull_request.target_ref = 'branch:{branch}:{commit_id}'.format(
885 888 branch=backend_git.default_branch_name,
886 889 commit_id=commit_ids['old-feature'])
887 890 pull_request.revisions = [commit_ids['new-feature']]
888 891 pull_request.title = u"Test"
889 892 pull_request.description = u"Description"
890 893 pull_request.author = UserModel().get_by_username(
891 894 TEST_USER_ADMIN_LOGIN)
892 895 Session().add(pull_request)
893 896 Session().commit()
894 897
895 898 vcs = repo.scm_instance()
896 899 vcs.remove_ref('refs/heads/{}'.format(branch_name))
897 900
898 901 response = self.app.get(route_path(
899 902 'pullrequest_show',
900 903 repo_name=repo.repo_name,
901 904 pull_request_id=pull_request.pull_request_id))
902 905
903 906 assert response.status_int == 200
904 907 assert_response = AssertResponse(response)
905 908 assert_response.element_contains(
906 909 '#changeset_compare_view_content .alert strong',
907 910 'Missing commits')
908 911 assert_response.element_contains(
909 912 '#changeset_compare_view_content .alert',
910 913 'This pull request cannot be displayed, because one or more'
911 914 ' commits no longer exist in the source repository.')
912 915
913 916 def test_strip_commits_from_pull_request(
914 917 self, backend, pr_util, csrf_token):
915 918 commits = [
916 919 {'message': 'initial-commit'},
917 920 {'message': 'old-feature'},
918 921 {'message': 'new-feature', 'parents': ['initial-commit']},
919 922 ]
920 923 pull_request = pr_util.create_pull_request(
921 924 commits, target_head='initial-commit', source_head='new-feature',
922 925 revisions=['new-feature'])
923 926
924 927 vcs = pr_util.source_repository.scm_instance()
925 928 if backend.alias == 'git':
926 929 vcs.strip(pr_util.commit_ids['new-feature'], branch_name='master')
927 930 else:
928 931 vcs.strip(pr_util.commit_ids['new-feature'])
929 932
930 933 response = self.app.get(route_path(
931 934 'pullrequest_show',
932 935 repo_name=pr_util.target_repository.repo_name,
933 936 pull_request_id=pull_request.pull_request_id))
934 937
935 938 assert response.status_int == 200
936 939 assert_response = AssertResponse(response)
937 940 assert_response.element_contains(
938 941 '#changeset_compare_view_content .alert strong',
939 942 'Missing commits')
940 943 assert_response.element_contains(
941 944 '#changeset_compare_view_content .alert',
942 945 'This pull request cannot be displayed, because one or more'
943 946 ' commits no longer exist in the source repository.')
944 947 assert_response.element_contains(
945 948 '#update_commits',
946 949 'Update commits')
947 950
948 951 def test_strip_commits_and_update(
949 952 self, backend, pr_util, csrf_token):
950 953 commits = [
951 954 {'message': 'initial-commit'},
952 955 {'message': 'old-feature'},
953 956 {'message': 'new-feature', 'parents': ['old-feature']},
954 957 ]
955 958 pull_request = pr_util.create_pull_request(
956 959 commits, target_head='old-feature', source_head='new-feature',
957 960 revisions=['new-feature'], mergeable=True)
958 961
959 962 vcs = pr_util.source_repository.scm_instance()
960 963 if backend.alias == 'git':
961 964 vcs.strip(pr_util.commit_ids['new-feature'], branch_name='master')
962 965 else:
963 966 vcs.strip(pr_util.commit_ids['new-feature'])
964 967
965 968 response = self.app.post(
966 969 route_path('pullrequest_update',
967 970 repo_name=pull_request.target_repo.repo_name,
968 971 pull_request_id=pull_request.pull_request_id),
969 972 params={'update_commits': 'true',
970 973 'csrf_token': csrf_token})
971 974
972 975 assert response.status_int == 200
973 976 assert response.body == 'true'
974 977
975 978 # Make sure that after update, it won't raise 500 errors
976 979 response = self.app.get(route_path(
977 980 'pullrequest_show',
978 981 repo_name=pr_util.target_repository.repo_name,
979 982 pull_request_id=pull_request.pull_request_id))
980 983
981 984 assert response.status_int == 200
982 985 assert_response = AssertResponse(response)
983 986 assert_response.element_contains(
984 987 '#changeset_compare_view_content .alert strong',
985 988 'Missing commits')
986 989
987 990 def test_branch_is_a_link(self, pr_util):
988 991 pull_request = pr_util.create_pull_request()
989 992 pull_request.source_ref = 'branch:origin:1234567890abcdef'
990 993 pull_request.target_ref = 'branch:target:abcdef1234567890'
991 994 Session().add(pull_request)
992 995 Session().commit()
993 996
994 997 response = self.app.get(route_path(
995 998 'pullrequest_show',
996 999 repo_name=pull_request.target_repo.scm_instance().name,
997 1000 pull_request_id=pull_request.pull_request_id))
998 1001 assert response.status_int == 200
999 1002 assert_response = AssertResponse(response)
1000 1003
1001 1004 origin = assert_response.get_element('.pr-origininfo .tag')
1002 1005 origin_children = origin.getchildren()
1003 1006 assert len(origin_children) == 1
1004 1007 target = assert_response.get_element('.pr-targetinfo .tag')
1005 1008 target_children = target.getchildren()
1006 1009 assert len(target_children) == 1
1007 1010
1008 1011 expected_origin_link = route_path(
1009 1012 'repo_changelog',
1010 1013 repo_name=pull_request.source_repo.scm_instance().name,
1011 1014 params=dict(branch='origin'))
1012 1015 expected_target_link = route_path(
1013 1016 'repo_changelog',
1014 1017 repo_name=pull_request.target_repo.scm_instance().name,
1015 1018 params=dict(branch='target'))
1016 1019 assert origin_children[0].attrib['href'] == expected_origin_link
1017 1020 assert origin_children[0].text == 'branch: origin'
1018 1021 assert target_children[0].attrib['href'] == expected_target_link
1019 1022 assert target_children[0].text == 'branch: target'
1020 1023
1021 1024 def test_bookmark_is_not_a_link(self, pr_util):
1022 1025 pull_request = pr_util.create_pull_request()
1023 1026 pull_request.source_ref = 'bookmark:origin:1234567890abcdef'
1024 1027 pull_request.target_ref = 'bookmark:target:abcdef1234567890'
1025 1028 Session().add(pull_request)
1026 1029 Session().commit()
1027 1030
1028 1031 response = self.app.get(route_path(
1029 1032 'pullrequest_show',
1030 1033 repo_name=pull_request.target_repo.scm_instance().name,
1031 1034 pull_request_id=pull_request.pull_request_id))
1032 1035 assert response.status_int == 200
1033 1036 assert_response = AssertResponse(response)
1034 1037
1035 1038 origin = assert_response.get_element('.pr-origininfo .tag')
1036 1039 assert origin.text.strip() == 'bookmark: origin'
1037 1040 assert origin.getchildren() == []
1038 1041
1039 1042 target = assert_response.get_element('.pr-targetinfo .tag')
1040 1043 assert target.text.strip() == 'bookmark: target'
1041 1044 assert target.getchildren() == []
1042 1045
1043 1046 def test_tag_is_not_a_link(self, pr_util):
1044 1047 pull_request = pr_util.create_pull_request()
1045 1048 pull_request.source_ref = 'tag:origin:1234567890abcdef'
1046 1049 pull_request.target_ref = 'tag:target:abcdef1234567890'
1047 1050 Session().add(pull_request)
1048 1051 Session().commit()
1049 1052
1050 1053 response = self.app.get(route_path(
1051 1054 'pullrequest_show',
1052 1055 repo_name=pull_request.target_repo.scm_instance().name,
1053 1056 pull_request_id=pull_request.pull_request_id))
1054 1057 assert response.status_int == 200
1055 1058 assert_response = AssertResponse(response)
1056 1059
1057 1060 origin = assert_response.get_element('.pr-origininfo .tag')
1058 1061 assert origin.text.strip() == 'tag: origin'
1059 1062 assert origin.getchildren() == []
1060 1063
1061 1064 target = assert_response.get_element('.pr-targetinfo .tag')
1062 1065 assert target.text.strip() == 'tag: target'
1063 1066 assert target.getchildren() == []
1064 1067
1065 1068 @pytest.mark.parametrize('mergeable', [True, False])
1066 1069 def test_shadow_repository_link(
1067 1070 self, mergeable, pr_util, http_host_only_stub):
1068 1071 """
1069 1072 Check that the pull request summary page displays a link to the shadow
1070 1073 repository if the pull request is mergeable. If it is not mergeable
1071 1074 the link should not be displayed.
1072 1075 """
1073 1076 pull_request = pr_util.create_pull_request(
1074 1077 mergeable=mergeable, enable_notifications=False)
1075 1078 target_repo = pull_request.target_repo.scm_instance()
1076 1079 pr_id = pull_request.pull_request_id
1077 1080 shadow_url = '{host}/{repo}/pull-request/{pr_id}/repository'.format(
1078 1081 host=http_host_only_stub, repo=target_repo.name, pr_id=pr_id)
1079 1082
1080 1083 response = self.app.get(route_path(
1081 1084 'pullrequest_show',
1082 1085 repo_name=target_repo.name,
1083 1086 pull_request_id=pr_id))
1084 1087
1085 1088 assertr = AssertResponse(response)
1086 1089 if mergeable:
1087 1090 assertr.element_value_contains('input.pr-mergeinfo', shadow_url)
1088 1091 assertr.element_value_contains('input.pr-mergeinfo ', 'pr-merge')
1089 1092 else:
1090 1093 assertr.no_element_exists('.pr-mergeinfo')
1091 1094
1092 1095
1093 1096 @pytest.mark.usefixtures('app')
1094 1097 @pytest.mark.backends("git", "hg")
1095 1098 class TestPullrequestsControllerDelete(object):
1096 1099 def test_pull_request_delete_button_permissions_admin(
1097 1100 self, autologin_user, user_admin, pr_util):
1098 1101 pull_request = pr_util.create_pull_request(
1099 1102 author=user_admin.username, enable_notifications=False)
1100 1103
1101 1104 response = self.app.get(route_path(
1102 1105 'pullrequest_show',
1103 1106 repo_name=pull_request.target_repo.scm_instance().name,
1104 1107 pull_request_id=pull_request.pull_request_id))
1105 1108
1106 1109 response.mustcontain('id="delete_pullrequest"')
1107 1110 response.mustcontain('Confirm to delete this pull request')
1108 1111
1109 1112 def test_pull_request_delete_button_permissions_owner(
1110 1113 self, autologin_regular_user, user_regular, pr_util):
1111 1114 pull_request = pr_util.create_pull_request(
1112 1115 author=user_regular.username, enable_notifications=False)
1113 1116
1114 1117 response = self.app.get(route_path(
1115 1118 'pullrequest_show',
1116 1119 repo_name=pull_request.target_repo.scm_instance().name,
1117 1120 pull_request_id=pull_request.pull_request_id))
1118 1121
1119 1122 response.mustcontain('id="delete_pullrequest"')
1120 1123 response.mustcontain('Confirm to delete this pull request')
1121 1124
1122 1125 def test_pull_request_delete_button_permissions_forbidden(
1123 1126 self, autologin_regular_user, user_regular, user_admin, pr_util):
1124 1127 pull_request = pr_util.create_pull_request(
1125 1128 author=user_admin.username, enable_notifications=False)
1126 1129
1127 1130 response = self.app.get(route_path(
1128 1131 'pullrequest_show',
1129 1132 repo_name=pull_request.target_repo.scm_instance().name,
1130 1133 pull_request_id=pull_request.pull_request_id))
1131 1134 response.mustcontain(no=['id="delete_pullrequest"'])
1132 1135 response.mustcontain(no=['Confirm to delete this pull request'])
1133 1136
1134 1137 def test_pull_request_delete_button_permissions_can_update_cannot_delete(
1135 1138 self, autologin_regular_user, user_regular, user_admin, pr_util,
1136 1139 user_util):
1137 1140
1138 1141 pull_request = pr_util.create_pull_request(
1139 1142 author=user_admin.username, enable_notifications=False)
1140 1143
1141 1144 user_util.grant_user_permission_to_repo(
1142 1145 pull_request.target_repo, user_regular,
1143 1146 'repository.write')
1144 1147
1145 1148 response = self.app.get(route_path(
1146 1149 'pullrequest_show',
1147 1150 repo_name=pull_request.target_repo.scm_instance().name,
1148 1151 pull_request_id=pull_request.pull_request_id))
1149 1152
1150 1153 response.mustcontain('id="open_edit_pullrequest"')
1151 1154 response.mustcontain('id="delete_pullrequest"')
1152 1155 response.mustcontain(no=['Confirm to delete this pull request'])
1153 1156
1154 1157 def test_delete_comment_returns_404_if_comment_does_not_exist(
1155 1158 self, autologin_user, pr_util, user_admin, csrf_token, xhr_header):
1156 1159
1157 1160 pull_request = pr_util.create_pull_request(
1158 1161 author=user_admin.username, enable_notifications=False)
1159 1162
1160 1163 self.app.post(
1161 1164 route_path(
1162 1165 'pullrequest_comment_delete',
1163 1166 repo_name=pull_request.target_repo.scm_instance().name,
1164 1167 pull_request_id=pull_request.pull_request_id,
1165 1168 comment_id=1024404),
1166 1169 extra_environ=xhr_header,
1167 1170 params={'csrf_token': csrf_token},
1168 1171 status=404
1169 1172 )
1170 1173
1171 1174 def test_delete_comment(
1172 1175 self, autologin_user, pr_util, user_admin, csrf_token, xhr_header):
1173 1176
1174 1177 pull_request = pr_util.create_pull_request(
1175 1178 author=user_admin.username, enable_notifications=False)
1176 1179 comment = pr_util.create_comment()
1177 1180 comment_id = comment.comment_id
1178 1181
1179 1182 response = self.app.post(
1180 1183 route_path(
1181 1184 'pullrequest_comment_delete',
1182 1185 repo_name=pull_request.target_repo.scm_instance().name,
1183 1186 pull_request_id=pull_request.pull_request_id,
1184 1187 comment_id=comment_id),
1185 1188 extra_environ=xhr_header,
1186 1189 params={'csrf_token': csrf_token},
1187 1190 status=200
1188 1191 )
1189 1192 assert response.body == 'true'
1190 1193
1191 1194
1192 1195 def assert_pull_request_status(pull_request, expected_status):
1193 1196 status = ChangesetStatusModel().calculated_review_status(
1194 1197 pull_request=pull_request)
1195 1198 assert status == expected_status
1196 1199
1197 1200
1198 1201 @pytest.mark.parametrize('route', ['pullrequest_new', 'pullrequest_create'])
1199 1202 @pytest.mark.usefixtures("autologin_user")
1200 1203 def test_forbidde_to_repo_summary_for_svn_repositories(backend_svn, app, route):
1201 1204 response = app.get(
1202 1205 route_path(route, repo_name=backend_svn.repo_name), status=404)
1203 1206
@@ -1,1316 +1,1324 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import logging
22 22 import collections
23 23
24 24 import formencode
25 25 import formencode.htmlfill
26 26 import peppercorn
27 27 from pyramid.httpexceptions import (
28 28 HTTPFound, HTTPNotFound, HTTPForbidden, HTTPBadRequest)
29 29 from pyramid.view import view_config
30 30 from pyramid.renderers import render
31 31
32 32 from rhodecode import events
33 33 from rhodecode.apps._base import RepoAppView, DataGridAppView
34 34
35 35 from rhodecode.lib import helpers as h, diffs, codeblocks, channelstream
36 36 from rhodecode.lib.base import vcs_operation_context
37 37 from rhodecode.lib.diffs import load_cached_diff, cache_diff, diff_cache_exist
38 38 from rhodecode.lib.ext_json import json
39 39 from rhodecode.lib.auth import (
40 40 LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator,
41 41 NotAnonymous, CSRFRequired)
42 42 from rhodecode.lib.utils2 import str2bool, safe_str, safe_unicode
43 43 from rhodecode.lib.vcs.backends.base import EmptyCommit, UpdateFailureReason
44 44 from rhodecode.lib.vcs.exceptions import (CommitDoesNotExistError,
45 45 RepositoryRequirementError, EmptyRepositoryError)
46 46 from rhodecode.model.changeset_status import ChangesetStatusModel
47 47 from rhodecode.model.comment import CommentsModel
48 48 from rhodecode.model.db import (func, or_, PullRequest, PullRequestVersion,
49 49 ChangesetComment, ChangesetStatus, Repository)
50 50 from rhodecode.model.forms import PullRequestForm
51 51 from rhodecode.model.meta import Session
52 52 from rhodecode.model.pull_request import PullRequestModel, MergeCheck
53 53 from rhodecode.model.scm import ScmModel
54 54
55 55 log = logging.getLogger(__name__)
56 56
57 57
58 58 class RepoPullRequestsView(RepoAppView, DataGridAppView):
59 59
60 60 def load_default_context(self):
61 61 c = self._get_local_tmpl_context(include_app_defaults=True)
62 62 c.REVIEW_STATUS_APPROVED = ChangesetStatus.STATUS_APPROVED
63 63 c.REVIEW_STATUS_REJECTED = ChangesetStatus.STATUS_REJECTED
64
64 # backward compat., we use for OLD PRs a plain renderer
65 c.renderer = 'plain'
65 66 return c
66 67
67 68 def _get_pull_requests_list(
68 69 self, repo_name, source, filter_type, opened_by, statuses):
69 70
70 71 draw, start, limit = self._extract_chunk(self.request)
71 72 search_q, order_by, order_dir = self._extract_ordering(self.request)
72 73 _render = self.request.get_partial_renderer(
73 74 'rhodecode:templates/data_table/_dt_elements.mako')
74 75
75 76 # pagination
76 77
77 78 if filter_type == 'awaiting_review':
78 79 pull_requests = PullRequestModel().get_awaiting_review(
79 80 repo_name, source=source, opened_by=opened_by,
80 81 statuses=statuses, offset=start, length=limit,
81 82 order_by=order_by, order_dir=order_dir)
82 83 pull_requests_total_count = PullRequestModel().count_awaiting_review(
83 84 repo_name, source=source, statuses=statuses,
84 85 opened_by=opened_by)
85 86 elif filter_type == 'awaiting_my_review':
86 87 pull_requests = PullRequestModel().get_awaiting_my_review(
87 88 repo_name, source=source, opened_by=opened_by,
88 89 user_id=self._rhodecode_user.user_id, statuses=statuses,
89 90 offset=start, length=limit, order_by=order_by,
90 91 order_dir=order_dir)
91 92 pull_requests_total_count = PullRequestModel().count_awaiting_my_review(
92 93 repo_name, source=source, user_id=self._rhodecode_user.user_id,
93 94 statuses=statuses, opened_by=opened_by)
94 95 else:
95 96 pull_requests = PullRequestModel().get_all(
96 97 repo_name, source=source, opened_by=opened_by,
97 98 statuses=statuses, offset=start, length=limit,
98 99 order_by=order_by, order_dir=order_dir)
99 100 pull_requests_total_count = PullRequestModel().count_all(
100 101 repo_name, source=source, statuses=statuses,
101 102 opened_by=opened_by)
102 103
103 104 data = []
104 105 comments_model = CommentsModel()
105 106 for pr in pull_requests:
106 107 comments = comments_model.get_all_comments(
107 108 self.db_repo.repo_id, pull_request=pr)
108 109
109 110 data.append({
110 111 'name': _render('pullrequest_name',
111 112 pr.pull_request_id, pr.target_repo.repo_name),
112 113 'name_raw': pr.pull_request_id,
113 114 'status': _render('pullrequest_status',
114 115 pr.calculated_review_status()),
115 116 'title': _render(
116 117 'pullrequest_title', pr.title, pr.description),
117 118 'description': h.escape(pr.description),
118 119 'updated_on': _render('pullrequest_updated_on',
119 120 h.datetime_to_time(pr.updated_on)),
120 121 'updated_on_raw': h.datetime_to_time(pr.updated_on),
121 122 'created_on': _render('pullrequest_updated_on',
122 123 h.datetime_to_time(pr.created_on)),
123 124 'created_on_raw': h.datetime_to_time(pr.created_on),
124 125 'author': _render('pullrequest_author',
125 126 pr.author.full_contact, ),
126 127 'author_raw': pr.author.full_name,
127 128 'comments': _render('pullrequest_comments', len(comments)),
128 129 'comments_raw': len(comments),
129 130 'closed': pr.is_closed(),
130 131 })
131 132
132 133 data = ({
133 134 'draw': draw,
134 135 'data': data,
135 136 'recordsTotal': pull_requests_total_count,
136 137 'recordsFiltered': pull_requests_total_count,
137 138 })
138 139 return data
139 140
140 141 @LoginRequired()
141 142 @HasRepoPermissionAnyDecorator(
142 143 'repository.read', 'repository.write', 'repository.admin')
143 144 @view_config(
144 145 route_name='pullrequest_show_all', request_method='GET',
145 146 renderer='rhodecode:templates/pullrequests/pullrequests.mako')
146 147 def pull_request_list(self):
147 148 c = self.load_default_context()
148 149
149 150 req_get = self.request.GET
150 151 c.source = str2bool(req_get.get('source'))
151 152 c.closed = str2bool(req_get.get('closed'))
152 153 c.my = str2bool(req_get.get('my'))
153 154 c.awaiting_review = str2bool(req_get.get('awaiting_review'))
154 155 c.awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
155 156
156 157 c.active = 'open'
157 158 if c.my:
158 159 c.active = 'my'
159 160 if c.closed:
160 161 c.active = 'closed'
161 162 if c.awaiting_review and not c.source:
162 163 c.active = 'awaiting'
163 164 if c.source and not c.awaiting_review:
164 165 c.active = 'source'
165 166 if c.awaiting_my_review:
166 167 c.active = 'awaiting_my'
167 168
168 169 return self._get_template_context(c)
169 170
170 171 @LoginRequired()
171 172 @HasRepoPermissionAnyDecorator(
172 173 'repository.read', 'repository.write', 'repository.admin')
173 174 @view_config(
174 175 route_name='pullrequest_show_all_data', request_method='GET',
175 176 renderer='json_ext', xhr=True)
176 177 def pull_request_list_data(self):
177 178 self.load_default_context()
178 179
179 180 # additional filters
180 181 req_get = self.request.GET
181 182 source = str2bool(req_get.get('source'))
182 183 closed = str2bool(req_get.get('closed'))
183 184 my = str2bool(req_get.get('my'))
184 185 awaiting_review = str2bool(req_get.get('awaiting_review'))
185 186 awaiting_my_review = str2bool(req_get.get('awaiting_my_review'))
186 187
187 188 filter_type = 'awaiting_review' if awaiting_review \
188 189 else 'awaiting_my_review' if awaiting_my_review \
189 190 else None
190 191
191 192 opened_by = None
192 193 if my:
193 194 opened_by = [self._rhodecode_user.user_id]
194 195
195 196 statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN]
196 197 if closed:
197 198 statuses = [PullRequest.STATUS_CLOSED]
198 199
199 200 data = self._get_pull_requests_list(
200 201 repo_name=self.db_repo_name, source=source,
201 202 filter_type=filter_type, opened_by=opened_by, statuses=statuses)
202 203
203 204 return data
204 205
205 206 def _is_diff_cache_enabled(self, target_repo):
206 207 caching_enabled = self._get_general_setting(
207 208 target_repo, 'rhodecode_diff_cache')
208 209 log.debug('Diff caching enabled: %s', caching_enabled)
209 210 return caching_enabled
210 211
211 212 def _get_diffset(self, source_repo_name, source_repo,
212 213 source_ref_id, target_ref_id,
213 214 target_commit, source_commit, diff_limit, file_limit,
214 215 fulldiff):
215 216
216 217 vcs_diff = PullRequestModel().get_diff(
217 218 source_repo, source_ref_id, target_ref_id)
218 219
219 220 diff_processor = diffs.DiffProcessor(
220 221 vcs_diff, format='newdiff', diff_limit=diff_limit,
221 222 file_limit=file_limit, show_full_diff=fulldiff)
222 223
223 224 _parsed = diff_processor.prepare()
224 225
225 226 diffset = codeblocks.DiffSet(
226 227 repo_name=self.db_repo_name,
227 228 source_repo_name=source_repo_name,
228 229 source_node_getter=codeblocks.diffset_node_getter(target_commit),
229 230 target_node_getter=codeblocks.diffset_node_getter(source_commit),
230 231 )
231 232 diffset = self.path_filter.render_patchset_filtered(
232 233 diffset, _parsed, target_commit.raw_id, source_commit.raw_id)
233 234
234 235 return diffset
235 236
236 237 @LoginRequired()
237 238 @HasRepoPermissionAnyDecorator(
238 239 'repository.read', 'repository.write', 'repository.admin')
239 240 @view_config(
240 241 route_name='pullrequest_show', request_method='GET',
241 242 renderer='rhodecode:templates/pullrequests/pullrequest_show.mako')
242 243 def pull_request_show(self):
243 244 pull_request_id = self.request.matchdict['pull_request_id']
244 245
245 246 c = self.load_default_context()
246 247
247 248 version = self.request.GET.get('version')
248 249 from_version = self.request.GET.get('from_version') or version
249 250 merge_checks = self.request.GET.get('merge_checks')
250 251 c.fulldiff = str2bool(self.request.GET.get('fulldiff'))
251 252 force_refresh = str2bool(self.request.GET.get('force_refresh'))
252 253
253 254 (pull_request_latest,
254 255 pull_request_at_ver,
255 256 pull_request_display_obj,
256 257 at_version) = PullRequestModel().get_pr_version(
257 258 pull_request_id, version=version)
258 259 pr_closed = pull_request_latest.is_closed()
259 260
260 261 if pr_closed and (version or from_version):
261 262 # not allow to browse versions
262 263 raise HTTPFound(h.route_path(
263 264 'pullrequest_show', repo_name=self.db_repo_name,
264 265 pull_request_id=pull_request_id))
265 266
266 267 versions = pull_request_display_obj.versions()
267 268
268 269 c.at_version = at_version
269 270 c.at_version_num = (at_version
270 271 if at_version and at_version != 'latest'
271 272 else None)
272 273 c.at_version_pos = ChangesetComment.get_index_from_version(
273 274 c.at_version_num, versions)
274 275
275 276 (prev_pull_request_latest,
276 277 prev_pull_request_at_ver,
277 278 prev_pull_request_display_obj,
278 279 prev_at_version) = PullRequestModel().get_pr_version(
279 280 pull_request_id, version=from_version)
280 281
281 282 c.from_version = prev_at_version
282 283 c.from_version_num = (prev_at_version
283 284 if prev_at_version and prev_at_version != 'latest'
284 285 else None)
285 286 c.from_version_pos = ChangesetComment.get_index_from_version(
286 287 c.from_version_num, versions)
287 288
288 289 # define if we're in COMPARE mode or VIEW at version mode
289 290 compare = at_version != prev_at_version
290 291
291 292 # pull_requests repo_name we opened it against
292 293 # ie. target_repo must match
293 294 if self.db_repo_name != pull_request_at_ver.target_repo.repo_name:
294 295 raise HTTPNotFound()
295 296
296 297 c.shadow_clone_url = PullRequestModel().get_shadow_clone_url(
297 298 pull_request_at_ver)
298 299
299 300 c.pull_request = pull_request_display_obj
301 c.renderer = pull_request_at_ver.description_renderer or c.renderer
300 302 c.pull_request_latest = pull_request_latest
301 303
302 304 if compare or (at_version and not at_version == 'latest'):
303 305 c.allowed_to_change_status = False
304 306 c.allowed_to_update = False
305 307 c.allowed_to_merge = False
306 308 c.allowed_to_delete = False
307 309 c.allowed_to_comment = False
308 310 c.allowed_to_close = False
309 311 else:
310 312 can_change_status = PullRequestModel().check_user_change_status(
311 313 pull_request_at_ver, self._rhodecode_user)
312 314 c.allowed_to_change_status = can_change_status and not pr_closed
313 315
314 316 c.allowed_to_update = PullRequestModel().check_user_update(
315 317 pull_request_latest, self._rhodecode_user) and not pr_closed
316 318 c.allowed_to_merge = PullRequestModel().check_user_merge(
317 319 pull_request_latest, self._rhodecode_user) and not pr_closed
318 320 c.allowed_to_delete = PullRequestModel().check_user_delete(
319 321 pull_request_latest, self._rhodecode_user) and not pr_closed
320 322 c.allowed_to_comment = not pr_closed
321 323 c.allowed_to_close = c.allowed_to_merge and not pr_closed
322 324
323 325 c.forbid_adding_reviewers = False
324 326 c.forbid_author_to_review = False
325 327 c.forbid_commit_author_to_review = False
326 328
327 329 if pull_request_latest.reviewer_data and \
328 330 'rules' in pull_request_latest.reviewer_data:
329 331 rules = pull_request_latest.reviewer_data['rules'] or {}
330 332 try:
331 333 c.forbid_adding_reviewers = rules.get(
332 334 'forbid_adding_reviewers')
333 335 c.forbid_author_to_review = rules.get(
334 336 'forbid_author_to_review')
335 337 c.forbid_commit_author_to_review = rules.get(
336 338 'forbid_commit_author_to_review')
337 339 except Exception:
338 340 pass
339 341
340 342 # check merge capabilities
341 343 _merge_check = MergeCheck.validate(
342 344 pull_request_latest, user=self._rhodecode_user,
343 345 translator=self.request.translate,
344 346 force_shadow_repo_refresh=force_refresh)
345 347 c.pr_merge_errors = _merge_check.error_details
346 348 c.pr_merge_possible = not _merge_check.failed
347 349 c.pr_merge_message = _merge_check.merge_msg
348 350
349 351 c.pr_merge_info = MergeCheck.get_merge_conditions(
350 352 pull_request_latest, translator=self.request.translate)
351 353
352 354 c.pull_request_review_status = _merge_check.review_status
353 355 if merge_checks:
354 356 self.request.override_renderer = \
355 357 'rhodecode:templates/pullrequests/pullrequest_merge_checks.mako'
356 358 return self._get_template_context(c)
357 359
358 360 comments_model = CommentsModel()
359 361
360 362 # reviewers and statuses
361 363 c.pull_request_reviewers = pull_request_at_ver.reviewers_statuses()
362 364 allowed_reviewers = [x[0].user_id for x in c.pull_request_reviewers]
363 365
364 366 # GENERAL COMMENTS with versions #
365 367 q = comments_model._all_general_comments_of_pull_request(pull_request_latest)
366 368 q = q.order_by(ChangesetComment.comment_id.asc())
367 369 general_comments = q
368 370
369 371 # pick comments we want to render at current version
370 372 c.comment_versions = comments_model.aggregate_comments(
371 373 general_comments, versions, c.at_version_num)
372 374 c.comments = c.comment_versions[c.at_version_num]['until']
373 375
374 376 # INLINE COMMENTS with versions #
375 377 q = comments_model._all_inline_comments_of_pull_request(pull_request_latest)
376 378 q = q.order_by(ChangesetComment.comment_id.asc())
377 379 inline_comments = q
378 380
379 381 c.inline_versions = comments_model.aggregate_comments(
380 382 inline_comments, versions, c.at_version_num, inline=True)
381 383
382 384 # inject latest version
383 385 latest_ver = PullRequest.get_pr_display_object(
384 386 pull_request_latest, pull_request_latest)
385 387
386 388 c.versions = versions + [latest_ver]
387 389
388 390 # if we use version, then do not show later comments
389 391 # than current version
390 392 display_inline_comments = collections.defaultdict(
391 393 lambda: collections.defaultdict(list))
392 394 for co in inline_comments:
393 395 if c.at_version_num:
394 396 # pick comments that are at least UPTO given version, so we
395 397 # don't render comments for higher version
396 398 should_render = co.pull_request_version_id and \
397 399 co.pull_request_version_id <= c.at_version_num
398 400 else:
399 401 # showing all, for 'latest'
400 402 should_render = True
401 403
402 404 if should_render:
403 405 display_inline_comments[co.f_path][co.line_no].append(co)
404 406
405 407 # load diff data into template context, if we use compare mode then
406 408 # diff is calculated based on changes between versions of PR
407 409
408 410 source_repo = pull_request_at_ver.source_repo
409 411 source_ref_id = pull_request_at_ver.source_ref_parts.commit_id
410 412
411 413 target_repo = pull_request_at_ver.target_repo
412 414 target_ref_id = pull_request_at_ver.target_ref_parts.commit_id
413 415
414 416 if compare:
415 417 # in compare switch the diff base to latest commit from prev version
416 418 target_ref_id = prev_pull_request_display_obj.revisions[0]
417 419
418 420 # despite opening commits for bookmarks/branches/tags, we always
419 421 # convert this to rev to prevent changes after bookmark or branch change
420 422 c.source_ref_type = 'rev'
421 423 c.source_ref = source_ref_id
422 424
423 425 c.target_ref_type = 'rev'
424 426 c.target_ref = target_ref_id
425 427
426 428 c.source_repo = source_repo
427 429 c.target_repo = target_repo
428 430
429 431 c.commit_ranges = []
430 432 source_commit = EmptyCommit()
431 433 target_commit = EmptyCommit()
432 434 c.missing_requirements = False
433 435
434 436 source_scm = source_repo.scm_instance()
435 437 target_scm = target_repo.scm_instance()
436 438
437 439 shadow_scm = None
438 440 try:
439 441 shadow_scm = pull_request_latest.get_shadow_repo()
440 442 except Exception:
441 443 log.debug('Failed to get shadow repo', exc_info=True)
442 444 # try first the existing source_repo, and then shadow
443 445 # repo if we can obtain one
444 446 commits_source_repo = source_scm or shadow_scm
445 447
446 448 c.commits_source_repo = commits_source_repo
447 449 c.ancestor = None # set it to None, to hide it from PR view
448 450
449 451 # empty version means latest, so we keep this to prevent
450 452 # double caching
451 453 version_normalized = version or 'latest'
452 454 from_version_normalized = from_version or 'latest'
453 455
454 456 cache_path = self.rhodecode_vcs_repo.get_create_shadow_cache_pr_path(
455 457 target_repo)
456 458 cache_file_path = diff_cache_exist(
457 459 cache_path, 'pull_request', pull_request_id, version_normalized,
458 460 from_version_normalized, source_ref_id, target_ref_id, c.fulldiff)
459 461
460 462 caching_enabled = self._is_diff_cache_enabled(c.target_repo)
461 463 force_recache = str2bool(self.request.GET.get('force_recache'))
462 464
463 465 cached_diff = None
464 466 if caching_enabled:
465 467 cached_diff = load_cached_diff(cache_file_path)
466 468
467 469 has_proper_commit_cache = (
468 470 cached_diff and cached_diff.get('commits')
469 471 and len(cached_diff.get('commits', [])) == 5
470 472 and cached_diff.get('commits')[0]
471 473 and cached_diff.get('commits')[3])
472 474 if not force_recache and has_proper_commit_cache:
473 475 diff_commit_cache = \
474 476 (ancestor_commit, commit_cache, missing_requirements,
475 477 source_commit, target_commit) = cached_diff['commits']
476 478 else:
477 479 diff_commit_cache = \
478 480 (ancestor_commit, commit_cache, missing_requirements,
479 481 source_commit, target_commit) = self.get_commits(
480 482 commits_source_repo,
481 483 pull_request_at_ver,
482 484 source_commit,
483 485 source_ref_id,
484 486 source_scm,
485 487 target_commit,
486 488 target_ref_id,
487 489 target_scm)
488 490
489 491 # register our commit range
490 492 for comm in commit_cache.values():
491 493 c.commit_ranges.append(comm)
492 494
493 495 c.missing_requirements = missing_requirements
494 496 c.ancestor_commit = ancestor_commit
495 497 c.statuses = source_repo.statuses(
496 498 [x.raw_id for x in c.commit_ranges])
497 499
498 500 # auto collapse if we have more than limit
499 501 collapse_limit = diffs.DiffProcessor._collapse_commits_over
500 502 c.collapse_all_commits = len(c.commit_ranges) > collapse_limit
501 503 c.compare_mode = compare
502 504
503 505 # diff_limit is the old behavior, will cut off the whole diff
504 506 # if the limit is applied otherwise will just hide the
505 507 # big files from the front-end
506 508 diff_limit = c.visual.cut_off_limit_diff
507 509 file_limit = c.visual.cut_off_limit_file
508 510
509 511 c.missing_commits = False
510 512 if (c.missing_requirements
511 513 or isinstance(source_commit, EmptyCommit)
512 514 or source_commit == target_commit):
513 515
514 516 c.missing_commits = True
515 517 else:
516 518 c.inline_comments = display_inline_comments
517 519
518 520 has_proper_diff_cache = cached_diff and cached_diff.get('commits')
519 521 if not force_recache and has_proper_diff_cache:
520 522 c.diffset = cached_diff['diff']
521 523 (ancestor_commit, commit_cache, missing_requirements,
522 524 source_commit, target_commit) = cached_diff['commits']
523 525 else:
524 526 c.diffset = self._get_diffset(
525 527 c.source_repo.repo_name, commits_source_repo,
526 528 source_ref_id, target_ref_id,
527 529 target_commit, source_commit,
528 530 diff_limit, file_limit, c.fulldiff)
529 531
530 532 # save cached diff
531 533 if caching_enabled:
532 534 cache_diff(cache_file_path, c.diffset, diff_commit_cache)
533 535
534 536 c.limited_diff = c.diffset.limited_diff
535 537
536 538 # calculate removed files that are bound to comments
537 539 comment_deleted_files = [
538 540 fname for fname in display_inline_comments
539 541 if fname not in c.diffset.file_stats]
540 542
541 543 c.deleted_files_comments = collections.defaultdict(dict)
542 544 for fname, per_line_comments in display_inline_comments.items():
543 545 if fname in comment_deleted_files:
544 546 c.deleted_files_comments[fname]['stats'] = 0
545 547 c.deleted_files_comments[fname]['comments'] = list()
546 548 for lno, comments in per_line_comments.items():
547 549 c.deleted_files_comments[fname]['comments'].extend(
548 550 comments)
549 551
550 552 # this is a hack to properly display links, when creating PR, the
551 553 # compare view and others uses different notation, and
552 554 # compare_commits.mako renders links based on the target_repo.
553 555 # We need to swap that here to generate it properly on the html side
554 556 c.target_repo = c.source_repo
555 557
556 558 c.commit_statuses = ChangesetStatus.STATUSES
557 559
558 560 c.show_version_changes = not pr_closed
559 561 if c.show_version_changes:
560 562 cur_obj = pull_request_at_ver
561 563 prev_obj = prev_pull_request_at_ver
562 564
563 565 old_commit_ids = prev_obj.revisions
564 566 new_commit_ids = cur_obj.revisions
565 567 commit_changes = PullRequestModel()._calculate_commit_id_changes(
566 568 old_commit_ids, new_commit_ids)
567 569 c.commit_changes_summary = commit_changes
568 570
569 571 # calculate the diff for commits between versions
570 572 c.commit_changes = []
571 573 mark = lambda cs, fw: list(
572 574 h.itertools.izip_longest([], cs, fillvalue=fw))
573 575 for c_type, raw_id in mark(commit_changes.added, 'a') \
574 576 + mark(commit_changes.removed, 'r') \
575 577 + mark(commit_changes.common, 'c'):
576 578
577 579 if raw_id in commit_cache:
578 580 commit = commit_cache[raw_id]
579 581 else:
580 582 try:
581 583 commit = commits_source_repo.get_commit(raw_id)
582 584 except CommitDoesNotExistError:
583 585 # in case we fail extracting still use "dummy" commit
584 586 # for display in commit diff
585 587 commit = h.AttributeDict(
586 588 {'raw_id': raw_id,
587 589 'message': 'EMPTY or MISSING COMMIT'})
588 590 c.commit_changes.append([c_type, commit])
589 591
590 592 # current user review statuses for each version
591 593 c.review_versions = {}
592 594 if self._rhodecode_user.user_id in allowed_reviewers:
593 595 for co in general_comments:
594 596 if co.author.user_id == self._rhodecode_user.user_id:
595 597 status = co.status_change
596 598 if status:
597 599 _ver_pr = status[0].comment.pull_request_version_id
598 600 c.review_versions[_ver_pr] = status[0]
599 601
600 602 return self._get_template_context(c)
601 603
602 604 def get_commits(
603 605 self, commits_source_repo, pull_request_at_ver, source_commit,
604 606 source_ref_id, source_scm, target_commit, target_ref_id, target_scm):
605 607 commit_cache = collections.OrderedDict()
606 608 missing_requirements = False
607 609 try:
608 610 pre_load = ["author", "branch", "date", "message"]
609 611 show_revs = pull_request_at_ver.revisions
610 612 for rev in show_revs:
611 613 comm = commits_source_repo.get_commit(
612 614 commit_id=rev, pre_load=pre_load)
613 615 commit_cache[comm.raw_id] = comm
614 616
615 617 # Order here matters, we first need to get target, and then
616 618 # the source
617 619 target_commit = commits_source_repo.get_commit(
618 620 commit_id=safe_str(target_ref_id))
619 621
620 622 source_commit = commits_source_repo.get_commit(
621 623 commit_id=safe_str(source_ref_id))
622 624 except CommitDoesNotExistError:
623 625 log.warning(
624 626 'Failed to get commit from `{}` repo'.format(
625 627 commits_source_repo), exc_info=True)
626 628 except RepositoryRequirementError:
627 629 log.warning(
628 630 'Failed to get all required data from repo', exc_info=True)
629 631 missing_requirements = True
630 632 ancestor_commit = None
631 633 try:
632 634 ancestor_id = source_scm.get_common_ancestor(
633 635 source_commit.raw_id, target_commit.raw_id, target_scm)
634 636 ancestor_commit = source_scm.get_commit(ancestor_id)
635 637 except Exception:
636 638 ancestor_commit = None
637 639 return ancestor_commit, commit_cache, missing_requirements, source_commit, target_commit
638 640
639 641 def assure_not_empty_repo(self):
640 642 _ = self.request.translate
641 643
642 644 try:
643 645 self.db_repo.scm_instance().get_commit()
644 646 except EmptyRepositoryError:
645 647 h.flash(h.literal(_('There are no commits yet')),
646 648 category='warning')
647 649 raise HTTPFound(
648 650 h.route_path('repo_summary', repo_name=self.db_repo.repo_name))
649 651
650 652 @LoginRequired()
651 653 @NotAnonymous()
652 654 @HasRepoPermissionAnyDecorator(
653 655 'repository.read', 'repository.write', 'repository.admin')
654 656 @view_config(
655 657 route_name='pullrequest_new', request_method='GET',
656 658 renderer='rhodecode:templates/pullrequests/pullrequest.mako')
657 659 def pull_request_new(self):
658 660 _ = self.request.translate
659 661 c = self.load_default_context()
660 662
661 663 self.assure_not_empty_repo()
662 664 source_repo = self.db_repo
663 665
664 666 commit_id = self.request.GET.get('commit')
665 667 branch_ref = self.request.GET.get('branch')
666 668 bookmark_ref = self.request.GET.get('bookmark')
667 669
668 670 try:
669 671 source_repo_data = PullRequestModel().generate_repo_data(
670 672 source_repo, commit_id=commit_id,
671 673 branch=branch_ref, bookmark=bookmark_ref,
672 674 translator=self.request.translate)
673 675 except CommitDoesNotExistError as e:
674 676 log.exception(e)
675 677 h.flash(_('Commit does not exist'), 'error')
676 678 raise HTTPFound(
677 679 h.route_path('pullrequest_new', repo_name=source_repo.repo_name))
678 680
679 681 default_target_repo = source_repo
680 682
681 683 if source_repo.parent:
682 684 parent_vcs_obj = source_repo.parent.scm_instance()
683 685 if parent_vcs_obj and not parent_vcs_obj.is_empty():
684 686 # change default if we have a parent repo
685 687 default_target_repo = source_repo.parent
686 688
687 689 target_repo_data = PullRequestModel().generate_repo_data(
688 690 default_target_repo, translator=self.request.translate)
689 691
690 692 selected_source_ref = source_repo_data['refs']['selected_ref']
691 693 title_source_ref = ''
692 694 if selected_source_ref:
693 695 title_source_ref = selected_source_ref.split(':', 2)[1]
694 696 c.default_title = PullRequestModel().generate_pullrequest_title(
695 697 source=source_repo.repo_name,
696 698 source_ref=title_source_ref,
697 699 target=default_target_repo.repo_name
698 700 )
699 701
700 702 c.default_repo_data = {
701 703 'source_repo_name': source_repo.repo_name,
702 704 'source_refs_json': json.dumps(source_repo_data),
703 705 'target_repo_name': default_target_repo.repo_name,
704 706 'target_refs_json': json.dumps(target_repo_data),
705 707 }
706 708 c.default_source_ref = selected_source_ref
707 709
708 710 return self._get_template_context(c)
709 711
710 712 @LoginRequired()
711 713 @NotAnonymous()
712 714 @HasRepoPermissionAnyDecorator(
713 715 'repository.read', 'repository.write', 'repository.admin')
714 716 @view_config(
715 717 route_name='pullrequest_repo_refs', request_method='GET',
716 718 renderer='json_ext', xhr=True)
717 719 def pull_request_repo_refs(self):
718 720 self.load_default_context()
719 721 target_repo_name = self.request.matchdict['target_repo_name']
720 722 repo = Repository.get_by_repo_name(target_repo_name)
721 723 if not repo:
722 724 raise HTTPNotFound()
723 725
724 726 target_perm = HasRepoPermissionAny(
725 727 'repository.read', 'repository.write', 'repository.admin')(
726 728 target_repo_name)
727 729 if not target_perm:
728 730 raise HTTPNotFound()
729 731
730 732 return PullRequestModel().generate_repo_data(
731 733 repo, translator=self.request.translate)
732 734
733 735 @LoginRequired()
734 736 @NotAnonymous()
735 737 @HasRepoPermissionAnyDecorator(
736 738 'repository.read', 'repository.write', 'repository.admin')
737 739 @view_config(
738 740 route_name='pullrequest_repo_destinations', request_method='GET',
739 741 renderer='json_ext', xhr=True)
740 742 def pull_request_repo_destinations(self):
741 743 _ = self.request.translate
742 744 filter_query = self.request.GET.get('query')
743 745
744 746 query = Repository.query() \
745 747 .order_by(func.length(Repository.repo_name)) \
746 748 .filter(
747 749 or_(Repository.repo_name == self.db_repo.repo_name,
748 750 Repository.fork_id == self.db_repo.repo_id))
749 751
750 752 if filter_query:
751 753 ilike_expression = u'%{}%'.format(safe_unicode(filter_query))
752 754 query = query.filter(
753 755 Repository.repo_name.ilike(ilike_expression))
754 756
755 757 add_parent = False
756 758 if self.db_repo.parent:
757 759 if filter_query in self.db_repo.parent.repo_name:
758 760 parent_vcs_obj = self.db_repo.parent.scm_instance()
759 761 if parent_vcs_obj and not parent_vcs_obj.is_empty():
760 762 add_parent = True
761 763
762 764 limit = 20 - 1 if add_parent else 20
763 765 all_repos = query.limit(limit).all()
764 766 if add_parent:
765 767 all_repos += [self.db_repo.parent]
766 768
767 769 repos = []
768 770 for obj in ScmModel().get_repos(all_repos):
769 771 repos.append({
770 772 'id': obj['name'],
771 773 'text': obj['name'],
772 774 'type': 'repo',
773 775 'repo_id': obj['dbrepo']['repo_id'],
774 776 'repo_type': obj['dbrepo']['repo_type'],
775 777 'private': obj['dbrepo']['private'],
776 778
777 779 })
778 780
779 781 data = {
780 782 'more': False,
781 783 'results': [{
782 784 'text': _('Repositories'),
783 785 'children': repos
784 786 }] if repos else []
785 787 }
786 788 return data
787 789
788 790 @LoginRequired()
789 791 @NotAnonymous()
790 792 @HasRepoPermissionAnyDecorator(
791 793 'repository.read', 'repository.write', 'repository.admin')
792 794 @CSRFRequired()
793 795 @view_config(
794 796 route_name='pullrequest_create', request_method='POST',
795 797 renderer=None)
796 798 def pull_request_create(self):
797 799 _ = self.request.translate
798 800 self.assure_not_empty_repo()
799 801 self.load_default_context()
800 802
801 803 controls = peppercorn.parse(self.request.POST.items())
802 804
803 805 try:
804 806 form = PullRequestForm(
805 807 self.request.translate, self.db_repo.repo_id)()
806 808 _form = form.to_python(controls)
807 809 except formencode.Invalid as errors:
808 810 if errors.error_dict.get('revisions'):
809 811 msg = 'Revisions: %s' % errors.error_dict['revisions']
810 812 elif errors.error_dict.get('pullrequest_title'):
811 813 msg = errors.error_dict.get('pullrequest_title')
812 814 else:
813 815 msg = _('Error creating pull request: {}').format(errors)
814 816 log.exception(msg)
815 817 h.flash(msg, 'error')
816 818
817 819 # would rather just go back to form ...
818 820 raise HTTPFound(
819 821 h.route_path('pullrequest_new', repo_name=self.db_repo_name))
820 822
821 823 source_repo = _form['source_repo']
822 824 source_ref = _form['source_ref']
823 825 target_repo = _form['target_repo']
824 826 target_ref = _form['target_ref']
825 827 commit_ids = _form['revisions'][::-1]
826 828
827 829 # find the ancestor for this pr
828 830 source_db_repo = Repository.get_by_repo_name(_form['source_repo'])
829 831 target_db_repo = Repository.get_by_repo_name(_form['target_repo'])
830 832
831 833 # re-check permissions again here
832 834 # source_repo we must have read permissions
833 835
834 836 source_perm = HasRepoPermissionAny(
835 837 'repository.read',
836 838 'repository.write', 'repository.admin')(source_db_repo.repo_name)
837 839 if not source_perm:
838 840 msg = _('Not Enough permissions to source repo `{}`.'.format(
839 841 source_db_repo.repo_name))
840 842 h.flash(msg, category='error')
841 843 # copy the args back to redirect
842 844 org_query = self.request.GET.mixed()
843 845 raise HTTPFound(
844 846 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
845 847 _query=org_query))
846 848
847 849 # target repo we must have read permissions, and also later on
848 850 # we want to check branch permissions here
849 851 target_perm = HasRepoPermissionAny(
850 852 'repository.read',
851 853 'repository.write', 'repository.admin')(target_db_repo.repo_name)
852 854 if not target_perm:
853 855 msg = _('Not Enough permissions to target repo `{}`.'.format(
854 856 target_db_repo.repo_name))
855 857 h.flash(msg, category='error')
856 858 # copy the args back to redirect
857 859 org_query = self.request.GET.mixed()
858 860 raise HTTPFound(
859 861 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
860 862 _query=org_query))
861 863
862 864 source_scm = source_db_repo.scm_instance()
863 865 target_scm = target_db_repo.scm_instance()
864 866
865 867 source_commit = source_scm.get_commit(source_ref.split(':')[-1])
866 868 target_commit = target_scm.get_commit(target_ref.split(':')[-1])
867 869
868 870 ancestor = source_scm.get_common_ancestor(
869 871 source_commit.raw_id, target_commit.raw_id, target_scm)
870 872
871 873 # recalculate target ref based on ancestor
872 874 target_ref_type, target_ref_name, __ = _form['target_ref'].split(':')
873 875 target_ref = ':'.join((target_ref_type, target_ref_name, ancestor))
874 876
875 877 get_default_reviewers_data, validate_default_reviewers = \
876 878 PullRequestModel().get_reviewer_functions()
877 879
878 880 # recalculate reviewers logic, to make sure we can validate this
879 881 reviewer_rules = get_default_reviewers_data(
880 882 self._rhodecode_db_user, source_db_repo,
881 883 source_commit, target_db_repo, target_commit)
882 884
883 885 given_reviewers = _form['review_members']
884 886 reviewers = validate_default_reviewers(
885 887 given_reviewers, reviewer_rules)
886 888
887 889 pullrequest_title = _form['pullrequest_title']
888 890 title_source_ref = source_ref.split(':', 2)[1]
889 891 if not pullrequest_title:
890 892 pullrequest_title = PullRequestModel().generate_pullrequest_title(
891 893 source=source_repo,
892 894 source_ref=title_source_ref,
893 895 target=target_repo
894 896 )
895 897
896 898 description = _form['pullrequest_desc']
899 description_renderer = _form['description_renderer']
897 900
898 901 try:
899 902 pull_request = PullRequestModel().create(
900 903 created_by=self._rhodecode_user.user_id,
901 904 source_repo=source_repo,
902 905 source_ref=source_ref,
903 906 target_repo=target_repo,
904 907 target_ref=target_ref,
905 908 revisions=commit_ids,
906 909 reviewers=reviewers,
907 910 title=pullrequest_title,
908 911 description=description,
912 description_renderer=description_renderer,
909 913 reviewer_data=reviewer_rules,
910 914 auth_user=self._rhodecode_user
911 915 )
912 916 Session().commit()
913 917
914 918 h.flash(_('Successfully opened new pull request'),
915 919 category='success')
916 920 except Exception:
917 921 msg = _('Error occurred during creation of this pull request.')
918 922 log.exception(msg)
919 923 h.flash(msg, category='error')
920 924
921 925 # copy the args back to redirect
922 926 org_query = self.request.GET.mixed()
923 927 raise HTTPFound(
924 928 h.route_path('pullrequest_new', repo_name=self.db_repo_name,
925 929 _query=org_query))
926 930
927 931 raise HTTPFound(
928 932 h.route_path('pullrequest_show', repo_name=target_repo,
929 933 pull_request_id=pull_request.pull_request_id))
930 934
931 935 @LoginRequired()
932 936 @NotAnonymous()
933 937 @HasRepoPermissionAnyDecorator(
934 938 'repository.read', 'repository.write', 'repository.admin')
935 939 @CSRFRequired()
936 940 @view_config(
937 941 route_name='pullrequest_update', request_method='POST',
938 942 renderer='json_ext')
939 943 def pull_request_update(self):
940 944 pull_request = PullRequest.get_or_404(
941 945 self.request.matchdict['pull_request_id'])
942 946 _ = self.request.translate
943 947
944 948 self.load_default_context()
945 949
946 950 if pull_request.is_closed():
947 951 log.debug('update: forbidden because pull request is closed')
948 952 msg = _(u'Cannot update closed pull requests.')
949 953 h.flash(msg, category='error')
950 954 return True
951 955
952 956 # only owner or admin can update it
953 957 allowed_to_update = PullRequestModel().check_user_update(
954 958 pull_request, self._rhodecode_user)
955 959 if allowed_to_update:
956 960 controls = peppercorn.parse(self.request.POST.items())
957 961
958 962 if 'review_members' in controls:
959 963 self._update_reviewers(
960 964 pull_request, controls['review_members'],
961 965 pull_request.reviewer_data)
962 966 elif str2bool(self.request.POST.get('update_commits', 'false')):
963 967 self._update_commits(pull_request)
964 968 elif str2bool(self.request.POST.get('edit_pull_request', 'false')):
965 969 self._edit_pull_request(pull_request)
966 970 else:
967 971 raise HTTPBadRequest()
968 972 return True
969 973 raise HTTPForbidden()
970 974
971 975 def _edit_pull_request(self, pull_request):
972 976 _ = self.request.translate
977
973 978 try:
974 979 PullRequestModel().edit(
975 pull_request, self.request.POST.get('title'),
976 self.request.POST.get('description'), self._rhodecode_user)
980 pull_request,
981 self.request.POST.get('title'),
982 self.request.POST.get('description'),
983 self.request.POST.get('description_renderer'),
984 self._rhodecode_user)
977 985 except ValueError:
978 986 msg = _(u'Cannot update closed pull requests.')
979 987 h.flash(msg, category='error')
980 988 return
981 989 else:
982 990 Session().commit()
983 991
984 992 msg = _(u'Pull request title & description updated.')
985 993 h.flash(msg, category='success')
986 994 return
987 995
988 996 def _update_commits(self, pull_request):
989 997 _ = self.request.translate
990 998 resp = PullRequestModel().update_commits(pull_request)
991 999
992 1000 if resp.executed:
993 1001
994 1002 if resp.target_changed and resp.source_changed:
995 1003 changed = 'target and source repositories'
996 1004 elif resp.target_changed and not resp.source_changed:
997 1005 changed = 'target repository'
998 1006 elif not resp.target_changed and resp.source_changed:
999 1007 changed = 'source repository'
1000 1008 else:
1001 1009 changed = 'nothing'
1002 1010
1003 1011 msg = _(
1004 1012 u'Pull request updated to "{source_commit_id}" with '
1005 1013 u'{count_added} added, {count_removed} removed commits. '
1006 1014 u'Source of changes: {change_source}')
1007 1015 msg = msg.format(
1008 1016 source_commit_id=pull_request.source_ref_parts.commit_id,
1009 1017 count_added=len(resp.changes.added),
1010 1018 count_removed=len(resp.changes.removed),
1011 1019 change_source=changed)
1012 1020 h.flash(msg, category='success')
1013 1021
1014 1022 channel = '/repo${}$/pr/{}'.format(
1015 1023 pull_request.target_repo.repo_name,
1016 1024 pull_request.pull_request_id)
1017 1025 message = msg + (
1018 1026 ' - <a onclick="window.location.reload()">'
1019 1027 '<strong>{}</strong></a>'.format(_('Reload page')))
1020 1028 channelstream.post_message(
1021 1029 channel, message, self._rhodecode_user.username,
1022 1030 registry=self.request.registry)
1023 1031 else:
1024 1032 msg = PullRequestModel.UPDATE_STATUS_MESSAGES[resp.reason]
1025 1033 warning_reasons = [
1026 1034 UpdateFailureReason.NO_CHANGE,
1027 1035 UpdateFailureReason.WRONG_REF_TYPE,
1028 1036 ]
1029 1037 category = 'warning' if resp.reason in warning_reasons else 'error'
1030 1038 h.flash(msg, category=category)
1031 1039
1032 1040 @LoginRequired()
1033 1041 @NotAnonymous()
1034 1042 @HasRepoPermissionAnyDecorator(
1035 1043 'repository.read', 'repository.write', 'repository.admin')
1036 1044 @CSRFRequired()
1037 1045 @view_config(
1038 1046 route_name='pullrequest_merge', request_method='POST',
1039 1047 renderer='json_ext')
1040 1048 def pull_request_merge(self):
1041 1049 """
1042 1050 Merge will perform a server-side merge of the specified
1043 1051 pull request, if the pull request is approved and mergeable.
1044 1052 After successful merging, the pull request is automatically
1045 1053 closed, with a relevant comment.
1046 1054 """
1047 1055 pull_request = PullRequest.get_or_404(
1048 1056 self.request.matchdict['pull_request_id'])
1049 1057
1050 1058 self.load_default_context()
1051 1059 check = MergeCheck.validate(pull_request, self._rhodecode_db_user,
1052 1060 translator=self.request.translate)
1053 1061 merge_possible = not check.failed
1054 1062
1055 1063 for err_type, error_msg in check.errors:
1056 1064 h.flash(error_msg, category=err_type)
1057 1065
1058 1066 if merge_possible:
1059 1067 log.debug("Pre-conditions checked, trying to merge.")
1060 1068 extras = vcs_operation_context(
1061 1069 self.request.environ, repo_name=pull_request.target_repo.repo_name,
1062 1070 username=self._rhodecode_db_user.username, action='push',
1063 1071 scm=pull_request.target_repo.repo_type)
1064 1072 self._merge_pull_request(
1065 1073 pull_request, self._rhodecode_db_user, extras)
1066 1074 else:
1067 1075 log.debug("Pre-conditions failed, NOT merging.")
1068 1076
1069 1077 raise HTTPFound(
1070 1078 h.route_path('pullrequest_show',
1071 1079 repo_name=pull_request.target_repo.repo_name,
1072 1080 pull_request_id=pull_request.pull_request_id))
1073 1081
1074 1082 def _merge_pull_request(self, pull_request, user, extras):
1075 1083 _ = self.request.translate
1076 1084 merge_resp = PullRequestModel().merge_repo(pull_request, user, extras=extras)
1077 1085
1078 1086 if merge_resp.executed:
1079 1087 log.debug("The merge was successful, closing the pull request.")
1080 1088 PullRequestModel().close_pull_request(
1081 1089 pull_request.pull_request_id, user)
1082 1090 Session().commit()
1083 1091 msg = _('Pull request was successfully merged and closed.')
1084 1092 h.flash(msg, category='success')
1085 1093 else:
1086 1094 log.debug(
1087 1095 "The merge was not successful. Merge response: %s",
1088 1096 merge_resp)
1089 1097 msg = PullRequestModel().merge_status_message(
1090 1098 merge_resp.failure_reason)
1091 1099 h.flash(msg, category='error')
1092 1100
1093 1101 def _update_reviewers(self, pull_request, review_members, reviewer_rules):
1094 1102 _ = self.request.translate
1095 1103 get_default_reviewers_data, validate_default_reviewers = \
1096 1104 PullRequestModel().get_reviewer_functions()
1097 1105
1098 1106 try:
1099 1107 reviewers = validate_default_reviewers(review_members, reviewer_rules)
1100 1108 except ValueError as e:
1101 1109 log.error('Reviewers Validation: {}'.format(e))
1102 1110 h.flash(e, category='error')
1103 1111 return
1104 1112
1105 1113 PullRequestModel().update_reviewers(
1106 1114 pull_request, reviewers, self._rhodecode_user)
1107 1115 h.flash(_('Pull request reviewers updated.'), category='success')
1108 1116 Session().commit()
1109 1117
1110 1118 @LoginRequired()
1111 1119 @NotAnonymous()
1112 1120 @HasRepoPermissionAnyDecorator(
1113 1121 'repository.read', 'repository.write', 'repository.admin')
1114 1122 @CSRFRequired()
1115 1123 @view_config(
1116 1124 route_name='pullrequest_delete', request_method='POST',
1117 1125 renderer='json_ext')
1118 1126 def pull_request_delete(self):
1119 1127 _ = self.request.translate
1120 1128
1121 1129 pull_request = PullRequest.get_or_404(
1122 1130 self.request.matchdict['pull_request_id'])
1123 1131 self.load_default_context()
1124 1132
1125 1133 pr_closed = pull_request.is_closed()
1126 1134 allowed_to_delete = PullRequestModel().check_user_delete(
1127 1135 pull_request, self._rhodecode_user) and not pr_closed
1128 1136
1129 1137 # only owner can delete it !
1130 1138 if allowed_to_delete:
1131 1139 PullRequestModel().delete(pull_request, self._rhodecode_user)
1132 1140 Session().commit()
1133 1141 h.flash(_('Successfully deleted pull request'),
1134 1142 category='success')
1135 1143 raise HTTPFound(h.route_path('pullrequest_show_all',
1136 1144 repo_name=self.db_repo_name))
1137 1145
1138 1146 log.warning('user %s tried to delete pull request without access',
1139 1147 self._rhodecode_user)
1140 1148 raise HTTPNotFound()
1141 1149
1142 1150 @LoginRequired()
1143 1151 @NotAnonymous()
1144 1152 @HasRepoPermissionAnyDecorator(
1145 1153 'repository.read', 'repository.write', 'repository.admin')
1146 1154 @CSRFRequired()
1147 1155 @view_config(
1148 1156 route_name='pullrequest_comment_create', request_method='POST',
1149 1157 renderer='json_ext')
1150 1158 def pull_request_comment_create(self):
1151 1159 _ = self.request.translate
1152 1160
1153 1161 pull_request = PullRequest.get_or_404(
1154 1162 self.request.matchdict['pull_request_id'])
1155 1163 pull_request_id = pull_request.pull_request_id
1156 1164
1157 1165 if pull_request.is_closed():
1158 1166 log.debug('comment: forbidden because pull request is closed')
1159 1167 raise HTTPForbidden()
1160 1168
1161 1169 allowed_to_comment = PullRequestModel().check_user_comment(
1162 1170 pull_request, self._rhodecode_user)
1163 1171 if not allowed_to_comment:
1164 1172 log.debug(
1165 1173 'comment: forbidden because pull request is from forbidden repo')
1166 1174 raise HTTPForbidden()
1167 1175
1168 1176 c = self.load_default_context()
1169 1177
1170 1178 status = self.request.POST.get('changeset_status', None)
1171 1179 text = self.request.POST.get('text')
1172 1180 comment_type = self.request.POST.get('comment_type')
1173 1181 resolves_comment_id = self.request.POST.get('resolves_comment_id', None)
1174 1182 close_pull_request = self.request.POST.get('close_pull_request')
1175 1183
1176 1184 # the logic here should work like following, if we submit close
1177 1185 # pr comment, use `close_pull_request_with_comment` function
1178 1186 # else handle regular comment logic
1179 1187
1180 1188 if close_pull_request:
1181 1189 # only owner or admin or person with write permissions
1182 1190 allowed_to_close = PullRequestModel().check_user_update(
1183 1191 pull_request, self._rhodecode_user)
1184 1192 if not allowed_to_close:
1185 1193 log.debug('comment: forbidden because not allowed to close '
1186 1194 'pull request %s', pull_request_id)
1187 1195 raise HTTPForbidden()
1188 1196 comment, status = PullRequestModel().close_pull_request_with_comment(
1189 1197 pull_request, self._rhodecode_user, self.db_repo, message=text)
1190 1198 Session().flush()
1191 1199 events.trigger(
1192 1200 events.PullRequestCommentEvent(pull_request, comment))
1193 1201
1194 1202 else:
1195 1203 # regular comment case, could be inline, or one with status.
1196 1204 # for that one we check also permissions
1197 1205
1198 1206 allowed_to_change_status = PullRequestModel().check_user_change_status(
1199 1207 pull_request, self._rhodecode_user)
1200 1208
1201 1209 if status and allowed_to_change_status:
1202 1210 message = (_('Status change %(transition_icon)s %(status)s')
1203 1211 % {'transition_icon': '>',
1204 1212 'status': ChangesetStatus.get_status_lbl(status)})
1205 1213 text = text or message
1206 1214
1207 1215 comment = CommentsModel().create(
1208 1216 text=text,
1209 1217 repo=self.db_repo.repo_id,
1210 1218 user=self._rhodecode_user.user_id,
1211 1219 pull_request=pull_request,
1212 1220 f_path=self.request.POST.get('f_path'),
1213 1221 line_no=self.request.POST.get('line'),
1214 1222 status_change=(ChangesetStatus.get_status_lbl(status)
1215 1223 if status and allowed_to_change_status else None),
1216 1224 status_change_type=(status
1217 1225 if status and allowed_to_change_status else None),
1218 1226 comment_type=comment_type,
1219 1227 resolves_comment_id=resolves_comment_id,
1220 1228 auth_user=self._rhodecode_user
1221 1229 )
1222 1230
1223 1231 if allowed_to_change_status:
1224 1232 # calculate old status before we change it
1225 1233 old_calculated_status = pull_request.calculated_review_status()
1226 1234
1227 1235 # get status if set !
1228 1236 if status:
1229 1237 ChangesetStatusModel().set_status(
1230 1238 self.db_repo.repo_id,
1231 1239 status,
1232 1240 self._rhodecode_user.user_id,
1233 1241 comment,
1234 1242 pull_request=pull_request
1235 1243 )
1236 1244
1237 1245 Session().flush()
1238 1246 # this is somehow required to get access to some relationship
1239 1247 # loaded on comment
1240 1248 Session().refresh(comment)
1241 1249
1242 1250 events.trigger(
1243 1251 events.PullRequestCommentEvent(pull_request, comment))
1244 1252
1245 1253 # we now calculate the status of pull request, and based on that
1246 1254 # calculation we set the commits status
1247 1255 calculated_status = pull_request.calculated_review_status()
1248 1256 if old_calculated_status != calculated_status:
1249 1257 PullRequestModel()._trigger_pull_request_hook(
1250 1258 pull_request, self._rhodecode_user, 'review_status_change')
1251 1259
1252 1260 Session().commit()
1253 1261
1254 1262 data = {
1255 1263 'target_id': h.safeid(h.safe_unicode(
1256 1264 self.request.POST.get('f_path'))),
1257 1265 }
1258 1266 if comment:
1259 1267 c.co = comment
1260 1268 rendered_comment = render(
1261 1269 'rhodecode:templates/changeset/changeset_comment_block.mako',
1262 1270 self._get_template_context(c), self.request)
1263 1271
1264 1272 data.update(comment.get_dict())
1265 1273 data.update({'rendered_text': rendered_comment})
1266 1274
1267 1275 return data
1268 1276
1269 1277 @LoginRequired()
1270 1278 @NotAnonymous()
1271 1279 @HasRepoPermissionAnyDecorator(
1272 1280 'repository.read', 'repository.write', 'repository.admin')
1273 1281 @CSRFRequired()
1274 1282 @view_config(
1275 1283 route_name='pullrequest_comment_delete', request_method='POST',
1276 1284 renderer='json_ext')
1277 1285 def pull_request_comment_delete(self):
1278 1286 pull_request = PullRequest.get_or_404(
1279 1287 self.request.matchdict['pull_request_id'])
1280 1288
1281 1289 comment = ChangesetComment.get_or_404(
1282 1290 self.request.matchdict['comment_id'])
1283 1291 comment_id = comment.comment_id
1284 1292
1285 1293 if pull_request.is_closed():
1286 1294 log.debug('comment: forbidden because pull request is closed')
1287 1295 raise HTTPForbidden()
1288 1296
1289 1297 if not comment:
1290 1298 log.debug('Comment with id:%s not found, skipping', comment_id)
1291 1299 # comment already deleted in another call probably
1292 1300 return True
1293 1301
1294 1302 if comment.pull_request.is_closed():
1295 1303 # don't allow deleting comments on closed pull request
1296 1304 raise HTTPForbidden()
1297 1305
1298 1306 is_repo_admin = h.HasRepoPermissionAny('repository.admin')(self.db_repo_name)
1299 1307 super_admin = h.HasPermissionAny('hg.admin')()
1300 1308 comment_owner = comment.author.user_id == self._rhodecode_user.user_id
1301 1309 is_repo_comment = comment.repo.repo_name == self.db_repo_name
1302 1310 comment_repo_admin = is_repo_admin and is_repo_comment
1303 1311
1304 1312 if super_admin or comment_owner or comment_repo_admin:
1305 1313 old_calculated_status = comment.pull_request.calculated_review_status()
1306 1314 CommentsModel().delete(comment=comment, auth_user=self._rhodecode_user)
1307 1315 Session().commit()
1308 1316 calculated_status = comment.pull_request.calculated_review_status()
1309 1317 if old_calculated_status != calculated_status:
1310 1318 PullRequestModel()._trigger_pull_request_hook(
1311 1319 comment.pull_request, self._rhodecode_user, 'review_status_change')
1312 1320 return True
1313 1321 else:
1314 1322 log.warning('No permissions for user %s to delete comment_id: %s',
1315 1323 self._rhodecode_db_user, comment_id)
1316 1324 raise HTTPNotFound()
@@ -1,2095 +1,2101 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Helper functions
23 23
24 24 Consists of functions to typically be used within templates, but also
25 25 available to Controllers. This module is available to both as 'h'.
26 26 """
27 27
28 28 import os
29 29 import random
30 30 import hashlib
31 31 import StringIO
32 32 import textwrap
33 33 import urllib
34 34 import math
35 35 import logging
36 36 import re
37 37 import urlparse
38 38 import time
39 39 import string
40 40 import hashlib
41 41 from collections import OrderedDict
42 42
43 43 import pygments
44 44 import itertools
45 45 import fnmatch
46 46
47 47 from datetime import datetime
48 48 from functools import partial
49 49 from pygments.formatters.html import HtmlFormatter
50 50 from pygments import highlight as code_highlight
51 51 from pygments.lexers import (
52 52 get_lexer_by_name, get_lexer_for_filename, get_lexer_for_mimetype)
53 53
54 54 from pyramid.threadlocal import get_current_request
55 55
56 56 from webhelpers.html import literal, HTML, escape
57 57 from webhelpers.html.tools import *
58 58 from webhelpers.html.builder import make_tag
59 59 from webhelpers.html.tags import auto_discovery_link, checkbox, css_classes, \
60 60 end_form, file, form as wh_form, hidden, image, javascript_link, link_to, \
61 61 link_to_if, link_to_unless, ol, required_legend, select, stylesheet_link, \
62 62 submit, text, password, textarea, title, ul, xml_declaration, radio
63 63 from webhelpers.html.tools import auto_link, button_to, highlight, \
64 64 js_obfuscate, mail_to, strip_links, strip_tags, tag_re
65 65 from webhelpers.text import chop_at, collapse, convert_accented_entities, \
66 66 convert_misc_entities, lchop, plural, rchop, remove_formatting, \
67 67 replace_whitespace, urlify, truncate, wrap_paragraphs
68 68 from webhelpers.date import time_ago_in_words
69 69 from webhelpers.paginate import Page as _Page
70 70 from webhelpers.html.tags import _set_input_attrs, _set_id_attr, \
71 71 convert_boolean_attrs, NotGiven, _make_safe_id_component
72 72 from webhelpers2.number import format_byte_size
73 73
74 74 from rhodecode.lib.action_parser import action_parser
75 75 from rhodecode.lib.ext_json import json
76 76 from rhodecode.lib.utils import repo_name_slug, get_custom_lexer
77 77 from rhodecode.lib.utils2 import str2bool, safe_unicode, safe_str, \
78 78 get_commit_safe, datetime_to_time, time_to_datetime, time_to_utcdatetime, \
79 79 AttributeDict, safe_int, md5, md5_safe
80 80 from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links
81 81 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
82 82 from rhodecode.lib.vcs.backends.base import BaseChangeset, EmptyCommit
83 83 from rhodecode.config.conf import DATE_FORMAT, DATETIME_FORMAT
84 84 from rhodecode.model.changeset_status import ChangesetStatusModel
85 85 from rhodecode.model.db import Permission, User, Repository
86 86 from rhodecode.model.repo_group import RepoGroupModel
87 87 from rhodecode.model.settings import IssueTrackerSettingsModel
88 88
89 89 log = logging.getLogger(__name__)
90 90
91 91
92 92 DEFAULT_USER = User.DEFAULT_USER
93 93 DEFAULT_USER_EMAIL = User.DEFAULT_USER_EMAIL
94 94
95 95
96 96 def asset(path, ver=None, **kwargs):
97 97 """
98 98 Helper to generate a static asset file path for rhodecode assets
99 99
100 100 eg. h.asset('images/image.png', ver='3923')
101 101
102 102 :param path: path of asset
103 103 :param ver: optional version query param to append as ?ver=
104 104 """
105 105 request = get_current_request()
106 106 query = {}
107 107 query.update(kwargs)
108 108 if ver:
109 109 query = {'ver': ver}
110 110 return request.static_path(
111 111 'rhodecode:public/{}'.format(path), _query=query)
112 112
113 113
114 114 default_html_escape_table = {
115 115 ord('&'): u'&amp;',
116 116 ord('<'): u'&lt;',
117 117 ord('>'): u'&gt;',
118 118 ord('"'): u'&quot;',
119 119 ord("'"): u'&#39;',
120 120 }
121 121
122 122
123 123 def html_escape(text, html_escape_table=default_html_escape_table):
124 124 """Produce entities within text."""
125 125 return text.translate(html_escape_table)
126 126
127 127
128 128 def chop_at_smart(s, sub, inclusive=False, suffix_if_chopped=None):
129 129 """
130 130 Truncate string ``s`` at the first occurrence of ``sub``.
131 131
132 132 If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
133 133 """
134 134 suffix_if_chopped = suffix_if_chopped or ''
135 135 pos = s.find(sub)
136 136 if pos == -1:
137 137 return s
138 138
139 139 if inclusive:
140 140 pos += len(sub)
141 141
142 142 chopped = s[:pos]
143 143 left = s[pos:].strip()
144 144
145 145 if left and suffix_if_chopped:
146 146 chopped += suffix_if_chopped
147 147
148 148 return chopped
149 149
150 150
151 151 def shorter(text, size=20):
152 152 postfix = '...'
153 153 if len(text) > size:
154 154 return text[:size - len(postfix)] + postfix
155 155 return text
156 156
157 157
158 158 def _reset(name, value=None, id=NotGiven, type="reset", **attrs):
159 159 """
160 160 Reset button
161 161 """
162 162 _set_input_attrs(attrs, type, name, value)
163 163 _set_id_attr(attrs, id, name)
164 164 convert_boolean_attrs(attrs, ["disabled"])
165 165 return HTML.input(**attrs)
166 166
167 167 reset = _reset
168 168 safeid = _make_safe_id_component
169 169
170 170
171 171 def branding(name, length=40):
172 172 return truncate(name, length, indicator="")
173 173
174 174
175 175 def FID(raw_id, path):
176 176 """
177 177 Creates a unique ID for filenode based on it's hash of path and commit
178 178 it's safe to use in urls
179 179
180 180 :param raw_id:
181 181 :param path:
182 182 """
183 183
184 184 return 'c-%s-%s' % (short_id(raw_id), md5_safe(path)[:12])
185 185
186 186
187 187 class _GetError(object):
188 188 """Get error from form_errors, and represent it as span wrapped error
189 189 message
190 190
191 191 :param field_name: field to fetch errors for
192 192 :param form_errors: form errors dict
193 193 """
194 194
195 195 def __call__(self, field_name, form_errors):
196 196 tmpl = """<span class="error_msg">%s</span>"""
197 197 if form_errors and field_name in form_errors:
198 198 return literal(tmpl % form_errors.get(field_name))
199 199
200 200 get_error = _GetError()
201 201
202 202
203 203 class _ToolTip(object):
204 204
205 205 def __call__(self, tooltip_title, trim_at=50):
206 206 """
207 207 Special function just to wrap our text into nice formatted
208 208 autowrapped text
209 209
210 210 :param tooltip_title:
211 211 """
212 212 tooltip_title = escape(tooltip_title)
213 213 tooltip_title = tooltip_title.replace('<', '&lt;').replace('>', '&gt;')
214 214 return tooltip_title
215 215 tooltip = _ToolTip()
216 216
217 217
218 218 def files_breadcrumbs(repo_name, commit_id, file_path):
219 219 if isinstance(file_path, str):
220 220 file_path = safe_unicode(file_path)
221 221
222 222 # TODO: johbo: Is this always a url like path, or is this operating
223 223 # system dependent?
224 224 path_segments = file_path.split('/')
225 225
226 226 repo_name_html = escape(repo_name)
227 227 if len(path_segments) == 1 and path_segments[0] == '':
228 228 url_segments = [repo_name_html]
229 229 else:
230 230 url_segments = [
231 231 link_to(
232 232 repo_name_html,
233 233 route_path(
234 234 'repo_files',
235 235 repo_name=repo_name,
236 236 commit_id=commit_id,
237 237 f_path=''),
238 238 class_='pjax-link')]
239 239
240 240 last_cnt = len(path_segments) - 1
241 241 for cnt, segment in enumerate(path_segments):
242 242 if not segment:
243 243 continue
244 244 segment_html = escape(segment)
245 245
246 246 if cnt != last_cnt:
247 247 url_segments.append(
248 248 link_to(
249 249 segment_html,
250 250 route_path(
251 251 'repo_files',
252 252 repo_name=repo_name,
253 253 commit_id=commit_id,
254 254 f_path='/'.join(path_segments[:cnt + 1])),
255 255 class_='pjax-link'))
256 256 else:
257 257 url_segments.append(segment_html)
258 258
259 259 return literal('/'.join(url_segments))
260 260
261 261
262 262 class CodeHtmlFormatter(HtmlFormatter):
263 263 """
264 264 My code Html Formatter for source codes
265 265 """
266 266
267 267 def wrap(self, source, outfile):
268 268 return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
269 269
270 270 def _wrap_code(self, source):
271 271 for cnt, it in enumerate(source):
272 272 i, t = it
273 273 t = '<div id="L%s">%s</div>' % (cnt + 1, t)
274 274 yield i, t
275 275
276 276 def _wrap_tablelinenos(self, inner):
277 277 dummyoutfile = StringIO.StringIO()
278 278 lncount = 0
279 279 for t, line in inner:
280 280 if t:
281 281 lncount += 1
282 282 dummyoutfile.write(line)
283 283
284 284 fl = self.linenostart
285 285 mw = len(str(lncount + fl - 1))
286 286 sp = self.linenospecial
287 287 st = self.linenostep
288 288 la = self.lineanchors
289 289 aln = self.anchorlinenos
290 290 nocls = self.noclasses
291 291 if sp:
292 292 lines = []
293 293
294 294 for i in range(fl, fl + lncount):
295 295 if i % st == 0:
296 296 if i % sp == 0:
297 297 if aln:
298 298 lines.append('<a href="#%s%d" class="special">%*d</a>' %
299 299 (la, i, mw, i))
300 300 else:
301 301 lines.append('<span class="special">%*d</span>' % (mw, i))
302 302 else:
303 303 if aln:
304 304 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
305 305 else:
306 306 lines.append('%*d' % (mw, i))
307 307 else:
308 308 lines.append('')
309 309 ls = '\n'.join(lines)
310 310 else:
311 311 lines = []
312 312 for i in range(fl, fl + lncount):
313 313 if i % st == 0:
314 314 if aln:
315 315 lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
316 316 else:
317 317 lines.append('%*d' % (mw, i))
318 318 else:
319 319 lines.append('')
320 320 ls = '\n'.join(lines)
321 321
322 322 # in case you wonder about the seemingly redundant <div> here: since the
323 323 # content in the other cell also is wrapped in a div, some browsers in
324 324 # some configurations seem to mess up the formatting...
325 325 if nocls:
326 326 yield 0, ('<table class="%stable">' % self.cssclass +
327 327 '<tr><td><div class="linenodiv" '
328 328 'style="background-color: #f0f0f0; padding-right: 10px">'
329 329 '<pre style="line-height: 125%">' +
330 330 ls + '</pre></div></td><td id="hlcode" class="code">')
331 331 else:
332 332 yield 0, ('<table class="%stable">' % self.cssclass +
333 333 '<tr><td class="linenos"><div class="linenodiv"><pre>' +
334 334 ls + '</pre></div></td><td id="hlcode" class="code">')
335 335 yield 0, dummyoutfile.getvalue()
336 336 yield 0, '</td></tr></table>'
337 337
338 338
339 339 class SearchContentCodeHtmlFormatter(CodeHtmlFormatter):
340 340 def __init__(self, **kw):
341 341 # only show these line numbers if set
342 342 self.only_lines = kw.pop('only_line_numbers', [])
343 343 self.query_terms = kw.pop('query_terms', [])
344 344 self.max_lines = kw.pop('max_lines', 5)
345 345 self.line_context = kw.pop('line_context', 3)
346 346 self.url = kw.pop('url', None)
347 347
348 348 super(CodeHtmlFormatter, self).__init__(**kw)
349 349
350 350 def _wrap_code(self, source):
351 351 for cnt, it in enumerate(source):
352 352 i, t = it
353 353 t = '<pre>%s</pre>' % t
354 354 yield i, t
355 355
356 356 def _wrap_tablelinenos(self, inner):
357 357 yield 0, '<table class="code-highlight %stable">' % self.cssclass
358 358
359 359 last_shown_line_number = 0
360 360 current_line_number = 1
361 361
362 362 for t, line in inner:
363 363 if not t:
364 364 yield t, line
365 365 continue
366 366
367 367 if current_line_number in self.only_lines:
368 368 if last_shown_line_number + 1 != current_line_number:
369 369 yield 0, '<tr>'
370 370 yield 0, '<td class="line">...</td>'
371 371 yield 0, '<td id="hlcode" class="code"></td>'
372 372 yield 0, '</tr>'
373 373
374 374 yield 0, '<tr>'
375 375 if self.url:
376 376 yield 0, '<td class="line"><a href="%s#L%i">%i</a></td>' % (
377 377 self.url, current_line_number, current_line_number)
378 378 else:
379 379 yield 0, '<td class="line"><a href="">%i</a></td>' % (
380 380 current_line_number)
381 381 yield 0, '<td id="hlcode" class="code">' + line + '</td>'
382 382 yield 0, '</tr>'
383 383
384 384 last_shown_line_number = current_line_number
385 385
386 386 current_line_number += 1
387 387
388 388
389 389 yield 0, '</table>'
390 390
391 391
392 392 def extract_phrases(text_query):
393 393 """
394 394 Extracts phrases from search term string making sure phrases
395 395 contained in double quotes are kept together - and discarding empty values
396 396 or fully whitespace values eg.
397 397
398 398 'some text "a phrase" more' => ['some', 'text', 'a phrase', 'more']
399 399
400 400 """
401 401
402 402 in_phrase = False
403 403 buf = ''
404 404 phrases = []
405 405 for char in text_query:
406 406 if in_phrase:
407 407 if char == '"': # end phrase
408 408 phrases.append(buf)
409 409 buf = ''
410 410 in_phrase = False
411 411 continue
412 412 else:
413 413 buf += char
414 414 continue
415 415 else:
416 416 if char == '"': # start phrase
417 417 in_phrase = True
418 418 phrases.append(buf)
419 419 buf = ''
420 420 continue
421 421 elif char == ' ':
422 422 phrases.append(buf)
423 423 buf = ''
424 424 continue
425 425 else:
426 426 buf += char
427 427
428 428 phrases.append(buf)
429 429 phrases = [phrase.strip() for phrase in phrases if phrase.strip()]
430 430 return phrases
431 431
432 432
433 433 def get_matching_offsets(text, phrases):
434 434 """
435 435 Returns a list of string offsets in `text` that the list of `terms` match
436 436
437 437 >>> get_matching_offsets('some text here', ['some', 'here'])
438 438 [(0, 4), (10, 14)]
439 439
440 440 """
441 441 offsets = []
442 442 for phrase in phrases:
443 443 for match in re.finditer(phrase, text):
444 444 offsets.append((match.start(), match.end()))
445 445
446 446 return offsets
447 447
448 448
449 449 def normalize_text_for_matching(x):
450 450 """
451 451 Replaces all non alnum characters to spaces and lower cases the string,
452 452 useful for comparing two text strings without punctuation
453 453 """
454 454 return re.sub(r'[^\w]', ' ', x.lower())
455 455
456 456
457 457 def get_matching_line_offsets(lines, terms):
458 458 """ Return a set of `lines` indices (starting from 1) matching a
459 459 text search query, along with `context` lines above/below matching lines
460 460
461 461 :param lines: list of strings representing lines
462 462 :param terms: search term string to match in lines eg. 'some text'
463 463 :param context: number of lines above/below a matching line to add to result
464 464 :param max_lines: cut off for lines of interest
465 465 eg.
466 466
467 467 text = '''
468 468 words words words
469 469 words words words
470 470 some text some
471 471 words words words
472 472 words words words
473 473 text here what
474 474 '''
475 475 get_matching_line_offsets(text, 'text', context=1)
476 476 {3: [(5, 9)], 6: [(0, 4)]]
477 477
478 478 """
479 479 matching_lines = {}
480 480 phrases = [normalize_text_for_matching(phrase)
481 481 for phrase in extract_phrases(terms)]
482 482
483 483 for line_index, line in enumerate(lines, start=1):
484 484 match_offsets = get_matching_offsets(
485 485 normalize_text_for_matching(line), phrases)
486 486 if match_offsets:
487 487 matching_lines[line_index] = match_offsets
488 488
489 489 return matching_lines
490 490
491 491
492 492 def hsv_to_rgb(h, s, v):
493 493 """ Convert hsv color values to rgb """
494 494
495 495 if s == 0.0:
496 496 return v, v, v
497 497 i = int(h * 6.0) # XXX assume int() truncates!
498 498 f = (h * 6.0) - i
499 499 p = v * (1.0 - s)
500 500 q = v * (1.0 - s * f)
501 501 t = v * (1.0 - s * (1.0 - f))
502 502 i = i % 6
503 503 if i == 0:
504 504 return v, t, p
505 505 if i == 1:
506 506 return q, v, p
507 507 if i == 2:
508 508 return p, v, t
509 509 if i == 3:
510 510 return p, q, v
511 511 if i == 4:
512 512 return t, p, v
513 513 if i == 5:
514 514 return v, p, q
515 515
516 516
517 517 def unique_color_generator(n=10000, saturation=0.10, lightness=0.95):
518 518 """
519 519 Generator for getting n of evenly distributed colors using
520 520 hsv color and golden ratio. It always return same order of colors
521 521
522 522 :param n: number of colors to generate
523 523 :param saturation: saturation of returned colors
524 524 :param lightness: lightness of returned colors
525 525 :returns: RGB tuple
526 526 """
527 527
528 528 golden_ratio = 0.618033988749895
529 529 h = 0.22717784590367374
530 530
531 531 for _ in xrange(n):
532 532 h += golden_ratio
533 533 h %= 1
534 534 HSV_tuple = [h, saturation, lightness]
535 535 RGB_tuple = hsv_to_rgb(*HSV_tuple)
536 536 yield map(lambda x: str(int(x * 256)), RGB_tuple)
537 537
538 538
539 539 def color_hasher(n=10000, saturation=0.10, lightness=0.95):
540 540 """
541 541 Returns a function which when called with an argument returns a unique
542 542 color for that argument, eg.
543 543
544 544 :param n: number of colors to generate
545 545 :param saturation: saturation of returned colors
546 546 :param lightness: lightness of returned colors
547 547 :returns: css RGB string
548 548
549 549 >>> color_hash = color_hasher()
550 550 >>> color_hash('hello')
551 551 'rgb(34, 12, 59)'
552 552 >>> color_hash('hello')
553 553 'rgb(34, 12, 59)'
554 554 >>> color_hash('other')
555 555 'rgb(90, 224, 159)'
556 556 """
557 557
558 558 color_dict = {}
559 559 cgenerator = unique_color_generator(
560 560 saturation=saturation, lightness=lightness)
561 561
562 562 def get_color_string(thing):
563 563 if thing in color_dict:
564 564 col = color_dict[thing]
565 565 else:
566 566 col = color_dict[thing] = cgenerator.next()
567 567 return "rgb(%s)" % (', '.join(col))
568 568
569 569 return get_color_string
570 570
571 571
572 572 def get_lexer_safe(mimetype=None, filepath=None):
573 573 """
574 574 Tries to return a relevant pygments lexer using mimetype/filepath name,
575 575 defaulting to plain text if none could be found
576 576 """
577 577 lexer = None
578 578 try:
579 579 if mimetype:
580 580 lexer = get_lexer_for_mimetype(mimetype)
581 581 if not lexer:
582 582 lexer = get_lexer_for_filename(filepath)
583 583 except pygments.util.ClassNotFound:
584 584 pass
585 585
586 586 if not lexer:
587 587 lexer = get_lexer_by_name('text')
588 588
589 589 return lexer
590 590
591 591
592 592 def get_lexer_for_filenode(filenode):
593 593 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
594 594 return lexer
595 595
596 596
597 597 def pygmentize(filenode, **kwargs):
598 598 """
599 599 pygmentize function using pygments
600 600
601 601 :param filenode:
602 602 """
603 603 lexer = get_lexer_for_filenode(filenode)
604 604 return literal(code_highlight(filenode.content, lexer,
605 605 CodeHtmlFormatter(**kwargs)))
606 606
607 607
608 608 def is_following_repo(repo_name, user_id):
609 609 from rhodecode.model.scm import ScmModel
610 610 return ScmModel().is_following_repo(repo_name, user_id)
611 611
612 612
613 613 class _Message(object):
614 614 """A message returned by ``Flash.pop_messages()``.
615 615
616 616 Converting the message to a string returns the message text. Instances
617 617 also have the following attributes:
618 618
619 619 * ``message``: the message text.
620 620 * ``category``: the category specified when the message was created.
621 621 """
622 622
623 623 def __init__(self, category, message):
624 624 self.category = category
625 625 self.message = message
626 626
627 627 def __str__(self):
628 628 return self.message
629 629
630 630 __unicode__ = __str__
631 631
632 632 def __html__(self):
633 633 return escape(safe_unicode(self.message))
634 634
635 635
636 636 class Flash(object):
637 637 # List of allowed categories. If None, allow any category.
638 638 categories = ["warning", "notice", "error", "success"]
639 639
640 640 # Default category if none is specified.
641 641 default_category = "notice"
642 642
643 643 def __init__(self, session_key="flash", categories=None,
644 644 default_category=None):
645 645 """
646 646 Instantiate a ``Flash`` object.
647 647
648 648 ``session_key`` is the key to save the messages under in the user's
649 649 session.
650 650
651 651 ``categories`` is an optional list which overrides the default list
652 652 of categories.
653 653
654 654 ``default_category`` overrides the default category used for messages
655 655 when none is specified.
656 656 """
657 657 self.session_key = session_key
658 658 if categories is not None:
659 659 self.categories = categories
660 660 if default_category is not None:
661 661 self.default_category = default_category
662 662 if self.categories and self.default_category not in self.categories:
663 663 raise ValueError(
664 664 "unrecognized default category %r" % (self.default_category,))
665 665
666 666 def pop_messages(self, session=None, request=None):
667 667 """
668 668 Return all accumulated messages and delete them from the session.
669 669
670 670 The return value is a list of ``Message`` objects.
671 671 """
672 672 messages = []
673 673
674 674 if not session:
675 675 if not request:
676 676 request = get_current_request()
677 677 session = request.session
678 678
679 679 # Pop the 'old' pylons flash messages. They are tuples of the form
680 680 # (category, message)
681 681 for cat, msg in session.pop(self.session_key, []):
682 682 messages.append(_Message(cat, msg))
683 683
684 684 # Pop the 'new' pyramid flash messages for each category as list
685 685 # of strings.
686 686 for cat in self.categories:
687 687 for msg in session.pop_flash(queue=cat):
688 688 messages.append(_Message(cat, msg))
689 689 # Map messages from the default queue to the 'notice' category.
690 690 for msg in session.pop_flash():
691 691 messages.append(_Message('notice', msg))
692 692
693 693 session.save()
694 694 return messages
695 695
696 696 def json_alerts(self, session=None, request=None):
697 697 payloads = []
698 698 messages = flash.pop_messages(session=session, request=request)
699 699 if messages:
700 700 for message in messages:
701 701 subdata = {}
702 702 if hasattr(message.message, 'rsplit'):
703 703 flash_data = message.message.rsplit('|DELIM|', 1)
704 704 org_message = flash_data[0]
705 705 if len(flash_data) > 1:
706 706 subdata = json.loads(flash_data[1])
707 707 else:
708 708 org_message = message.message
709 709 payloads.append({
710 710 'message': {
711 711 'message': u'{}'.format(org_message),
712 712 'level': message.category,
713 713 'force': True,
714 714 'subdata': subdata
715 715 }
716 716 })
717 717 return json.dumps(payloads)
718 718
719 719 def __call__(self, message, category=None, ignore_duplicate=False,
720 720 session=None, request=None):
721 721
722 722 if not session:
723 723 if not request:
724 724 request = get_current_request()
725 725 session = request.session
726 726
727 727 session.flash(
728 728 message, queue=category, allow_duplicate=not ignore_duplicate)
729 729
730 730
731 731 flash = Flash()
732 732
733 733 #==============================================================================
734 734 # SCM FILTERS available via h.
735 735 #==============================================================================
736 736 from rhodecode.lib.vcs.utils import author_name, author_email
737 737 from rhodecode.lib.utils2 import credentials_filter, age as _age
738 738 from rhodecode.model.db import User, ChangesetStatus
739 739
740 740 age = _age
741 741 capitalize = lambda x: x.capitalize()
742 742 email = author_email
743 743 short_id = lambda x: x[:12]
744 744 hide_credentials = lambda x: ''.join(credentials_filter(x))
745 745
746 746
747 747 import pytz
748 748 import tzlocal
749 749 local_timezone = tzlocal.get_localzone()
750 750
751 751
752 752 def age_component(datetime_iso, value=None, time_is_local=False):
753 753 title = value or format_date(datetime_iso)
754 754 tzinfo = '+00:00'
755 755
756 756 # detect if we have a timezone info, otherwise, add it
757 757 if time_is_local and isinstance(datetime_iso, datetime) and not datetime_iso.tzinfo:
758 758 force_timezone = os.environ.get('RC_TIMEZONE', '')
759 759 if force_timezone:
760 760 force_timezone = pytz.timezone(force_timezone)
761 761 timezone = force_timezone or local_timezone
762 762 offset = timezone.localize(datetime_iso).strftime('%z')
763 763 tzinfo = '{}:{}'.format(offset[:-2], offset[-2:])
764 764
765 765 return literal(
766 766 '<time class="timeago tooltip" '
767 767 'title="{1}{2}" datetime="{0}{2}">{1}</time>'.format(
768 768 datetime_iso, title, tzinfo))
769 769
770 770
771 771 def _shorten_commit_id(commit_id):
772 772 from rhodecode import CONFIG
773 773 def_len = safe_int(CONFIG.get('rhodecode_show_sha_length', 12))
774 774 return commit_id[:def_len]
775 775
776 776
777 777 def show_id(commit):
778 778 """
779 779 Configurable function that shows ID
780 780 by default it's r123:fffeeefffeee
781 781
782 782 :param commit: commit instance
783 783 """
784 784 from rhodecode import CONFIG
785 785 show_idx = str2bool(CONFIG.get('rhodecode_show_revision_number', True))
786 786
787 787 raw_id = _shorten_commit_id(commit.raw_id)
788 788 if show_idx:
789 789 return 'r%s:%s' % (commit.idx, raw_id)
790 790 else:
791 791 return '%s' % (raw_id, )
792 792
793 793
794 794 def format_date(date):
795 795 """
796 796 use a standardized formatting for dates used in RhodeCode
797 797
798 798 :param date: date/datetime object
799 799 :return: formatted date
800 800 """
801 801
802 802 if date:
803 803 _fmt = "%a, %d %b %Y %H:%M:%S"
804 804 return safe_unicode(date.strftime(_fmt))
805 805
806 806 return u""
807 807
808 808
809 809 class _RepoChecker(object):
810 810
811 811 def __init__(self, backend_alias):
812 812 self._backend_alias = backend_alias
813 813
814 814 def __call__(self, repository):
815 815 if hasattr(repository, 'alias'):
816 816 _type = repository.alias
817 817 elif hasattr(repository, 'repo_type'):
818 818 _type = repository.repo_type
819 819 else:
820 820 _type = repository
821 821 return _type == self._backend_alias
822 822
823 823 is_git = _RepoChecker('git')
824 824 is_hg = _RepoChecker('hg')
825 825 is_svn = _RepoChecker('svn')
826 826
827 827
828 828 def get_repo_type_by_name(repo_name):
829 829 repo = Repository.get_by_repo_name(repo_name)
830 830 return repo.repo_type
831 831
832 832
833 833 def is_svn_without_proxy(repository):
834 834 if is_svn(repository):
835 835 from rhodecode.model.settings import VcsSettingsModel
836 836 conf = VcsSettingsModel().get_ui_settings_as_config_obj()
837 837 return not str2bool(conf.get('vcs_svn_proxy', 'http_requests_enabled'))
838 838 return False
839 839
840 840
841 841 def discover_user(author):
842 842 """
843 843 Tries to discover RhodeCode User based on the autho string. Author string
844 844 is typically `FirstName LastName <email@address.com>`
845 845 """
846 846
847 847 # if author is already an instance use it for extraction
848 848 if isinstance(author, User):
849 849 return author
850 850
851 851 # Valid email in the attribute passed, see if they're in the system
852 852 _email = author_email(author)
853 853 if _email != '':
854 854 user = User.get_by_email(_email, case_insensitive=True, cache=True)
855 855 if user is not None:
856 856 return user
857 857
858 858 # Maybe it's a username, we try to extract it and fetch by username ?
859 859 _author = author_name(author)
860 860 user = User.get_by_username(_author, case_insensitive=True, cache=True)
861 861 if user is not None:
862 862 return user
863 863
864 864 return None
865 865
866 866
867 867 def email_or_none(author):
868 868 # extract email from the commit string
869 869 _email = author_email(author)
870 870
871 871 # If we have an email, use it, otherwise
872 872 # see if it contains a username we can get an email from
873 873 if _email != '':
874 874 return _email
875 875 else:
876 876 user = User.get_by_username(
877 877 author_name(author), case_insensitive=True, cache=True)
878 878
879 879 if user is not None:
880 880 return user.email
881 881
882 882 # No valid email, not a valid user in the system, none!
883 883 return None
884 884
885 885
886 886 def link_to_user(author, length=0, **kwargs):
887 887 user = discover_user(author)
888 888 # user can be None, but if we have it already it means we can re-use it
889 889 # in the person() function, so we save 1 intensive-query
890 890 if user:
891 891 author = user
892 892
893 893 display_person = person(author, 'username_or_name_or_email')
894 894 if length:
895 895 display_person = shorter(display_person, length)
896 896
897 897 if user:
898 898 return link_to(
899 899 escape(display_person),
900 900 route_path('user_profile', username=user.username),
901 901 **kwargs)
902 902 else:
903 903 return escape(display_person)
904 904
905 905
906 906 def link_to_group(users_group_name, **kwargs):
907 907 return link_to(
908 908 escape(users_group_name),
909 909 route_path('user_group_profile', user_group_name=users_group_name),
910 910 **kwargs)
911 911
912 912
913 913 def person(author, show_attr="username_and_name"):
914 914 user = discover_user(author)
915 915 if user:
916 916 return getattr(user, show_attr)
917 917 else:
918 918 _author = author_name(author)
919 919 _email = email(author)
920 920 return _author or _email
921 921
922 922
923 923 def author_string(email):
924 924 if email:
925 925 user = User.get_by_email(email, case_insensitive=True, cache=True)
926 926 if user:
927 927 if user.first_name or user.last_name:
928 928 return '%s %s &lt;%s&gt;' % (
929 929 user.first_name, user.last_name, email)
930 930 else:
931 931 return email
932 932 else:
933 933 return email
934 934 else:
935 935 return None
936 936
937 937
938 938 def person_by_id(id_, show_attr="username_and_name"):
939 939 # attr to return from fetched user
940 940 person_getter = lambda usr: getattr(usr, show_attr)
941 941
942 942 #maybe it's an ID ?
943 943 if str(id_).isdigit() or isinstance(id_, int):
944 944 id_ = int(id_)
945 945 user = User.get(id_)
946 946 if user is not None:
947 947 return person_getter(user)
948 948 return id_
949 949
950 950
951 951 def gravatar_with_user(request, author, show_disabled=False):
952 952 _render = request.get_partial_renderer(
953 953 'rhodecode:templates/base/base.mako')
954 954 return _render('gravatar_with_user', author, show_disabled=show_disabled)
955 955
956 956
957 957 tags_paterns = OrderedDict((
958 958 ('lang', (re.compile(r'\[(lang|language)\ \=\&gt;\ *([a-zA-Z\-\/\#\+\.]*)\]'),
959 959 '<div class="metatag" tag="lang">\\2</div>')),
960 960
961 961 ('see', (re.compile(r'\[see\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]'),
962 962 '<div class="metatag" tag="see">see: \\1 </div>')),
963 963
964 964 ('url', (re.compile(r'\[url\ \=\&gt;\ \[([a-zA-Z0-9\ \.\-\_]+)\]\((http://|https://|/)(.*?)\)\]'),
965 965 '<div class="metatag" tag="url"> <a href="\\2\\3">\\1</a> </div>')),
966 966
967 967 ('license', (re.compile(r'\[license\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]'),
968 968 '<div class="metatag" tag="license"><a href="http:\/\/www.opensource.org/licenses/\\1">\\1</a></div>')),
969 969
970 970 ('ref', (re.compile(r'\[(requires|recommends|conflicts|base)\ \=\&gt;\ *([a-zA-Z0-9\-\/]*)\]'),
971 971 '<div class="metatag" tag="ref \\1">\\1: <a href="/\\2">\\2</a></div>')),
972 972
973 973 ('state', (re.compile(r'\[(stable|featured|stale|dead|dev|deprecated)\]'),
974 974 '<div class="metatag" tag="state \\1">\\1</div>')),
975 975
976 976 # label in grey
977 977 ('label', (re.compile(r'\[([a-z]+)\]'),
978 978 '<div class="metatag" tag="label">\\1</div>')),
979 979
980 980 # generic catch all in grey
981 981 ('generic', (re.compile(r'\[([a-zA-Z0-9\.\-\_]+)\]'),
982 982 '<div class="metatag" tag="generic">\\1</div>')),
983 983 ))
984 984
985 985
986 986 def extract_metatags(value):
987 987 """
988 988 Extract supported meta-tags from given text value
989 989 """
990 990 tags = []
991 991 if not value:
992 992 return tags, ''
993 993
994 994 for key, val in tags_paterns.items():
995 995 pat, replace_html = val
996 996 tags.extend([(key, x.group()) for x in pat.finditer(value)])
997 997 value = pat.sub('', value)
998 998
999 999 return tags, value
1000 1000
1001 1001
1002 1002 def style_metatag(tag_type, value):
1003 1003 """
1004 1004 converts tags from value into html equivalent
1005 1005 """
1006 1006 if not value:
1007 1007 return ''
1008 1008
1009 1009 html_value = value
1010 1010 tag_data = tags_paterns.get(tag_type)
1011 1011 if tag_data:
1012 1012 pat, replace_html = tag_data
1013 1013 # convert to plain `unicode` instead of a markup tag to be used in
1014 1014 # regex expressions. safe_unicode doesn't work here
1015 1015 html_value = pat.sub(replace_html, unicode(value))
1016 1016
1017 1017 return html_value
1018 1018
1019 1019
1020 1020 def bool2icon(value):
1021 1021 """
1022 1022 Returns boolean value of a given value, represented as html element with
1023 1023 classes that will represent icons
1024 1024
1025 1025 :param value: given value to convert to html node
1026 1026 """
1027 1027
1028 1028 if value: # does bool conversion
1029 1029 return HTML.tag('i', class_="icon-true")
1030 1030 else: # not true as bool
1031 1031 return HTML.tag('i', class_="icon-false")
1032 1032
1033 1033
1034 1034 #==============================================================================
1035 1035 # PERMS
1036 1036 #==============================================================================
1037 1037 from rhodecode.lib.auth import HasPermissionAny, HasPermissionAll, \
1038 1038 HasRepoPermissionAny, HasRepoPermissionAll, HasRepoGroupPermissionAll, \
1039 1039 HasRepoGroupPermissionAny, HasRepoPermissionAnyApi, get_csrf_token, \
1040 1040 csrf_token_key
1041 1041
1042 1042
1043 1043 #==============================================================================
1044 1044 # GRAVATAR URL
1045 1045 #==============================================================================
1046 1046 class InitialsGravatar(object):
1047 1047 def __init__(self, email_address, first_name, last_name, size=30,
1048 1048 background=None, text_color='#fff'):
1049 1049 self.size = size
1050 1050 self.first_name = first_name
1051 1051 self.last_name = last_name
1052 1052 self.email_address = email_address
1053 1053 self.background = background or self.str2color(email_address)
1054 1054 self.text_color = text_color
1055 1055
1056 1056 def get_color_bank(self):
1057 1057 """
1058 1058 returns a predefined list of colors that gravatars can use.
1059 1059 Those are randomized distinct colors that guarantee readability and
1060 1060 uniqueness.
1061 1061
1062 1062 generated with: http://phrogz.net/css/distinct-colors.html
1063 1063 """
1064 1064 return [
1065 1065 '#bf3030', '#a67f53', '#00ff00', '#5989b3', '#392040', '#d90000',
1066 1066 '#402910', '#204020', '#79baf2', '#a700b3', '#bf6060', '#7f5320',
1067 1067 '#008000', '#003059', '#ee00ff', '#ff0000', '#8c4b00', '#007300',
1068 1068 '#005fb3', '#de73e6', '#ff4040', '#ffaa00', '#3df255', '#203140',
1069 1069 '#47004d', '#591616', '#664400', '#59b365', '#0d2133', '#83008c',
1070 1070 '#592d2d', '#bf9f60', '#73e682', '#1d3f73', '#73006b', '#402020',
1071 1071 '#b2862d', '#397341', '#597db3', '#e600d6', '#a60000', '#736039',
1072 1072 '#00b318', '#79aaf2', '#330d30', '#ff8080', '#403010', '#16591f',
1073 1073 '#002459', '#8c4688', '#e50000', '#ffbf40', '#00732e', '#102340',
1074 1074 '#bf60ac', '#8c4646', '#cc8800', '#00a642', '#1d3473', '#b32d98',
1075 1075 '#660e00', '#ffd580', '#80ffb2', '#7391e6', '#733967', '#d97b6c',
1076 1076 '#8c5e00', '#59b389', '#3967e6', '#590047', '#73281d', '#665200',
1077 1077 '#00e67a', '#2d50b3', '#8c2377', '#734139', '#b2982d', '#16593a',
1078 1078 '#001859', '#ff00aa', '#a65e53', '#ffcc00', '#0d3321', '#2d3959',
1079 1079 '#731d56', '#401610', '#4c3d00', '#468c6c', '#002ca6', '#d936a3',
1080 1080 '#d94c36', '#403920', '#36d9a3', '#0d1733', '#592d4a', '#993626',
1081 1081 '#cca300', '#00734d', '#46598c', '#8c005e', '#7f1100', '#8c7000',
1082 1082 '#00a66f', '#7382e6', '#b32d74', '#d9896c', '#ffe680', '#1d7362',
1083 1083 '#364cd9', '#73003d', '#d93a00', '#998a4d', '#59b3a1', '#5965b3',
1084 1084 '#e5007a', '#73341d', '#665f00', '#00b38f', '#0018b3', '#59163a',
1085 1085 '#b2502d', '#bfb960', '#00ffcc', '#23318c', '#a6537f', '#734939',
1086 1086 '#b2a700', '#104036', '#3d3df2', '#402031', '#e56739', '#736f39',
1087 1087 '#79f2ea', '#000059', '#401029', '#4c1400', '#ffee00', '#005953',
1088 1088 '#101040', '#990052', '#402820', '#403d10', '#00ffee', '#0000d9',
1089 1089 '#ff80c4', '#a66953', '#eeff00', '#00ccbe', '#8080ff', '#e673a1',
1090 1090 '#a62c00', '#474d00', '#1a3331', '#46468c', '#733950', '#662900',
1091 1091 '#858c23', '#238c85', '#0f0073', '#b20047', '#d9986c', '#becc00',
1092 1092 '#396f73', '#281d73', '#ff0066', '#ff6600', '#dee673', '#59adb3',
1093 1093 '#6559b3', '#590024', '#b2622d', '#98b32d', '#36ced9', '#332d59',
1094 1094 '#40001a', '#733f1d', '#526600', '#005359', '#242040', '#bf6079',
1095 1095 '#735039', '#cef23d', '#007780', '#5630bf', '#66001b', '#b24700',
1096 1096 '#acbf60', '#1d6273', '#25008c', '#731d34', '#a67453', '#50592d',
1097 1097 '#00ccff', '#6600ff', '#ff0044', '#4c1f00', '#8a994d', '#79daf2',
1098 1098 '#a173e6', '#d93662', '#402310', '#aaff00', '#2d98b3', '#8c40ff',
1099 1099 '#592d39', '#ff8c40', '#354020', '#103640', '#1a0040', '#331a20',
1100 1100 '#331400', '#334d00', '#1d5673', '#583973', '#7f0022', '#4c3626',
1101 1101 '#88cc00', '#36a3d9', '#3d0073', '#d9364c', '#33241a', '#698c23',
1102 1102 '#5995b3', '#300059', '#e57382', '#7f3300', '#366600', '#00aaff',
1103 1103 '#3a1659', '#733941', '#663600', '#74b32d', '#003c59', '#7f53a6',
1104 1104 '#73000f', '#ff8800', '#baf279', '#79caf2', '#291040', '#a6293a',
1105 1105 '#b2742d', '#587339', '#0077b3', '#632699', '#400009', '#d9a66c',
1106 1106 '#294010', '#2d4a59', '#aa00ff', '#4c131b', '#b25f00', '#5ce600',
1107 1107 '#267399', '#a336d9', '#990014', '#664e33', '#86bf60', '#0088ff',
1108 1108 '#7700b3', '#593a16', '#073300', '#1d4b73', '#ac60bf', '#e59539',
1109 1109 '#4f8c46', '#368dd9', '#5c0073'
1110 1110 ]
1111 1111
1112 1112 def rgb_to_hex_color(self, rgb_tuple):
1113 1113 """
1114 1114 Converts an rgb_tuple passed to an hex color.
1115 1115
1116 1116 :param rgb_tuple: tuple with 3 ints represents rgb color space
1117 1117 """
1118 1118 return '#' + ("".join(map(chr, rgb_tuple)).encode('hex'))
1119 1119
1120 1120 def email_to_int_list(self, email_str):
1121 1121 """
1122 1122 Get every byte of the hex digest value of email and turn it to integer.
1123 1123 It's going to be always between 0-255
1124 1124 """
1125 1125 digest = md5_safe(email_str.lower())
1126 1126 return [int(digest[i * 2:i * 2 + 2], 16) for i in range(16)]
1127 1127
1128 1128 def pick_color_bank_index(self, email_str, color_bank):
1129 1129 return self.email_to_int_list(email_str)[0] % len(color_bank)
1130 1130
1131 1131 def str2color(self, email_str):
1132 1132 """
1133 1133 Tries to map in a stable algorithm an email to color
1134 1134
1135 1135 :param email_str:
1136 1136 """
1137 1137 color_bank = self.get_color_bank()
1138 1138 # pick position (module it's length so we always find it in the
1139 1139 # bank even if it's smaller than 256 values
1140 1140 pos = self.pick_color_bank_index(email_str, color_bank)
1141 1141 return color_bank[pos]
1142 1142
1143 1143 def normalize_email(self, email_address):
1144 1144 import unicodedata
1145 1145 # default host used to fill in the fake/missing email
1146 1146 default_host = u'localhost'
1147 1147
1148 1148 if not email_address:
1149 1149 email_address = u'%s@%s' % (User.DEFAULT_USER, default_host)
1150 1150
1151 1151 email_address = safe_unicode(email_address)
1152 1152
1153 1153 if u'@' not in email_address:
1154 1154 email_address = u'%s@%s' % (email_address, default_host)
1155 1155
1156 1156 if email_address.endswith(u'@'):
1157 1157 email_address = u'%s%s' % (email_address, default_host)
1158 1158
1159 1159 email_address = unicodedata.normalize('NFKD', email_address)\
1160 1160 .encode('ascii', 'ignore')
1161 1161 return email_address
1162 1162
1163 1163 def get_initials(self):
1164 1164 """
1165 1165 Returns 2 letter initials calculated based on the input.
1166 1166 The algorithm picks first given email address, and takes first letter
1167 1167 of part before @, and then the first letter of server name. In case
1168 1168 the part before @ is in a format of `somestring.somestring2` it replaces
1169 1169 the server letter with first letter of somestring2
1170 1170
1171 1171 In case function was initialized with both first and lastname, this
1172 1172 overrides the extraction from email by first letter of the first and
1173 1173 last name. We add special logic to that functionality, In case Full name
1174 1174 is compound, like Guido Von Rossum, we use last part of the last name
1175 1175 (Von Rossum) picking `R`.
1176 1176
1177 1177 Function also normalizes the non-ascii characters to they ascii
1178 1178 representation, eg Ą => A
1179 1179 """
1180 1180 import unicodedata
1181 1181 # replace non-ascii to ascii
1182 1182 first_name = unicodedata.normalize(
1183 1183 'NFKD', safe_unicode(self.first_name)).encode('ascii', 'ignore')
1184 1184 last_name = unicodedata.normalize(
1185 1185 'NFKD', safe_unicode(self.last_name)).encode('ascii', 'ignore')
1186 1186
1187 1187 # do NFKD encoding, and also make sure email has proper format
1188 1188 email_address = self.normalize_email(self.email_address)
1189 1189
1190 1190 # first push the email initials
1191 1191 prefix, server = email_address.split('@', 1)
1192 1192
1193 1193 # check if prefix is maybe a 'first_name.last_name' syntax
1194 1194 _dot_split = prefix.rsplit('.', 1)
1195 1195 if len(_dot_split) == 2 and _dot_split[1]:
1196 1196 initials = [_dot_split[0][0], _dot_split[1][0]]
1197 1197 else:
1198 1198 initials = [prefix[0], server[0]]
1199 1199
1200 1200 # then try to replace either first_name or last_name
1201 1201 fn_letter = (first_name or " ")[0].strip()
1202 1202 ln_letter = (last_name.split(' ', 1)[-1] or " ")[0].strip()
1203 1203
1204 1204 if fn_letter:
1205 1205 initials[0] = fn_letter
1206 1206
1207 1207 if ln_letter:
1208 1208 initials[1] = ln_letter
1209 1209
1210 1210 return ''.join(initials).upper()
1211 1211
1212 1212 def get_img_data_by_type(self, font_family, img_type):
1213 1213 default_user = """
1214 1214 <svg xmlns="http://www.w3.org/2000/svg"
1215 1215 version="1.1" x="0px" y="0px" width="{size}" height="{size}"
1216 1216 viewBox="-15 -10 439.165 429.164"
1217 1217
1218 1218 xml:space="preserve"
1219 1219 style="background:{background};" >
1220 1220
1221 1221 <path d="M204.583,216.671c50.664,0,91.74-48.075,
1222 1222 91.74-107.378c0-82.237-41.074-107.377-91.74-107.377
1223 1223 c-50.668,0-91.74,25.14-91.74,107.377C112.844,
1224 1224 168.596,153.916,216.671,
1225 1225 204.583,216.671z" fill="{text_color}"/>
1226 1226 <path d="M407.164,374.717L360.88,
1227 1227 270.454c-2.117-4.771-5.836-8.728-10.465-11.138l-71.83-37.392
1228 1228 c-1.584-0.823-3.502-0.663-4.926,0.415c-20.316,
1229 1229 15.366-44.203,23.488-69.076,23.488c-24.877,
1230 1230 0-48.762-8.122-69.078-23.488
1231 1231 c-1.428-1.078-3.346-1.238-4.93-0.415L58.75,
1232 1232 259.316c-4.631,2.41-8.346,6.365-10.465,11.138L2.001,374.717
1233 1233 c-3.191,7.188-2.537,15.412,1.75,22.005c4.285,
1234 1234 6.592,11.537,10.526,19.4,10.526h362.861c7.863,0,15.117-3.936,
1235 1235 19.402-10.527 C409.699,390.129,
1236 1236 410.355,381.902,407.164,374.717z" fill="{text_color}"/>
1237 1237 </svg>""".format(
1238 1238 size=self.size,
1239 1239 background='#979797', # @grey4
1240 1240 text_color=self.text_color,
1241 1241 font_family=font_family)
1242 1242
1243 1243 return {
1244 1244 "default_user": default_user
1245 1245 }[img_type]
1246 1246
1247 1247 def get_img_data(self, svg_type=None):
1248 1248 """
1249 1249 generates the svg metadata for image
1250 1250 """
1251 1251
1252 1252 font_family = ','.join([
1253 1253 'proximanovaregular',
1254 1254 'Proxima Nova Regular',
1255 1255 'Proxima Nova',
1256 1256 'Arial',
1257 1257 'Lucida Grande',
1258 1258 'sans-serif'
1259 1259 ])
1260 1260 if svg_type:
1261 1261 return self.get_img_data_by_type(font_family, svg_type)
1262 1262
1263 1263 initials = self.get_initials()
1264 1264 img_data = """
1265 1265 <svg xmlns="http://www.w3.org/2000/svg" pointer-events="none"
1266 1266 width="{size}" height="{size}"
1267 1267 style="width: 100%; height: 100%; background-color: {background}"
1268 1268 viewBox="0 0 {size} {size}">
1269 1269 <text text-anchor="middle" y="50%" x="50%" dy="0.35em"
1270 1270 pointer-events="auto" fill="{text_color}"
1271 1271 font-family="{font_family}"
1272 1272 style="font-weight: 400; font-size: {f_size}px;">{text}
1273 1273 </text>
1274 1274 </svg>""".format(
1275 1275 size=self.size,
1276 1276 f_size=self.size/1.85, # scale the text inside the box nicely
1277 1277 background=self.background,
1278 1278 text_color=self.text_color,
1279 1279 text=initials.upper(),
1280 1280 font_family=font_family)
1281 1281
1282 1282 return img_data
1283 1283
1284 1284 def generate_svg(self, svg_type=None):
1285 1285 img_data = self.get_img_data(svg_type)
1286 1286 return "data:image/svg+xml;base64,%s" % img_data.encode('base64')
1287 1287
1288 1288
1289 1289 def initials_gravatar(email_address, first_name, last_name, size=30):
1290 1290 svg_type = None
1291 1291 if email_address == User.DEFAULT_USER_EMAIL:
1292 1292 svg_type = 'default_user'
1293 1293 klass = InitialsGravatar(email_address, first_name, last_name, size)
1294 1294 return klass.generate_svg(svg_type=svg_type)
1295 1295
1296 1296
1297 1297 def gravatar_url(email_address, size=30, request=None):
1298 1298 request = get_current_request()
1299 1299 _use_gravatar = request.call_context.visual.use_gravatar
1300 1300 _gravatar_url = request.call_context.visual.gravatar_url
1301 1301
1302 1302 _gravatar_url = _gravatar_url or User.DEFAULT_GRAVATAR_URL
1303 1303
1304 1304 email_address = email_address or User.DEFAULT_USER_EMAIL
1305 1305 if isinstance(email_address, unicode):
1306 1306 # hashlib crashes on unicode items
1307 1307 email_address = safe_str(email_address)
1308 1308
1309 1309 # empty email or default user
1310 1310 if not email_address or email_address == User.DEFAULT_USER_EMAIL:
1311 1311 return initials_gravatar(User.DEFAULT_USER_EMAIL, '', '', size=size)
1312 1312
1313 1313 if _use_gravatar:
1314 1314 # TODO: Disuse pyramid thread locals. Think about another solution to
1315 1315 # get the host and schema here.
1316 1316 request = get_current_request()
1317 1317 tmpl = safe_str(_gravatar_url)
1318 1318 tmpl = tmpl.replace('{email}', email_address)\
1319 1319 .replace('{md5email}', md5_safe(email_address.lower())) \
1320 1320 .replace('{netloc}', request.host)\
1321 1321 .replace('{scheme}', request.scheme)\
1322 1322 .replace('{size}', safe_str(size))
1323 1323 return tmpl
1324 1324 else:
1325 1325 return initials_gravatar(email_address, '', '', size=size)
1326 1326
1327 1327
1328 1328 class Page(_Page):
1329 1329 """
1330 1330 Custom pager to match rendering style with paginator
1331 1331 """
1332 1332
1333 1333 def _get_pos(self, cur_page, max_page, items):
1334 1334 edge = (items / 2) + 1
1335 1335 if (cur_page <= edge):
1336 1336 radius = max(items / 2, items - cur_page)
1337 1337 elif (max_page - cur_page) < edge:
1338 1338 radius = (items - 1) - (max_page - cur_page)
1339 1339 else:
1340 1340 radius = items / 2
1341 1341
1342 1342 left = max(1, (cur_page - (radius)))
1343 1343 right = min(max_page, cur_page + (radius))
1344 1344 return left, cur_page, right
1345 1345
1346 1346 def _range(self, regexp_match):
1347 1347 """
1348 1348 Return range of linked pages (e.g. '1 2 [3] 4 5 6 7 8').
1349 1349
1350 1350 Arguments:
1351 1351
1352 1352 regexp_match
1353 1353 A "re" (regular expressions) match object containing the
1354 1354 radius of linked pages around the current page in
1355 1355 regexp_match.group(1) as a string
1356 1356
1357 1357 This function is supposed to be called as a callable in
1358 1358 re.sub.
1359 1359
1360 1360 """
1361 1361 radius = int(regexp_match.group(1))
1362 1362
1363 1363 # Compute the first and last page number within the radius
1364 1364 # e.g. '1 .. 5 6 [7] 8 9 .. 12'
1365 1365 # -> leftmost_page = 5
1366 1366 # -> rightmost_page = 9
1367 1367 leftmost_page, _cur, rightmost_page = self._get_pos(self.page,
1368 1368 self.last_page,
1369 1369 (radius * 2) + 1)
1370 1370 nav_items = []
1371 1371
1372 1372 # Create a link to the first page (unless we are on the first page
1373 1373 # or there would be no need to insert '..' spacers)
1374 1374 if self.page != self.first_page and self.first_page < leftmost_page:
1375 1375 nav_items.append(self._pagerlink(self.first_page, self.first_page))
1376 1376
1377 1377 # Insert dots if there are pages between the first page
1378 1378 # and the currently displayed page range
1379 1379 if leftmost_page - self.first_page > 1:
1380 1380 # Wrap in a SPAN tag if nolink_attr is set
1381 1381 text = '..'
1382 1382 if self.dotdot_attr:
1383 1383 text = HTML.span(c=text, **self.dotdot_attr)
1384 1384 nav_items.append(text)
1385 1385
1386 1386 for thispage in xrange(leftmost_page, rightmost_page + 1):
1387 1387 # Hilight the current page number and do not use a link
1388 1388 if thispage == self.page:
1389 1389 text = '%s' % (thispage,)
1390 1390 # Wrap in a SPAN tag if nolink_attr is set
1391 1391 if self.curpage_attr:
1392 1392 text = HTML.span(c=text, **self.curpage_attr)
1393 1393 nav_items.append(text)
1394 1394 # Otherwise create just a link to that page
1395 1395 else:
1396 1396 text = '%s' % (thispage,)
1397 1397 nav_items.append(self._pagerlink(thispage, text))
1398 1398
1399 1399 # Insert dots if there are pages between the displayed
1400 1400 # page numbers and the end of the page range
1401 1401 if self.last_page - rightmost_page > 1:
1402 1402 text = '..'
1403 1403 # Wrap in a SPAN tag if nolink_attr is set
1404 1404 if self.dotdot_attr:
1405 1405 text = HTML.span(c=text, **self.dotdot_attr)
1406 1406 nav_items.append(text)
1407 1407
1408 1408 # Create a link to the very last page (unless we are on the last
1409 1409 # page or there would be no need to insert '..' spacers)
1410 1410 if self.page != self.last_page and rightmost_page < self.last_page:
1411 1411 nav_items.append(self._pagerlink(self.last_page, self.last_page))
1412 1412
1413 1413 ## prerender links
1414 1414 #_page_link = url.current()
1415 1415 #nav_items.append(literal('<link rel="prerender" href="%s?page=%s">' % (_page_link, str(int(self.page)+1))))
1416 1416 #nav_items.append(literal('<link rel="prefetch" href="%s?page=%s">' % (_page_link, str(int(self.page)+1))))
1417 1417 return self.separator.join(nav_items)
1418 1418
1419 1419 def pager(self, format='~2~', page_param='page', partial_param='partial',
1420 1420 show_if_single_page=False, separator=' ', onclick=None,
1421 1421 symbol_first='<<', symbol_last='>>',
1422 1422 symbol_previous='<', symbol_next='>',
1423 1423 link_attr={'class': 'pager_link', 'rel': 'prerender'},
1424 1424 curpage_attr={'class': 'pager_curpage'},
1425 1425 dotdot_attr={'class': 'pager_dotdot'}, **kwargs):
1426 1426
1427 1427 self.curpage_attr = curpage_attr
1428 1428 self.separator = separator
1429 1429 self.pager_kwargs = kwargs
1430 1430 self.page_param = page_param
1431 1431 self.partial_param = partial_param
1432 1432 self.onclick = onclick
1433 1433 self.link_attr = link_attr
1434 1434 self.dotdot_attr = dotdot_attr
1435 1435
1436 1436 # Don't show navigator if there is no more than one page
1437 1437 if self.page_count == 0 or (self.page_count == 1 and not show_if_single_page):
1438 1438 return ''
1439 1439
1440 1440 from string import Template
1441 1441 # Replace ~...~ in token format by range of pages
1442 1442 result = re.sub(r'~(\d+)~', self._range, format)
1443 1443
1444 1444 # Interpolate '%' variables
1445 1445 result = Template(result).safe_substitute({
1446 1446 'first_page': self.first_page,
1447 1447 'last_page': self.last_page,
1448 1448 'page': self.page,
1449 1449 'page_count': self.page_count,
1450 1450 'items_per_page': self.items_per_page,
1451 1451 'first_item': self.first_item,
1452 1452 'last_item': self.last_item,
1453 1453 'item_count': self.item_count,
1454 1454 'link_first': self.page > self.first_page and \
1455 1455 self._pagerlink(self.first_page, symbol_first) or '',
1456 1456 'link_last': self.page < self.last_page and \
1457 1457 self._pagerlink(self.last_page, symbol_last) or '',
1458 1458 'link_previous': self.previous_page and \
1459 1459 self._pagerlink(self.previous_page, symbol_previous) \
1460 1460 or HTML.span(symbol_previous, class_="pg-previous disabled"),
1461 1461 'link_next': self.next_page and \
1462 1462 self._pagerlink(self.next_page, symbol_next) \
1463 1463 or HTML.span(symbol_next, class_="pg-next disabled")
1464 1464 })
1465 1465
1466 1466 return literal(result)
1467 1467
1468 1468
1469 1469 #==============================================================================
1470 1470 # REPO PAGER, PAGER FOR REPOSITORY
1471 1471 #==============================================================================
1472 1472 class RepoPage(Page):
1473 1473
1474 1474 def __init__(self, collection, page=1, items_per_page=20,
1475 1475 item_count=None, url=None, **kwargs):
1476 1476
1477 1477 """Create a "RepoPage" instance. special pager for paging
1478 1478 repository
1479 1479 """
1480 1480 self._url_generator = url
1481 1481
1482 1482 # Safe the kwargs class-wide so they can be used in the pager() method
1483 1483 self.kwargs = kwargs
1484 1484
1485 1485 # Save a reference to the collection
1486 1486 self.original_collection = collection
1487 1487
1488 1488 self.collection = collection
1489 1489
1490 1490 # The self.page is the number of the current page.
1491 1491 # The first page has the number 1!
1492 1492 try:
1493 1493 self.page = int(page) # make it int() if we get it as a string
1494 1494 except (ValueError, TypeError):
1495 1495 self.page = 1
1496 1496
1497 1497 self.items_per_page = items_per_page
1498 1498
1499 1499 # Unless the user tells us how many items the collections has
1500 1500 # we calculate that ourselves.
1501 1501 if item_count is not None:
1502 1502 self.item_count = item_count
1503 1503 else:
1504 1504 self.item_count = len(self.collection)
1505 1505
1506 1506 # Compute the number of the first and last available page
1507 1507 if self.item_count > 0:
1508 1508 self.first_page = 1
1509 1509 self.page_count = int(math.ceil(float(self.item_count) /
1510 1510 self.items_per_page))
1511 1511 self.last_page = self.first_page + self.page_count - 1
1512 1512
1513 1513 # Make sure that the requested page number is the range of
1514 1514 # valid pages
1515 1515 if self.page > self.last_page:
1516 1516 self.page = self.last_page
1517 1517 elif self.page < self.first_page:
1518 1518 self.page = self.first_page
1519 1519
1520 1520 # Note: the number of items on this page can be less than
1521 1521 # items_per_page if the last page is not full
1522 1522 self.first_item = max(0, (self.item_count) - (self.page *
1523 1523 items_per_page))
1524 1524 self.last_item = ((self.item_count - 1) - items_per_page *
1525 1525 (self.page - 1))
1526 1526
1527 1527 self.items = list(self.collection[self.first_item:self.last_item + 1])
1528 1528
1529 1529 # Links to previous and next page
1530 1530 if self.page > self.first_page:
1531 1531 self.previous_page = self.page - 1
1532 1532 else:
1533 1533 self.previous_page = None
1534 1534
1535 1535 if self.page < self.last_page:
1536 1536 self.next_page = self.page + 1
1537 1537 else:
1538 1538 self.next_page = None
1539 1539
1540 1540 # No items available
1541 1541 else:
1542 1542 self.first_page = None
1543 1543 self.page_count = 0
1544 1544 self.last_page = None
1545 1545 self.first_item = None
1546 1546 self.last_item = None
1547 1547 self.previous_page = None
1548 1548 self.next_page = None
1549 1549 self.items = []
1550 1550
1551 1551 # This is a subclass of the 'list' type. Initialise the list now.
1552 1552 list.__init__(self, reversed(self.items))
1553 1553
1554 1554
1555 1555 def breadcrumb_repo_link(repo):
1556 1556 """
1557 1557 Makes a breadcrumbs path link to repo
1558 1558
1559 1559 ex::
1560 1560 group >> subgroup >> repo
1561 1561
1562 1562 :param repo: a Repository instance
1563 1563 """
1564 1564
1565 1565 path = [
1566 1566 link_to(group.name, route_path('repo_group_home', repo_group_name=group.group_name))
1567 1567 for group in repo.groups_with_parents
1568 1568 ] + [
1569 1569 link_to(repo.just_name, route_path('repo_summary', repo_name=repo.repo_name))
1570 1570 ]
1571 1571
1572 1572 return literal(' &raquo; '.join(path))
1573 1573
1574 1574
1575 1575 def format_byte_size_binary(file_size):
1576 1576 """
1577 1577 Formats file/folder sizes to standard.
1578 1578 """
1579 1579 if file_size is None:
1580 1580 file_size = 0
1581 1581
1582 1582 formatted_size = format_byte_size(file_size, binary=True)
1583 1583 return formatted_size
1584 1584
1585 1585
1586 1586 def urlify_text(text_, safe=True):
1587 1587 """
1588 1588 Extrac urls from text and make html links out of them
1589 1589
1590 1590 :param text_:
1591 1591 """
1592 1592
1593 1593 url_pat = re.compile(r'''(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@#.&+]'''
1594 1594 '''|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)''')
1595 1595
1596 1596 def url_func(match_obj):
1597 1597 url_full = match_obj.groups()[0]
1598 1598 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
1599 1599 _newtext = url_pat.sub(url_func, text_)
1600 1600 if safe:
1601 1601 return literal(_newtext)
1602 1602 return _newtext
1603 1603
1604 1604
1605 1605 def urlify_commits(text_, repository):
1606 1606 """
1607 1607 Extract commit ids from text and make link from them
1608 1608
1609 1609 :param text_:
1610 1610 :param repository: repo name to build the URL with
1611 1611 """
1612 1612
1613 1613 URL_PAT = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)')
1614 1614
1615 1615 def url_func(match_obj):
1616 1616 commit_id = match_obj.groups()[1]
1617 1617 pref = match_obj.groups()[0]
1618 1618 suf = match_obj.groups()[2]
1619 1619
1620 1620 tmpl = (
1621 1621 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1622 1622 '%(commit_id)s</a>%(suf)s'
1623 1623 )
1624 1624 return tmpl % {
1625 1625 'pref': pref,
1626 1626 'cls': 'revision-link',
1627 1627 'url': route_url('repo_commit', repo_name=repository,
1628 1628 commit_id=commit_id),
1629 1629 'commit_id': commit_id,
1630 1630 'suf': suf
1631 1631 }
1632 1632
1633 1633 newtext = URL_PAT.sub(url_func, text_)
1634 1634
1635 1635 return newtext
1636 1636
1637 1637
1638 1638 def _process_url_func(match_obj, repo_name, uid, entry,
1639 1639 return_raw_data=False, link_format='html'):
1640 1640 pref = ''
1641 1641 if match_obj.group().startswith(' '):
1642 1642 pref = ' '
1643 1643
1644 1644 issue_id = ''.join(match_obj.groups())
1645 1645
1646 1646 if link_format == 'html':
1647 1647 tmpl = (
1648 1648 '%(pref)s<a class="%(cls)s" href="%(url)s">'
1649 1649 '%(issue-prefix)s%(id-repr)s'
1650 1650 '</a>')
1651 1651 elif link_format == 'rst':
1652 1652 tmpl = '`%(issue-prefix)s%(id-repr)s <%(url)s>`_'
1653 1653 elif link_format == 'markdown':
1654 1654 tmpl = '[%(issue-prefix)s%(id-repr)s](%(url)s)'
1655 1655 else:
1656 1656 raise ValueError('Bad link_format:{}'.format(link_format))
1657 1657
1658 1658 (repo_name_cleaned,
1659 1659 parent_group_name) = RepoGroupModel().\
1660 1660 _get_group_name_and_parent(repo_name)
1661 1661
1662 1662 # variables replacement
1663 1663 named_vars = {
1664 1664 'id': issue_id,
1665 1665 'repo': repo_name,
1666 1666 'repo_name': repo_name_cleaned,
1667 1667 'group_name': parent_group_name
1668 1668 }
1669 1669 # named regex variables
1670 1670 named_vars.update(match_obj.groupdict())
1671 1671 _url = string.Template(entry['url']).safe_substitute(**named_vars)
1672 1672
1673 1673 data = {
1674 1674 'pref': pref,
1675 1675 'cls': 'issue-tracker-link',
1676 1676 'url': _url,
1677 1677 'id-repr': issue_id,
1678 1678 'issue-prefix': entry['pref'],
1679 1679 'serv': entry['url'],
1680 1680 }
1681 1681 if return_raw_data:
1682 1682 return {
1683 1683 'id': issue_id,
1684 1684 'url': _url
1685 1685 }
1686 1686 return tmpl % data
1687 1687
1688 1688
1689 1689 def get_active_pattern_entries(repo_name):
1690 1690 repo = None
1691 1691 if repo_name:
1692 1692 # Retrieving repo_name to avoid invalid repo_name to explode on
1693 1693 # IssueTrackerSettingsModel but still passing invalid name further down
1694 1694 repo = Repository.get_by_repo_name(repo_name, cache=True)
1695 1695
1696 1696 settings_model = IssueTrackerSettingsModel(repo=repo)
1697 1697 active_entries = settings_model.get_settings(cache=True)
1698 1698 return active_entries
1699 1699
1700 1700
1701 1701 def process_patterns(text_string, repo_name, link_format='html',
1702 1702 active_entries=None):
1703 1703
1704 1704 allowed_formats = ['html', 'rst', 'markdown']
1705 1705 if link_format not in allowed_formats:
1706 1706 raise ValueError('Link format can be only one of:{} got {}'.format(
1707 1707 allowed_formats, link_format))
1708 1708
1709 1709 active_entries = active_entries or get_active_pattern_entries(repo_name)
1710 1710 issues_data = []
1711 1711 newtext = text_string
1712 1712
1713 1713 for uid, entry in active_entries.items():
1714 1714 log.debug('found issue tracker entry with uid %s' % (uid,))
1715 1715
1716 1716 if not (entry['pat'] and entry['url']):
1717 1717 log.debug('skipping due to missing data')
1718 1718 continue
1719 1719
1720 1720 log.debug('issue tracker entry: uid: `%s` PAT:%s URL:%s PREFIX:%s'
1721 1721 % (uid, entry['pat'], entry['url'], entry['pref']))
1722 1722
1723 1723 try:
1724 1724 pattern = re.compile(r'%s' % entry['pat'])
1725 1725 except re.error:
1726 1726 log.exception(
1727 1727 'issue tracker pattern: `%s` failed to compile',
1728 1728 entry['pat'])
1729 1729 continue
1730 1730
1731 1731 data_func = partial(
1732 1732 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
1733 1733 return_raw_data=True)
1734 1734
1735 1735 for match_obj in pattern.finditer(text_string):
1736 1736 issues_data.append(data_func(match_obj))
1737 1737
1738 1738 url_func = partial(
1739 1739 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
1740 1740 link_format=link_format)
1741 1741
1742 1742 newtext = pattern.sub(url_func, newtext)
1743 1743 log.debug('processed prefix:uid `%s`' % (uid,))
1744 1744
1745 1745 return newtext, issues_data
1746 1746
1747 1747
1748 1748 def urlify_commit_message(commit_text, repository=None,
1749 1749 active_pattern_entries=None):
1750 1750 """
1751 1751 Parses given text message and makes proper links.
1752 1752 issues are linked to given issue-server, and rest is a commit link
1753 1753
1754 1754 :param commit_text:
1755 1755 :param repository:
1756 1756 """
1757 1757 def escaper(string):
1758 1758 return string.replace('<', '&lt;').replace('>', '&gt;')
1759 1759
1760 1760 newtext = escaper(commit_text)
1761 1761
1762 1762 # extract http/https links and make them real urls
1763 1763 newtext = urlify_text(newtext, safe=False)
1764 1764
1765 1765 # urlify commits - extract commit ids and make link out of them, if we have
1766 1766 # the scope of repository present.
1767 1767 if repository:
1768 1768 newtext = urlify_commits(newtext, repository)
1769 1769
1770 1770 # process issue tracker patterns
1771 1771 newtext, issues = process_patterns(newtext, repository or '',
1772 1772 active_entries=active_pattern_entries)
1773 1773
1774 1774 return literal(newtext)
1775 1775
1776 1776
1777 1777 def render_binary(repo_name, file_obj):
1778 1778 """
1779 1779 Choose how to render a binary file
1780 1780 """
1781 1781 filename = file_obj.name
1782 1782
1783 1783 # images
1784 1784 for ext in ['*.png', '*.jpg', '*.ico', '*.gif']:
1785 1785 if fnmatch.fnmatch(filename, pat=ext):
1786 1786 alt = filename
1787 1787 src = route_path(
1788 1788 'repo_file_raw', repo_name=repo_name,
1789 1789 commit_id=file_obj.commit.raw_id, f_path=file_obj.path)
1790 1790 return literal('<img class="rendered-binary" alt="{}" src="{}">'.format(alt, src))
1791 1791
1792 1792
1793 1793 def renderer_from_filename(filename, exclude=None):
1794 1794 """
1795 1795 choose a renderer based on filename, this works only for text based files
1796 1796 """
1797 1797
1798 1798 # ipython
1799 1799 for ext in ['*.ipynb']:
1800 1800 if fnmatch.fnmatch(filename, pat=ext):
1801 1801 return 'jupyter'
1802 1802
1803 1803 is_markup = MarkupRenderer.renderer_from_filename(filename, exclude=exclude)
1804 1804 if is_markup:
1805 1805 return is_markup
1806 1806 return None
1807 1807
1808 1808
1809 1809 def render(source, renderer='rst', mentions=False, relative_urls=None,
1810 1810 repo_name=None):
1811 1811
1812 1812 def maybe_convert_relative_links(html_source):
1813 1813 if relative_urls:
1814 1814 return relative_links(html_source, relative_urls)
1815 1815 return html_source
1816 1816
1817 if renderer == 'rst':
1817 if renderer == 'plain':
1818 return literal(
1819 MarkupRenderer.plain(source, leading_newline=False))
1820
1821 elif renderer == 'rst':
1818 1822 if repo_name:
1819 1823 # process patterns on comments if we pass in repo name
1820 1824 source, issues = process_patterns(
1821 1825 source, repo_name, link_format='rst')
1822 1826
1823 1827 return literal(
1824 1828 '<div class="rst-block">%s</div>' %
1825 1829 maybe_convert_relative_links(
1826 1830 MarkupRenderer.rst(source, mentions=mentions)))
1831
1827 1832 elif renderer == 'markdown':
1828 1833 if repo_name:
1829 1834 # process patterns on comments if we pass in repo name
1830 1835 source, issues = process_patterns(
1831 1836 source, repo_name, link_format='markdown')
1832 1837
1833 1838 return literal(
1834 1839 '<div class="markdown-block">%s</div>' %
1835 1840 maybe_convert_relative_links(
1836 1841 MarkupRenderer.markdown(source, flavored=True,
1837 1842 mentions=mentions)))
1843
1838 1844 elif renderer == 'jupyter':
1839 1845 return literal(
1840 1846 '<div class="ipynb">%s</div>' %
1841 1847 maybe_convert_relative_links(
1842 1848 MarkupRenderer.jupyter(source)))
1843 1849
1844 1850 # None means just show the file-source
1845 1851 return None
1846 1852
1847 1853
1848 1854 def commit_status(repo, commit_id):
1849 1855 return ChangesetStatusModel().get_status(repo, commit_id)
1850 1856
1851 1857
1852 1858 def commit_status_lbl(commit_status):
1853 1859 return dict(ChangesetStatus.STATUSES).get(commit_status)
1854 1860
1855 1861
1856 1862 def commit_time(repo_name, commit_id):
1857 1863 repo = Repository.get_by_repo_name(repo_name)
1858 1864 commit = repo.get_commit(commit_id=commit_id)
1859 1865 return commit.date
1860 1866
1861 1867
1862 1868 def get_permission_name(key):
1863 1869 return dict(Permission.PERMS).get(key)
1864 1870
1865 1871
1866 1872 def journal_filter_help(request):
1867 1873 _ = request.translate
1868 1874 from rhodecode.lib.audit_logger import ACTIONS
1869 1875 actions = '\n'.join(textwrap.wrap(', '.join(sorted(ACTIONS.keys())), 80))
1870 1876
1871 1877 return _(
1872 1878 'Example filter terms:\n' +
1873 1879 ' repository:vcs\n' +
1874 1880 ' username:marcin\n' +
1875 1881 ' username:(NOT marcin)\n' +
1876 1882 ' action:*push*\n' +
1877 1883 ' ip:127.0.0.1\n' +
1878 1884 ' date:20120101\n' +
1879 1885 ' date:[20120101100000 TO 20120102]\n' +
1880 1886 '\n' +
1881 1887 'Actions: {actions}\n' +
1882 1888 '\n' +
1883 1889 'Generate wildcards using \'*\' character:\n' +
1884 1890 ' "repository:vcs*" - search everything starting with \'vcs\'\n' +
1885 1891 ' "repository:*vcs*" - search for repository containing \'vcs\'\n' +
1886 1892 '\n' +
1887 1893 'Optional AND / OR operators in queries\n' +
1888 1894 ' "repository:vcs OR repository:test"\n' +
1889 1895 ' "username:test AND repository:test*"\n'
1890 1896 ).format(actions=actions)
1891 1897
1892 1898
1893 1899 def search_filter_help(searcher, request):
1894 1900 _ = request.translate
1895 1901
1896 1902 terms = ''
1897 1903 return _(
1898 1904 'Example filter terms for `{searcher}` search:\n' +
1899 1905 '{terms}\n' +
1900 1906 'Generate wildcards using \'*\' character:\n' +
1901 1907 ' "repo_name:vcs*" - search everything starting with \'vcs\'\n' +
1902 1908 ' "repo_name:*vcs*" - search for repository containing \'vcs\'\n' +
1903 1909 '\n' +
1904 1910 'Optional AND / OR operators in queries\n' +
1905 1911 ' "repo_name:vcs OR repo_name:test"\n' +
1906 1912 ' "owner:test AND repo_name:test*"\n' +
1907 1913 'More: {search_doc}'
1908 1914 ).format(searcher=searcher.name,
1909 1915 terms=terms, search_doc=searcher.query_lang_doc)
1910 1916
1911 1917
1912 1918 def not_mapped_error(repo_name):
1913 1919 from rhodecode.translation import _
1914 1920 flash(_('%s repository is not mapped to db perhaps'
1915 1921 ' it was created or renamed from the filesystem'
1916 1922 ' please run the application again'
1917 1923 ' in order to rescan repositories') % repo_name, category='error')
1918 1924
1919 1925
1920 1926 def ip_range(ip_addr):
1921 1927 from rhodecode.model.db import UserIpMap
1922 1928 s, e = UserIpMap._get_ip_range(ip_addr)
1923 1929 return '%s - %s' % (s, e)
1924 1930
1925 1931
1926 1932 def form(url, method='post', needs_csrf_token=True, **attrs):
1927 1933 """Wrapper around webhelpers.tags.form to prevent CSRF attacks."""
1928 1934 if method.lower() != 'get' and needs_csrf_token:
1929 1935 raise Exception(
1930 1936 'Forms to POST/PUT/DELETE endpoints should have (in general) a ' +
1931 1937 'CSRF token. If the endpoint does not require such token you can ' +
1932 1938 'explicitly set the parameter needs_csrf_token to false.')
1933 1939
1934 1940 return wh_form(url, method=method, **attrs)
1935 1941
1936 1942
1937 1943 def secure_form(form_url, method="POST", multipart=False, **attrs):
1938 1944 """Start a form tag that points the action to an url. This
1939 1945 form tag will also include the hidden field containing
1940 1946 the auth token.
1941 1947
1942 1948 The url options should be given either as a string, or as a
1943 1949 ``url()`` function. The method for the form defaults to POST.
1944 1950
1945 1951 Options:
1946 1952
1947 1953 ``multipart``
1948 1954 If set to True, the enctype is set to "multipart/form-data".
1949 1955 ``method``
1950 1956 The method to use when submitting the form, usually either
1951 1957 "GET" or "POST". If "PUT", "DELETE", or another verb is used, a
1952 1958 hidden input with name _method is added to simulate the verb
1953 1959 over POST.
1954 1960
1955 1961 """
1956 1962 from webhelpers.pylonslib.secure_form import insecure_form
1957 1963
1958 1964 if 'request' in attrs:
1959 1965 session = attrs['request'].session
1960 1966 del attrs['request']
1961 1967 else:
1962 1968 raise ValueError(
1963 1969 'Calling this form requires request= to be passed as argument')
1964 1970
1965 1971 form = insecure_form(form_url, method, multipart, **attrs)
1966 1972 token = literal(
1967 1973 '<input type="hidden" id="{}" name="{}" value="{}">'.format(
1968 1974 csrf_token_key, csrf_token_key, get_csrf_token(session)))
1969 1975
1970 1976 return literal("%s\n%s" % (form, token))
1971 1977
1972 1978
1973 1979 def dropdownmenu(name, selected, options, enable_filter=False, **attrs):
1974 1980 select_html = select(name, selected, options, **attrs)
1975 1981 select2 = """
1976 1982 <script>
1977 1983 $(document).ready(function() {
1978 1984 $('#%s').select2({
1979 1985 containerCssClass: 'drop-menu',
1980 1986 dropdownCssClass: 'drop-menu-dropdown',
1981 1987 dropdownAutoWidth: true%s
1982 1988 });
1983 1989 });
1984 1990 </script>
1985 1991 """
1986 1992 filter_option = """,
1987 1993 minimumResultsForSearch: -1
1988 1994 """
1989 1995 input_id = attrs.get('id') or name
1990 1996 filter_enabled = "" if enable_filter else filter_option
1991 1997 select_script = literal(select2 % (input_id, filter_enabled))
1992 1998
1993 1999 return literal(select_html+select_script)
1994 2000
1995 2001
1996 2002 def get_visual_attr(tmpl_context_var, attr_name):
1997 2003 """
1998 2004 A safe way to get a variable from visual variable of template context
1999 2005
2000 2006 :param tmpl_context_var: instance of tmpl_context, usually present as `c`
2001 2007 :param attr_name: name of the attribute we fetch from the c.visual
2002 2008 """
2003 2009 visual = getattr(tmpl_context_var, 'visual', None)
2004 2010 if not visual:
2005 2011 return
2006 2012 else:
2007 2013 return getattr(visual, attr_name, None)
2008 2014
2009 2015
2010 2016 def get_last_path_part(file_node):
2011 2017 if not file_node.path:
2012 2018 return u''
2013 2019
2014 2020 path = safe_unicode(file_node.path.split('/')[-1])
2015 2021 return u'../' + path
2016 2022
2017 2023
2018 2024 def route_url(*args, **kwargs):
2019 2025 """
2020 2026 Wrapper around pyramids `route_url` (fully qualified url) function.
2021 2027 """
2022 2028 req = get_current_request()
2023 2029 return req.route_url(*args, **kwargs)
2024 2030
2025 2031
2026 2032 def route_path(*args, **kwargs):
2027 2033 """
2028 2034 Wrapper around pyramids `route_path` function.
2029 2035 """
2030 2036 req = get_current_request()
2031 2037 return req.route_path(*args, **kwargs)
2032 2038
2033 2039
2034 2040 def route_path_or_none(*args, **kwargs):
2035 2041 try:
2036 2042 return route_path(*args, **kwargs)
2037 2043 except KeyError:
2038 2044 return None
2039 2045
2040 2046
2041 2047 def current_route_path(request, **kw):
2042 2048 new_args = request.GET.mixed()
2043 2049 new_args.update(kw)
2044 2050 return request.current_route_path(_query=new_args)
2045 2051
2046 2052
2047 2053 def api_call_example(method, args):
2048 2054 """
2049 2055 Generates an API call example via CURL
2050 2056 """
2051 2057 args_json = json.dumps(OrderedDict([
2052 2058 ('id', 1),
2053 2059 ('auth_token', 'SECRET'),
2054 2060 ('method', method),
2055 2061 ('args', args)
2056 2062 ]))
2057 2063 return literal(
2058 2064 "curl {api_url} -X POST -H 'content-type:text/plain' --data-binary '{data}'"
2059 2065 "<br/><br/>SECRET can be found in <a href=\"{token_url}\">auth-tokens</a> page, "
2060 2066 "and needs to be of `api calls` role."
2061 2067 .format(
2062 2068 api_url=route_url('apiv2'),
2063 2069 token_url=route_url('my_account_auth_tokens'),
2064 2070 data=args_json))
2065 2071
2066 2072
2067 2073 def notification_description(notification, request):
2068 2074 """
2069 2075 Generate notification human readable description based on notification type
2070 2076 """
2071 2077 from rhodecode.model.notification import NotificationModel
2072 2078 return NotificationModel().make_description(
2073 2079 notification, translate=request.translate)
2074 2080
2075 2081
2076 2082 def go_import_header(request, db_repo=None):
2077 2083 """
2078 2084 Creates a header for go-import functionality in Go Lang
2079 2085 """
2080 2086
2081 2087 if not db_repo:
2082 2088 return
2083 2089 if 'go-get' not in request.GET:
2084 2090 return
2085 2091
2086 2092 clone_url = db_repo.clone_url()
2087 2093 prefix = re.split(r'^https?:\/\/', clone_url)[-1]
2088 2094 # we have a repo and go-get flag,
2089 2095 return literal('<meta name="go-import" content="{} {} {}">'.format(
2090 2096 prefix, db_repo.repo_type, clone_url))
2091 2097
2092 2098
2093 2099 def reviewer_as_json(*args, **kwargs):
2094 2100 from rhodecode.apps.repository.utils import reviewer_as_json as _reviewer_as_json
2095 2101 return _reviewer_as_json(*args, **kwargs)
@@ -1,515 +1,519 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 """
23 23 Renderer for markup languages with ability to parse using rst or markdown
24 24 """
25 25
26 26 import re
27 27 import os
28 28 import lxml
29 29 import logging
30 30 import urlparse
31 31 import bleach
32 32
33 33 from mako.lookup import TemplateLookup
34 34 from mako.template import Template as MakoTemplate
35 35
36 36 from docutils.core import publish_parts
37 37 from docutils.parsers.rst import directives
38 38 from docutils import writers
39 39 from docutils.writers import html4css1
40 40 import markdown
41 41
42 42 from rhodecode.lib.markdown_ext import GithubFlavoredMarkdownExtension
43 43 from rhodecode.lib.utils2 import (
44 44 safe_str, safe_unicode, md5_safe, MENTIONS_REGEX)
45 45
46 46 log = logging.getLogger(__name__)
47 47
48 48 # default renderer used to generate automated comments
49 49 DEFAULT_COMMENTS_RENDERER = 'rst'
50 50
51 51
52 52 class CustomHTMLTranslator(writers.html4css1.HTMLTranslator):
53 53 """
54 54 Custom HTML Translator used for sandboxing potential
55 55 JS injections in ref links
56 56 """
57 57
58 58 def visit_reference(self, node):
59 59 if 'refuri' in node.attributes:
60 60 refuri = node['refuri']
61 61 if ':' in refuri:
62 62 prefix, link = refuri.lstrip().split(':', 1)
63 63 if prefix == 'javascript':
64 64 # we don't allow javascript type of refs...
65 65 node['refuri'] = 'javascript:alert("SandBoxedJavascript")'
66 66
67 67 # old style class requires this...
68 68 return html4css1.HTMLTranslator.visit_reference(self, node)
69 69
70 70
71 71 class RhodeCodeWriter(writers.html4css1.Writer):
72 72 def __init__(self):
73 73 writers.Writer.__init__(self)
74 74 self.translator_class = CustomHTMLTranslator
75 75
76 76
77 77 def relative_links(html_source, server_paths):
78 78 if not html_source:
79 79 return html_source
80 80
81 81 try:
82 82 from lxml.html import fromstring
83 83 from lxml.html import tostring
84 84 except ImportError:
85 85 log.exception('Failed to import lxml')
86 86 return html_source
87 87
88 88 try:
89 89 doc = lxml.html.fromstring(html_source)
90 90 except Exception:
91 91 return html_source
92 92
93 93 for el in doc.cssselect('img, video'):
94 94 src = el.attrib.get('src')
95 95 if src:
96 96 el.attrib['src'] = relative_path(src, server_paths['raw'])
97 97
98 98 for el in doc.cssselect('a:not(.gfm)'):
99 99 src = el.attrib.get('href')
100 100 if src:
101 101 raw_mode = el.attrib['href'].endswith('?raw=1')
102 102 if raw_mode:
103 103 el.attrib['href'] = relative_path(src, server_paths['raw'])
104 104 else:
105 105 el.attrib['href'] = relative_path(src, server_paths['standard'])
106 106
107 107 return lxml.html.tostring(doc)
108 108
109 109
110 110 def relative_path(path, request_path, is_repo_file=None):
111 111 """
112 112 relative link support, path is a rel path, and request_path is current
113 113 server path (not absolute)
114 114
115 115 e.g.
116 116
117 117 path = '../logo.png'
118 118 request_path= '/repo/files/path/file.md'
119 119 produces: '/repo/files/logo.png'
120 120 """
121 121 # TODO(marcink): unicode/str support ?
122 122 # maybe=> safe_unicode(urllib.quote(safe_str(final_path), '/:'))
123 123
124 124 def dummy_check(p):
125 125 return True # assume default is a valid file path
126 126
127 127 is_repo_file = is_repo_file or dummy_check
128 128 if not path:
129 129 return request_path
130 130
131 131 path = safe_unicode(path)
132 132 request_path = safe_unicode(request_path)
133 133
134 134 if path.startswith((u'data:', u'javascript:', u'#', u':')):
135 135 # skip data, anchor, invalid links
136 136 return path
137 137
138 138 is_absolute = bool(urlparse.urlparse(path).netloc)
139 139 if is_absolute:
140 140 return path
141 141
142 142 if not request_path:
143 143 return path
144 144
145 145 if path.startswith(u'/'):
146 146 path = path[1:]
147 147
148 148 if path.startswith(u'./'):
149 149 path = path[2:]
150 150
151 151 parts = request_path.split('/')
152 152 # compute how deep we need to traverse the request_path
153 153 depth = 0
154 154
155 155 if is_repo_file(request_path):
156 156 # if request path is a VALID file, we use a relative path with
157 157 # one level up
158 158 depth += 1
159 159
160 160 while path.startswith(u'../'):
161 161 depth += 1
162 162 path = path[3:]
163 163
164 164 if depth > 0:
165 165 parts = parts[:-depth]
166 166
167 167 parts.append(path)
168 168 final_path = u'/'.join(parts).lstrip(u'/')
169 169
170 170 return u'/' + final_path
171 171
172 172
173 173 class MarkupRenderer(object):
174 174 RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES = ['include', 'meta', 'raw']
175 175
176 176 MARKDOWN_PAT = re.compile(r'\.(md|mkdn?|mdown|markdown)$', re.IGNORECASE)
177 177 RST_PAT = re.compile(r'\.re?st$', re.IGNORECASE)
178 178 JUPYTER_PAT = re.compile(r'\.(ipynb)$', re.IGNORECASE)
179 179 PLAIN_PAT = re.compile(r'^readme$', re.IGNORECASE)
180 180
181 181 URL_PAT = re.compile(r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]'
182 182 r'|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)')
183 183
184 184 extensions = ['codehilite', 'extra', 'def_list', 'sane_lists']
185 185 output_format = 'html4'
186 186 markdown_renderer = markdown.Markdown(
187 187 extensions, enable_attributes=False, output_format=output_format)
188 188
189 189 markdown_renderer_flavored = markdown.Markdown(
190 190 extensions + [GithubFlavoredMarkdownExtension()],
191 191 enable_attributes=False, output_format=output_format)
192 192
193 193 # extension together with weights. Lower is first means we control how
194 194 # extensions are attached to readme names with those.
195 195 PLAIN_EXTS = [
196 196 # prefer no extension
197 197 ('', 0), # special case that renders READMES names without extension
198 198 ('.text', 2), ('.TEXT', 2),
199 199 ('.txt', 3), ('.TXT', 3)
200 200 ]
201 201
202 202 RST_EXTS = [
203 203 ('.rst', 1), ('.rest', 1),
204 204 ('.RST', 2), ('.REST', 2)
205 205 ]
206 206
207 207 MARKDOWN_EXTS = [
208 208 ('.md', 1), ('.MD', 1),
209 209 ('.mkdn', 2), ('.MKDN', 2),
210 210 ('.mdown', 3), ('.MDOWN', 3),
211 211 ('.markdown', 4), ('.MARKDOWN', 4)
212 212 ]
213 213
214 214 def _detect_renderer(self, source, filename=None):
215 215 """
216 216 runs detection of what renderer should be used for generating html
217 217 from a markup language
218 218
219 219 filename can be also explicitly a renderer name
220 220
221 221 :param source:
222 222 :param filename:
223 223 """
224 224
225 225 if MarkupRenderer.MARKDOWN_PAT.findall(filename):
226 226 detected_renderer = 'markdown'
227 227 elif MarkupRenderer.RST_PAT.findall(filename):
228 228 detected_renderer = 'rst'
229 229 elif MarkupRenderer.JUPYTER_PAT.findall(filename):
230 230 detected_renderer = 'jupyter'
231 231 elif MarkupRenderer.PLAIN_PAT.findall(filename):
232 232 detected_renderer = 'plain'
233 233 else:
234 234 detected_renderer = 'plain'
235 235
236 236 return getattr(MarkupRenderer, detected_renderer)
237 237
238 238 @classmethod
239 239 def bleach_clean(cls, text):
240 240 from .bleach_whitelist import markdown_attrs, markdown_tags
241 241 allowed_tags = markdown_tags
242 242 allowed_attrs = markdown_attrs
243 243 return bleach.clean(text, tags=allowed_tags, attributes=allowed_attrs)
244 244
245 245 @classmethod
246 246 def renderer_from_filename(cls, filename, exclude):
247 247 """
248 248 Detect renderer markdown/rst from filename and optionally use exclude
249 249 list to remove some options. This is mostly used in helpers.
250 250 Returns None when no renderer can be detected.
251 251 """
252 252 def _filter(elements):
253 253 if isinstance(exclude, (list, tuple)):
254 254 return [x for x in elements if x not in exclude]
255 255 return elements
256 256
257 257 if filename.endswith(
258 258 tuple(_filter([x[0] for x in cls.MARKDOWN_EXTS if x[0]]))):
259 259 return 'markdown'
260 260 if filename.endswith(tuple(_filter([x[0] for x in cls.RST_EXTS if x[0]]))):
261 261 return 'rst'
262 262
263 263 return None
264 264
265 265 def render(self, source, filename=None):
266 266 """
267 267 Renders a given filename using detected renderer
268 268 it detects renderers based on file extension or mimetype.
269 269 At last it will just do a simple html replacing new lines with <br/>
270 270
271 271 :param file_name:
272 272 :param source:
273 273 """
274 274
275 275 renderer = self._detect_renderer(source, filename)
276 276 readme_data = renderer(source)
277 277 return readme_data
278 278
279 279 @classmethod
280 280 def _flavored_markdown(cls, text):
281 281 """
282 282 Github style flavored markdown
283 283
284 284 :param text:
285 285 """
286 286
287 287 # Extract pre blocks.
288 288 extractions = {}
289 289
290 290 def pre_extraction_callback(matchobj):
291 291 digest = md5_safe(matchobj.group(0))
292 292 extractions[digest] = matchobj.group(0)
293 293 return "{gfm-extraction-%s}" % digest
294 294 pattern = re.compile(r'<pre>.*?</pre>', re.MULTILINE | re.DOTALL)
295 295 text = re.sub(pattern, pre_extraction_callback, text)
296 296
297 297 # Prevent foo_bar_baz from ending up with an italic word in the middle.
298 298 def italic_callback(matchobj):
299 299 s = matchobj.group(0)
300 300 if list(s).count('_') >= 2:
301 301 return s.replace('_', r'\_')
302 302 return s
303 303 text = re.sub(r'^(?! {4}|\t)\w+_\w+_\w[\w_]*', italic_callback, text)
304 304
305 305 # Insert pre block extractions.
306 306 def pre_insert_callback(matchobj):
307 307 return '\n\n' + extractions[matchobj.group(1)]
308 308 text = re.sub(r'\{gfm-extraction-([0-9a-f]{32})\}',
309 309 pre_insert_callback, text)
310 310
311 311 return text
312 312
313 313 @classmethod
314 314 def urlify_text(cls, text):
315 315 def url_func(match_obj):
316 316 url_full = match_obj.groups()[0]
317 317 return '<a href="%(url)s">%(url)s</a>' % ({'url': url_full})
318 318
319 319 return cls.URL_PAT.sub(url_func, text)
320 320
321 321 @classmethod
322 def plain(cls, source, universal_newline=True):
322 def plain(cls, source, universal_newline=True, leading_newline=True):
323 323 source = safe_unicode(source)
324 324 if universal_newline:
325 325 newline = '\n'
326 326 source = newline.join(source.splitlines())
327 327
328 source = cls.urlify_text(source)
329 return '<br />' + source.replace("\n", '<br />')
328 rendered_source = cls.urlify_text(source)
329 source = ''
330 if leading_newline:
331 source += '<br />'
332 source += rendered_source.replace("\n", '<br />')
333 return source
330 334
331 335 @classmethod
332 336 def markdown(cls, source, safe=True, flavored=True, mentions=False,
333 337 clean_html=True):
334 338 """
335 339 returns markdown rendered code cleaned by the bleach library
336 340 """
337 341
338 342 if flavored:
339 343 markdown_renderer = cls.markdown_renderer_flavored
340 344 else:
341 345 markdown_renderer = cls.markdown_renderer
342 346
343 347 if mentions:
344 348 mention_pat = re.compile(MENTIONS_REGEX)
345 349
346 350 def wrapp(match_obj):
347 351 uname = match_obj.groups()[0]
348 352 return ' **@%(uname)s** ' % {'uname': uname}
349 353 mention_hl = mention_pat.sub(wrapp, source).strip()
350 354 # we extracted mentions render with this using Mentions false
351 355 return cls.markdown(mention_hl, safe=safe, flavored=flavored,
352 356 mentions=False)
353 357
354 358 source = safe_unicode(source)
355 359
356 360 try:
357 361 if flavored:
358 362 source = cls._flavored_markdown(source)
359 363 rendered = markdown_renderer.convert(source)
360 364 if clean_html:
361 365 rendered = cls.bleach_clean(rendered)
362 366 return rendered
363 367 except Exception:
364 368 log.exception('Error when rendering Markdown')
365 369 if safe:
366 370 log.debug('Fallback to render in plain mode')
367 371 return cls.plain(source)
368 372 else:
369 373 raise
370 374
371 375 @classmethod
372 376 def rst(cls, source, safe=True, mentions=False, clean_html=False):
373 377 if mentions:
374 378 mention_pat = re.compile(MENTIONS_REGEX)
375 379
376 380 def wrapp(match_obj):
377 381 uname = match_obj.groups()[0]
378 382 return ' **@%(uname)s** ' % {'uname': uname}
379 383 mention_hl = mention_pat.sub(wrapp, source).strip()
380 384 # we extracted mentions render with this using Mentions false
381 385 return cls.rst(mention_hl, safe=safe, mentions=False)
382 386
383 387 source = safe_unicode(source)
384 388 try:
385 389 docutils_settings = dict(
386 390 [(alias, None) for alias in
387 391 cls.RESTRUCTUREDTEXT_DISALLOWED_DIRECTIVES])
388 392
389 393 docutils_settings.update({
390 394 'input_encoding': 'unicode', 'report_level': 4})
391 395
392 396 for k, v in docutils_settings.iteritems():
393 397 directives.register_directive(k, v)
394 398
395 399 parts = publish_parts(source=source,
396 400 writer=RhodeCodeWriter(),
397 401 settings_overrides=docutils_settings)
398 402 rendered = parts["fragment"]
399 403 if clean_html:
400 404 rendered = cls.bleach_clean(rendered)
401 405 return parts['html_title'] + rendered
402 406 except Exception:
403 407 log.exception('Error when rendering RST')
404 408 if safe:
405 409 log.debug('Fallbacking to render in plain mode')
406 410 return cls.plain(source)
407 411 else:
408 412 raise
409 413
410 414 @classmethod
411 415 def jupyter(cls, source, safe=True):
412 416 from rhodecode.lib import helpers
413 417
414 418 from traitlets.config import Config
415 419 import nbformat
416 420 from nbconvert import HTMLExporter
417 421 from nbconvert.preprocessors import Preprocessor
418 422
419 423 class CustomHTMLExporter(HTMLExporter):
420 424 def _template_file_default(self):
421 425 return 'basic'
422 426
423 427 class Sandbox(Preprocessor):
424 428
425 429 def preprocess(self, nb, resources):
426 430 sandbox_text = 'SandBoxed(IPython.core.display.Javascript object)'
427 431 for cell in nb['cells']:
428 432 if safe and 'outputs' in cell:
429 433 for cell_output in cell['outputs']:
430 434 if 'data' in cell_output:
431 435 if 'application/javascript' in cell_output['data']:
432 436 cell_output['data']['text/plain'] = sandbox_text
433 437 cell_output['data'].pop('application/javascript', None)
434 438 return nb, resources
435 439
436 440 def _sanitize_resources(resources):
437 441 """
438 442 Skip/sanitize some of the CSS generated and included in jupyter
439 443 so it doesn't messes up UI so much
440 444 """
441 445
442 446 # TODO(marcink): probably we should replace this with whole custom
443 447 # CSS set that doesn't screw up, but jupyter generated html has some
444 448 # special markers, so it requires Custom HTML exporter template with
445 449 # _default_template_path_default, to achieve that
446 450
447 451 # strip the reset CSS
448 452 resources[0] = resources[0][resources[0].find('/*! Source'):]
449 453 return resources
450 454
451 455 def as_html(notebook):
452 456 conf = Config()
453 457 conf.CustomHTMLExporter.preprocessors = [Sandbox]
454 458 html_exporter = CustomHTMLExporter(config=conf)
455 459
456 460 (body, resources) = html_exporter.from_notebook_node(notebook)
457 461 header = '<!-- ## IPYTHON NOTEBOOK RENDERING ## -->'
458 462 js = MakoTemplate(r'''
459 463 <!-- Load mathjax -->
460 464 <!-- MathJax configuration -->
461 465 <script type="text/x-mathjax-config">
462 466 MathJax.Hub.Config({
463 467 jax: ["input/TeX","output/HTML-CSS", "output/PreviewHTML"],
464 468 extensions: ["tex2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "[Contrib]/a11y/accessibility-menu.js"],
465 469 TeX: {
466 470 extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
467 471 },
468 472 tex2jax: {
469 473 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
470 474 displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
471 475 processEscapes: true,
472 476 processEnvironments: true
473 477 },
474 478 // Center justify equations in code and markdown cells. Elsewhere
475 479 // we use CSS to left justify single line equations in code cells.
476 480 displayAlign: 'center',
477 481 "HTML-CSS": {
478 482 styles: {'.MathJax_Display': {"margin": 0}},
479 483 linebreaks: { automatic: true },
480 484 availableFonts: ["STIX", "TeX"]
481 485 },
482 486 showMathMenu: false
483 487 });
484 488 </script>
485 489 <!-- End of mathjax configuration -->
486 490 <script src="${h.asset('js/src/math_jax/MathJax.js')}"></script>
487 491 ''').render(h=helpers)
488 492
489 493 css = '<style>{}</style>'.format(
490 494 ''.join(_sanitize_resources(resources['inlining']['css'])))
491 495
492 496 body = '\n'.join([header, css, js, body])
493 497 return body, resources
494 498
495 499 notebook = nbformat.reads(source, as_version=4)
496 500 (body, resources) = as_html(notebook)
497 501 return body
498 502
499 503
500 504 class RstTemplateRenderer(object):
501 505
502 506 def __init__(self):
503 507 base = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
504 508 rst_template_dirs = [os.path.join(base, 'templates', 'rst_templates')]
505 509 self.template_store = TemplateLookup(
506 510 directories=rst_template_dirs,
507 511 input_encoding='utf-8',
508 512 imports=['from rhodecode.lib import helpers as h'])
509 513
510 514 def _get_template(self, templatename):
511 515 return self.template_store.get_template(templatename)
512 516
513 517 def render(self, template_name, **kwargs):
514 518 template = self._get_template(template_name)
515 519 return template.render(**kwargs)
@@ -1,4538 +1,4540 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Database Models for RhodeCode Enterprise
23 23 """
24 24
25 25 import re
26 26 import os
27 27 import time
28 28 import hashlib
29 29 import logging
30 30 import datetime
31 31 import warnings
32 32 import ipaddress
33 33 import functools
34 34 import traceback
35 35 import collections
36 36
37 37 from sqlalchemy import (
38 38 or_, and_, not_, func, TypeDecorator, event,
39 39 Index, Sequence, UniqueConstraint, ForeignKey, CheckConstraint, Column,
40 40 Boolean, String, Unicode, UnicodeText, DateTime, Integer, LargeBinary,
41 41 Text, Float, PickleType)
42 42 from sqlalchemy.sql.expression import true, false
43 43 from sqlalchemy.sql.functions import coalesce, count # noqa
44 44 from sqlalchemy.orm import (
45 45 relationship, joinedload, class_mapper, validates, aliased)
46 46 from sqlalchemy.ext.declarative import declared_attr
47 47 from sqlalchemy.ext.hybrid import hybrid_property
48 48 from sqlalchemy.exc import IntegrityError # noqa
49 49 from sqlalchemy.dialects.mysql import LONGTEXT
50 50 from beaker.cache import cache_region
51 51 from zope.cachedescriptors.property import Lazy as LazyProperty
52 52
53 53 from pyramid.threadlocal import get_current_request
54 54
55 55 from rhodecode.translation import _
56 56 from rhodecode.lib.vcs import get_vcs_instance
57 57 from rhodecode.lib.vcs.backends.base import EmptyCommit, Reference
58 58 from rhodecode.lib.utils2 import (
59 59 str2bool, safe_str, get_commit_safe, safe_unicode, sha1_safe,
60 60 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict,
61 61 glob2re, StrictAttributeDict, cleaned_uri)
62 62 from rhodecode.lib.jsonalchemy import MutationObj, MutationList, JsonType, \
63 63 JsonRaw
64 64 from rhodecode.lib.ext_json import json
65 65 from rhodecode.lib.caching_query import FromCache
66 66 from rhodecode.lib.encrypt import AESCipher
67 67
68 68 from rhodecode.model.meta import Base, Session
69 69
70 70 URL_SEP = '/'
71 71 log = logging.getLogger(__name__)
72 72
73 73 # =============================================================================
74 74 # BASE CLASSES
75 75 # =============================================================================
76 76
77 77 # this is propagated from .ini file rhodecode.encrypted_values.secret or
78 78 # beaker.session.secret if first is not set.
79 79 # and initialized at environment.py
80 80 ENCRYPTION_KEY = None
81 81
82 82 # used to sort permissions by types, '#' used here is not allowed to be in
83 83 # usernames, and it's very early in sorted string.printable table.
84 84 PERMISSION_TYPE_SORT = {
85 85 'admin': '####',
86 86 'write': '###',
87 87 'read': '##',
88 88 'none': '#',
89 89 }
90 90
91 91
92 92 def display_user_sort(obj):
93 93 """
94 94 Sort function used to sort permissions in .permissions() function of
95 95 Repository, RepoGroup, UserGroup. Also it put the default user in front
96 96 of all other resources
97 97 """
98 98
99 99 if obj.username == User.DEFAULT_USER:
100 100 return '#####'
101 101 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
102 102 return prefix + obj.username
103 103
104 104
105 105 def display_user_group_sort(obj):
106 106 """
107 107 Sort function used to sort permissions in .permissions() function of
108 108 Repository, RepoGroup, UserGroup. Also it put the default user in front
109 109 of all other resources
110 110 """
111 111
112 112 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
113 113 return prefix + obj.users_group_name
114 114
115 115
116 116 def _hash_key(k):
117 117 return sha1_safe(k)
118 118
119 119
120 120 def in_filter_generator(qry, items, limit=500):
121 121 """
122 122 Splits IN() into multiple with OR
123 123 e.g.::
124 124 cnt = Repository.query().filter(
125 125 or_(
126 126 *in_filter_generator(Repository.repo_id, range(100000))
127 127 )).count()
128 128 """
129 129 if not items:
130 130 # empty list will cause empty query which might cause security issues
131 131 # this can lead to hidden unpleasant results
132 132 items = [-1]
133 133
134 134 parts = []
135 135 for chunk in xrange(0, len(items), limit):
136 136 parts.append(
137 137 qry.in_(items[chunk: chunk + limit])
138 138 )
139 139
140 140 return parts
141 141
142 142
143 143 base_table_args = {
144 144 'extend_existing': True,
145 145 'mysql_engine': 'InnoDB',
146 146 'mysql_charset': 'utf8',
147 147 'sqlite_autoincrement': True
148 148 }
149 149
150 150
151 151 class EncryptedTextValue(TypeDecorator):
152 152 """
153 153 Special column for encrypted long text data, use like::
154 154
155 155 value = Column("encrypted_value", EncryptedValue(), nullable=False)
156 156
157 157 This column is intelligent so if value is in unencrypted form it return
158 158 unencrypted form, but on save it always encrypts
159 159 """
160 160 impl = Text
161 161
162 162 def process_bind_param(self, value, dialect):
163 163 if not value:
164 164 return value
165 165 if value.startswith('enc$aes$') or value.startswith('enc$aes_hmac$'):
166 166 # protect against double encrypting if someone manually starts
167 167 # doing
168 168 raise ValueError('value needs to be in unencrypted format, ie. '
169 169 'not starting with enc$aes')
170 170 return 'enc$aes_hmac$%s' % AESCipher(
171 171 ENCRYPTION_KEY, hmac=True).encrypt(value)
172 172
173 173 def process_result_value(self, value, dialect):
174 174 import rhodecode
175 175
176 176 if not value:
177 177 return value
178 178
179 179 parts = value.split('$', 3)
180 180 if not len(parts) == 3:
181 181 # probably not encrypted values
182 182 return value
183 183 else:
184 184 if parts[0] != 'enc':
185 185 # parts ok but without our header ?
186 186 return value
187 187 enc_strict_mode = str2bool(rhodecode.CONFIG.get(
188 188 'rhodecode.encrypted_values.strict') or True)
189 189 # at that stage we know it's our encryption
190 190 if parts[1] == 'aes':
191 191 decrypted_data = AESCipher(ENCRYPTION_KEY).decrypt(parts[2])
192 192 elif parts[1] == 'aes_hmac':
193 193 decrypted_data = AESCipher(
194 194 ENCRYPTION_KEY, hmac=True,
195 195 strict_verification=enc_strict_mode).decrypt(parts[2])
196 196 else:
197 197 raise ValueError(
198 198 'Encryption type part is wrong, must be `aes` '
199 199 'or `aes_hmac`, got `%s` instead' % (parts[1]))
200 200 return decrypted_data
201 201
202 202
203 203 class BaseModel(object):
204 204 """
205 205 Base Model for all classes
206 206 """
207 207
208 208 @classmethod
209 209 def _get_keys(cls):
210 210 """return column names for this model """
211 211 return class_mapper(cls).c.keys()
212 212
213 213 def get_dict(self):
214 214 """
215 215 return dict with keys and values corresponding
216 216 to this model data """
217 217
218 218 d = {}
219 219 for k in self._get_keys():
220 220 d[k] = getattr(self, k)
221 221
222 222 # also use __json__() if present to get additional fields
223 223 _json_attr = getattr(self, '__json__', None)
224 224 if _json_attr:
225 225 # update with attributes from __json__
226 226 if callable(_json_attr):
227 227 _json_attr = _json_attr()
228 228 for k, val in _json_attr.iteritems():
229 229 d[k] = val
230 230 return d
231 231
232 232 def get_appstruct(self):
233 233 """return list with keys and values tuples corresponding
234 234 to this model data """
235 235
236 236 lst = []
237 237 for k in self._get_keys():
238 238 lst.append((k, getattr(self, k),))
239 239 return lst
240 240
241 241 def populate_obj(self, populate_dict):
242 242 """populate model with data from given populate_dict"""
243 243
244 244 for k in self._get_keys():
245 245 if k in populate_dict:
246 246 setattr(self, k, populate_dict[k])
247 247
248 248 @classmethod
249 249 def query(cls):
250 250 return Session().query(cls)
251 251
252 252 @classmethod
253 253 def get(cls, id_):
254 254 if id_:
255 255 return cls.query().get(id_)
256 256
257 257 @classmethod
258 258 def get_or_404(cls, id_):
259 259 from pyramid.httpexceptions import HTTPNotFound
260 260
261 261 try:
262 262 id_ = int(id_)
263 263 except (TypeError, ValueError):
264 264 raise HTTPNotFound()
265 265
266 266 res = cls.query().get(id_)
267 267 if not res:
268 268 raise HTTPNotFound()
269 269 return res
270 270
271 271 @classmethod
272 272 def getAll(cls):
273 273 # deprecated and left for backward compatibility
274 274 return cls.get_all()
275 275
276 276 @classmethod
277 277 def get_all(cls):
278 278 return cls.query().all()
279 279
280 280 @classmethod
281 281 def delete(cls, id_):
282 282 obj = cls.query().get(id_)
283 283 Session().delete(obj)
284 284
285 285 @classmethod
286 286 def identity_cache(cls, session, attr_name, value):
287 287 exist_in_session = []
288 288 for (item_cls, pkey), instance in session.identity_map.items():
289 289 if cls == item_cls and getattr(instance, attr_name) == value:
290 290 exist_in_session.append(instance)
291 291 if exist_in_session:
292 292 if len(exist_in_session) == 1:
293 293 return exist_in_session[0]
294 294 log.exception(
295 295 'multiple objects with attr %s and '
296 296 'value %s found with same name: %r',
297 297 attr_name, value, exist_in_session)
298 298
299 299 def __repr__(self):
300 300 if hasattr(self, '__unicode__'):
301 301 # python repr needs to return str
302 302 try:
303 303 return safe_str(self.__unicode__())
304 304 except UnicodeDecodeError:
305 305 pass
306 306 return '<DB:%s>' % (self.__class__.__name__)
307 307
308 308
309 309 class RhodeCodeSetting(Base, BaseModel):
310 310 __tablename__ = 'rhodecode_settings'
311 311 __table_args__ = (
312 312 UniqueConstraint('app_settings_name'),
313 313 base_table_args
314 314 )
315 315
316 316 SETTINGS_TYPES = {
317 317 'str': safe_str,
318 318 'int': safe_int,
319 319 'unicode': safe_unicode,
320 320 'bool': str2bool,
321 321 'list': functools.partial(aslist, sep=',')
322 322 }
323 323 DEFAULT_UPDATE_URL = 'https://rhodecode.com/api/v1/info/versions'
324 324 GLOBAL_CONF_KEY = 'app_settings'
325 325
326 326 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
327 327 app_settings_name = Column("app_settings_name", String(255), nullable=True, unique=None, default=None)
328 328 _app_settings_value = Column("app_settings_value", String(4096), nullable=True, unique=None, default=None)
329 329 _app_settings_type = Column("app_settings_type", String(255), nullable=True, unique=None, default=None)
330 330
331 331 def __init__(self, key='', val='', type='unicode'):
332 332 self.app_settings_name = key
333 333 self.app_settings_type = type
334 334 self.app_settings_value = val
335 335
336 336 @validates('_app_settings_value')
337 337 def validate_settings_value(self, key, val):
338 338 assert type(val) == unicode
339 339 return val
340 340
341 341 @hybrid_property
342 342 def app_settings_value(self):
343 343 v = self._app_settings_value
344 344 _type = self.app_settings_type
345 345 if _type:
346 346 _type = self.app_settings_type.split('.')[0]
347 347 # decode the encrypted value
348 348 if 'encrypted' in self.app_settings_type:
349 349 cipher = EncryptedTextValue()
350 350 v = safe_unicode(cipher.process_result_value(v, None))
351 351
352 352 converter = self.SETTINGS_TYPES.get(_type) or \
353 353 self.SETTINGS_TYPES['unicode']
354 354 return converter(v)
355 355
356 356 @app_settings_value.setter
357 357 def app_settings_value(self, val):
358 358 """
359 359 Setter that will always make sure we use unicode in app_settings_value
360 360
361 361 :param val:
362 362 """
363 363 val = safe_unicode(val)
364 364 # encode the encrypted value
365 365 if 'encrypted' in self.app_settings_type:
366 366 cipher = EncryptedTextValue()
367 367 val = safe_unicode(cipher.process_bind_param(val, None))
368 368 self._app_settings_value = val
369 369
370 370 @hybrid_property
371 371 def app_settings_type(self):
372 372 return self._app_settings_type
373 373
374 374 @app_settings_type.setter
375 375 def app_settings_type(self, val):
376 376 if val.split('.')[0] not in self.SETTINGS_TYPES:
377 377 raise Exception('type must be one of %s got %s'
378 378 % (self.SETTINGS_TYPES.keys(), val))
379 379 self._app_settings_type = val
380 380
381 381 def __unicode__(self):
382 382 return u"<%s('%s:%s[%s]')>" % (
383 383 self.__class__.__name__,
384 384 self.app_settings_name, self.app_settings_value,
385 385 self.app_settings_type
386 386 )
387 387
388 388
389 389 class RhodeCodeUi(Base, BaseModel):
390 390 __tablename__ = 'rhodecode_ui'
391 391 __table_args__ = (
392 392 UniqueConstraint('ui_key'),
393 393 base_table_args
394 394 )
395 395
396 396 HOOK_REPO_SIZE = 'changegroup.repo_size'
397 397 # HG
398 398 HOOK_PRE_PULL = 'preoutgoing.pre_pull'
399 399 HOOK_PULL = 'outgoing.pull_logger'
400 400 HOOK_PRE_PUSH = 'prechangegroup.pre_push'
401 401 HOOK_PRETX_PUSH = 'pretxnchangegroup.pre_push'
402 402 HOOK_PUSH = 'changegroup.push_logger'
403 403 HOOK_PUSH_KEY = 'pushkey.key_push'
404 404
405 405 # TODO: johbo: Unify way how hooks are configured for git and hg,
406 406 # git part is currently hardcoded.
407 407
408 408 # SVN PATTERNS
409 409 SVN_BRANCH_ID = 'vcs_svn_branch'
410 410 SVN_TAG_ID = 'vcs_svn_tag'
411 411
412 412 ui_id = Column(
413 413 "ui_id", Integer(), nullable=False, unique=True, default=None,
414 414 primary_key=True)
415 415 ui_section = Column(
416 416 "ui_section", String(255), nullable=True, unique=None, default=None)
417 417 ui_key = Column(
418 418 "ui_key", String(255), nullable=True, unique=None, default=None)
419 419 ui_value = Column(
420 420 "ui_value", String(255), nullable=True, unique=None, default=None)
421 421 ui_active = Column(
422 422 "ui_active", Boolean(), nullable=True, unique=None, default=True)
423 423
424 424 def __repr__(self):
425 425 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
426 426 self.ui_key, self.ui_value)
427 427
428 428
429 429 class RepoRhodeCodeSetting(Base, BaseModel):
430 430 __tablename__ = 'repo_rhodecode_settings'
431 431 __table_args__ = (
432 432 UniqueConstraint(
433 433 'app_settings_name', 'repository_id',
434 434 name='uq_repo_rhodecode_setting_name_repo_id'),
435 435 base_table_args
436 436 )
437 437
438 438 repository_id = Column(
439 439 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
440 440 nullable=False)
441 441 app_settings_id = Column(
442 442 "app_settings_id", Integer(), nullable=False, unique=True,
443 443 default=None, primary_key=True)
444 444 app_settings_name = Column(
445 445 "app_settings_name", String(255), nullable=True, unique=None,
446 446 default=None)
447 447 _app_settings_value = Column(
448 448 "app_settings_value", String(4096), nullable=True, unique=None,
449 449 default=None)
450 450 _app_settings_type = Column(
451 451 "app_settings_type", String(255), nullable=True, unique=None,
452 452 default=None)
453 453
454 454 repository = relationship('Repository')
455 455
456 456 def __init__(self, repository_id, key='', val='', type='unicode'):
457 457 self.repository_id = repository_id
458 458 self.app_settings_name = key
459 459 self.app_settings_type = type
460 460 self.app_settings_value = val
461 461
462 462 @validates('_app_settings_value')
463 463 def validate_settings_value(self, key, val):
464 464 assert type(val) == unicode
465 465 return val
466 466
467 467 @hybrid_property
468 468 def app_settings_value(self):
469 469 v = self._app_settings_value
470 470 type_ = self.app_settings_type
471 471 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
472 472 converter = SETTINGS_TYPES.get(type_) or SETTINGS_TYPES['unicode']
473 473 return converter(v)
474 474
475 475 @app_settings_value.setter
476 476 def app_settings_value(self, val):
477 477 """
478 478 Setter that will always make sure we use unicode in app_settings_value
479 479
480 480 :param val:
481 481 """
482 482 self._app_settings_value = safe_unicode(val)
483 483
484 484 @hybrid_property
485 485 def app_settings_type(self):
486 486 return self._app_settings_type
487 487
488 488 @app_settings_type.setter
489 489 def app_settings_type(self, val):
490 490 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
491 491 if val not in SETTINGS_TYPES:
492 492 raise Exception('type must be one of %s got %s'
493 493 % (SETTINGS_TYPES.keys(), val))
494 494 self._app_settings_type = val
495 495
496 496 def __unicode__(self):
497 497 return u"<%s('%s:%s:%s[%s]')>" % (
498 498 self.__class__.__name__, self.repository.repo_name,
499 499 self.app_settings_name, self.app_settings_value,
500 500 self.app_settings_type
501 501 )
502 502
503 503
504 504 class RepoRhodeCodeUi(Base, BaseModel):
505 505 __tablename__ = 'repo_rhodecode_ui'
506 506 __table_args__ = (
507 507 UniqueConstraint(
508 508 'repository_id', 'ui_section', 'ui_key',
509 509 name='uq_repo_rhodecode_ui_repository_id_section_key'),
510 510 base_table_args
511 511 )
512 512
513 513 repository_id = Column(
514 514 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
515 515 nullable=False)
516 516 ui_id = Column(
517 517 "ui_id", Integer(), nullable=False, unique=True, default=None,
518 518 primary_key=True)
519 519 ui_section = Column(
520 520 "ui_section", String(255), nullable=True, unique=None, default=None)
521 521 ui_key = Column(
522 522 "ui_key", String(255), nullable=True, unique=None, default=None)
523 523 ui_value = Column(
524 524 "ui_value", String(255), nullable=True, unique=None, default=None)
525 525 ui_active = Column(
526 526 "ui_active", Boolean(), nullable=True, unique=None, default=True)
527 527
528 528 repository = relationship('Repository')
529 529
530 530 def __repr__(self):
531 531 return '<%s[%s:%s]%s=>%s]>' % (
532 532 self.__class__.__name__, self.repository.repo_name,
533 533 self.ui_section, self.ui_key, self.ui_value)
534 534
535 535
536 536 class User(Base, BaseModel):
537 537 __tablename__ = 'users'
538 538 __table_args__ = (
539 539 UniqueConstraint('username'), UniqueConstraint('email'),
540 540 Index('u_username_idx', 'username'),
541 541 Index('u_email_idx', 'email'),
542 542 base_table_args
543 543 )
544 544
545 545 DEFAULT_USER = 'default'
546 546 DEFAULT_USER_EMAIL = 'anonymous@rhodecode.org'
547 547 DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
548 548
549 549 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
550 550 username = Column("username", String(255), nullable=True, unique=None, default=None)
551 551 password = Column("password", String(255), nullable=True, unique=None, default=None)
552 552 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
553 553 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
554 554 name = Column("firstname", String(255), nullable=True, unique=None, default=None)
555 555 lastname = Column("lastname", String(255), nullable=True, unique=None, default=None)
556 556 _email = Column("email", String(255), nullable=True, unique=None, default=None)
557 557 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
558 558 last_activity = Column('last_activity', DateTime(timezone=False), nullable=True, unique=None, default=None)
559 559
560 560 extern_type = Column("extern_type", String(255), nullable=True, unique=None, default=None)
561 561 extern_name = Column("extern_name", String(255), nullable=True, unique=None, default=None)
562 562 _api_key = Column("api_key", String(255), nullable=True, unique=None, default=None)
563 563 inherit_default_permissions = Column("inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
564 564 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
565 565 _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data
566 566
567 567 user_log = relationship('UserLog')
568 568 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
569 569
570 570 repositories = relationship('Repository')
571 571 repository_groups = relationship('RepoGroup')
572 572 user_groups = relationship('UserGroup')
573 573
574 574 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
575 575 followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all')
576 576
577 577 repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all')
578 578 repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all')
579 579 user_group_to_perm = relationship('UserUserGroupToPerm', primaryjoin='UserUserGroupToPerm.user_id==User.user_id', cascade='all')
580 580
581 581 group_member = relationship('UserGroupMember', cascade='all')
582 582
583 583 notifications = relationship('UserNotification', cascade='all')
584 584 # notifications assigned to this user
585 585 user_created_notifications = relationship('Notification', cascade='all')
586 586 # comments created by this user
587 587 user_comments = relationship('ChangesetComment', cascade='all')
588 588 # user profile extra info
589 589 user_emails = relationship('UserEmailMap', cascade='all')
590 590 user_ip_map = relationship('UserIpMap', cascade='all')
591 591 user_auth_tokens = relationship('UserApiKeys', cascade='all')
592 592 user_ssh_keys = relationship('UserSshKeys', cascade='all')
593 593
594 594 # gists
595 595 user_gists = relationship('Gist', cascade='all')
596 596 # user pull requests
597 597 user_pull_requests = relationship('PullRequest', cascade='all')
598 598 # external identities
599 599 extenal_identities = relationship(
600 600 'ExternalIdentity',
601 601 primaryjoin="User.user_id==ExternalIdentity.local_user_id",
602 602 cascade='all')
603 603 # review rules
604 604 user_review_rules = relationship('RepoReviewRuleUser', cascade='all')
605 605
606 606 def __unicode__(self):
607 607 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
608 608 self.user_id, self.username)
609 609
610 610 @hybrid_property
611 611 def email(self):
612 612 return self._email
613 613
614 614 @email.setter
615 615 def email(self, val):
616 616 self._email = val.lower() if val else None
617 617
618 618 @hybrid_property
619 619 def first_name(self):
620 620 from rhodecode.lib import helpers as h
621 621 if self.name:
622 622 return h.escape(self.name)
623 623 return self.name
624 624
625 625 @hybrid_property
626 626 def last_name(self):
627 627 from rhodecode.lib import helpers as h
628 628 if self.lastname:
629 629 return h.escape(self.lastname)
630 630 return self.lastname
631 631
632 632 @hybrid_property
633 633 def api_key(self):
634 634 """
635 635 Fetch if exist an auth-token with role ALL connected to this user
636 636 """
637 637 user_auth_token = UserApiKeys.query()\
638 638 .filter(UserApiKeys.user_id == self.user_id)\
639 639 .filter(or_(UserApiKeys.expires == -1,
640 640 UserApiKeys.expires >= time.time()))\
641 641 .filter(UserApiKeys.role == UserApiKeys.ROLE_ALL).first()
642 642 if user_auth_token:
643 643 user_auth_token = user_auth_token.api_key
644 644
645 645 return user_auth_token
646 646
647 647 @api_key.setter
648 648 def api_key(self, val):
649 649 # don't allow to set API key this is deprecated for now
650 650 self._api_key = None
651 651
652 652 @property
653 653 def reviewer_pull_requests(self):
654 654 return PullRequestReviewers.query() \
655 655 .options(joinedload(PullRequestReviewers.pull_request)) \
656 656 .filter(PullRequestReviewers.user_id == self.user_id) \
657 657 .all()
658 658
659 659 @property
660 660 def firstname(self):
661 661 # alias for future
662 662 return self.name
663 663
664 664 @property
665 665 def emails(self):
666 666 other = UserEmailMap.query()\
667 667 .filter(UserEmailMap.user == self) \
668 668 .order_by(UserEmailMap.email_id.asc()) \
669 669 .all()
670 670 return [self.email] + [x.email for x in other]
671 671
672 672 @property
673 673 def auth_tokens(self):
674 674 auth_tokens = self.get_auth_tokens()
675 675 return [x.api_key for x in auth_tokens]
676 676
677 677 def get_auth_tokens(self):
678 678 return UserApiKeys.query()\
679 679 .filter(UserApiKeys.user == self)\
680 680 .order_by(UserApiKeys.user_api_key_id.asc())\
681 681 .all()
682 682
683 683 @LazyProperty
684 684 def feed_token(self):
685 685 return self.get_feed_token()
686 686
687 687 def get_feed_token(self, cache=True):
688 688 feed_tokens = UserApiKeys.query()\
689 689 .filter(UserApiKeys.user == self)\
690 690 .filter(UserApiKeys.role == UserApiKeys.ROLE_FEED)
691 691 if cache:
692 692 feed_tokens = feed_tokens.options(
693 693 FromCache("sql_cache_short", "get_user_feed_token_%s" % self.user_id))
694 694
695 695 feed_tokens = feed_tokens.all()
696 696 if feed_tokens:
697 697 return feed_tokens[0].api_key
698 698 return 'NO_FEED_TOKEN_AVAILABLE'
699 699
700 700 @classmethod
701 701 def get(cls, user_id, cache=False):
702 702 if not user_id:
703 703 return
704 704
705 705 user = cls.query()
706 706 if cache:
707 707 user = user.options(
708 708 FromCache("sql_cache_short", "get_users_%s" % user_id))
709 709 return user.get(user_id)
710 710
711 711 @classmethod
712 712 def extra_valid_auth_tokens(cls, user, role=None):
713 713 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
714 714 .filter(or_(UserApiKeys.expires == -1,
715 715 UserApiKeys.expires >= time.time()))
716 716 if role:
717 717 tokens = tokens.filter(or_(UserApiKeys.role == role,
718 718 UserApiKeys.role == UserApiKeys.ROLE_ALL))
719 719 return tokens.all()
720 720
721 721 def authenticate_by_token(self, auth_token, roles=None, scope_repo_id=None):
722 722 from rhodecode.lib import auth
723 723
724 724 log.debug('Trying to authenticate user: %s via auth-token, '
725 725 'and roles: %s', self, roles)
726 726
727 727 if not auth_token:
728 728 return False
729 729
730 730 crypto_backend = auth.crypto_backend()
731 731
732 732 roles = (roles or []) + [UserApiKeys.ROLE_ALL]
733 733 tokens_q = UserApiKeys.query()\
734 734 .filter(UserApiKeys.user_id == self.user_id)\
735 735 .filter(or_(UserApiKeys.expires == -1,
736 736 UserApiKeys.expires >= time.time()))
737 737
738 738 tokens_q = tokens_q.filter(UserApiKeys.role.in_(roles))
739 739
740 740 plain_tokens = []
741 741 hash_tokens = []
742 742
743 743 for token in tokens_q.all():
744 744 # verify scope first
745 745 if token.repo_id:
746 746 # token has a scope, we need to verify it
747 747 if scope_repo_id != token.repo_id:
748 748 log.debug(
749 749 'Scope mismatch: token has a set repo scope: %s, '
750 750 'and calling scope is:%s, skipping further checks',
751 751 token.repo, scope_repo_id)
752 752 # token has a scope, and it doesn't match, skip token
753 753 continue
754 754
755 755 if token.api_key.startswith(crypto_backend.ENC_PREF):
756 756 hash_tokens.append(token.api_key)
757 757 else:
758 758 plain_tokens.append(token.api_key)
759 759
760 760 is_plain_match = auth_token in plain_tokens
761 761 if is_plain_match:
762 762 return True
763 763
764 764 for hashed in hash_tokens:
765 765 # TODO(marcink): this is expensive to calculate, but most secure
766 766 match = crypto_backend.hash_check(auth_token, hashed)
767 767 if match:
768 768 return True
769 769
770 770 return False
771 771
772 772 @property
773 773 def ip_addresses(self):
774 774 ret = UserIpMap.query().filter(UserIpMap.user == self).all()
775 775 return [x.ip_addr for x in ret]
776 776
777 777 @property
778 778 def username_and_name(self):
779 779 return '%s (%s %s)' % (self.username, self.first_name, self.last_name)
780 780
781 781 @property
782 782 def username_or_name_or_email(self):
783 783 full_name = self.full_name if self.full_name is not ' ' else None
784 784 return self.username or full_name or self.email
785 785
786 786 @property
787 787 def full_name(self):
788 788 return '%s %s' % (self.first_name, self.last_name)
789 789
790 790 @property
791 791 def full_name_or_username(self):
792 792 return ('%s %s' % (self.first_name, self.last_name)
793 793 if (self.first_name and self.last_name) else self.username)
794 794
795 795 @property
796 796 def full_contact(self):
797 797 return '%s %s <%s>' % (self.first_name, self.last_name, self.email)
798 798
799 799 @property
800 800 def short_contact(self):
801 801 return '%s %s' % (self.first_name, self.last_name)
802 802
803 803 @property
804 804 def is_admin(self):
805 805 return self.admin
806 806
807 807 def AuthUser(self, **kwargs):
808 808 """
809 809 Returns instance of AuthUser for this user
810 810 """
811 811 from rhodecode.lib.auth import AuthUser
812 812 return AuthUser(user_id=self.user_id, username=self.username, **kwargs)
813 813
814 814 @hybrid_property
815 815 def user_data(self):
816 816 if not self._user_data:
817 817 return {}
818 818
819 819 try:
820 820 return json.loads(self._user_data)
821 821 except TypeError:
822 822 return {}
823 823
824 824 @user_data.setter
825 825 def user_data(self, val):
826 826 if not isinstance(val, dict):
827 827 raise Exception('user_data must be dict, got %s' % type(val))
828 828 try:
829 829 self._user_data = json.dumps(val)
830 830 except Exception:
831 831 log.error(traceback.format_exc())
832 832
833 833 @classmethod
834 834 def get_by_username(cls, username, case_insensitive=False,
835 835 cache=False, identity_cache=False):
836 836 session = Session()
837 837
838 838 if case_insensitive:
839 839 q = cls.query().filter(
840 840 func.lower(cls.username) == func.lower(username))
841 841 else:
842 842 q = cls.query().filter(cls.username == username)
843 843
844 844 if cache:
845 845 if identity_cache:
846 846 val = cls.identity_cache(session, 'username', username)
847 847 if val:
848 848 return val
849 849 else:
850 850 cache_key = "get_user_by_name_%s" % _hash_key(username)
851 851 q = q.options(
852 852 FromCache("sql_cache_short", cache_key))
853 853
854 854 return q.scalar()
855 855
856 856 @classmethod
857 857 def get_by_auth_token(cls, auth_token, cache=False):
858 858 q = UserApiKeys.query()\
859 859 .filter(UserApiKeys.api_key == auth_token)\
860 860 .filter(or_(UserApiKeys.expires == -1,
861 861 UserApiKeys.expires >= time.time()))
862 862 if cache:
863 863 q = q.options(
864 864 FromCache("sql_cache_short", "get_auth_token_%s" % auth_token))
865 865
866 866 match = q.first()
867 867 if match:
868 868 return match.user
869 869
870 870 @classmethod
871 871 def get_by_email(cls, email, case_insensitive=False, cache=False):
872 872
873 873 if case_insensitive:
874 874 q = cls.query().filter(func.lower(cls.email) == func.lower(email))
875 875
876 876 else:
877 877 q = cls.query().filter(cls.email == email)
878 878
879 879 email_key = _hash_key(email)
880 880 if cache:
881 881 q = q.options(
882 882 FromCache("sql_cache_short", "get_email_key_%s" % email_key))
883 883
884 884 ret = q.scalar()
885 885 if ret is None:
886 886 q = UserEmailMap.query()
887 887 # try fetching in alternate email map
888 888 if case_insensitive:
889 889 q = q.filter(func.lower(UserEmailMap.email) == func.lower(email))
890 890 else:
891 891 q = q.filter(UserEmailMap.email == email)
892 892 q = q.options(joinedload(UserEmailMap.user))
893 893 if cache:
894 894 q = q.options(
895 895 FromCache("sql_cache_short", "get_email_map_key_%s" % email_key))
896 896 ret = getattr(q.scalar(), 'user', None)
897 897
898 898 return ret
899 899
900 900 @classmethod
901 901 def get_from_cs_author(cls, author):
902 902 """
903 903 Tries to get User objects out of commit author string
904 904
905 905 :param author:
906 906 """
907 907 from rhodecode.lib.helpers import email, author_name
908 908 # Valid email in the attribute passed, see if they're in the system
909 909 _email = email(author)
910 910 if _email:
911 911 user = cls.get_by_email(_email, case_insensitive=True)
912 912 if user:
913 913 return user
914 914 # Maybe we can match by username?
915 915 _author = author_name(author)
916 916 user = cls.get_by_username(_author, case_insensitive=True)
917 917 if user:
918 918 return user
919 919
920 920 def update_userdata(self, **kwargs):
921 921 usr = self
922 922 old = usr.user_data
923 923 old.update(**kwargs)
924 924 usr.user_data = old
925 925 Session().add(usr)
926 926 log.debug('updated userdata with ', kwargs)
927 927
928 928 def update_lastlogin(self):
929 929 """Update user lastlogin"""
930 930 self.last_login = datetime.datetime.now()
931 931 Session().add(self)
932 932 log.debug('updated user %s lastlogin', self.username)
933 933
934 934 def update_lastactivity(self):
935 935 """Update user lastactivity"""
936 936 self.last_activity = datetime.datetime.now()
937 937 Session().add(self)
938 938 log.debug('updated user `%s` last activity', self.username)
939 939
940 940 def update_password(self, new_password):
941 941 from rhodecode.lib.auth import get_crypt_password
942 942
943 943 self.password = get_crypt_password(new_password)
944 944 Session().add(self)
945 945
946 946 @classmethod
947 947 def get_first_super_admin(cls):
948 948 user = User.query().filter(User.admin == true()).first()
949 949 if user is None:
950 950 raise Exception('FATAL: Missing administrative account!')
951 951 return user
952 952
953 953 @classmethod
954 954 def get_all_super_admins(cls):
955 955 """
956 956 Returns all admin accounts sorted by username
957 957 """
958 958 return User.query().filter(User.admin == true())\
959 959 .order_by(User.username.asc()).all()
960 960
961 961 @classmethod
962 962 def get_default_user(cls, cache=False, refresh=False):
963 963 user = User.get_by_username(User.DEFAULT_USER, cache=cache)
964 964 if user is None:
965 965 raise Exception('FATAL: Missing default account!')
966 966 if refresh:
967 967 # The default user might be based on outdated state which
968 968 # has been loaded from the cache.
969 969 # A call to refresh() ensures that the
970 970 # latest state from the database is used.
971 971 Session().refresh(user)
972 972 return user
973 973
974 974 def _get_default_perms(self, user, suffix=''):
975 975 from rhodecode.model.permission import PermissionModel
976 976 return PermissionModel().get_default_perms(user.user_perms, suffix)
977 977
978 978 def get_default_perms(self, suffix=''):
979 979 return self._get_default_perms(self, suffix)
980 980
981 981 def get_api_data(self, include_secrets=False, details='full'):
982 982 """
983 983 Common function for generating user related data for API
984 984
985 985 :param include_secrets: By default secrets in the API data will be replaced
986 986 by a placeholder value to prevent exposing this data by accident. In case
987 987 this data shall be exposed, set this flag to ``True``.
988 988
989 989 :param details: details can be 'basic|full' basic gives only a subset of
990 990 the available user information that includes user_id, name and emails.
991 991 """
992 992 user = self
993 993 user_data = self.user_data
994 994 data = {
995 995 'user_id': user.user_id,
996 996 'username': user.username,
997 997 'firstname': user.name,
998 998 'lastname': user.lastname,
999 999 'email': user.email,
1000 1000 'emails': user.emails,
1001 1001 }
1002 1002 if details == 'basic':
1003 1003 return data
1004 1004
1005 1005 auth_token_length = 40
1006 1006 auth_token_replacement = '*' * auth_token_length
1007 1007
1008 1008 extras = {
1009 1009 'auth_tokens': [auth_token_replacement],
1010 1010 'active': user.active,
1011 1011 'admin': user.admin,
1012 1012 'extern_type': user.extern_type,
1013 1013 'extern_name': user.extern_name,
1014 1014 'last_login': user.last_login,
1015 1015 'last_activity': user.last_activity,
1016 1016 'ip_addresses': user.ip_addresses,
1017 1017 'language': user_data.get('language')
1018 1018 }
1019 1019 data.update(extras)
1020 1020
1021 1021 if include_secrets:
1022 1022 data['auth_tokens'] = user.auth_tokens
1023 1023 return data
1024 1024
1025 1025 def __json__(self):
1026 1026 data = {
1027 1027 'full_name': self.full_name,
1028 1028 'full_name_or_username': self.full_name_or_username,
1029 1029 'short_contact': self.short_contact,
1030 1030 'full_contact': self.full_contact,
1031 1031 }
1032 1032 data.update(self.get_api_data())
1033 1033 return data
1034 1034
1035 1035
1036 1036 class UserApiKeys(Base, BaseModel):
1037 1037 __tablename__ = 'user_api_keys'
1038 1038 __table_args__ = (
1039 1039 Index('uak_api_key_idx', 'api_key', unique=True),
1040 1040 Index('uak_api_key_expires_idx', 'api_key', 'expires'),
1041 1041 base_table_args
1042 1042 )
1043 1043 __mapper_args__ = {}
1044 1044
1045 1045 # ApiKey role
1046 1046 ROLE_ALL = 'token_role_all'
1047 1047 ROLE_HTTP = 'token_role_http'
1048 1048 ROLE_VCS = 'token_role_vcs'
1049 1049 ROLE_API = 'token_role_api'
1050 1050 ROLE_FEED = 'token_role_feed'
1051 1051 ROLE_PASSWORD_RESET = 'token_password_reset'
1052 1052
1053 1053 ROLES = [ROLE_ALL, ROLE_HTTP, ROLE_VCS, ROLE_API, ROLE_FEED]
1054 1054
1055 1055 user_api_key_id = Column("user_api_key_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1056 1056 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1057 1057 api_key = Column("api_key", String(255), nullable=False, unique=True)
1058 1058 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1059 1059 expires = Column('expires', Float(53), nullable=False)
1060 1060 role = Column('role', String(255), nullable=True)
1061 1061 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1062 1062
1063 1063 # scope columns
1064 1064 repo_id = Column(
1065 1065 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
1066 1066 nullable=True, unique=None, default=None)
1067 1067 repo = relationship('Repository', lazy='joined')
1068 1068
1069 1069 repo_group_id = Column(
1070 1070 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
1071 1071 nullable=True, unique=None, default=None)
1072 1072 repo_group = relationship('RepoGroup', lazy='joined')
1073 1073
1074 1074 user = relationship('User', lazy='joined')
1075 1075
1076 1076 def __unicode__(self):
1077 1077 return u"<%s('%s')>" % (self.__class__.__name__, self.role)
1078 1078
1079 1079 def __json__(self):
1080 1080 data = {
1081 1081 'auth_token': self.api_key,
1082 1082 'role': self.role,
1083 1083 'scope': self.scope_humanized,
1084 1084 'expired': self.expired
1085 1085 }
1086 1086 return data
1087 1087
1088 1088 def get_api_data(self, include_secrets=False):
1089 1089 data = self.__json__()
1090 1090 if include_secrets:
1091 1091 return data
1092 1092 else:
1093 1093 data['auth_token'] = self.token_obfuscated
1094 1094 return data
1095 1095
1096 1096 @hybrid_property
1097 1097 def description_safe(self):
1098 1098 from rhodecode.lib import helpers as h
1099 1099 return h.escape(self.description)
1100 1100
1101 1101 @property
1102 1102 def expired(self):
1103 1103 if self.expires == -1:
1104 1104 return False
1105 1105 return time.time() > self.expires
1106 1106
1107 1107 @classmethod
1108 1108 def _get_role_name(cls, role):
1109 1109 return {
1110 1110 cls.ROLE_ALL: _('all'),
1111 1111 cls.ROLE_HTTP: _('http/web interface'),
1112 1112 cls.ROLE_VCS: _('vcs (git/hg/svn protocol)'),
1113 1113 cls.ROLE_API: _('api calls'),
1114 1114 cls.ROLE_FEED: _('feed access'),
1115 1115 }.get(role, role)
1116 1116
1117 1117 @property
1118 1118 def role_humanized(self):
1119 1119 return self._get_role_name(self.role)
1120 1120
1121 1121 def _get_scope(self):
1122 1122 if self.repo:
1123 1123 return repr(self.repo)
1124 1124 if self.repo_group:
1125 1125 return repr(self.repo_group) + ' (recursive)'
1126 1126 return 'global'
1127 1127
1128 1128 @property
1129 1129 def scope_humanized(self):
1130 1130 return self._get_scope()
1131 1131
1132 1132 @property
1133 1133 def token_obfuscated(self):
1134 1134 if self.api_key:
1135 1135 return self.api_key[:4] + "****"
1136 1136
1137 1137
1138 1138 class UserEmailMap(Base, BaseModel):
1139 1139 __tablename__ = 'user_email_map'
1140 1140 __table_args__ = (
1141 1141 Index('uem_email_idx', 'email'),
1142 1142 UniqueConstraint('email'),
1143 1143 base_table_args
1144 1144 )
1145 1145 __mapper_args__ = {}
1146 1146
1147 1147 email_id = Column("email_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1148 1148 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1149 1149 _email = Column("email", String(255), nullable=True, unique=False, default=None)
1150 1150 user = relationship('User', lazy='joined')
1151 1151
1152 1152 @validates('_email')
1153 1153 def validate_email(self, key, email):
1154 1154 # check if this email is not main one
1155 1155 main_email = Session().query(User).filter(User.email == email).scalar()
1156 1156 if main_email is not None:
1157 1157 raise AttributeError('email %s is present is user table' % email)
1158 1158 return email
1159 1159
1160 1160 @hybrid_property
1161 1161 def email(self):
1162 1162 return self._email
1163 1163
1164 1164 @email.setter
1165 1165 def email(self, val):
1166 1166 self._email = val.lower() if val else None
1167 1167
1168 1168
1169 1169 class UserIpMap(Base, BaseModel):
1170 1170 __tablename__ = 'user_ip_map'
1171 1171 __table_args__ = (
1172 1172 UniqueConstraint('user_id', 'ip_addr'),
1173 1173 base_table_args
1174 1174 )
1175 1175 __mapper_args__ = {}
1176 1176
1177 1177 ip_id = Column("ip_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1178 1178 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1179 1179 ip_addr = Column("ip_addr", String(255), nullable=True, unique=False, default=None)
1180 1180 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
1181 1181 description = Column("description", String(10000), nullable=True, unique=None, default=None)
1182 1182 user = relationship('User', lazy='joined')
1183 1183
1184 1184 @hybrid_property
1185 1185 def description_safe(self):
1186 1186 from rhodecode.lib import helpers as h
1187 1187 return h.escape(self.description)
1188 1188
1189 1189 @classmethod
1190 1190 def _get_ip_range(cls, ip_addr):
1191 1191 net = ipaddress.ip_network(safe_unicode(ip_addr), strict=False)
1192 1192 return [str(net.network_address), str(net.broadcast_address)]
1193 1193
1194 1194 def __json__(self):
1195 1195 return {
1196 1196 'ip_addr': self.ip_addr,
1197 1197 'ip_range': self._get_ip_range(self.ip_addr),
1198 1198 }
1199 1199
1200 1200 def __unicode__(self):
1201 1201 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
1202 1202 self.user_id, self.ip_addr)
1203 1203
1204 1204
1205 1205 class UserSshKeys(Base, BaseModel):
1206 1206 __tablename__ = 'user_ssh_keys'
1207 1207 __table_args__ = (
1208 1208 Index('usk_ssh_key_fingerprint_idx', 'ssh_key_fingerprint'),
1209 1209
1210 1210 UniqueConstraint('ssh_key_fingerprint'),
1211 1211
1212 1212 base_table_args
1213 1213 )
1214 1214 __mapper_args__ = {}
1215 1215
1216 1216 ssh_key_id = Column('ssh_key_id', Integer(), nullable=False, unique=True, default=None, primary_key=True)
1217 1217 ssh_key_data = Column('ssh_key_data', String(10240), nullable=False, unique=None, default=None)
1218 1218 ssh_key_fingerprint = Column('ssh_key_fingerprint', String(255), nullable=False, unique=None, default=None)
1219 1219
1220 1220 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
1221 1221
1222 1222 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1223 1223 accessed_on = Column('accessed_on', DateTime(timezone=False), nullable=True, default=None)
1224 1224 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
1225 1225
1226 1226 user = relationship('User', lazy='joined')
1227 1227
1228 1228 def __json__(self):
1229 1229 data = {
1230 1230 'ssh_fingerprint': self.ssh_key_fingerprint,
1231 1231 'description': self.description,
1232 1232 'created_on': self.created_on
1233 1233 }
1234 1234 return data
1235 1235
1236 1236 def get_api_data(self):
1237 1237 data = self.__json__()
1238 1238 return data
1239 1239
1240 1240
1241 1241 class UserLog(Base, BaseModel):
1242 1242 __tablename__ = 'user_logs'
1243 1243 __table_args__ = (
1244 1244 base_table_args,
1245 1245 )
1246 1246
1247 1247 VERSION_1 = 'v1'
1248 1248 VERSION_2 = 'v2'
1249 1249 VERSIONS = [VERSION_1, VERSION_2]
1250 1250
1251 1251 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1252 1252 user_id = Column("user_id", Integer(), ForeignKey('users.user_id',ondelete='SET NULL'), nullable=True, unique=None, default=None)
1253 1253 username = Column("username", String(255), nullable=True, unique=None, default=None)
1254 1254 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id', ondelete='SET NULL'), nullable=True, unique=None, default=None)
1255 1255 repository_name = Column("repository_name", String(255), nullable=True, unique=None, default=None)
1256 1256 user_ip = Column("user_ip", String(255), nullable=True, unique=None, default=None)
1257 1257 action = Column("action", Text().with_variant(Text(1200000), 'mysql'), nullable=True, unique=None, default=None)
1258 1258 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
1259 1259
1260 1260 version = Column("version", String(255), nullable=True, default=VERSION_1)
1261 1261 user_data = Column('user_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1262 1262 action_data = Column('action_data_json', MutationObj.as_mutable(JsonType(dialect_map=dict(mysql=LONGTEXT()))))
1263 1263
1264 1264 def __unicode__(self):
1265 1265 return u"<%s('id:%s:%s')>" % (
1266 1266 self.__class__.__name__, self.repository_name, self.action)
1267 1267
1268 1268 def __json__(self):
1269 1269 return {
1270 1270 'user_id': self.user_id,
1271 1271 'username': self.username,
1272 1272 'repository_id': self.repository_id,
1273 1273 'repository_name': self.repository_name,
1274 1274 'user_ip': self.user_ip,
1275 1275 'action_date': self.action_date,
1276 1276 'action': self.action,
1277 1277 }
1278 1278
1279 1279 @hybrid_property
1280 1280 def entry_id(self):
1281 1281 return self.user_log_id
1282 1282
1283 1283 @property
1284 1284 def action_as_day(self):
1285 1285 return datetime.date(*self.action_date.timetuple()[:3])
1286 1286
1287 1287 user = relationship('User')
1288 1288 repository = relationship('Repository', cascade='')
1289 1289
1290 1290
1291 1291 class UserGroup(Base, BaseModel):
1292 1292 __tablename__ = 'users_groups'
1293 1293 __table_args__ = (
1294 1294 base_table_args,
1295 1295 )
1296 1296
1297 1297 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1298 1298 users_group_name = Column("users_group_name", String(255), nullable=False, unique=True, default=None)
1299 1299 user_group_description = Column("user_group_description", String(10000), nullable=True, unique=None, default=None)
1300 1300 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
1301 1301 inherit_default_permissions = Column("users_group_inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
1302 1302 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
1303 1303 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1304 1304 _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data
1305 1305
1306 1306 members = relationship('UserGroupMember', cascade="all, delete, delete-orphan", lazy="joined")
1307 1307 users_group_to_perm = relationship('UserGroupToPerm', cascade='all')
1308 1308 users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1309 1309 users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
1310 1310 user_user_group_to_perm = relationship('UserUserGroupToPerm', cascade='all')
1311 1311 user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all')
1312 1312
1313 1313 user_group_review_rules = relationship('RepoReviewRuleUserGroup', cascade='all')
1314 1314 user = relationship('User', primaryjoin="User.user_id==UserGroup.user_id")
1315 1315
1316 1316 @classmethod
1317 1317 def _load_group_data(cls, column):
1318 1318 if not column:
1319 1319 return {}
1320 1320
1321 1321 try:
1322 1322 return json.loads(column) or {}
1323 1323 except TypeError:
1324 1324 return {}
1325 1325
1326 1326 @hybrid_property
1327 1327 def description_safe(self):
1328 1328 from rhodecode.lib import helpers as h
1329 1329 return h.escape(self.user_group_description)
1330 1330
1331 1331 @hybrid_property
1332 1332 def group_data(self):
1333 1333 return self._load_group_data(self._group_data)
1334 1334
1335 1335 @group_data.expression
1336 1336 def group_data(self, **kwargs):
1337 1337 return self._group_data
1338 1338
1339 1339 @group_data.setter
1340 1340 def group_data(self, val):
1341 1341 try:
1342 1342 self._group_data = json.dumps(val)
1343 1343 except Exception:
1344 1344 log.error(traceback.format_exc())
1345 1345
1346 1346 @classmethod
1347 1347 def _load_sync(cls, group_data):
1348 1348 if group_data:
1349 1349 return group_data.get('extern_type')
1350 1350
1351 1351 @property
1352 1352 def sync(self):
1353 1353 return self._load_sync(self.group_data)
1354 1354
1355 1355 def __unicode__(self):
1356 1356 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1357 1357 self.users_group_id,
1358 1358 self.users_group_name)
1359 1359
1360 1360 @classmethod
1361 1361 def get_by_group_name(cls, group_name, cache=False,
1362 1362 case_insensitive=False):
1363 1363 if case_insensitive:
1364 1364 q = cls.query().filter(func.lower(cls.users_group_name) ==
1365 1365 func.lower(group_name))
1366 1366
1367 1367 else:
1368 1368 q = cls.query().filter(cls.users_group_name == group_name)
1369 1369 if cache:
1370 1370 q = q.options(
1371 1371 FromCache("sql_cache_short", "get_group_%s" % _hash_key(group_name)))
1372 1372 return q.scalar()
1373 1373
1374 1374 @classmethod
1375 1375 def get(cls, user_group_id, cache=False):
1376 1376 if not user_group_id:
1377 1377 return
1378 1378
1379 1379 user_group = cls.query()
1380 1380 if cache:
1381 1381 user_group = user_group.options(
1382 1382 FromCache("sql_cache_short", "get_users_group_%s" % user_group_id))
1383 1383 return user_group.get(user_group_id)
1384 1384
1385 1385 def permissions(self, with_admins=True, with_owner=True):
1386 1386 q = UserUserGroupToPerm.query().filter(UserUserGroupToPerm.user_group == self)
1387 1387 q = q.options(joinedload(UserUserGroupToPerm.user_group),
1388 1388 joinedload(UserUserGroupToPerm.user),
1389 1389 joinedload(UserUserGroupToPerm.permission),)
1390 1390
1391 1391 # get owners and admins and permissions. We do a trick of re-writing
1392 1392 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1393 1393 # has a global reference and changing one object propagates to all
1394 1394 # others. This means if admin is also an owner admin_row that change
1395 1395 # would propagate to both objects
1396 1396 perm_rows = []
1397 1397 for _usr in q.all():
1398 1398 usr = AttributeDict(_usr.user.get_dict())
1399 1399 usr.permission = _usr.permission.permission_name
1400 1400 perm_rows.append(usr)
1401 1401
1402 1402 # filter the perm rows by 'default' first and then sort them by
1403 1403 # admin,write,read,none permissions sorted again alphabetically in
1404 1404 # each group
1405 1405 perm_rows = sorted(perm_rows, key=display_user_sort)
1406 1406
1407 1407 _admin_perm = 'usergroup.admin'
1408 1408 owner_row = []
1409 1409 if with_owner:
1410 1410 usr = AttributeDict(self.user.get_dict())
1411 1411 usr.owner_row = True
1412 1412 usr.permission = _admin_perm
1413 1413 owner_row.append(usr)
1414 1414
1415 1415 super_admin_rows = []
1416 1416 if with_admins:
1417 1417 for usr in User.get_all_super_admins():
1418 1418 # if this admin is also owner, don't double the record
1419 1419 if usr.user_id == owner_row[0].user_id:
1420 1420 owner_row[0].admin_row = True
1421 1421 else:
1422 1422 usr = AttributeDict(usr.get_dict())
1423 1423 usr.admin_row = True
1424 1424 usr.permission = _admin_perm
1425 1425 super_admin_rows.append(usr)
1426 1426
1427 1427 return super_admin_rows + owner_row + perm_rows
1428 1428
1429 1429 def permission_user_groups(self):
1430 1430 q = UserGroupUserGroupToPerm.query().filter(UserGroupUserGroupToPerm.target_user_group == self)
1431 1431 q = q.options(joinedload(UserGroupUserGroupToPerm.user_group),
1432 1432 joinedload(UserGroupUserGroupToPerm.target_user_group),
1433 1433 joinedload(UserGroupUserGroupToPerm.permission),)
1434 1434
1435 1435 perm_rows = []
1436 1436 for _user_group in q.all():
1437 1437 usr = AttributeDict(_user_group.user_group.get_dict())
1438 1438 usr.permission = _user_group.permission.permission_name
1439 1439 perm_rows.append(usr)
1440 1440
1441 1441 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1442 1442 return perm_rows
1443 1443
1444 1444 def _get_default_perms(self, user_group, suffix=''):
1445 1445 from rhodecode.model.permission import PermissionModel
1446 1446 return PermissionModel().get_default_perms(user_group.users_group_to_perm, suffix)
1447 1447
1448 1448 def get_default_perms(self, suffix=''):
1449 1449 return self._get_default_perms(self, suffix)
1450 1450
1451 1451 def get_api_data(self, with_group_members=True, include_secrets=False):
1452 1452 """
1453 1453 :param include_secrets: See :meth:`User.get_api_data`, this parameter is
1454 1454 basically forwarded.
1455 1455
1456 1456 """
1457 1457 user_group = self
1458 1458 data = {
1459 1459 'users_group_id': user_group.users_group_id,
1460 1460 'group_name': user_group.users_group_name,
1461 1461 'group_description': user_group.user_group_description,
1462 1462 'active': user_group.users_group_active,
1463 1463 'owner': user_group.user.username,
1464 1464 'sync': user_group.sync,
1465 1465 'owner_email': user_group.user.email,
1466 1466 }
1467 1467
1468 1468 if with_group_members:
1469 1469 users = []
1470 1470 for user in user_group.members:
1471 1471 user = user.user
1472 1472 users.append(user.get_api_data(include_secrets=include_secrets))
1473 1473 data['users'] = users
1474 1474
1475 1475 return data
1476 1476
1477 1477
1478 1478 class UserGroupMember(Base, BaseModel):
1479 1479 __tablename__ = 'users_groups_members'
1480 1480 __table_args__ = (
1481 1481 base_table_args,
1482 1482 )
1483 1483
1484 1484 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1485 1485 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
1486 1486 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
1487 1487
1488 1488 user = relationship('User', lazy='joined')
1489 1489 users_group = relationship('UserGroup')
1490 1490
1491 1491 def __init__(self, gr_id='', u_id=''):
1492 1492 self.users_group_id = gr_id
1493 1493 self.user_id = u_id
1494 1494
1495 1495
1496 1496 class RepositoryField(Base, BaseModel):
1497 1497 __tablename__ = 'repositories_fields'
1498 1498 __table_args__ = (
1499 1499 UniqueConstraint('repository_id', 'field_key'), # no-multi field
1500 1500 base_table_args,
1501 1501 )
1502 1502
1503 1503 PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields
1504 1504
1505 1505 repo_field_id = Column("repo_field_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1506 1506 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
1507 1507 field_key = Column("field_key", String(250))
1508 1508 field_label = Column("field_label", String(1024), nullable=False)
1509 1509 field_value = Column("field_value", String(10000), nullable=False)
1510 1510 field_desc = Column("field_desc", String(1024), nullable=False)
1511 1511 field_type = Column("field_type", String(255), nullable=False, unique=None)
1512 1512 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1513 1513
1514 1514 repository = relationship('Repository')
1515 1515
1516 1516 @property
1517 1517 def field_key_prefixed(self):
1518 1518 return 'ex_%s' % self.field_key
1519 1519
1520 1520 @classmethod
1521 1521 def un_prefix_key(cls, key):
1522 1522 if key.startswith(cls.PREFIX):
1523 1523 return key[len(cls.PREFIX):]
1524 1524 return key
1525 1525
1526 1526 @classmethod
1527 1527 def get_by_key_name(cls, key, repo):
1528 1528 row = cls.query()\
1529 1529 .filter(cls.repository == repo)\
1530 1530 .filter(cls.field_key == key).scalar()
1531 1531 return row
1532 1532
1533 1533
1534 1534 class Repository(Base, BaseModel):
1535 1535 __tablename__ = 'repositories'
1536 1536 __table_args__ = (
1537 1537 Index('r_repo_name_idx', 'repo_name', mysql_length=255),
1538 1538 base_table_args,
1539 1539 )
1540 1540 DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}'
1541 1541 DEFAULT_CLONE_URI_ID = '{scheme}://{user}@{netloc}/_{repoid}'
1542 1542 DEFAULT_CLONE_URI_SSH = 'ssh://{sys_user}@{hostname}/{repo}'
1543 1543
1544 1544 STATE_CREATED = 'repo_state_created'
1545 1545 STATE_PENDING = 'repo_state_pending'
1546 1546 STATE_ERROR = 'repo_state_error'
1547 1547
1548 1548 LOCK_AUTOMATIC = 'lock_auto'
1549 1549 LOCK_API = 'lock_api'
1550 1550 LOCK_WEB = 'lock_web'
1551 1551 LOCK_PULL = 'lock_pull'
1552 1552
1553 1553 NAME_SEP = URL_SEP
1554 1554
1555 1555 repo_id = Column(
1556 1556 "repo_id", Integer(), nullable=False, unique=True, default=None,
1557 1557 primary_key=True)
1558 1558 _repo_name = Column(
1559 1559 "repo_name", Text(), nullable=False, default=None)
1560 1560 _repo_name_hash = Column(
1561 1561 "repo_name_hash", String(255), nullable=False, unique=True)
1562 1562 repo_state = Column("repo_state", String(255), nullable=True)
1563 1563
1564 1564 clone_uri = Column(
1565 1565 "clone_uri", EncryptedTextValue(), nullable=True, unique=False,
1566 1566 default=None)
1567 1567 push_uri = Column(
1568 1568 "push_uri", EncryptedTextValue(), nullable=True, unique=False,
1569 1569 default=None)
1570 1570 repo_type = Column(
1571 1571 "repo_type", String(255), nullable=False, unique=False, default=None)
1572 1572 user_id = Column(
1573 1573 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
1574 1574 unique=False, default=None)
1575 1575 private = Column(
1576 1576 "private", Boolean(), nullable=True, unique=None, default=None)
1577 1577 enable_statistics = Column(
1578 1578 "statistics", Boolean(), nullable=True, unique=None, default=True)
1579 1579 enable_downloads = Column(
1580 1580 "downloads", Boolean(), nullable=True, unique=None, default=True)
1581 1581 description = Column(
1582 1582 "description", String(10000), nullable=True, unique=None, default=None)
1583 1583 created_on = Column(
1584 1584 'created_on', DateTime(timezone=False), nullable=True, unique=None,
1585 1585 default=datetime.datetime.now)
1586 1586 updated_on = Column(
1587 1587 'updated_on', DateTime(timezone=False), nullable=True, unique=None,
1588 1588 default=datetime.datetime.now)
1589 1589 _landing_revision = Column(
1590 1590 "landing_revision", String(255), nullable=False, unique=False,
1591 1591 default=None)
1592 1592 enable_locking = Column(
1593 1593 "enable_locking", Boolean(), nullable=False, unique=None,
1594 1594 default=False)
1595 1595 _locked = Column(
1596 1596 "locked", String(255), nullable=True, unique=False, default=None)
1597 1597 _changeset_cache = Column(
1598 1598 "changeset_cache", LargeBinary(), nullable=True) # JSON data
1599 1599
1600 1600 fork_id = Column(
1601 1601 "fork_id", Integer(), ForeignKey('repositories.repo_id'),
1602 1602 nullable=True, unique=False, default=None)
1603 1603 group_id = Column(
1604 1604 "group_id", Integer(), ForeignKey('groups.group_id'), nullable=True,
1605 1605 unique=False, default=None)
1606 1606
1607 1607 user = relationship('User', lazy='joined')
1608 1608 fork = relationship('Repository', remote_side=repo_id, lazy='joined')
1609 1609 group = relationship('RepoGroup', lazy='joined')
1610 1610 repo_to_perm = relationship(
1611 1611 'UserRepoToPerm', cascade='all',
1612 1612 order_by='UserRepoToPerm.repo_to_perm_id')
1613 1613 users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1614 1614 stats = relationship('Statistics', cascade='all', uselist=False)
1615 1615
1616 1616 followers = relationship(
1617 1617 'UserFollowing',
1618 1618 primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id',
1619 1619 cascade='all')
1620 1620 extra_fields = relationship(
1621 1621 'RepositoryField', cascade="all, delete, delete-orphan")
1622 1622 logs = relationship('UserLog')
1623 1623 comments = relationship(
1624 1624 'ChangesetComment', cascade="all, delete, delete-orphan")
1625 1625 pull_requests_source = relationship(
1626 1626 'PullRequest',
1627 1627 primaryjoin='PullRequest.source_repo_id==Repository.repo_id',
1628 1628 cascade="all, delete, delete-orphan")
1629 1629 pull_requests_target = relationship(
1630 1630 'PullRequest',
1631 1631 primaryjoin='PullRequest.target_repo_id==Repository.repo_id',
1632 1632 cascade="all, delete, delete-orphan")
1633 1633 ui = relationship('RepoRhodeCodeUi', cascade="all")
1634 1634 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1635 1635 integrations = relationship('Integration',
1636 1636 cascade="all, delete, delete-orphan")
1637 1637
1638 1638 scoped_tokens = relationship('UserApiKeys', cascade="all")
1639 1639
1640 1640 def __unicode__(self):
1641 1641 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1642 1642 safe_unicode(self.repo_name))
1643 1643
1644 1644 @hybrid_property
1645 1645 def description_safe(self):
1646 1646 from rhodecode.lib import helpers as h
1647 1647 return h.escape(self.description)
1648 1648
1649 1649 @hybrid_property
1650 1650 def landing_rev(self):
1651 1651 # always should return [rev_type, rev]
1652 1652 if self._landing_revision:
1653 1653 _rev_info = self._landing_revision.split(':')
1654 1654 if len(_rev_info) < 2:
1655 1655 _rev_info.insert(0, 'rev')
1656 1656 return [_rev_info[0], _rev_info[1]]
1657 1657 return [None, None]
1658 1658
1659 1659 @landing_rev.setter
1660 1660 def landing_rev(self, val):
1661 1661 if ':' not in val:
1662 1662 raise ValueError('value must be delimited with `:` and consist '
1663 1663 'of <rev_type>:<rev>, got %s instead' % val)
1664 1664 self._landing_revision = val
1665 1665
1666 1666 @hybrid_property
1667 1667 def locked(self):
1668 1668 if self._locked:
1669 1669 user_id, timelocked, reason = self._locked.split(':')
1670 1670 lock_values = int(user_id), timelocked, reason
1671 1671 else:
1672 1672 lock_values = [None, None, None]
1673 1673 return lock_values
1674 1674
1675 1675 @locked.setter
1676 1676 def locked(self, val):
1677 1677 if val and isinstance(val, (list, tuple)):
1678 1678 self._locked = ':'.join(map(str, val))
1679 1679 else:
1680 1680 self._locked = None
1681 1681
1682 1682 @hybrid_property
1683 1683 def changeset_cache(self):
1684 1684 from rhodecode.lib.vcs.backends.base import EmptyCommit
1685 1685 dummy = EmptyCommit().__json__()
1686 1686 if not self._changeset_cache:
1687 1687 return dummy
1688 1688 try:
1689 1689 return json.loads(self._changeset_cache)
1690 1690 except TypeError:
1691 1691 return dummy
1692 1692 except Exception:
1693 1693 log.error(traceback.format_exc())
1694 1694 return dummy
1695 1695
1696 1696 @changeset_cache.setter
1697 1697 def changeset_cache(self, val):
1698 1698 try:
1699 1699 self._changeset_cache = json.dumps(val)
1700 1700 except Exception:
1701 1701 log.error(traceback.format_exc())
1702 1702
1703 1703 @hybrid_property
1704 1704 def repo_name(self):
1705 1705 return self._repo_name
1706 1706
1707 1707 @repo_name.setter
1708 1708 def repo_name(self, value):
1709 1709 self._repo_name = value
1710 1710 self._repo_name_hash = hashlib.sha1(safe_str(value)).hexdigest()
1711 1711
1712 1712 @classmethod
1713 1713 def normalize_repo_name(cls, repo_name):
1714 1714 """
1715 1715 Normalizes os specific repo_name to the format internally stored inside
1716 1716 database using URL_SEP
1717 1717
1718 1718 :param cls:
1719 1719 :param repo_name:
1720 1720 """
1721 1721 return cls.NAME_SEP.join(repo_name.split(os.sep))
1722 1722
1723 1723 @classmethod
1724 1724 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1725 1725 session = Session()
1726 1726 q = session.query(cls).filter(cls.repo_name == repo_name)
1727 1727
1728 1728 if cache:
1729 1729 if identity_cache:
1730 1730 val = cls.identity_cache(session, 'repo_name', repo_name)
1731 1731 if val:
1732 1732 return val
1733 1733 else:
1734 1734 cache_key = "get_repo_by_name_%s" % _hash_key(repo_name)
1735 1735 q = q.options(
1736 1736 FromCache("sql_cache_short", cache_key))
1737 1737
1738 1738 return q.scalar()
1739 1739
1740 1740 @classmethod
1741 1741 def get_by_id_or_repo_name(cls, repoid):
1742 1742 if isinstance(repoid, (int, long)):
1743 1743 try:
1744 1744 repo = cls.get(repoid)
1745 1745 except ValueError:
1746 1746 repo = None
1747 1747 else:
1748 1748 repo = cls.get_by_repo_name(repoid)
1749 1749 return repo
1750 1750
1751 1751 @classmethod
1752 1752 def get_by_full_path(cls, repo_full_path):
1753 1753 repo_name = repo_full_path.split(cls.base_path(), 1)[-1]
1754 1754 repo_name = cls.normalize_repo_name(repo_name)
1755 1755 return cls.get_by_repo_name(repo_name.strip(URL_SEP))
1756 1756
1757 1757 @classmethod
1758 1758 def get_repo_forks(cls, repo_id):
1759 1759 return cls.query().filter(Repository.fork_id == repo_id)
1760 1760
1761 1761 @classmethod
1762 1762 def base_path(cls):
1763 1763 """
1764 1764 Returns base path when all repos are stored
1765 1765
1766 1766 :param cls:
1767 1767 """
1768 1768 q = Session().query(RhodeCodeUi)\
1769 1769 .filter(RhodeCodeUi.ui_key == cls.NAME_SEP)
1770 1770 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1771 1771 return q.one().ui_value
1772 1772
1773 1773 @classmethod
1774 1774 def get_all_repos(cls, user_id=Optional(None), group_id=Optional(None),
1775 1775 case_insensitive=True):
1776 1776 q = Repository.query()
1777 1777
1778 1778 if not isinstance(user_id, Optional):
1779 1779 q = q.filter(Repository.user_id == user_id)
1780 1780
1781 1781 if not isinstance(group_id, Optional):
1782 1782 q = q.filter(Repository.group_id == group_id)
1783 1783
1784 1784 if case_insensitive:
1785 1785 q = q.order_by(func.lower(Repository.repo_name))
1786 1786 else:
1787 1787 q = q.order_by(Repository.repo_name)
1788 1788 return q.all()
1789 1789
1790 1790 @property
1791 1791 def forks(self):
1792 1792 """
1793 1793 Return forks of this repo
1794 1794 """
1795 1795 return Repository.get_repo_forks(self.repo_id)
1796 1796
1797 1797 @property
1798 1798 def parent(self):
1799 1799 """
1800 1800 Returns fork parent
1801 1801 """
1802 1802 return self.fork
1803 1803
1804 1804 @property
1805 1805 def just_name(self):
1806 1806 return self.repo_name.split(self.NAME_SEP)[-1]
1807 1807
1808 1808 @property
1809 1809 def groups_with_parents(self):
1810 1810 groups = []
1811 1811 if self.group is None:
1812 1812 return groups
1813 1813
1814 1814 cur_gr = self.group
1815 1815 groups.insert(0, cur_gr)
1816 1816 while 1:
1817 1817 gr = getattr(cur_gr, 'parent_group', None)
1818 1818 cur_gr = cur_gr.parent_group
1819 1819 if gr is None:
1820 1820 break
1821 1821 groups.insert(0, gr)
1822 1822
1823 1823 return groups
1824 1824
1825 1825 @property
1826 1826 def groups_and_repo(self):
1827 1827 return self.groups_with_parents, self
1828 1828
1829 1829 @LazyProperty
1830 1830 def repo_path(self):
1831 1831 """
1832 1832 Returns base full path for that repository means where it actually
1833 1833 exists on a filesystem
1834 1834 """
1835 1835 q = Session().query(RhodeCodeUi).filter(
1836 1836 RhodeCodeUi.ui_key == self.NAME_SEP)
1837 1837 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1838 1838 return q.one().ui_value
1839 1839
1840 1840 @property
1841 1841 def repo_full_path(self):
1842 1842 p = [self.repo_path]
1843 1843 # we need to split the name by / since this is how we store the
1844 1844 # names in the database, but that eventually needs to be converted
1845 1845 # into a valid system path
1846 1846 p += self.repo_name.split(self.NAME_SEP)
1847 1847 return os.path.join(*map(safe_unicode, p))
1848 1848
1849 1849 @property
1850 1850 def cache_keys(self):
1851 1851 """
1852 1852 Returns associated cache keys for that repo
1853 1853 """
1854 1854 return CacheKey.query()\
1855 1855 .filter(CacheKey.cache_args == self.repo_name)\
1856 1856 .order_by(CacheKey.cache_key)\
1857 1857 .all()
1858 1858
1859 1859 @property
1860 1860 def cached_diffs_relative_dir(self):
1861 1861 """
1862 1862 Return a relative to the repository store path of cached diffs
1863 1863 used for safe display for users, who shouldn't know the absolute store
1864 1864 path
1865 1865 """
1866 1866 return os.path.join(
1867 1867 os.path.dirname(self.repo_name),
1868 1868 self.cached_diffs_dir.split(os.path.sep)[-1])
1869 1869
1870 1870 @property
1871 1871 def cached_diffs_dir(self):
1872 1872 path = self.repo_full_path
1873 1873 return os.path.join(
1874 1874 os.path.dirname(path),
1875 1875 '.__shadow_diff_cache_repo_{}'.format(self.repo_id))
1876 1876
1877 1877 def cached_diffs(self):
1878 1878 diff_cache_dir = self.cached_diffs_dir
1879 1879 if os.path.isdir(diff_cache_dir):
1880 1880 return os.listdir(diff_cache_dir)
1881 1881 return []
1882 1882
1883 1883 def shadow_repos(self):
1884 1884 shadow_repos_pattern = '.__shadow_repo_{}'.format(self.repo_id)
1885 1885 return [
1886 1886 x for x in os.listdir(os.path.dirname(self.repo_full_path))
1887 1887 if x.startswith(shadow_repos_pattern)]
1888 1888
1889 1889 def get_new_name(self, repo_name):
1890 1890 """
1891 1891 returns new full repository name based on assigned group and new new
1892 1892
1893 1893 :param group_name:
1894 1894 """
1895 1895 path_prefix = self.group.full_path_splitted if self.group else []
1896 1896 return self.NAME_SEP.join(path_prefix + [repo_name])
1897 1897
1898 1898 @property
1899 1899 def _config(self):
1900 1900 """
1901 1901 Returns db based config object.
1902 1902 """
1903 1903 from rhodecode.lib.utils import make_db_config
1904 1904 return make_db_config(clear_session=False, repo=self)
1905 1905
1906 1906 def permissions(self, with_admins=True, with_owner=True):
1907 1907 q = UserRepoToPerm.query().filter(UserRepoToPerm.repository == self)
1908 1908 q = q.options(joinedload(UserRepoToPerm.repository),
1909 1909 joinedload(UserRepoToPerm.user),
1910 1910 joinedload(UserRepoToPerm.permission),)
1911 1911
1912 1912 # get owners and admins and permissions. We do a trick of re-writing
1913 1913 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1914 1914 # has a global reference and changing one object propagates to all
1915 1915 # others. This means if admin is also an owner admin_row that change
1916 1916 # would propagate to both objects
1917 1917 perm_rows = []
1918 1918 for _usr in q.all():
1919 1919 usr = AttributeDict(_usr.user.get_dict())
1920 1920 usr.permission = _usr.permission.permission_name
1921 1921 perm_rows.append(usr)
1922 1922
1923 1923 # filter the perm rows by 'default' first and then sort them by
1924 1924 # admin,write,read,none permissions sorted again alphabetically in
1925 1925 # each group
1926 1926 perm_rows = sorted(perm_rows, key=display_user_sort)
1927 1927
1928 1928 _admin_perm = 'repository.admin'
1929 1929 owner_row = []
1930 1930 if with_owner:
1931 1931 usr = AttributeDict(self.user.get_dict())
1932 1932 usr.owner_row = True
1933 1933 usr.permission = _admin_perm
1934 1934 owner_row.append(usr)
1935 1935
1936 1936 super_admin_rows = []
1937 1937 if with_admins:
1938 1938 for usr in User.get_all_super_admins():
1939 1939 # if this admin is also owner, don't double the record
1940 1940 if usr.user_id == owner_row[0].user_id:
1941 1941 owner_row[0].admin_row = True
1942 1942 else:
1943 1943 usr = AttributeDict(usr.get_dict())
1944 1944 usr.admin_row = True
1945 1945 usr.permission = _admin_perm
1946 1946 super_admin_rows.append(usr)
1947 1947
1948 1948 return super_admin_rows + owner_row + perm_rows
1949 1949
1950 1950 def permission_user_groups(self):
1951 1951 q = UserGroupRepoToPerm.query().filter(
1952 1952 UserGroupRepoToPerm.repository == self)
1953 1953 q = q.options(joinedload(UserGroupRepoToPerm.repository),
1954 1954 joinedload(UserGroupRepoToPerm.users_group),
1955 1955 joinedload(UserGroupRepoToPerm.permission),)
1956 1956
1957 1957 perm_rows = []
1958 1958 for _user_group in q.all():
1959 1959 usr = AttributeDict(_user_group.users_group.get_dict())
1960 1960 usr.permission = _user_group.permission.permission_name
1961 1961 perm_rows.append(usr)
1962 1962
1963 1963 perm_rows = sorted(perm_rows, key=display_user_group_sort)
1964 1964 return perm_rows
1965 1965
1966 1966 def get_api_data(self, include_secrets=False):
1967 1967 """
1968 1968 Common function for generating repo api data
1969 1969
1970 1970 :param include_secrets: See :meth:`User.get_api_data`.
1971 1971
1972 1972 """
1973 1973 # TODO: mikhail: Here there is an anti-pattern, we probably need to
1974 1974 # move this methods on models level.
1975 1975 from rhodecode.model.settings import SettingsModel
1976 1976 from rhodecode.model.repo import RepoModel
1977 1977
1978 1978 repo = self
1979 1979 _user_id, _time, _reason = self.locked
1980 1980
1981 1981 data = {
1982 1982 'repo_id': repo.repo_id,
1983 1983 'repo_name': repo.repo_name,
1984 1984 'repo_type': repo.repo_type,
1985 1985 'clone_uri': repo.clone_uri or '',
1986 1986 'push_uri': repo.push_uri or '',
1987 1987 'url': RepoModel().get_url(self),
1988 1988 'private': repo.private,
1989 1989 'created_on': repo.created_on,
1990 1990 'description': repo.description_safe,
1991 1991 'landing_rev': repo.landing_rev,
1992 1992 'owner': repo.user.username,
1993 1993 'fork_of': repo.fork.repo_name if repo.fork else None,
1994 1994 'fork_of_id': repo.fork.repo_id if repo.fork else None,
1995 1995 'enable_statistics': repo.enable_statistics,
1996 1996 'enable_locking': repo.enable_locking,
1997 1997 'enable_downloads': repo.enable_downloads,
1998 1998 'last_changeset': repo.changeset_cache,
1999 1999 'locked_by': User.get(_user_id).get_api_data(
2000 2000 include_secrets=include_secrets) if _user_id else None,
2001 2001 'locked_date': time_to_datetime(_time) if _time else None,
2002 2002 'lock_reason': _reason if _reason else None,
2003 2003 }
2004 2004
2005 2005 # TODO: mikhail: should be per-repo settings here
2006 2006 rc_config = SettingsModel().get_all_settings()
2007 2007 repository_fields = str2bool(
2008 2008 rc_config.get('rhodecode_repository_fields'))
2009 2009 if repository_fields:
2010 2010 for f in self.extra_fields:
2011 2011 data[f.field_key_prefixed] = f.field_value
2012 2012
2013 2013 return data
2014 2014
2015 2015 @classmethod
2016 2016 def lock(cls, repo, user_id, lock_time=None, lock_reason=None):
2017 2017 if not lock_time:
2018 2018 lock_time = time.time()
2019 2019 if not lock_reason:
2020 2020 lock_reason = cls.LOCK_AUTOMATIC
2021 2021 repo.locked = [user_id, lock_time, lock_reason]
2022 2022 Session().add(repo)
2023 2023 Session().commit()
2024 2024
2025 2025 @classmethod
2026 2026 def unlock(cls, repo):
2027 2027 repo.locked = None
2028 2028 Session().add(repo)
2029 2029 Session().commit()
2030 2030
2031 2031 @classmethod
2032 2032 def getlock(cls, repo):
2033 2033 return repo.locked
2034 2034
2035 2035 def is_user_lock(self, user_id):
2036 2036 if self.lock[0]:
2037 2037 lock_user_id = safe_int(self.lock[0])
2038 2038 user_id = safe_int(user_id)
2039 2039 # both are ints, and they are equal
2040 2040 return all([lock_user_id, user_id]) and lock_user_id == user_id
2041 2041
2042 2042 return False
2043 2043
2044 2044 def get_locking_state(self, action, user_id, only_when_enabled=True):
2045 2045 """
2046 2046 Checks locking on this repository, if locking is enabled and lock is
2047 2047 present returns a tuple of make_lock, locked, locked_by.
2048 2048 make_lock can have 3 states None (do nothing) True, make lock
2049 2049 False release lock, This value is later propagated to hooks, which
2050 2050 do the locking. Think about this as signals passed to hooks what to do.
2051 2051
2052 2052 """
2053 2053 # TODO: johbo: This is part of the business logic and should be moved
2054 2054 # into the RepositoryModel.
2055 2055
2056 2056 if action not in ('push', 'pull'):
2057 2057 raise ValueError("Invalid action value: %s" % repr(action))
2058 2058
2059 2059 # defines if locked error should be thrown to user
2060 2060 currently_locked = False
2061 2061 # defines if new lock should be made, tri-state
2062 2062 make_lock = None
2063 2063 repo = self
2064 2064 user = User.get(user_id)
2065 2065
2066 2066 lock_info = repo.locked
2067 2067
2068 2068 if repo and (repo.enable_locking or not only_when_enabled):
2069 2069 if action == 'push':
2070 2070 # check if it's already locked !, if it is compare users
2071 2071 locked_by_user_id = lock_info[0]
2072 2072 if user.user_id == locked_by_user_id:
2073 2073 log.debug(
2074 2074 'Got `push` action from user %s, now unlocking', user)
2075 2075 # unlock if we have push from user who locked
2076 2076 make_lock = False
2077 2077 else:
2078 2078 # we're not the same user who locked, ban with
2079 2079 # code defined in settings (default is 423 HTTP Locked) !
2080 2080 log.debug('Repo %s is currently locked by %s', repo, user)
2081 2081 currently_locked = True
2082 2082 elif action == 'pull':
2083 2083 # [0] user [1] date
2084 2084 if lock_info[0] and lock_info[1]:
2085 2085 log.debug('Repo %s is currently locked by %s', repo, user)
2086 2086 currently_locked = True
2087 2087 else:
2088 2088 log.debug('Setting lock on repo %s by %s', repo, user)
2089 2089 make_lock = True
2090 2090
2091 2091 else:
2092 2092 log.debug('Repository %s do not have locking enabled', repo)
2093 2093
2094 2094 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
2095 2095 make_lock, currently_locked, lock_info)
2096 2096
2097 2097 from rhodecode.lib.auth import HasRepoPermissionAny
2098 2098 perm_check = HasRepoPermissionAny('repository.write', 'repository.admin')
2099 2099 if make_lock and not perm_check(repo_name=repo.repo_name, user=user):
2100 2100 # if we don't have at least write permission we cannot make a lock
2101 2101 log.debug('lock state reset back to FALSE due to lack '
2102 2102 'of at least read permission')
2103 2103 make_lock = False
2104 2104
2105 2105 return make_lock, currently_locked, lock_info
2106 2106
2107 2107 @property
2108 2108 def last_db_change(self):
2109 2109 return self.updated_on
2110 2110
2111 2111 @property
2112 2112 def clone_uri_hidden(self):
2113 2113 clone_uri = self.clone_uri
2114 2114 if clone_uri:
2115 2115 import urlobject
2116 2116 url_obj = urlobject.URLObject(cleaned_uri(clone_uri))
2117 2117 if url_obj.password:
2118 2118 clone_uri = url_obj.with_password('*****')
2119 2119 return clone_uri
2120 2120
2121 2121 @property
2122 2122 def push_uri_hidden(self):
2123 2123 push_uri = self.push_uri
2124 2124 if push_uri:
2125 2125 import urlobject
2126 2126 url_obj = urlobject.URLObject(cleaned_uri(push_uri))
2127 2127 if url_obj.password:
2128 2128 push_uri = url_obj.with_password('*****')
2129 2129 return push_uri
2130 2130
2131 2131 def clone_url(self, **override):
2132 2132 from rhodecode.model.settings import SettingsModel
2133 2133
2134 2134 uri_tmpl = None
2135 2135 if 'with_id' in override:
2136 2136 uri_tmpl = self.DEFAULT_CLONE_URI_ID
2137 2137 del override['with_id']
2138 2138
2139 2139 if 'uri_tmpl' in override:
2140 2140 uri_tmpl = override['uri_tmpl']
2141 2141 del override['uri_tmpl']
2142 2142
2143 2143 ssh = False
2144 2144 if 'ssh' in override:
2145 2145 ssh = True
2146 2146 del override['ssh']
2147 2147
2148 2148 # we didn't override our tmpl from **overrides
2149 2149 if not uri_tmpl:
2150 2150 rc_config = SettingsModel().get_all_settings(cache=True)
2151 2151 if ssh:
2152 2152 uri_tmpl = rc_config.get(
2153 2153 'rhodecode_clone_uri_ssh_tmpl') or self.DEFAULT_CLONE_URI_SSH
2154 2154 else:
2155 2155 uri_tmpl = rc_config.get(
2156 2156 'rhodecode_clone_uri_tmpl') or self.DEFAULT_CLONE_URI
2157 2157
2158 2158 request = get_current_request()
2159 2159 return get_clone_url(request=request,
2160 2160 uri_tmpl=uri_tmpl,
2161 2161 repo_name=self.repo_name,
2162 2162 repo_id=self.repo_id, **override)
2163 2163
2164 2164 def set_state(self, state):
2165 2165 self.repo_state = state
2166 2166 Session().add(self)
2167 2167 #==========================================================================
2168 2168 # SCM PROPERTIES
2169 2169 #==========================================================================
2170 2170
2171 2171 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None):
2172 2172 return get_commit_safe(
2173 2173 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load)
2174 2174
2175 2175 def get_changeset(self, rev=None, pre_load=None):
2176 2176 warnings.warn("Use get_commit", DeprecationWarning)
2177 2177 commit_id = None
2178 2178 commit_idx = None
2179 2179 if isinstance(rev, basestring):
2180 2180 commit_id = rev
2181 2181 else:
2182 2182 commit_idx = rev
2183 2183 return self.get_commit(commit_id=commit_id, commit_idx=commit_idx,
2184 2184 pre_load=pre_load)
2185 2185
2186 2186 def get_landing_commit(self):
2187 2187 """
2188 2188 Returns landing commit, or if that doesn't exist returns the tip
2189 2189 """
2190 2190 _rev_type, _rev = self.landing_rev
2191 2191 commit = self.get_commit(_rev)
2192 2192 if isinstance(commit, EmptyCommit):
2193 2193 return self.get_commit()
2194 2194 return commit
2195 2195
2196 2196 def update_commit_cache(self, cs_cache=None, config=None):
2197 2197 """
2198 2198 Update cache of last changeset for repository, keys should be::
2199 2199
2200 2200 short_id
2201 2201 raw_id
2202 2202 revision
2203 2203 parents
2204 2204 message
2205 2205 date
2206 2206 author
2207 2207
2208 2208 :param cs_cache:
2209 2209 """
2210 2210 from rhodecode.lib.vcs.backends.base import BaseChangeset
2211 2211 if cs_cache is None:
2212 2212 # use no-cache version here
2213 2213 scm_repo = self.scm_instance(cache=False, config=config)
2214 2214 if scm_repo:
2215 2215 cs_cache = scm_repo.get_commit(
2216 2216 pre_load=["author", "date", "message", "parents"])
2217 2217 else:
2218 2218 cs_cache = EmptyCommit()
2219 2219
2220 2220 if isinstance(cs_cache, BaseChangeset):
2221 2221 cs_cache = cs_cache.__json__()
2222 2222
2223 2223 def is_outdated(new_cs_cache):
2224 2224 if (new_cs_cache['raw_id'] != self.changeset_cache['raw_id'] or
2225 2225 new_cs_cache['revision'] != self.changeset_cache['revision']):
2226 2226 return True
2227 2227 return False
2228 2228
2229 2229 # check if we have maybe already latest cached revision
2230 2230 if is_outdated(cs_cache) or not self.changeset_cache:
2231 2231 _default = datetime.datetime.utcnow()
2232 2232 last_change = cs_cache.get('date') or _default
2233 2233 if self.updated_on and self.updated_on > last_change:
2234 2234 # we check if last update is newer than the new value
2235 2235 # if yes, we use the current timestamp instead. Imagine you get
2236 2236 # old commit pushed 1y ago, we'd set last update 1y to ago.
2237 2237 last_change = _default
2238 2238 log.debug('updated repo %s with new cs cache %s',
2239 2239 self.repo_name, cs_cache)
2240 2240 self.updated_on = last_change
2241 2241 self.changeset_cache = cs_cache
2242 2242 Session().add(self)
2243 2243 Session().commit()
2244 2244 else:
2245 2245 log.debug('Skipping update_commit_cache for repo:`%s` '
2246 2246 'commit already with latest changes', self.repo_name)
2247 2247
2248 2248 @property
2249 2249 def tip(self):
2250 2250 return self.get_commit('tip')
2251 2251
2252 2252 @property
2253 2253 def author(self):
2254 2254 return self.tip.author
2255 2255
2256 2256 @property
2257 2257 def last_change(self):
2258 2258 return self.scm_instance().last_change
2259 2259
2260 2260 def get_comments(self, revisions=None):
2261 2261 """
2262 2262 Returns comments for this repository grouped by revisions
2263 2263
2264 2264 :param revisions: filter query by revisions only
2265 2265 """
2266 2266 cmts = ChangesetComment.query()\
2267 2267 .filter(ChangesetComment.repo == self)
2268 2268 if revisions:
2269 2269 cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
2270 2270 grouped = collections.defaultdict(list)
2271 2271 for cmt in cmts.all():
2272 2272 grouped[cmt.revision].append(cmt)
2273 2273 return grouped
2274 2274
2275 2275 def statuses(self, revisions=None):
2276 2276 """
2277 2277 Returns statuses for this repository
2278 2278
2279 2279 :param revisions: list of revisions to get statuses for
2280 2280 """
2281 2281 statuses = ChangesetStatus.query()\
2282 2282 .filter(ChangesetStatus.repo == self)\
2283 2283 .filter(ChangesetStatus.version == 0)
2284 2284
2285 2285 if revisions:
2286 2286 # Try doing the filtering in chunks to avoid hitting limits
2287 2287 size = 500
2288 2288 status_results = []
2289 2289 for chunk in xrange(0, len(revisions), size):
2290 2290 status_results += statuses.filter(
2291 2291 ChangesetStatus.revision.in_(
2292 2292 revisions[chunk: chunk+size])
2293 2293 ).all()
2294 2294 else:
2295 2295 status_results = statuses.all()
2296 2296
2297 2297 grouped = {}
2298 2298
2299 2299 # maybe we have open new pullrequest without a status?
2300 2300 stat = ChangesetStatus.STATUS_UNDER_REVIEW
2301 2301 status_lbl = ChangesetStatus.get_status_lbl(stat)
2302 2302 for pr in PullRequest.query().filter(PullRequest.source_repo == self).all():
2303 2303 for rev in pr.revisions:
2304 2304 pr_id = pr.pull_request_id
2305 2305 pr_repo = pr.target_repo.repo_name
2306 2306 grouped[rev] = [stat, status_lbl, pr_id, pr_repo]
2307 2307
2308 2308 for stat in status_results:
2309 2309 pr_id = pr_repo = None
2310 2310 if stat.pull_request:
2311 2311 pr_id = stat.pull_request.pull_request_id
2312 2312 pr_repo = stat.pull_request.target_repo.repo_name
2313 2313 grouped[stat.revision] = [str(stat.status), stat.status_lbl,
2314 2314 pr_id, pr_repo]
2315 2315 return grouped
2316 2316
2317 2317 # ==========================================================================
2318 2318 # SCM CACHE INSTANCE
2319 2319 # ==========================================================================
2320 2320
2321 2321 def scm_instance(self, **kwargs):
2322 2322 import rhodecode
2323 2323
2324 2324 # Passing a config will not hit the cache currently only used
2325 2325 # for repo2dbmapper
2326 2326 config = kwargs.pop('config', None)
2327 2327 cache = kwargs.pop('cache', None)
2328 2328 full_cache = str2bool(rhodecode.CONFIG.get('vcs_full_cache'))
2329 2329 # if cache is NOT defined use default global, else we have a full
2330 2330 # control over cache behaviour
2331 2331 if cache is None and full_cache and not config:
2332 2332 return self._get_instance_cached()
2333 2333 return self._get_instance(cache=bool(cache), config=config)
2334 2334
2335 2335 def _get_instance_cached(self):
2336 2336 @cache_region('long_term')
2337 2337 def _get_repo(cache_key):
2338 2338 return self._get_instance()
2339 2339
2340 2340 invalidator_context = CacheKey.repo_context_cache(
2341 2341 _get_repo, self.repo_name, None, thread_scoped=True)
2342 2342
2343 2343 with invalidator_context as context:
2344 2344 context.invalidate()
2345 2345 repo = context.compute()
2346 2346
2347 2347 return repo
2348 2348
2349 2349 def _get_instance(self, cache=True, config=None):
2350 2350 config = config or self._config
2351 2351 custom_wire = {
2352 2352 'cache': cache # controls the vcs.remote cache
2353 2353 }
2354 2354 repo = get_vcs_instance(
2355 2355 repo_path=safe_str(self.repo_full_path),
2356 2356 config=config,
2357 2357 with_wire=custom_wire,
2358 2358 create=False,
2359 2359 _vcs_alias=self.repo_type)
2360 2360
2361 2361 return repo
2362 2362
2363 2363 def __json__(self):
2364 2364 return {'landing_rev': self.landing_rev}
2365 2365
2366 2366 def get_dict(self):
2367 2367
2368 2368 # Since we transformed `repo_name` to a hybrid property, we need to
2369 2369 # keep compatibility with the code which uses `repo_name` field.
2370 2370
2371 2371 result = super(Repository, self).get_dict()
2372 2372 result['repo_name'] = result.pop('_repo_name', None)
2373 2373 return result
2374 2374
2375 2375
2376 2376 class RepoGroup(Base, BaseModel):
2377 2377 __tablename__ = 'groups'
2378 2378 __table_args__ = (
2379 2379 UniqueConstraint('group_name', 'group_parent_id'),
2380 2380 CheckConstraint('group_id != group_parent_id'),
2381 2381 base_table_args,
2382 2382 )
2383 2383 __mapper_args__ = {'order_by': 'group_name'}
2384 2384
2385 2385 CHOICES_SEPARATOR = '/' # used to generate select2 choices for nested groups
2386 2386
2387 2387 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2388 2388 group_name = Column("group_name", String(255), nullable=False, unique=True, default=None)
2389 2389 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
2390 2390 group_description = Column("group_description", String(10000), nullable=True, unique=None, default=None)
2391 2391 enable_locking = Column("enable_locking", Boolean(), nullable=False, unique=None, default=False)
2392 2392 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
2393 2393 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2394 2394 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
2395 2395 personal = Column('personal', Boolean(), nullable=True, unique=None, default=None)
2396 2396
2397 2397 repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
2398 2398 users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
2399 2399 parent_group = relationship('RepoGroup', remote_side=group_id)
2400 2400 user = relationship('User')
2401 2401 integrations = relationship('Integration',
2402 2402 cascade="all, delete, delete-orphan")
2403 2403
2404 2404 def __init__(self, group_name='', parent_group=None):
2405 2405 self.group_name = group_name
2406 2406 self.parent_group = parent_group
2407 2407
2408 2408 def __unicode__(self):
2409 2409 return u"<%s('id:%s:%s')>" % (
2410 2410 self.__class__.__name__, self.group_id, self.group_name)
2411 2411
2412 2412 @hybrid_property
2413 2413 def description_safe(self):
2414 2414 from rhodecode.lib import helpers as h
2415 2415 return h.escape(self.group_description)
2416 2416
2417 2417 @classmethod
2418 2418 def _generate_choice(cls, repo_group):
2419 2419 from webhelpers.html import literal as _literal
2420 2420 _name = lambda k: _literal(cls.CHOICES_SEPARATOR.join(k))
2421 2421 return repo_group.group_id, _name(repo_group.full_path_splitted)
2422 2422
2423 2423 @classmethod
2424 2424 def groups_choices(cls, groups=None, show_empty_group=True):
2425 2425 if not groups:
2426 2426 groups = cls.query().all()
2427 2427
2428 2428 repo_groups = []
2429 2429 if show_empty_group:
2430 2430 repo_groups = [(-1, u'-- %s --' % _('No parent'))]
2431 2431
2432 2432 repo_groups.extend([cls._generate_choice(x) for x in groups])
2433 2433
2434 2434 repo_groups = sorted(
2435 2435 repo_groups, key=lambda t: t[1].split(cls.CHOICES_SEPARATOR)[0])
2436 2436 return repo_groups
2437 2437
2438 2438 @classmethod
2439 2439 def url_sep(cls):
2440 2440 return URL_SEP
2441 2441
2442 2442 @classmethod
2443 2443 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2444 2444 if case_insensitive:
2445 2445 gr = cls.query().filter(func.lower(cls.group_name)
2446 2446 == func.lower(group_name))
2447 2447 else:
2448 2448 gr = cls.query().filter(cls.group_name == group_name)
2449 2449 if cache:
2450 2450 name_key = _hash_key(group_name)
2451 2451 gr = gr.options(
2452 2452 FromCache("sql_cache_short", "get_group_%s" % name_key))
2453 2453 return gr.scalar()
2454 2454
2455 2455 @classmethod
2456 2456 def get_user_personal_repo_group(cls, user_id):
2457 2457 user = User.get(user_id)
2458 2458 if user.username == User.DEFAULT_USER:
2459 2459 return None
2460 2460
2461 2461 return cls.query()\
2462 2462 .filter(cls.personal == true()) \
2463 2463 .filter(cls.user == user).scalar()
2464 2464
2465 2465 @classmethod
2466 2466 def get_all_repo_groups(cls, user_id=Optional(None), group_id=Optional(None),
2467 2467 case_insensitive=True):
2468 2468 q = RepoGroup.query()
2469 2469
2470 2470 if not isinstance(user_id, Optional):
2471 2471 q = q.filter(RepoGroup.user_id == user_id)
2472 2472
2473 2473 if not isinstance(group_id, Optional):
2474 2474 q = q.filter(RepoGroup.group_parent_id == group_id)
2475 2475
2476 2476 if case_insensitive:
2477 2477 q = q.order_by(func.lower(RepoGroup.group_name))
2478 2478 else:
2479 2479 q = q.order_by(RepoGroup.group_name)
2480 2480 return q.all()
2481 2481
2482 2482 @property
2483 2483 def parents(self):
2484 2484 parents_recursion_limit = 10
2485 2485 groups = []
2486 2486 if self.parent_group is None:
2487 2487 return groups
2488 2488 cur_gr = self.parent_group
2489 2489 groups.insert(0, cur_gr)
2490 2490 cnt = 0
2491 2491 while 1:
2492 2492 cnt += 1
2493 2493 gr = getattr(cur_gr, 'parent_group', None)
2494 2494 cur_gr = cur_gr.parent_group
2495 2495 if gr is None:
2496 2496 break
2497 2497 if cnt == parents_recursion_limit:
2498 2498 # this will prevent accidental infinit loops
2499 2499 log.error(('more than %s parents found for group %s, stopping '
2500 2500 'recursive parent fetching' % (parents_recursion_limit, self)))
2501 2501 break
2502 2502
2503 2503 groups.insert(0, gr)
2504 2504 return groups
2505 2505
2506 2506 @property
2507 2507 def last_db_change(self):
2508 2508 return self.updated_on
2509 2509
2510 2510 @property
2511 2511 def children(self):
2512 2512 return RepoGroup.query().filter(RepoGroup.parent_group == self)
2513 2513
2514 2514 @property
2515 2515 def name(self):
2516 2516 return self.group_name.split(RepoGroup.url_sep())[-1]
2517 2517
2518 2518 @property
2519 2519 def full_path(self):
2520 2520 return self.group_name
2521 2521
2522 2522 @property
2523 2523 def full_path_splitted(self):
2524 2524 return self.group_name.split(RepoGroup.url_sep())
2525 2525
2526 2526 @property
2527 2527 def repositories(self):
2528 2528 return Repository.query()\
2529 2529 .filter(Repository.group == self)\
2530 2530 .order_by(Repository.repo_name)
2531 2531
2532 2532 @property
2533 2533 def repositories_recursive_count(self):
2534 2534 cnt = self.repositories.count()
2535 2535
2536 2536 def children_count(group):
2537 2537 cnt = 0
2538 2538 for child in group.children:
2539 2539 cnt += child.repositories.count()
2540 2540 cnt += children_count(child)
2541 2541 return cnt
2542 2542
2543 2543 return cnt + children_count(self)
2544 2544
2545 2545 def _recursive_objects(self, include_repos=True):
2546 2546 all_ = []
2547 2547
2548 2548 def _get_members(root_gr):
2549 2549 if include_repos:
2550 2550 for r in root_gr.repositories:
2551 2551 all_.append(r)
2552 2552 childs = root_gr.children.all()
2553 2553 if childs:
2554 2554 for gr in childs:
2555 2555 all_.append(gr)
2556 2556 _get_members(gr)
2557 2557
2558 2558 _get_members(self)
2559 2559 return [self] + all_
2560 2560
2561 2561 def recursive_groups_and_repos(self):
2562 2562 """
2563 2563 Recursive return all groups, with repositories in those groups
2564 2564 """
2565 2565 return self._recursive_objects()
2566 2566
2567 2567 def recursive_groups(self):
2568 2568 """
2569 2569 Returns all children groups for this group including children of children
2570 2570 """
2571 2571 return self._recursive_objects(include_repos=False)
2572 2572
2573 2573 def get_new_name(self, group_name):
2574 2574 """
2575 2575 returns new full group name based on parent and new name
2576 2576
2577 2577 :param group_name:
2578 2578 """
2579 2579 path_prefix = (self.parent_group.full_path_splitted if
2580 2580 self.parent_group else [])
2581 2581 return RepoGroup.url_sep().join(path_prefix + [group_name])
2582 2582
2583 2583 def permissions(self, with_admins=True, with_owner=True):
2584 2584 q = UserRepoGroupToPerm.query().filter(UserRepoGroupToPerm.group == self)
2585 2585 q = q.options(joinedload(UserRepoGroupToPerm.group),
2586 2586 joinedload(UserRepoGroupToPerm.user),
2587 2587 joinedload(UserRepoGroupToPerm.permission),)
2588 2588
2589 2589 # get owners and admins and permissions. We do a trick of re-writing
2590 2590 # objects from sqlalchemy to named-tuples due to sqlalchemy session
2591 2591 # has a global reference and changing one object propagates to all
2592 2592 # others. This means if admin is also an owner admin_row that change
2593 2593 # would propagate to both objects
2594 2594 perm_rows = []
2595 2595 for _usr in q.all():
2596 2596 usr = AttributeDict(_usr.user.get_dict())
2597 2597 usr.permission = _usr.permission.permission_name
2598 2598 perm_rows.append(usr)
2599 2599
2600 2600 # filter the perm rows by 'default' first and then sort them by
2601 2601 # admin,write,read,none permissions sorted again alphabetically in
2602 2602 # each group
2603 2603 perm_rows = sorted(perm_rows, key=display_user_sort)
2604 2604
2605 2605 _admin_perm = 'group.admin'
2606 2606 owner_row = []
2607 2607 if with_owner:
2608 2608 usr = AttributeDict(self.user.get_dict())
2609 2609 usr.owner_row = True
2610 2610 usr.permission = _admin_perm
2611 2611 owner_row.append(usr)
2612 2612
2613 2613 super_admin_rows = []
2614 2614 if with_admins:
2615 2615 for usr in User.get_all_super_admins():
2616 2616 # if this admin is also owner, don't double the record
2617 2617 if usr.user_id == owner_row[0].user_id:
2618 2618 owner_row[0].admin_row = True
2619 2619 else:
2620 2620 usr = AttributeDict(usr.get_dict())
2621 2621 usr.admin_row = True
2622 2622 usr.permission = _admin_perm
2623 2623 super_admin_rows.append(usr)
2624 2624
2625 2625 return super_admin_rows + owner_row + perm_rows
2626 2626
2627 2627 def permission_user_groups(self):
2628 2628 q = UserGroupRepoGroupToPerm.query().filter(UserGroupRepoGroupToPerm.group == self)
2629 2629 q = q.options(joinedload(UserGroupRepoGroupToPerm.group),
2630 2630 joinedload(UserGroupRepoGroupToPerm.users_group),
2631 2631 joinedload(UserGroupRepoGroupToPerm.permission),)
2632 2632
2633 2633 perm_rows = []
2634 2634 for _user_group in q.all():
2635 2635 usr = AttributeDict(_user_group.users_group.get_dict())
2636 2636 usr.permission = _user_group.permission.permission_name
2637 2637 perm_rows.append(usr)
2638 2638
2639 2639 perm_rows = sorted(perm_rows, key=display_user_group_sort)
2640 2640 return perm_rows
2641 2641
2642 2642 def get_api_data(self):
2643 2643 """
2644 2644 Common function for generating api data
2645 2645
2646 2646 """
2647 2647 group = self
2648 2648 data = {
2649 2649 'group_id': group.group_id,
2650 2650 'group_name': group.group_name,
2651 2651 'group_description': group.description_safe,
2652 2652 'parent_group': group.parent_group.group_name if group.parent_group else None,
2653 2653 'repositories': [x.repo_name for x in group.repositories],
2654 2654 'owner': group.user.username,
2655 2655 }
2656 2656 return data
2657 2657
2658 2658
2659 2659 class Permission(Base, BaseModel):
2660 2660 __tablename__ = 'permissions'
2661 2661 __table_args__ = (
2662 2662 Index('p_perm_name_idx', 'permission_name'),
2663 2663 base_table_args,
2664 2664 )
2665 2665
2666 2666 PERMS = [
2667 2667 ('hg.admin', _('RhodeCode Super Administrator')),
2668 2668
2669 2669 ('repository.none', _('Repository no access')),
2670 2670 ('repository.read', _('Repository read access')),
2671 2671 ('repository.write', _('Repository write access')),
2672 2672 ('repository.admin', _('Repository admin access')),
2673 2673
2674 2674 ('group.none', _('Repository group no access')),
2675 2675 ('group.read', _('Repository group read access')),
2676 2676 ('group.write', _('Repository group write access')),
2677 2677 ('group.admin', _('Repository group admin access')),
2678 2678
2679 2679 ('usergroup.none', _('User group no access')),
2680 2680 ('usergroup.read', _('User group read access')),
2681 2681 ('usergroup.write', _('User group write access')),
2682 2682 ('usergroup.admin', _('User group admin access')),
2683 2683
2684 2684 ('hg.repogroup.create.false', _('Repository Group creation disabled')),
2685 2685 ('hg.repogroup.create.true', _('Repository Group creation enabled')),
2686 2686
2687 2687 ('hg.usergroup.create.false', _('User Group creation disabled')),
2688 2688 ('hg.usergroup.create.true', _('User Group creation enabled')),
2689 2689
2690 2690 ('hg.create.none', _('Repository creation disabled')),
2691 2691 ('hg.create.repository', _('Repository creation enabled')),
2692 2692 ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')),
2693 2693 ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')),
2694 2694
2695 2695 ('hg.fork.none', _('Repository forking disabled')),
2696 2696 ('hg.fork.repository', _('Repository forking enabled')),
2697 2697
2698 2698 ('hg.register.none', _('Registration disabled')),
2699 2699 ('hg.register.manual_activate', _('User Registration with manual account activation')),
2700 2700 ('hg.register.auto_activate', _('User Registration with automatic account activation')),
2701 2701
2702 2702 ('hg.password_reset.enabled', _('Password reset enabled')),
2703 2703 ('hg.password_reset.hidden', _('Password reset hidden')),
2704 2704 ('hg.password_reset.disabled', _('Password reset disabled')),
2705 2705
2706 2706 ('hg.extern_activate.manual', _('Manual activation of external account')),
2707 2707 ('hg.extern_activate.auto', _('Automatic activation of external account')),
2708 2708
2709 2709 ('hg.inherit_default_perms.false', _('Inherit object permissions from default user disabled')),
2710 2710 ('hg.inherit_default_perms.true', _('Inherit object permissions from default user enabled')),
2711 2711 ]
2712 2712
2713 2713 # definition of system default permissions for DEFAULT user
2714 2714 DEFAULT_USER_PERMISSIONS = [
2715 2715 'repository.read',
2716 2716 'group.read',
2717 2717 'usergroup.read',
2718 2718 'hg.create.repository',
2719 2719 'hg.repogroup.create.false',
2720 2720 'hg.usergroup.create.false',
2721 2721 'hg.create.write_on_repogroup.true',
2722 2722 'hg.fork.repository',
2723 2723 'hg.register.manual_activate',
2724 2724 'hg.password_reset.enabled',
2725 2725 'hg.extern_activate.auto',
2726 2726 'hg.inherit_default_perms.true',
2727 2727 ]
2728 2728
2729 2729 # defines which permissions are more important higher the more important
2730 2730 # Weight defines which permissions are more important.
2731 2731 # The higher number the more important.
2732 2732 PERM_WEIGHTS = {
2733 2733 'repository.none': 0,
2734 2734 'repository.read': 1,
2735 2735 'repository.write': 3,
2736 2736 'repository.admin': 4,
2737 2737
2738 2738 'group.none': 0,
2739 2739 'group.read': 1,
2740 2740 'group.write': 3,
2741 2741 'group.admin': 4,
2742 2742
2743 2743 'usergroup.none': 0,
2744 2744 'usergroup.read': 1,
2745 2745 'usergroup.write': 3,
2746 2746 'usergroup.admin': 4,
2747 2747
2748 2748 'hg.repogroup.create.false': 0,
2749 2749 'hg.repogroup.create.true': 1,
2750 2750
2751 2751 'hg.usergroup.create.false': 0,
2752 2752 'hg.usergroup.create.true': 1,
2753 2753
2754 2754 'hg.fork.none': 0,
2755 2755 'hg.fork.repository': 1,
2756 2756 'hg.create.none': 0,
2757 2757 'hg.create.repository': 1
2758 2758 }
2759 2759
2760 2760 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2761 2761 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
2762 2762 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
2763 2763
2764 2764 def __unicode__(self):
2765 2765 return u"<%s('%s:%s')>" % (
2766 2766 self.__class__.__name__, self.permission_id, self.permission_name
2767 2767 )
2768 2768
2769 2769 @classmethod
2770 2770 def get_by_key(cls, key):
2771 2771 return cls.query().filter(cls.permission_name == key).scalar()
2772 2772
2773 2773 @classmethod
2774 2774 def get_default_repo_perms(cls, user_id, repo_id=None):
2775 2775 q = Session().query(UserRepoToPerm, Repository, Permission)\
2776 2776 .join((Permission, UserRepoToPerm.permission_id == Permission.permission_id))\
2777 2777 .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
2778 2778 .filter(UserRepoToPerm.user_id == user_id)
2779 2779 if repo_id:
2780 2780 q = q.filter(UserRepoToPerm.repository_id == repo_id)
2781 2781 return q.all()
2782 2782
2783 2783 @classmethod
2784 2784 def get_default_repo_perms_from_user_group(cls, user_id, repo_id=None):
2785 2785 q = Session().query(UserGroupRepoToPerm, Repository, Permission)\
2786 2786 .join(
2787 2787 Permission,
2788 2788 UserGroupRepoToPerm.permission_id == Permission.permission_id)\
2789 2789 .join(
2790 2790 Repository,
2791 2791 UserGroupRepoToPerm.repository_id == Repository.repo_id)\
2792 2792 .join(
2793 2793 UserGroup,
2794 2794 UserGroupRepoToPerm.users_group_id ==
2795 2795 UserGroup.users_group_id)\
2796 2796 .join(
2797 2797 UserGroupMember,
2798 2798 UserGroupRepoToPerm.users_group_id ==
2799 2799 UserGroupMember.users_group_id)\
2800 2800 .filter(
2801 2801 UserGroupMember.user_id == user_id,
2802 2802 UserGroup.users_group_active == true())
2803 2803 if repo_id:
2804 2804 q = q.filter(UserGroupRepoToPerm.repository_id == repo_id)
2805 2805 return q.all()
2806 2806
2807 2807 @classmethod
2808 2808 def get_default_group_perms(cls, user_id, repo_group_id=None):
2809 2809 q = Session().query(UserRepoGroupToPerm, RepoGroup, Permission)\
2810 2810 .join((Permission, UserRepoGroupToPerm.permission_id == Permission.permission_id))\
2811 2811 .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id))\
2812 2812 .filter(UserRepoGroupToPerm.user_id == user_id)
2813 2813 if repo_group_id:
2814 2814 q = q.filter(UserRepoGroupToPerm.group_id == repo_group_id)
2815 2815 return q.all()
2816 2816
2817 2817 @classmethod
2818 2818 def get_default_group_perms_from_user_group(
2819 2819 cls, user_id, repo_group_id=None):
2820 2820 q = Session().query(UserGroupRepoGroupToPerm, RepoGroup, Permission)\
2821 2821 .join(
2822 2822 Permission,
2823 2823 UserGroupRepoGroupToPerm.permission_id ==
2824 2824 Permission.permission_id)\
2825 2825 .join(
2826 2826 RepoGroup,
2827 2827 UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id)\
2828 2828 .join(
2829 2829 UserGroup,
2830 2830 UserGroupRepoGroupToPerm.users_group_id ==
2831 2831 UserGroup.users_group_id)\
2832 2832 .join(
2833 2833 UserGroupMember,
2834 2834 UserGroupRepoGroupToPerm.users_group_id ==
2835 2835 UserGroupMember.users_group_id)\
2836 2836 .filter(
2837 2837 UserGroupMember.user_id == user_id,
2838 2838 UserGroup.users_group_active == true())
2839 2839 if repo_group_id:
2840 2840 q = q.filter(UserGroupRepoGroupToPerm.group_id == repo_group_id)
2841 2841 return q.all()
2842 2842
2843 2843 @classmethod
2844 2844 def get_default_user_group_perms(cls, user_id, user_group_id=None):
2845 2845 q = Session().query(UserUserGroupToPerm, UserGroup, Permission)\
2846 2846 .join((Permission, UserUserGroupToPerm.permission_id == Permission.permission_id))\
2847 2847 .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id))\
2848 2848 .filter(UserUserGroupToPerm.user_id == user_id)
2849 2849 if user_group_id:
2850 2850 q = q.filter(UserUserGroupToPerm.user_group_id == user_group_id)
2851 2851 return q.all()
2852 2852
2853 2853 @classmethod
2854 2854 def get_default_user_group_perms_from_user_group(
2855 2855 cls, user_id, user_group_id=None):
2856 2856 TargetUserGroup = aliased(UserGroup, name='target_user_group')
2857 2857 q = Session().query(UserGroupUserGroupToPerm, UserGroup, Permission)\
2858 2858 .join(
2859 2859 Permission,
2860 2860 UserGroupUserGroupToPerm.permission_id ==
2861 2861 Permission.permission_id)\
2862 2862 .join(
2863 2863 TargetUserGroup,
2864 2864 UserGroupUserGroupToPerm.target_user_group_id ==
2865 2865 TargetUserGroup.users_group_id)\
2866 2866 .join(
2867 2867 UserGroup,
2868 2868 UserGroupUserGroupToPerm.user_group_id ==
2869 2869 UserGroup.users_group_id)\
2870 2870 .join(
2871 2871 UserGroupMember,
2872 2872 UserGroupUserGroupToPerm.user_group_id ==
2873 2873 UserGroupMember.users_group_id)\
2874 2874 .filter(
2875 2875 UserGroupMember.user_id == user_id,
2876 2876 UserGroup.users_group_active == true())
2877 2877 if user_group_id:
2878 2878 q = q.filter(
2879 2879 UserGroupUserGroupToPerm.user_group_id == user_group_id)
2880 2880
2881 2881 return q.all()
2882 2882
2883 2883
2884 2884 class UserRepoToPerm(Base, BaseModel):
2885 2885 __tablename__ = 'repo_to_perm'
2886 2886 __table_args__ = (
2887 2887 UniqueConstraint('user_id', 'repository_id', 'permission_id'),
2888 2888 base_table_args
2889 2889 )
2890 2890
2891 2891 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2892 2892 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2893 2893 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2894 2894 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2895 2895
2896 2896 user = relationship('User')
2897 2897 repository = relationship('Repository')
2898 2898 permission = relationship('Permission')
2899 2899
2900 2900 @classmethod
2901 2901 def create(cls, user, repository, permission):
2902 2902 n = cls()
2903 2903 n.user = user
2904 2904 n.repository = repository
2905 2905 n.permission = permission
2906 2906 Session().add(n)
2907 2907 return n
2908 2908
2909 2909 def __unicode__(self):
2910 2910 return u'<%s => %s >' % (self.user, self.repository)
2911 2911
2912 2912
2913 2913 class UserUserGroupToPerm(Base, BaseModel):
2914 2914 __tablename__ = 'user_user_group_to_perm'
2915 2915 __table_args__ = (
2916 2916 UniqueConstraint('user_id', 'user_group_id', 'permission_id'),
2917 2917 base_table_args
2918 2918 )
2919 2919
2920 2920 user_user_group_to_perm_id = Column("user_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2921 2921 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2922 2922 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2923 2923 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2924 2924
2925 2925 user = relationship('User')
2926 2926 user_group = relationship('UserGroup')
2927 2927 permission = relationship('Permission')
2928 2928
2929 2929 @classmethod
2930 2930 def create(cls, user, user_group, permission):
2931 2931 n = cls()
2932 2932 n.user = user
2933 2933 n.user_group = user_group
2934 2934 n.permission = permission
2935 2935 Session().add(n)
2936 2936 return n
2937 2937
2938 2938 def __unicode__(self):
2939 2939 return u'<%s => %s >' % (self.user, self.user_group)
2940 2940
2941 2941
2942 2942 class UserToPerm(Base, BaseModel):
2943 2943 __tablename__ = 'user_to_perm'
2944 2944 __table_args__ = (
2945 2945 UniqueConstraint('user_id', 'permission_id'),
2946 2946 base_table_args
2947 2947 )
2948 2948
2949 2949 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2950 2950 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2951 2951 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2952 2952
2953 2953 user = relationship('User')
2954 2954 permission = relationship('Permission', lazy='joined')
2955 2955
2956 2956 def __unicode__(self):
2957 2957 return u'<%s => %s >' % (self.user, self.permission)
2958 2958
2959 2959
2960 2960 class UserGroupRepoToPerm(Base, BaseModel):
2961 2961 __tablename__ = 'users_group_repo_to_perm'
2962 2962 __table_args__ = (
2963 2963 UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
2964 2964 base_table_args
2965 2965 )
2966 2966
2967 2967 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2968 2968 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2969 2969 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2970 2970 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2971 2971
2972 2972 users_group = relationship('UserGroup')
2973 2973 permission = relationship('Permission')
2974 2974 repository = relationship('Repository')
2975 2975
2976 2976 @classmethod
2977 2977 def create(cls, users_group, repository, permission):
2978 2978 n = cls()
2979 2979 n.users_group = users_group
2980 2980 n.repository = repository
2981 2981 n.permission = permission
2982 2982 Session().add(n)
2983 2983 return n
2984 2984
2985 2985 def __unicode__(self):
2986 2986 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
2987 2987
2988 2988
2989 2989 class UserGroupUserGroupToPerm(Base, BaseModel):
2990 2990 __tablename__ = 'user_group_user_group_to_perm'
2991 2991 __table_args__ = (
2992 2992 UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'),
2993 2993 CheckConstraint('target_user_group_id != user_group_id'),
2994 2994 base_table_args
2995 2995 )
2996 2996
2997 2997 user_group_user_group_to_perm_id = Column("user_group_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2998 2998 target_user_group_id = Column("target_user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2999 2999 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3000 3000 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3001 3001
3002 3002 target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id')
3003 3003 user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id')
3004 3004 permission = relationship('Permission')
3005 3005
3006 3006 @classmethod
3007 3007 def create(cls, target_user_group, user_group, permission):
3008 3008 n = cls()
3009 3009 n.target_user_group = target_user_group
3010 3010 n.user_group = user_group
3011 3011 n.permission = permission
3012 3012 Session().add(n)
3013 3013 return n
3014 3014
3015 3015 def __unicode__(self):
3016 3016 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
3017 3017
3018 3018
3019 3019 class UserGroupToPerm(Base, BaseModel):
3020 3020 __tablename__ = 'users_group_to_perm'
3021 3021 __table_args__ = (
3022 3022 UniqueConstraint('users_group_id', 'permission_id',),
3023 3023 base_table_args
3024 3024 )
3025 3025
3026 3026 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3027 3027 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3028 3028 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3029 3029
3030 3030 users_group = relationship('UserGroup')
3031 3031 permission = relationship('Permission')
3032 3032
3033 3033
3034 3034 class UserRepoGroupToPerm(Base, BaseModel):
3035 3035 __tablename__ = 'user_repo_group_to_perm'
3036 3036 __table_args__ = (
3037 3037 UniqueConstraint('user_id', 'group_id', 'permission_id'),
3038 3038 base_table_args
3039 3039 )
3040 3040
3041 3041 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3042 3042 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3043 3043 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3044 3044 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3045 3045
3046 3046 user = relationship('User')
3047 3047 group = relationship('RepoGroup')
3048 3048 permission = relationship('Permission')
3049 3049
3050 3050 @classmethod
3051 3051 def create(cls, user, repository_group, permission):
3052 3052 n = cls()
3053 3053 n.user = user
3054 3054 n.group = repository_group
3055 3055 n.permission = permission
3056 3056 Session().add(n)
3057 3057 return n
3058 3058
3059 3059
3060 3060 class UserGroupRepoGroupToPerm(Base, BaseModel):
3061 3061 __tablename__ = 'users_group_repo_group_to_perm'
3062 3062 __table_args__ = (
3063 3063 UniqueConstraint('users_group_id', 'group_id'),
3064 3064 base_table_args
3065 3065 )
3066 3066
3067 3067 users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3068 3068 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
3069 3069 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
3070 3070 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
3071 3071
3072 3072 users_group = relationship('UserGroup')
3073 3073 permission = relationship('Permission')
3074 3074 group = relationship('RepoGroup')
3075 3075
3076 3076 @classmethod
3077 3077 def create(cls, user_group, repository_group, permission):
3078 3078 n = cls()
3079 3079 n.users_group = user_group
3080 3080 n.group = repository_group
3081 3081 n.permission = permission
3082 3082 Session().add(n)
3083 3083 return n
3084 3084
3085 3085 def __unicode__(self):
3086 3086 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
3087 3087
3088 3088
3089 3089 class Statistics(Base, BaseModel):
3090 3090 __tablename__ = 'statistics'
3091 3091 __table_args__ = (
3092 3092 base_table_args
3093 3093 )
3094 3094
3095 3095 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3096 3096 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
3097 3097 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
3098 3098 commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
3099 3099 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
3100 3100 languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
3101 3101
3102 3102 repository = relationship('Repository', single_parent=True)
3103 3103
3104 3104
3105 3105 class UserFollowing(Base, BaseModel):
3106 3106 __tablename__ = 'user_followings'
3107 3107 __table_args__ = (
3108 3108 UniqueConstraint('user_id', 'follows_repository_id'),
3109 3109 UniqueConstraint('user_id', 'follows_user_id'),
3110 3110 base_table_args
3111 3111 )
3112 3112
3113 3113 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3114 3114 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
3115 3115 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
3116 3116 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
3117 3117 follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
3118 3118
3119 3119 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
3120 3120
3121 3121 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
3122 3122 follows_repository = relationship('Repository', order_by='Repository.repo_name')
3123 3123
3124 3124 @classmethod
3125 3125 def get_repo_followers(cls, repo_id):
3126 3126 return cls.query().filter(cls.follows_repo_id == repo_id)
3127 3127
3128 3128
3129 3129 class CacheKey(Base, BaseModel):
3130 3130 __tablename__ = 'cache_invalidation'
3131 3131 __table_args__ = (
3132 3132 UniqueConstraint('cache_key'),
3133 3133 Index('key_idx', 'cache_key'),
3134 3134 base_table_args,
3135 3135 )
3136 3136
3137 3137 CACHE_TYPE_ATOM = 'ATOM'
3138 3138 CACHE_TYPE_RSS = 'RSS'
3139 3139 CACHE_TYPE_README = 'README'
3140 3140
3141 3141 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
3142 3142 cache_key = Column("cache_key", String(255), nullable=True, unique=None, default=None)
3143 3143 cache_args = Column("cache_args", String(255), nullable=True, unique=None, default=None)
3144 3144 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
3145 3145
3146 3146 def __init__(self, cache_key, cache_args=''):
3147 3147 self.cache_key = cache_key
3148 3148 self.cache_args = cache_args
3149 3149 self.cache_active = False
3150 3150
3151 3151 def __unicode__(self):
3152 3152 return u"<%s('%s:%s[%s]')>" % (
3153 3153 self.__class__.__name__,
3154 3154 self.cache_id, self.cache_key, self.cache_active)
3155 3155
3156 3156 def _cache_key_partition(self):
3157 3157 prefix, repo_name, suffix = self.cache_key.partition(self.cache_args)
3158 3158 return prefix, repo_name, suffix
3159 3159
3160 3160 def get_prefix(self):
3161 3161 """
3162 3162 Try to extract prefix from existing cache key. The key could consist
3163 3163 of prefix, repo_name, suffix
3164 3164 """
3165 3165 # this returns prefix, repo_name, suffix
3166 3166 return self._cache_key_partition()[0]
3167 3167
3168 3168 def get_suffix(self):
3169 3169 """
3170 3170 get suffix that might have been used in _get_cache_key to
3171 3171 generate self.cache_key. Only used for informational purposes
3172 3172 in repo_edit.mako.
3173 3173 """
3174 3174 # prefix, repo_name, suffix
3175 3175 return self._cache_key_partition()[2]
3176 3176
3177 3177 @classmethod
3178 3178 def delete_all_cache(cls):
3179 3179 """
3180 3180 Delete all cache keys from database.
3181 3181 Should only be run when all instances are down and all entries
3182 3182 thus stale.
3183 3183 """
3184 3184 cls.query().delete()
3185 3185 Session().commit()
3186 3186
3187 3187 @classmethod
3188 3188 def get_cache_key(cls, repo_name, cache_type):
3189 3189 """
3190 3190
3191 3191 Generate a cache key for this process of RhodeCode instance.
3192 3192 Prefix most likely will be process id or maybe explicitly set
3193 3193 instance_id from .ini file.
3194 3194 """
3195 3195 import rhodecode
3196 3196 prefix = safe_unicode(rhodecode.CONFIG.get('instance_id') or '')
3197 3197
3198 3198 repo_as_unicode = safe_unicode(repo_name)
3199 3199 key = u'{}_{}'.format(repo_as_unicode, cache_type) \
3200 3200 if cache_type else repo_as_unicode
3201 3201
3202 3202 return u'{}{}'.format(prefix, key)
3203 3203
3204 3204 @classmethod
3205 3205 def set_invalidate(cls, repo_name, delete=False):
3206 3206 """
3207 3207 Mark all caches of a repo as invalid in the database.
3208 3208 """
3209 3209
3210 3210 try:
3211 3211 qry = Session().query(cls).filter(cls.cache_args == repo_name)
3212 3212 if delete:
3213 3213 log.debug('cache objects deleted for repo %s',
3214 3214 safe_str(repo_name))
3215 3215 qry.delete()
3216 3216 else:
3217 3217 log.debug('cache objects marked as invalid for repo %s',
3218 3218 safe_str(repo_name))
3219 3219 qry.update({"cache_active": False})
3220 3220
3221 3221 Session().commit()
3222 3222 except Exception:
3223 3223 log.exception(
3224 3224 'Cache key invalidation failed for repository %s',
3225 3225 safe_str(repo_name))
3226 3226 Session().rollback()
3227 3227
3228 3228 @classmethod
3229 3229 def get_active_cache(cls, cache_key):
3230 3230 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
3231 3231 if inv_obj:
3232 3232 return inv_obj
3233 3233 return None
3234 3234
3235 3235 @classmethod
3236 3236 def repo_context_cache(cls, compute_func, repo_name, cache_type,
3237 3237 thread_scoped=False):
3238 3238 """
3239 3239 @cache_region('long_term')
3240 3240 def _heavy_calculation(cache_key):
3241 3241 return 'result'
3242 3242
3243 3243 cache_context = CacheKey.repo_context_cache(
3244 3244 _heavy_calculation, repo_name, cache_type)
3245 3245
3246 3246 with cache_context as context:
3247 3247 context.invalidate()
3248 3248 computed = context.compute()
3249 3249
3250 3250 assert computed == 'result'
3251 3251 """
3252 3252 from rhodecode.lib import caches
3253 3253 return caches.InvalidationContext(
3254 3254 compute_func, repo_name, cache_type, thread_scoped=thread_scoped)
3255 3255
3256 3256
3257 3257 class ChangesetComment(Base, BaseModel):
3258 3258 __tablename__ = 'changeset_comments'
3259 3259 __table_args__ = (
3260 3260 Index('cc_revision_idx', 'revision'),
3261 3261 base_table_args,
3262 3262 )
3263 3263
3264 3264 COMMENT_OUTDATED = u'comment_outdated'
3265 3265 COMMENT_TYPE_NOTE = u'note'
3266 3266 COMMENT_TYPE_TODO = u'todo'
3267 3267 COMMENT_TYPES = [COMMENT_TYPE_NOTE, COMMENT_TYPE_TODO]
3268 3268
3269 3269 comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
3270 3270 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3271 3271 revision = Column('revision', String(40), nullable=True)
3272 3272 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3273 3273 pull_request_version_id = Column("pull_request_version_id", Integer(), ForeignKey('pull_request_versions.pull_request_version_id'), nullable=True)
3274 3274 line_no = Column('line_no', Unicode(10), nullable=True)
3275 3275 hl_lines = Column('hl_lines', Unicode(512), nullable=True)
3276 3276 f_path = Column('f_path', Unicode(1000), nullable=True)
3277 3277 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
3278 3278 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
3279 3279 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3280 3280 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3281 3281 renderer = Column('renderer', Unicode(64), nullable=True)
3282 3282 display_state = Column('display_state', Unicode(128), nullable=True)
3283 3283
3284 3284 comment_type = Column('comment_type', Unicode(128), nullable=True, default=COMMENT_TYPE_NOTE)
3285 3285 resolved_comment_id = Column('resolved_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'), nullable=True)
3286 3286 resolved_comment = relationship('ChangesetComment', remote_side=comment_id, backref='resolved_by')
3287 3287 author = relationship('User', lazy='joined')
3288 3288 repo = relationship('Repository')
3289 3289 status_change = relationship('ChangesetStatus', cascade="all, delete, delete-orphan", lazy='joined')
3290 3290 pull_request = relationship('PullRequest', lazy='joined')
3291 3291 pull_request_version = relationship('PullRequestVersion')
3292 3292
3293 3293 @classmethod
3294 3294 def get_users(cls, revision=None, pull_request_id=None):
3295 3295 """
3296 3296 Returns user associated with this ChangesetComment. ie those
3297 3297 who actually commented
3298 3298
3299 3299 :param cls:
3300 3300 :param revision:
3301 3301 """
3302 3302 q = Session().query(User)\
3303 3303 .join(ChangesetComment.author)
3304 3304 if revision:
3305 3305 q = q.filter(cls.revision == revision)
3306 3306 elif pull_request_id:
3307 3307 q = q.filter(cls.pull_request_id == pull_request_id)
3308 3308 return q.all()
3309 3309
3310 3310 @classmethod
3311 3311 def get_index_from_version(cls, pr_version, versions):
3312 3312 num_versions = [x.pull_request_version_id for x in versions]
3313 3313 try:
3314 3314 return num_versions.index(pr_version) +1
3315 3315 except (IndexError, ValueError):
3316 3316 return
3317 3317
3318 3318 @property
3319 3319 def outdated(self):
3320 3320 return self.display_state == self.COMMENT_OUTDATED
3321 3321
3322 3322 def outdated_at_version(self, version):
3323 3323 """
3324 3324 Checks if comment is outdated for given pull request version
3325 3325 """
3326 3326 return self.outdated and self.pull_request_version_id != version
3327 3327
3328 3328 def older_than_version(self, version):
3329 3329 """
3330 3330 Checks if comment is made from previous version than given
3331 3331 """
3332 3332 if version is None:
3333 3333 return self.pull_request_version_id is not None
3334 3334
3335 3335 return self.pull_request_version_id < version
3336 3336
3337 3337 @property
3338 3338 def resolved(self):
3339 3339 return self.resolved_by[0] if self.resolved_by else None
3340 3340
3341 3341 @property
3342 3342 def is_todo(self):
3343 3343 return self.comment_type == self.COMMENT_TYPE_TODO
3344 3344
3345 3345 @property
3346 3346 def is_inline(self):
3347 3347 return self.line_no and self.f_path
3348 3348
3349 3349 def get_index_version(self, versions):
3350 3350 return self.get_index_from_version(
3351 3351 self.pull_request_version_id, versions)
3352 3352
3353 3353 def __repr__(self):
3354 3354 if self.comment_id:
3355 3355 return '<DB:Comment #%s>' % self.comment_id
3356 3356 else:
3357 3357 return '<DB:Comment at %#x>' % id(self)
3358 3358
3359 3359 def get_api_data(self):
3360 3360 comment = self
3361 3361 data = {
3362 3362 'comment_id': comment.comment_id,
3363 3363 'comment_type': comment.comment_type,
3364 3364 'comment_text': comment.text,
3365 3365 'comment_status': comment.status_change,
3366 3366 'comment_f_path': comment.f_path,
3367 3367 'comment_lineno': comment.line_no,
3368 3368 'comment_author': comment.author,
3369 3369 'comment_created_on': comment.created_on
3370 3370 }
3371 3371 return data
3372 3372
3373 3373 def __json__(self):
3374 3374 data = dict()
3375 3375 data.update(self.get_api_data())
3376 3376 return data
3377 3377
3378 3378
3379 3379 class ChangesetStatus(Base, BaseModel):
3380 3380 __tablename__ = 'changeset_statuses'
3381 3381 __table_args__ = (
3382 3382 Index('cs_revision_idx', 'revision'),
3383 3383 Index('cs_version_idx', 'version'),
3384 3384 UniqueConstraint('repo_id', 'revision', 'version'),
3385 3385 base_table_args
3386 3386 )
3387 3387
3388 3388 STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed'
3389 3389 STATUS_APPROVED = 'approved'
3390 3390 STATUS_REJECTED = 'rejected'
3391 3391 STATUS_UNDER_REVIEW = 'under_review'
3392 3392
3393 3393 STATUSES = [
3394 3394 (STATUS_NOT_REVIEWED, _("Not Reviewed")), # (no icon) and default
3395 3395 (STATUS_APPROVED, _("Approved")),
3396 3396 (STATUS_REJECTED, _("Rejected")),
3397 3397 (STATUS_UNDER_REVIEW, _("Under Review")),
3398 3398 ]
3399 3399
3400 3400 changeset_status_id = Column('changeset_status_id', Integer(), nullable=False, primary_key=True)
3401 3401 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
3402 3402 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None)
3403 3403 revision = Column('revision', String(40), nullable=False)
3404 3404 status = Column('status', String(128), nullable=False, default=DEFAULT)
3405 3405 changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'))
3406 3406 modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now)
3407 3407 version = Column('version', Integer(), nullable=False, default=0)
3408 3408 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
3409 3409
3410 3410 author = relationship('User', lazy='joined')
3411 3411 repo = relationship('Repository')
3412 3412 comment = relationship('ChangesetComment', lazy='joined')
3413 3413 pull_request = relationship('PullRequest', lazy='joined')
3414 3414
3415 3415 def __unicode__(self):
3416 3416 return u"<%s('%s[v%s]:%s')>" % (
3417 3417 self.__class__.__name__,
3418 3418 self.status, self.version, self.author
3419 3419 )
3420 3420
3421 3421 @classmethod
3422 3422 def get_status_lbl(cls, value):
3423 3423 return dict(cls.STATUSES).get(value)
3424 3424
3425 3425 @property
3426 3426 def status_lbl(self):
3427 3427 return ChangesetStatus.get_status_lbl(self.status)
3428 3428
3429 3429 def get_api_data(self):
3430 3430 status = self
3431 3431 data = {
3432 3432 'status_id': status.changeset_status_id,
3433 3433 'status': status.status,
3434 3434 }
3435 3435 return data
3436 3436
3437 3437 def __json__(self):
3438 3438 data = dict()
3439 3439 data.update(self.get_api_data())
3440 3440 return data
3441 3441
3442 3442
3443 3443 class _PullRequestBase(BaseModel):
3444 3444 """
3445 3445 Common attributes of pull request and version entries.
3446 3446 """
3447 3447
3448 3448 # .status values
3449 3449 STATUS_NEW = u'new'
3450 3450 STATUS_OPEN = u'open'
3451 3451 STATUS_CLOSED = u'closed'
3452 3452
3453 3453 title = Column('title', Unicode(255), nullable=True)
3454 3454 description = Column(
3455 3455 'description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'),
3456 3456 nullable=True)
3457 description_renderer = Column('description_renderer', Unicode(64), nullable=True)
3458
3457 3459 # new/open/closed status of pull request (not approve/reject/etc)
3458 3460 status = Column('status', Unicode(255), nullable=False, default=STATUS_NEW)
3459 3461 created_on = Column(
3460 3462 'created_on', DateTime(timezone=False), nullable=False,
3461 3463 default=datetime.datetime.now)
3462 3464 updated_on = Column(
3463 3465 'updated_on', DateTime(timezone=False), nullable=False,
3464 3466 default=datetime.datetime.now)
3465 3467
3466 3468 @declared_attr
3467 3469 def user_id(cls):
3468 3470 return Column(
3469 3471 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
3470 3472 unique=None)
3471 3473
3472 3474 # 500 revisions max
3473 3475 _revisions = Column(
3474 3476 'revisions', UnicodeText().with_variant(UnicodeText(20500), 'mysql'))
3475 3477
3476 3478 @declared_attr
3477 3479 def source_repo_id(cls):
3478 3480 # TODO: dan: rename column to source_repo_id
3479 3481 return Column(
3480 3482 'org_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3481 3483 nullable=False)
3482 3484
3483 3485 source_ref = Column('org_ref', Unicode(255), nullable=False)
3484 3486
3485 3487 @declared_attr
3486 3488 def target_repo_id(cls):
3487 3489 # TODO: dan: rename column to target_repo_id
3488 3490 return Column(
3489 3491 'other_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3490 3492 nullable=False)
3491 3493
3492 3494 target_ref = Column('other_ref', Unicode(255), nullable=False)
3493 3495 _shadow_merge_ref = Column('shadow_merge_ref', Unicode(255), nullable=True)
3494 3496
3495 3497 # TODO: dan: rename column to last_merge_source_rev
3496 3498 _last_merge_source_rev = Column(
3497 3499 'last_merge_org_rev', String(40), nullable=True)
3498 3500 # TODO: dan: rename column to last_merge_target_rev
3499 3501 _last_merge_target_rev = Column(
3500 3502 'last_merge_other_rev', String(40), nullable=True)
3501 3503 _last_merge_status = Column('merge_status', Integer(), nullable=True)
3502 3504 merge_rev = Column('merge_rev', String(40), nullable=True)
3503 3505
3504 3506 reviewer_data = Column(
3505 3507 'reviewer_data_json', MutationObj.as_mutable(
3506 3508 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
3507 3509
3508 3510 @property
3509 3511 def reviewer_data_json(self):
3510 3512 return json.dumps(self.reviewer_data)
3511 3513
3512 3514 @hybrid_property
3513 3515 def description_safe(self):
3514 3516 from rhodecode.lib import helpers as h
3515 3517 return h.escape(self.description)
3516 3518
3517 3519 @hybrid_property
3518 3520 def revisions(self):
3519 3521 return self._revisions.split(':') if self._revisions else []
3520 3522
3521 3523 @revisions.setter
3522 3524 def revisions(self, val):
3523 3525 self._revisions = ':'.join(val)
3524 3526
3525 3527 @hybrid_property
3526 3528 def last_merge_status(self):
3527 3529 return safe_int(self._last_merge_status)
3528 3530
3529 3531 @last_merge_status.setter
3530 3532 def last_merge_status(self, val):
3531 3533 self._last_merge_status = val
3532 3534
3533 3535 @declared_attr
3534 3536 def author(cls):
3535 3537 return relationship('User', lazy='joined')
3536 3538
3537 3539 @declared_attr
3538 3540 def source_repo(cls):
3539 3541 return relationship(
3540 3542 'Repository',
3541 3543 primaryjoin='%s.source_repo_id==Repository.repo_id' % cls.__name__)
3542 3544
3543 3545 @property
3544 3546 def source_ref_parts(self):
3545 3547 return self.unicode_to_reference(self.source_ref)
3546 3548
3547 3549 @declared_attr
3548 3550 def target_repo(cls):
3549 3551 return relationship(
3550 3552 'Repository',
3551 3553 primaryjoin='%s.target_repo_id==Repository.repo_id' % cls.__name__)
3552 3554
3553 3555 @property
3554 3556 def target_ref_parts(self):
3555 3557 return self.unicode_to_reference(self.target_ref)
3556 3558
3557 3559 @property
3558 3560 def shadow_merge_ref(self):
3559 3561 return self.unicode_to_reference(self._shadow_merge_ref)
3560 3562
3561 3563 @shadow_merge_ref.setter
3562 3564 def shadow_merge_ref(self, ref):
3563 3565 self._shadow_merge_ref = self.reference_to_unicode(ref)
3564 3566
3565 3567 def unicode_to_reference(self, raw):
3566 3568 """
3567 3569 Convert a unicode (or string) to a reference object.
3568 3570 If unicode evaluates to False it returns None.
3569 3571 """
3570 3572 if raw:
3571 3573 refs = raw.split(':')
3572 3574 return Reference(*refs)
3573 3575 else:
3574 3576 return None
3575 3577
3576 3578 def reference_to_unicode(self, ref):
3577 3579 """
3578 3580 Convert a reference object to unicode.
3579 3581 If reference is None it returns None.
3580 3582 """
3581 3583 if ref:
3582 3584 return u':'.join(ref)
3583 3585 else:
3584 3586 return None
3585 3587
3586 3588 def get_api_data(self, with_merge_state=True):
3587 3589 from rhodecode.model.pull_request import PullRequestModel
3588 3590
3589 3591 pull_request = self
3590 3592 if with_merge_state:
3591 3593 merge_status = PullRequestModel().merge_status(pull_request)
3592 3594 merge_state = {
3593 3595 'status': merge_status[0],
3594 3596 'message': safe_unicode(merge_status[1]),
3595 3597 }
3596 3598 else:
3597 3599 merge_state = {'status': 'not_available',
3598 3600 'message': 'not_available'}
3599 3601
3600 3602 merge_data = {
3601 3603 'clone_url': PullRequestModel().get_shadow_clone_url(pull_request),
3602 3604 'reference': (
3603 3605 pull_request.shadow_merge_ref._asdict()
3604 3606 if pull_request.shadow_merge_ref else None),
3605 3607 }
3606 3608
3607 3609 data = {
3608 3610 'pull_request_id': pull_request.pull_request_id,
3609 3611 'url': PullRequestModel().get_url(pull_request),
3610 3612 'title': pull_request.title,
3611 3613 'description': pull_request.description,
3612 3614 'status': pull_request.status,
3613 3615 'created_on': pull_request.created_on,
3614 3616 'updated_on': pull_request.updated_on,
3615 3617 'commit_ids': pull_request.revisions,
3616 3618 'review_status': pull_request.calculated_review_status(),
3617 3619 'mergeable': merge_state,
3618 3620 'source': {
3619 3621 'clone_url': pull_request.source_repo.clone_url(),
3620 3622 'repository': pull_request.source_repo.repo_name,
3621 3623 'reference': {
3622 3624 'name': pull_request.source_ref_parts.name,
3623 3625 'type': pull_request.source_ref_parts.type,
3624 3626 'commit_id': pull_request.source_ref_parts.commit_id,
3625 3627 },
3626 3628 },
3627 3629 'target': {
3628 3630 'clone_url': pull_request.target_repo.clone_url(),
3629 3631 'repository': pull_request.target_repo.repo_name,
3630 3632 'reference': {
3631 3633 'name': pull_request.target_ref_parts.name,
3632 3634 'type': pull_request.target_ref_parts.type,
3633 3635 'commit_id': pull_request.target_ref_parts.commit_id,
3634 3636 },
3635 3637 },
3636 3638 'merge': merge_data,
3637 3639 'author': pull_request.author.get_api_data(include_secrets=False,
3638 3640 details='basic'),
3639 3641 'reviewers': [
3640 3642 {
3641 3643 'user': reviewer.get_api_data(include_secrets=False,
3642 3644 details='basic'),
3643 3645 'reasons': reasons,
3644 3646 'review_status': st[0][1].status if st else 'not_reviewed',
3645 3647 }
3646 3648 for obj, reviewer, reasons, mandatory, st in
3647 3649 pull_request.reviewers_statuses()
3648 3650 ]
3649 3651 }
3650 3652
3651 3653 return data
3652 3654
3653 3655
3654 3656 class PullRequest(Base, _PullRequestBase):
3655 3657 __tablename__ = 'pull_requests'
3656 3658 __table_args__ = (
3657 3659 base_table_args,
3658 3660 )
3659 3661
3660 3662 pull_request_id = Column(
3661 3663 'pull_request_id', Integer(), nullable=False, primary_key=True)
3662 3664
3663 3665 def __repr__(self):
3664 3666 if self.pull_request_id:
3665 3667 return '<DB:PullRequest #%s>' % self.pull_request_id
3666 3668 else:
3667 3669 return '<DB:PullRequest at %#x>' % id(self)
3668 3670
3669 3671 reviewers = relationship('PullRequestReviewers',
3670 3672 cascade="all, delete, delete-orphan")
3671 3673 statuses = relationship('ChangesetStatus',
3672 3674 cascade="all, delete, delete-orphan")
3673 3675 comments = relationship('ChangesetComment',
3674 3676 cascade="all, delete, delete-orphan")
3675 3677 versions = relationship('PullRequestVersion',
3676 3678 cascade="all, delete, delete-orphan",
3677 3679 lazy='dynamic')
3678 3680
3679 3681 @classmethod
3680 3682 def get_pr_display_object(cls, pull_request_obj, org_pull_request_obj,
3681 3683 internal_methods=None):
3682 3684
3683 3685 class PullRequestDisplay(object):
3684 3686 """
3685 3687 Special object wrapper for showing PullRequest data via Versions
3686 3688 It mimics PR object as close as possible. This is read only object
3687 3689 just for display
3688 3690 """
3689 3691
3690 3692 def __init__(self, attrs, internal=None):
3691 3693 self.attrs = attrs
3692 3694 # internal have priority over the given ones via attrs
3693 3695 self.internal = internal or ['versions']
3694 3696
3695 3697 def __getattr__(self, item):
3696 3698 if item in self.internal:
3697 3699 return getattr(self, item)
3698 3700 try:
3699 3701 return self.attrs[item]
3700 3702 except KeyError:
3701 3703 raise AttributeError(
3702 3704 '%s object has no attribute %s' % (self, item))
3703 3705
3704 3706 def __repr__(self):
3705 3707 return '<DB:PullRequestDisplay #%s>' % self.attrs.get('pull_request_id')
3706 3708
3707 3709 def versions(self):
3708 3710 return pull_request_obj.versions.order_by(
3709 3711 PullRequestVersion.pull_request_version_id).all()
3710 3712
3711 3713 def is_closed(self):
3712 3714 return pull_request_obj.is_closed()
3713 3715
3714 3716 @property
3715 3717 def pull_request_version_id(self):
3716 3718 return getattr(pull_request_obj, 'pull_request_version_id', None)
3717 3719
3718 3720 attrs = StrictAttributeDict(pull_request_obj.get_api_data())
3719 3721
3720 3722 attrs.author = StrictAttributeDict(
3721 3723 pull_request_obj.author.get_api_data())
3722 3724 if pull_request_obj.target_repo:
3723 3725 attrs.target_repo = StrictAttributeDict(
3724 3726 pull_request_obj.target_repo.get_api_data())
3725 3727 attrs.target_repo.clone_url = pull_request_obj.target_repo.clone_url
3726 3728
3727 3729 if pull_request_obj.source_repo:
3728 3730 attrs.source_repo = StrictAttributeDict(
3729 3731 pull_request_obj.source_repo.get_api_data())
3730 3732 attrs.source_repo.clone_url = pull_request_obj.source_repo.clone_url
3731 3733
3732 3734 attrs.source_ref_parts = pull_request_obj.source_ref_parts
3733 3735 attrs.target_ref_parts = pull_request_obj.target_ref_parts
3734 3736 attrs.revisions = pull_request_obj.revisions
3735 3737
3736 3738 attrs.shadow_merge_ref = org_pull_request_obj.shadow_merge_ref
3737 3739 attrs.reviewer_data = org_pull_request_obj.reviewer_data
3738 3740 attrs.reviewer_data_json = org_pull_request_obj.reviewer_data_json
3739 3741
3740 3742 return PullRequestDisplay(attrs, internal=internal_methods)
3741 3743
3742 3744 def is_closed(self):
3743 3745 return self.status == self.STATUS_CLOSED
3744 3746
3745 3747 def __json__(self):
3746 3748 return {
3747 3749 'revisions': self.revisions,
3748 3750 }
3749 3751
3750 3752 def calculated_review_status(self):
3751 3753 from rhodecode.model.changeset_status import ChangesetStatusModel
3752 3754 return ChangesetStatusModel().calculated_review_status(self)
3753 3755
3754 3756 def reviewers_statuses(self):
3755 3757 from rhodecode.model.changeset_status import ChangesetStatusModel
3756 3758 return ChangesetStatusModel().reviewers_statuses(self)
3757 3759
3758 3760 @property
3759 3761 def workspace_id(self):
3760 3762 from rhodecode.model.pull_request import PullRequestModel
3761 3763 return PullRequestModel()._workspace_id(self)
3762 3764
3763 3765 def get_shadow_repo(self):
3764 3766 workspace_id = self.workspace_id
3765 3767 vcs_obj = self.target_repo.scm_instance()
3766 3768 shadow_repository_path = vcs_obj._get_shadow_repository_path(
3767 3769 self.target_repo.repo_id, workspace_id)
3768 3770 if os.path.isdir(shadow_repository_path):
3769 3771 return vcs_obj._get_shadow_instance(shadow_repository_path)
3770 3772
3771 3773
3772 3774 class PullRequestVersion(Base, _PullRequestBase):
3773 3775 __tablename__ = 'pull_request_versions'
3774 3776 __table_args__ = (
3775 3777 base_table_args,
3776 3778 )
3777 3779
3778 3780 pull_request_version_id = Column(
3779 3781 'pull_request_version_id', Integer(), nullable=False, primary_key=True)
3780 3782 pull_request_id = Column(
3781 3783 'pull_request_id', Integer(),
3782 3784 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3783 3785 pull_request = relationship('PullRequest')
3784 3786
3785 3787 def __repr__(self):
3786 3788 if self.pull_request_version_id:
3787 3789 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
3788 3790 else:
3789 3791 return '<DB:PullRequestVersion at %#x>' % id(self)
3790 3792
3791 3793 @property
3792 3794 def reviewers(self):
3793 3795 return self.pull_request.reviewers
3794 3796
3795 3797 @property
3796 3798 def versions(self):
3797 3799 return self.pull_request.versions
3798 3800
3799 3801 def is_closed(self):
3800 3802 # calculate from original
3801 3803 return self.pull_request.status == self.STATUS_CLOSED
3802 3804
3803 3805 def calculated_review_status(self):
3804 3806 return self.pull_request.calculated_review_status()
3805 3807
3806 3808 def reviewers_statuses(self):
3807 3809 return self.pull_request.reviewers_statuses()
3808 3810
3809 3811
3810 3812 class PullRequestReviewers(Base, BaseModel):
3811 3813 __tablename__ = 'pull_request_reviewers'
3812 3814 __table_args__ = (
3813 3815 base_table_args,
3814 3816 )
3815 3817
3816 3818 @hybrid_property
3817 3819 def reasons(self):
3818 3820 if not self._reasons:
3819 3821 return []
3820 3822 return self._reasons
3821 3823
3822 3824 @reasons.setter
3823 3825 def reasons(self, val):
3824 3826 val = val or []
3825 3827 if any(not isinstance(x, basestring) for x in val):
3826 3828 raise Exception('invalid reasons type, must be list of strings')
3827 3829 self._reasons = val
3828 3830
3829 3831 pull_requests_reviewers_id = Column(
3830 3832 'pull_requests_reviewers_id', Integer(), nullable=False,
3831 3833 primary_key=True)
3832 3834 pull_request_id = Column(
3833 3835 "pull_request_id", Integer(),
3834 3836 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3835 3837 user_id = Column(
3836 3838 "user_id", Integer(), ForeignKey('users.user_id'), nullable=True)
3837 3839 _reasons = Column(
3838 3840 'reason', MutationList.as_mutable(
3839 3841 JsonType('list', dialect_map=dict(mysql=UnicodeText(16384)))))
3840 3842
3841 3843 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
3842 3844 user = relationship('User')
3843 3845 pull_request = relationship('PullRequest')
3844 3846
3845 3847 rule_data = Column(
3846 3848 'rule_data_json',
3847 3849 JsonType(dialect_map=dict(mysql=UnicodeText(16384))))
3848 3850
3849 3851 def rule_user_group_data(self):
3850 3852 """
3851 3853 Returns the voting user group rule data for this reviewer
3852 3854 """
3853 3855
3854 3856 if self.rule_data and 'vote_rule' in self.rule_data:
3855 3857 user_group_data = {}
3856 3858 if 'rule_user_group_entry_id' in self.rule_data:
3857 3859 # means a group with voting rules !
3858 3860 user_group_data['id'] = self.rule_data['rule_user_group_entry_id']
3859 3861 user_group_data['name'] = self.rule_data['rule_name']
3860 3862 user_group_data['vote_rule'] = self.rule_data['vote_rule']
3861 3863
3862 3864 return user_group_data
3863 3865
3864 3866 def __unicode__(self):
3865 3867 return u"<%s('id:%s')>" % (self.__class__.__name__,
3866 3868 self.pull_requests_reviewers_id)
3867 3869
3868 3870
3869 3871 class Notification(Base, BaseModel):
3870 3872 __tablename__ = 'notifications'
3871 3873 __table_args__ = (
3872 3874 Index('notification_type_idx', 'type'),
3873 3875 base_table_args,
3874 3876 )
3875 3877
3876 3878 TYPE_CHANGESET_COMMENT = u'cs_comment'
3877 3879 TYPE_MESSAGE = u'message'
3878 3880 TYPE_MENTION = u'mention'
3879 3881 TYPE_REGISTRATION = u'registration'
3880 3882 TYPE_PULL_REQUEST = u'pull_request'
3881 3883 TYPE_PULL_REQUEST_COMMENT = u'pull_request_comment'
3882 3884
3883 3885 notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
3884 3886 subject = Column('subject', Unicode(512), nullable=True)
3885 3887 body = Column('body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
3886 3888 created_by = Column("created_by", Integer(), ForeignKey('users.user_id'), nullable=True)
3887 3889 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3888 3890 type_ = Column('type', Unicode(255))
3889 3891
3890 3892 created_by_user = relationship('User')
3891 3893 notifications_to_users = relationship('UserNotification', lazy='joined',
3892 3894 cascade="all, delete, delete-orphan")
3893 3895
3894 3896 @property
3895 3897 def recipients(self):
3896 3898 return [x.user for x in UserNotification.query()\
3897 3899 .filter(UserNotification.notification == self)\
3898 3900 .order_by(UserNotification.user_id.asc()).all()]
3899 3901
3900 3902 @classmethod
3901 3903 def create(cls, created_by, subject, body, recipients, type_=None):
3902 3904 if type_ is None:
3903 3905 type_ = Notification.TYPE_MESSAGE
3904 3906
3905 3907 notification = cls()
3906 3908 notification.created_by_user = created_by
3907 3909 notification.subject = subject
3908 3910 notification.body = body
3909 3911 notification.type_ = type_
3910 3912 notification.created_on = datetime.datetime.now()
3911 3913
3912 3914 # For each recipient link the created notification to his account
3913 3915 for u in recipients:
3914 3916 assoc = UserNotification()
3915 3917 assoc.user_id = u.user_id
3916 3918 assoc.notification = notification
3917 3919
3918 3920 # if created_by is inside recipients mark his notification
3919 3921 # as read
3920 3922 if u.user_id == created_by.user_id:
3921 3923 assoc.read = True
3922 3924 Session().add(assoc)
3923 3925
3924 3926 Session().add(notification)
3925 3927
3926 3928 return notification
3927 3929
3928 3930
3929 3931 class UserNotification(Base, BaseModel):
3930 3932 __tablename__ = 'user_to_notification'
3931 3933 __table_args__ = (
3932 3934 UniqueConstraint('user_id', 'notification_id'),
3933 3935 base_table_args
3934 3936 )
3935 3937
3936 3938 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
3937 3939 notification_id = Column("notification_id", Integer(), ForeignKey('notifications.notification_id'), primary_key=True)
3938 3940 read = Column('read', Boolean, default=False)
3939 3941 sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
3940 3942
3941 3943 user = relationship('User', lazy="joined")
3942 3944 notification = relationship('Notification', lazy="joined",
3943 3945 order_by=lambda: Notification.created_on.desc(),)
3944 3946
3945 3947 def mark_as_read(self):
3946 3948 self.read = True
3947 3949 Session().add(self)
3948 3950
3949 3951
3950 3952 class Gist(Base, BaseModel):
3951 3953 __tablename__ = 'gists'
3952 3954 __table_args__ = (
3953 3955 Index('g_gist_access_id_idx', 'gist_access_id'),
3954 3956 Index('g_created_on_idx', 'created_on'),
3955 3957 base_table_args
3956 3958 )
3957 3959
3958 3960 GIST_PUBLIC = u'public'
3959 3961 GIST_PRIVATE = u'private'
3960 3962 DEFAULT_FILENAME = u'gistfile1.txt'
3961 3963
3962 3964 ACL_LEVEL_PUBLIC = u'acl_public'
3963 3965 ACL_LEVEL_PRIVATE = u'acl_private'
3964 3966
3965 3967 gist_id = Column('gist_id', Integer(), primary_key=True)
3966 3968 gist_access_id = Column('gist_access_id', Unicode(250))
3967 3969 gist_description = Column('gist_description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
3968 3970 gist_owner = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True)
3969 3971 gist_expires = Column('gist_expires', Float(53), nullable=False)
3970 3972 gist_type = Column('gist_type', Unicode(128), nullable=False)
3971 3973 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3972 3974 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3973 3975 acl_level = Column('acl_level', Unicode(128), nullable=True)
3974 3976
3975 3977 owner = relationship('User')
3976 3978
3977 3979 def __repr__(self):
3978 3980 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
3979 3981
3980 3982 @hybrid_property
3981 3983 def description_safe(self):
3982 3984 from rhodecode.lib import helpers as h
3983 3985 return h.escape(self.gist_description)
3984 3986
3985 3987 @classmethod
3986 3988 def get_or_404(cls, id_):
3987 3989 from pyramid.httpexceptions import HTTPNotFound
3988 3990
3989 3991 res = cls.query().filter(cls.gist_access_id == id_).scalar()
3990 3992 if not res:
3991 3993 raise HTTPNotFound()
3992 3994 return res
3993 3995
3994 3996 @classmethod
3995 3997 def get_by_access_id(cls, gist_access_id):
3996 3998 return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
3997 3999
3998 4000 def gist_url(self):
3999 4001 from rhodecode.model.gist import GistModel
4000 4002 return GistModel().get_url(self)
4001 4003
4002 4004 @classmethod
4003 4005 def base_path(cls):
4004 4006 """
4005 4007 Returns base path when all gists are stored
4006 4008
4007 4009 :param cls:
4008 4010 """
4009 4011 from rhodecode.model.gist import GIST_STORE_LOC
4010 4012 q = Session().query(RhodeCodeUi)\
4011 4013 .filter(RhodeCodeUi.ui_key == URL_SEP)
4012 4014 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
4013 4015 return os.path.join(q.one().ui_value, GIST_STORE_LOC)
4014 4016
4015 4017 def get_api_data(self):
4016 4018 """
4017 4019 Common function for generating gist related data for API
4018 4020 """
4019 4021 gist = self
4020 4022 data = {
4021 4023 'gist_id': gist.gist_id,
4022 4024 'type': gist.gist_type,
4023 4025 'access_id': gist.gist_access_id,
4024 4026 'description': gist.gist_description,
4025 4027 'url': gist.gist_url(),
4026 4028 'expires': gist.gist_expires,
4027 4029 'created_on': gist.created_on,
4028 4030 'modified_at': gist.modified_at,
4029 4031 'content': None,
4030 4032 'acl_level': gist.acl_level,
4031 4033 }
4032 4034 return data
4033 4035
4034 4036 def __json__(self):
4035 4037 data = dict(
4036 4038 )
4037 4039 data.update(self.get_api_data())
4038 4040 return data
4039 4041 # SCM functions
4040 4042
4041 4043 def scm_instance(self, **kwargs):
4042 4044 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
4043 4045 return get_vcs_instance(
4044 4046 repo_path=safe_str(full_repo_path), create=False)
4045 4047
4046 4048
4047 4049 class ExternalIdentity(Base, BaseModel):
4048 4050 __tablename__ = 'external_identities'
4049 4051 __table_args__ = (
4050 4052 Index('local_user_id_idx', 'local_user_id'),
4051 4053 Index('external_id_idx', 'external_id'),
4052 4054 base_table_args
4053 4055 )
4054 4056
4055 4057 external_id = Column('external_id', Unicode(255), default=u'',
4056 4058 primary_key=True)
4057 4059 external_username = Column('external_username', Unicode(1024), default=u'')
4058 4060 local_user_id = Column('local_user_id', Integer(),
4059 4061 ForeignKey('users.user_id'), primary_key=True)
4060 4062 provider_name = Column('provider_name', Unicode(255), default=u'',
4061 4063 primary_key=True)
4062 4064 access_token = Column('access_token', String(1024), default=u'')
4063 4065 alt_token = Column('alt_token', String(1024), default=u'')
4064 4066 token_secret = Column('token_secret', String(1024), default=u'')
4065 4067
4066 4068 @classmethod
4067 4069 def by_external_id_and_provider(cls, external_id, provider_name,
4068 4070 local_user_id=None):
4069 4071 """
4070 4072 Returns ExternalIdentity instance based on search params
4071 4073
4072 4074 :param external_id:
4073 4075 :param provider_name:
4074 4076 :return: ExternalIdentity
4075 4077 """
4076 4078 query = cls.query()
4077 4079 query = query.filter(cls.external_id == external_id)
4078 4080 query = query.filter(cls.provider_name == provider_name)
4079 4081 if local_user_id:
4080 4082 query = query.filter(cls.local_user_id == local_user_id)
4081 4083 return query.first()
4082 4084
4083 4085 @classmethod
4084 4086 def user_by_external_id_and_provider(cls, external_id, provider_name):
4085 4087 """
4086 4088 Returns User instance based on search params
4087 4089
4088 4090 :param external_id:
4089 4091 :param provider_name:
4090 4092 :return: User
4091 4093 """
4092 4094 query = User.query()
4093 4095 query = query.filter(cls.external_id == external_id)
4094 4096 query = query.filter(cls.provider_name == provider_name)
4095 4097 query = query.filter(User.user_id == cls.local_user_id)
4096 4098 return query.first()
4097 4099
4098 4100 @classmethod
4099 4101 def by_local_user_id(cls, local_user_id):
4100 4102 """
4101 4103 Returns all tokens for user
4102 4104
4103 4105 :param local_user_id:
4104 4106 :return: ExternalIdentity
4105 4107 """
4106 4108 query = cls.query()
4107 4109 query = query.filter(cls.local_user_id == local_user_id)
4108 4110 return query
4109 4111
4110 4112
4111 4113 class Integration(Base, BaseModel):
4112 4114 __tablename__ = 'integrations'
4113 4115 __table_args__ = (
4114 4116 base_table_args
4115 4117 )
4116 4118
4117 4119 integration_id = Column('integration_id', Integer(), primary_key=True)
4118 4120 integration_type = Column('integration_type', String(255))
4119 4121 enabled = Column('enabled', Boolean(), nullable=False)
4120 4122 name = Column('name', String(255), nullable=False)
4121 4123 child_repos_only = Column('child_repos_only', Boolean(), nullable=False,
4122 4124 default=False)
4123 4125
4124 4126 settings = Column(
4125 4127 'settings_json', MutationObj.as_mutable(
4126 4128 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
4127 4129 repo_id = Column(
4128 4130 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
4129 4131 nullable=True, unique=None, default=None)
4130 4132 repo = relationship('Repository', lazy='joined')
4131 4133
4132 4134 repo_group_id = Column(
4133 4135 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
4134 4136 nullable=True, unique=None, default=None)
4135 4137 repo_group = relationship('RepoGroup', lazy='joined')
4136 4138
4137 4139 @property
4138 4140 def scope(self):
4139 4141 if self.repo:
4140 4142 return repr(self.repo)
4141 4143 if self.repo_group:
4142 4144 if self.child_repos_only:
4143 4145 return repr(self.repo_group) + ' (child repos only)'
4144 4146 else:
4145 4147 return repr(self.repo_group) + ' (recursive)'
4146 4148 if self.child_repos_only:
4147 4149 return 'root_repos'
4148 4150 return 'global'
4149 4151
4150 4152 def __repr__(self):
4151 4153 return '<Integration(%r, %r)>' % (self.integration_type, self.scope)
4152 4154
4153 4155
4154 4156 class RepoReviewRuleUser(Base, BaseModel):
4155 4157 __tablename__ = 'repo_review_rules_users'
4156 4158 __table_args__ = (
4157 4159 base_table_args
4158 4160 )
4159 4161
4160 4162 repo_review_rule_user_id = Column('repo_review_rule_user_id', Integer(), primary_key=True)
4161 4163 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4162 4164 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False)
4163 4165 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4164 4166 user = relationship('User')
4165 4167
4166 4168 def rule_data(self):
4167 4169 return {
4168 4170 'mandatory': self.mandatory
4169 4171 }
4170 4172
4171 4173
4172 4174 class RepoReviewRuleUserGroup(Base, BaseModel):
4173 4175 __tablename__ = 'repo_review_rules_users_groups'
4174 4176 __table_args__ = (
4175 4177 base_table_args
4176 4178 )
4177 4179
4178 4180 VOTE_RULE_ALL = -1
4179 4181
4180 4182 repo_review_rule_users_group_id = Column('repo_review_rule_users_group_id', Integer(), primary_key=True)
4181 4183 repo_review_rule_id = Column("repo_review_rule_id", Integer(), ForeignKey('repo_review_rules.repo_review_rule_id'))
4182 4184 users_group_id = Column("users_group_id", Integer(),ForeignKey('users_groups.users_group_id'), nullable=False)
4183 4185 mandatory = Column("mandatory", Boolean(), nullable=False, default=False)
4184 4186 vote_rule = Column("vote_rule", Integer(), nullable=True, default=VOTE_RULE_ALL)
4185 4187 users_group = relationship('UserGroup')
4186 4188
4187 4189 def rule_data(self):
4188 4190 return {
4189 4191 'mandatory': self.mandatory,
4190 4192 'vote_rule': self.vote_rule
4191 4193 }
4192 4194
4193 4195 @property
4194 4196 def vote_rule_label(self):
4195 4197 if not self.vote_rule or self.vote_rule == self.VOTE_RULE_ALL:
4196 4198 return 'all must vote'
4197 4199 else:
4198 4200 return 'min. vote {}'.format(self.vote_rule)
4199 4201
4200 4202
4201 4203 class RepoReviewRule(Base, BaseModel):
4202 4204 __tablename__ = 'repo_review_rules'
4203 4205 __table_args__ = (
4204 4206 base_table_args
4205 4207 )
4206 4208
4207 4209 repo_review_rule_id = Column(
4208 4210 'repo_review_rule_id', Integer(), primary_key=True)
4209 4211 repo_id = Column(
4210 4212 "repo_id", Integer(), ForeignKey('repositories.repo_id'))
4211 4213 repo = relationship('Repository', backref='review_rules')
4212 4214
4213 4215 review_rule_name = Column('review_rule_name', String(255))
4214 4216 _branch_pattern = Column("branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4215 4217 _target_branch_pattern = Column("target_branch_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4216 4218 _file_pattern = Column("file_pattern", UnicodeText().with_variant(UnicodeText(255), 'mysql'), default=u'*') # glob
4217 4219
4218 4220 use_authors_for_review = Column("use_authors_for_review", Boolean(), nullable=False, default=False)
4219 4221 forbid_author_to_review = Column("forbid_author_to_review", Boolean(), nullable=False, default=False)
4220 4222 forbid_commit_author_to_review = Column("forbid_commit_author_to_review", Boolean(), nullable=False, default=False)
4221 4223 forbid_adding_reviewers = Column("forbid_adding_reviewers", Boolean(), nullable=False, default=False)
4222 4224
4223 4225 rule_users = relationship('RepoReviewRuleUser')
4224 4226 rule_user_groups = relationship('RepoReviewRuleUserGroup')
4225 4227
4226 4228 def _validate_pattern(self, value):
4227 4229 re.compile('^' + glob2re(value) + '$')
4228 4230
4229 4231 @hybrid_property
4230 4232 def source_branch_pattern(self):
4231 4233 return self._branch_pattern or '*'
4232 4234
4233 4235 @source_branch_pattern.setter
4234 4236 def source_branch_pattern(self, value):
4235 4237 self._validate_pattern(value)
4236 4238 self._branch_pattern = value or '*'
4237 4239
4238 4240 @hybrid_property
4239 4241 def target_branch_pattern(self):
4240 4242 return self._target_branch_pattern or '*'
4241 4243
4242 4244 @target_branch_pattern.setter
4243 4245 def target_branch_pattern(self, value):
4244 4246 self._validate_pattern(value)
4245 4247 self._target_branch_pattern = value or '*'
4246 4248
4247 4249 @hybrid_property
4248 4250 def file_pattern(self):
4249 4251 return self._file_pattern or '*'
4250 4252
4251 4253 @file_pattern.setter
4252 4254 def file_pattern(self, value):
4253 4255 self._validate_pattern(value)
4254 4256 self._file_pattern = value or '*'
4255 4257
4256 4258 def matches(self, source_branch, target_branch, files_changed):
4257 4259 """
4258 4260 Check if this review rule matches a branch/files in a pull request
4259 4261
4260 4262 :param source_branch: source branch name for the commit
4261 4263 :param target_branch: target branch name for the commit
4262 4264 :param files_changed: list of file paths changed in the pull request
4263 4265 """
4264 4266
4265 4267 source_branch = source_branch or ''
4266 4268 target_branch = target_branch or ''
4267 4269 files_changed = files_changed or []
4268 4270
4269 4271 branch_matches = True
4270 4272 if source_branch or target_branch:
4271 4273 if self.source_branch_pattern == '*':
4272 4274 source_branch_match = True
4273 4275 else:
4274 4276 if self.source_branch_pattern.startswith('re:'):
4275 4277 source_pattern = self.source_branch_pattern[3:]
4276 4278 else:
4277 4279 source_pattern = '^' + glob2re(self.source_branch_pattern) + '$'
4278 4280 source_branch_regex = re.compile(source_pattern)
4279 4281 source_branch_match = bool(source_branch_regex.search(source_branch))
4280 4282 if self.target_branch_pattern == '*':
4281 4283 target_branch_match = True
4282 4284 else:
4283 4285 if self.target_branch_pattern.startswith('re:'):
4284 4286 target_pattern = self.target_branch_pattern[3:]
4285 4287 else:
4286 4288 target_pattern = '^' + glob2re(self.target_branch_pattern) + '$'
4287 4289 target_branch_regex = re.compile(target_pattern)
4288 4290 target_branch_match = bool(target_branch_regex.search(target_branch))
4289 4291
4290 4292 branch_matches = source_branch_match and target_branch_match
4291 4293
4292 4294 files_matches = True
4293 4295 if self.file_pattern != '*':
4294 4296 files_matches = False
4295 4297 if self.file_pattern.startswith('re:'):
4296 4298 file_pattern = self.file_pattern[3:]
4297 4299 else:
4298 4300 file_pattern = glob2re(self.file_pattern)
4299 4301 file_regex = re.compile(file_pattern)
4300 4302 for filename in files_changed:
4301 4303 if file_regex.search(filename):
4302 4304 files_matches = True
4303 4305 break
4304 4306
4305 4307 return branch_matches and files_matches
4306 4308
4307 4309 @property
4308 4310 def review_users(self):
4309 4311 """ Returns the users which this rule applies to """
4310 4312
4311 4313 users = collections.OrderedDict()
4312 4314
4313 4315 for rule_user in self.rule_users:
4314 4316 if rule_user.user.active:
4315 4317 if rule_user.user not in users:
4316 4318 users[rule_user.user.username] = {
4317 4319 'user': rule_user.user,
4318 4320 'source': 'user',
4319 4321 'source_data': {},
4320 4322 'data': rule_user.rule_data()
4321 4323 }
4322 4324
4323 4325 for rule_user_group in self.rule_user_groups:
4324 4326 source_data = {
4325 4327 'user_group_id': rule_user_group.users_group.users_group_id,
4326 4328 'name': rule_user_group.users_group.users_group_name,
4327 4329 'members': len(rule_user_group.users_group.members)
4328 4330 }
4329 4331 for member in rule_user_group.users_group.members:
4330 4332 if member.user.active:
4331 4333 key = member.user.username
4332 4334 if key in users:
4333 4335 # skip this member as we have him already
4334 4336 # this prevents from override the "first" matched
4335 4337 # users with duplicates in multiple groups
4336 4338 continue
4337 4339
4338 4340 users[key] = {
4339 4341 'user': member.user,
4340 4342 'source': 'user_group',
4341 4343 'source_data': source_data,
4342 4344 'data': rule_user_group.rule_data()
4343 4345 }
4344 4346
4345 4347 return users
4346 4348
4347 4349 def user_group_vote_rule(self):
4348 4350 rules = []
4349 4351 if self.rule_user_groups:
4350 4352 for user_group in self.rule_user_groups:
4351 4353 rules.append(user_group)
4352 4354 return rules
4353 4355
4354 4356 def __repr__(self):
4355 4357 return '<RepoReviewerRule(id=%r, repo=%r)>' % (
4356 4358 self.repo_review_rule_id, self.repo)
4357 4359
4358 4360
4359 4361 class ScheduleEntry(Base, BaseModel):
4360 4362 __tablename__ = 'schedule_entries'
4361 4363 __table_args__ = (
4362 4364 UniqueConstraint('schedule_name', name='s_schedule_name_idx'),
4363 4365 UniqueConstraint('task_uid', name='s_task_uid_idx'),
4364 4366 base_table_args,
4365 4367 )
4366 4368
4367 4369 schedule_types = ['crontab', 'timedelta', 'integer']
4368 4370 schedule_entry_id = Column('schedule_entry_id', Integer(), primary_key=True)
4369 4371
4370 4372 schedule_name = Column("schedule_name", String(255), nullable=False, unique=None, default=None)
4371 4373 schedule_description = Column("schedule_description", String(10000), nullable=True, unique=None, default=None)
4372 4374 schedule_enabled = Column("schedule_enabled", Boolean(), nullable=False, unique=None, default=True)
4373 4375
4374 4376 _schedule_type = Column("schedule_type", String(255), nullable=False, unique=None, default=None)
4375 4377 schedule_definition = Column('schedule_definition_json', MutationObj.as_mutable(JsonType(default=lambda: "", dialect_map=dict(mysql=LONGTEXT()))))
4376 4378
4377 4379 schedule_last_run = Column('schedule_last_run', DateTime(timezone=False), nullable=True, unique=None, default=None)
4378 4380 schedule_total_run_count = Column('schedule_total_run_count', Integer(), nullable=True, unique=None, default=0)
4379 4381
4380 4382 # task
4381 4383 task_uid = Column("task_uid", String(255), nullable=False, unique=None, default=None)
4382 4384 task_dot_notation = Column("task_dot_notation", String(4096), nullable=False, unique=None, default=None)
4383 4385 task_args = Column('task_args_json', MutationObj.as_mutable(JsonType(default=list, dialect_map=dict(mysql=LONGTEXT()))))
4384 4386 task_kwargs = Column('task_kwargs_json', MutationObj.as_mutable(JsonType(default=dict, dialect_map=dict(mysql=LONGTEXT()))))
4385 4387
4386 4388 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
4387 4389 updated_on = Column('updated_on', DateTime(timezone=False), nullable=True, unique=None, default=None)
4388 4390
4389 4391 @hybrid_property
4390 4392 def schedule_type(self):
4391 4393 return self._schedule_type
4392 4394
4393 4395 @schedule_type.setter
4394 4396 def schedule_type(self, val):
4395 4397 if val not in self.schedule_types:
4396 4398 raise ValueError('Value must be on of `{}` and got `{}`'.format(
4397 4399 val, self.schedule_type))
4398 4400
4399 4401 self._schedule_type = val
4400 4402
4401 4403 @classmethod
4402 4404 def get_uid(cls, obj):
4403 4405 args = obj.task_args
4404 4406 kwargs = obj.task_kwargs
4405 4407 if isinstance(args, JsonRaw):
4406 4408 try:
4407 4409 args = json.loads(args)
4408 4410 except ValueError:
4409 4411 args = tuple()
4410 4412
4411 4413 if isinstance(kwargs, JsonRaw):
4412 4414 try:
4413 4415 kwargs = json.loads(kwargs)
4414 4416 except ValueError:
4415 4417 kwargs = dict()
4416 4418
4417 4419 dot_notation = obj.task_dot_notation
4418 4420 val = '.'.join(map(safe_str, [
4419 4421 sorted(dot_notation), args, sorted(kwargs.items())]))
4420 4422 return hashlib.sha1(val).hexdigest()
4421 4423
4422 4424 @classmethod
4423 4425 def get_by_schedule_name(cls, schedule_name):
4424 4426 return cls.query().filter(cls.schedule_name == schedule_name).scalar()
4425 4427
4426 4428 @classmethod
4427 4429 def get_by_schedule_id(cls, schedule_id):
4428 4430 return cls.query().filter(cls.schedule_entry_id == schedule_id).scalar()
4429 4431
4430 4432 @property
4431 4433 def task(self):
4432 4434 return self.task_dot_notation
4433 4435
4434 4436 @property
4435 4437 def schedule(self):
4436 4438 from rhodecode.lib.celerylib.utils import raw_2_schedule
4437 4439 schedule = raw_2_schedule(self.schedule_definition, self.schedule_type)
4438 4440 return schedule
4439 4441
4440 4442 @property
4441 4443 def args(self):
4442 4444 try:
4443 4445 return list(self.task_args or [])
4444 4446 except ValueError:
4445 4447 return list()
4446 4448
4447 4449 @property
4448 4450 def kwargs(self):
4449 4451 try:
4450 4452 return dict(self.task_kwargs or {})
4451 4453 except ValueError:
4452 4454 return dict()
4453 4455
4454 4456 def _as_raw(self, val):
4455 4457 if hasattr(val, 'de_coerce'):
4456 4458 val = val.de_coerce()
4457 4459 if val:
4458 4460 val = json.dumps(val)
4459 4461
4460 4462 return val
4461 4463
4462 4464 @property
4463 4465 def schedule_definition_raw(self):
4464 4466 return self._as_raw(self.schedule_definition)
4465 4467
4466 4468 @property
4467 4469 def args_raw(self):
4468 4470 return self._as_raw(self.task_args)
4469 4471
4470 4472 @property
4471 4473 def kwargs_raw(self):
4472 4474 return self._as_raw(self.task_kwargs)
4473 4475
4474 4476 def __repr__(self):
4475 4477 return '<DB:ScheduleEntry({}:{})>'.format(
4476 4478 self.schedule_entry_id, self.schedule_name)
4477 4479
4478 4480
4479 4481 @event.listens_for(ScheduleEntry, 'before_update')
4480 4482 def update_task_uid(mapper, connection, target):
4481 4483 target.task_uid = ScheduleEntry.get_uid(target)
4482 4484
4483 4485
4484 4486 @event.listens_for(ScheduleEntry, 'before_insert')
4485 4487 def set_task_uid(mapper, connection, target):
4486 4488 target.task_uid = ScheduleEntry.get_uid(target)
4487 4489
4488 4490
4489 4491 class DbMigrateVersion(Base, BaseModel):
4490 4492 __tablename__ = 'db_migrate_version'
4491 4493 __table_args__ = (
4492 4494 base_table_args,
4493 4495 )
4494 4496
4495 4497 repository_id = Column('repository_id', String(250), primary_key=True)
4496 4498 repository_path = Column('repository_path', Text)
4497 4499 version = Column('version', Integer)
4498 4500
4499 4501 @classmethod
4500 4502 def set_version(cls, version):
4501 4503 """
4502 4504 Helper for forcing a different version, usually for debugging purposes via ishell.
4503 4505 """
4504 4506 ver = DbMigrateVersion.query().first()
4505 4507 ver.version = version
4506 4508 Session().commit()
4507 4509
4508 4510
4509 4511 class DbSession(Base, BaseModel):
4510 4512 __tablename__ = 'db_session'
4511 4513 __table_args__ = (
4512 4514 base_table_args,
4513 4515 )
4514 4516
4515 4517 def __repr__(self):
4516 4518 return '<DB:DbSession({})>'.format(self.id)
4517 4519
4518 4520 id = Column('id', Integer())
4519 4521 namespace = Column('namespace', String(255), primary_key=True)
4520 4522 accessed = Column('accessed', DateTime, nullable=False)
4521 4523 created = Column('created', DateTime, nullable=False)
4522 4524 data = Column('data', PickleType, nullable=False)
4523 4525
4524 4526
4525 4527 class BeakerCache(Base, BaseModel):
4526 4528 __tablename__ = 'beaker_cache'
4527 4529 __table_args__ = (
4528 4530 base_table_args,
4529 4531 )
4530 4532
4531 4533 def __repr__(self):
4532 4534 return '<DB:DbSession({})>'.format(self.id)
4533 4535
4534 4536 id = Column('id', Integer())
4535 4537 namespace = Column('namespace', String(255), primary_key=True)
4536 4538 accessed = Column('accessed', DateTime, nullable=False)
4537 4539 created = Column('created', DateTime, nullable=False)
4538 4540 data = Column('data', PickleType, nullable=False)
@@ -1,620 +1,621 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 this is forms validation classes
23 23 http://formencode.org/module-formencode.validators.html
24 24 for list off all availible validators
25 25
26 26 we can create our own validators
27 27
28 28 The table below outlines the options which can be used in a schema in addition to the validators themselves
29 29 pre_validators [] These validators will be applied before the schema
30 30 chained_validators [] These validators will be applied after the schema
31 31 allow_extra_fields False If True, then it is not an error when keys that aren't associated with a validator are present
32 32 filter_extra_fields False If True, then keys that aren't associated with a validator are removed
33 33 if_key_missing NoDefault If this is given, then any keys that aren't available but are expected will be replaced with this value (and then validated). This does not override a present .if_missing attribute on validators. NoDefault is a special FormEncode class to mean that no default values has been specified and therefore missing keys shouldn't take a default value.
34 34 ignore_key_missing False If True, then missing keys will be missing in the result, if the validator doesn't have .if_missing on it already
35 35
36 36
37 37 <name> = formencode.validators.<name of validator>
38 38 <name> must equal form name
39 39 list=[1,2,3,4,5]
40 40 for SELECT use formencode.All(OneOf(list), Int())
41 41
42 42 """
43 43
44 44 import deform
45 45 import logging
46 46 import formencode
47 47
48 48 from pkg_resources import resource_filename
49 49 from formencode import All, Pipe
50 50
51 51 from pyramid.threadlocal import get_current_request
52 52
53 53 from rhodecode import BACKENDS
54 54 from rhodecode.lib import helpers
55 55 from rhodecode.model import validators as v
56 56
57 57 log = logging.getLogger(__name__)
58 58
59 59
60 60 deform_templates = resource_filename('deform', 'templates')
61 61 rhodecode_templates = resource_filename('rhodecode', 'templates/forms')
62 62 search_path = (rhodecode_templates, deform_templates)
63 63
64 64
65 65 class RhodecodeFormZPTRendererFactory(deform.ZPTRendererFactory):
66 66 """ Subclass of ZPTRendererFactory to add rhodecode context variables """
67 67 def __call__(self, template_name, **kw):
68 68 kw['h'] = helpers
69 69 kw['request'] = get_current_request()
70 70 return self.load(template_name)(**kw)
71 71
72 72
73 73 form_renderer = RhodecodeFormZPTRendererFactory(search_path)
74 74 deform.Form.set_default_renderer(form_renderer)
75 75
76 76
77 77 def LoginForm(localizer):
78 78 _ = localizer
79 79
80 80 class _LoginForm(formencode.Schema):
81 81 allow_extra_fields = True
82 82 filter_extra_fields = True
83 83 username = v.UnicodeString(
84 84 strip=True,
85 85 min=1,
86 86 not_empty=True,
87 87 messages={
88 88 'empty': _(u'Please enter a login'),
89 89 'tooShort': _(u'Enter a value %(min)i characters long or more')
90 90 }
91 91 )
92 92
93 93 password = v.UnicodeString(
94 94 strip=False,
95 95 min=3,
96 96 max=72,
97 97 not_empty=True,
98 98 messages={
99 99 'empty': _(u'Please enter a password'),
100 100 'tooShort': _(u'Enter %(min)i characters or more')}
101 101 )
102 102
103 103 remember = v.StringBoolean(if_missing=False)
104 104
105 105 chained_validators = [v.ValidAuth(localizer)]
106 106 return _LoginForm
107 107
108 108
109 109 def UserForm(localizer, edit=False, available_languages=None, old_data=None):
110 110 old_data = old_data or {}
111 111 available_languages = available_languages or []
112 112 _ = localizer
113 113
114 114 class _UserForm(formencode.Schema):
115 115 allow_extra_fields = True
116 116 filter_extra_fields = True
117 117 username = All(v.UnicodeString(strip=True, min=1, not_empty=True),
118 118 v.ValidUsername(localizer, edit, old_data))
119 119 if edit:
120 120 new_password = All(
121 121 v.ValidPassword(localizer),
122 122 v.UnicodeString(strip=False, min=6, max=72, not_empty=False)
123 123 )
124 124 password_confirmation = All(
125 125 v.ValidPassword(localizer),
126 126 v.UnicodeString(strip=False, min=6, max=72, not_empty=False),
127 127 )
128 128 admin = v.StringBoolean(if_missing=False)
129 129 else:
130 130 password = All(
131 131 v.ValidPassword(localizer),
132 132 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
133 133 )
134 134 password_confirmation = All(
135 135 v.ValidPassword(localizer),
136 136 v.UnicodeString(strip=False, min=6, max=72, not_empty=False)
137 137 )
138 138
139 139 password_change = v.StringBoolean(if_missing=False)
140 140 create_repo_group = v.StringBoolean(if_missing=False)
141 141
142 142 active = v.StringBoolean(if_missing=False)
143 143 firstname = v.UnicodeString(strip=True, min=1, not_empty=False)
144 144 lastname = v.UnicodeString(strip=True, min=1, not_empty=False)
145 145 email = All(v.UniqSystemEmail(localizer, old_data), v.Email(not_empty=True))
146 146 extern_name = v.UnicodeString(strip=True)
147 147 extern_type = v.UnicodeString(strip=True)
148 148 language = v.OneOf(available_languages, hideList=False,
149 149 testValueList=True, if_missing=None)
150 150 chained_validators = [v.ValidPasswordsMatch(localizer)]
151 151 return _UserForm
152 152
153 153
154 154 def UserGroupForm(localizer, edit=False, old_data=None, allow_disabled=False):
155 155 old_data = old_data or {}
156 156 _ = localizer
157 157
158 158 class _UserGroupForm(formencode.Schema):
159 159 allow_extra_fields = True
160 160 filter_extra_fields = True
161 161
162 162 users_group_name = All(
163 163 v.UnicodeString(strip=True, min=1, not_empty=True),
164 164 v.ValidUserGroup(localizer, edit, old_data)
165 165 )
166 166 user_group_description = v.UnicodeString(strip=True, min=1,
167 167 not_empty=False)
168 168
169 169 users_group_active = v.StringBoolean(if_missing=False)
170 170
171 171 if edit:
172 172 # this is user group owner
173 173 user = All(
174 174 v.UnicodeString(not_empty=True),
175 175 v.ValidRepoUser(localizer, allow_disabled))
176 176 return _UserGroupForm
177 177
178 178
179 179 def RepoGroupForm(localizer, edit=False, old_data=None, available_groups=None,
180 180 can_create_in_root=False, allow_disabled=False):
181 181 _ = localizer
182 182 old_data = old_data or {}
183 183 available_groups = available_groups or []
184 184
185 185 class _RepoGroupForm(formencode.Schema):
186 186 allow_extra_fields = True
187 187 filter_extra_fields = False
188 188
189 189 group_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
190 190 v.SlugifyName(localizer),)
191 191 group_description = v.UnicodeString(strip=True, min=1,
192 192 not_empty=False)
193 193 group_copy_permissions = v.StringBoolean(if_missing=False)
194 194
195 195 group_parent_id = v.OneOf(available_groups, hideList=False,
196 196 testValueList=True, not_empty=True)
197 197 enable_locking = v.StringBoolean(if_missing=False)
198 198 chained_validators = [
199 199 v.ValidRepoGroup(localizer, edit, old_data, can_create_in_root)]
200 200
201 201 if edit:
202 202 # this is repo group owner
203 203 user = All(
204 204 v.UnicodeString(not_empty=True),
205 205 v.ValidRepoUser(localizer, allow_disabled))
206 206 return _RepoGroupForm
207 207
208 208
209 209 def RegisterForm(localizer, edit=False, old_data=None):
210 210 _ = localizer
211 211 old_data = old_data or {}
212 212
213 213 class _RegisterForm(formencode.Schema):
214 214 allow_extra_fields = True
215 215 filter_extra_fields = True
216 216 username = All(
217 217 v.ValidUsername(localizer, edit, old_data),
218 218 v.UnicodeString(strip=True, min=1, not_empty=True)
219 219 )
220 220 password = All(
221 221 v.ValidPassword(localizer),
222 222 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
223 223 )
224 224 password_confirmation = All(
225 225 v.ValidPassword(localizer),
226 226 v.UnicodeString(strip=False, min=6, max=72, not_empty=True)
227 227 )
228 228 active = v.StringBoolean(if_missing=False)
229 229 firstname = v.UnicodeString(strip=True, min=1, not_empty=False)
230 230 lastname = v.UnicodeString(strip=True, min=1, not_empty=False)
231 231 email = All(v.UniqSystemEmail(localizer, old_data), v.Email(not_empty=True))
232 232
233 233 chained_validators = [v.ValidPasswordsMatch(localizer)]
234 234 return _RegisterForm
235 235
236 236
237 237 def PasswordResetForm(localizer):
238 238 _ = localizer
239 239
240 240 class _PasswordResetForm(formencode.Schema):
241 241 allow_extra_fields = True
242 242 filter_extra_fields = True
243 243 email = All(v.ValidSystemEmail(localizer), v.Email(not_empty=True))
244 244 return _PasswordResetForm
245 245
246 246
247 247 def RepoForm(localizer, edit=False, old_data=None, repo_groups=None,
248 248 landing_revs=None, allow_disabled=False):
249 249 _ = localizer
250 250 old_data = old_data or {}
251 251 repo_groups = repo_groups or []
252 252 landing_revs = landing_revs or []
253 253 supported_backends = BACKENDS.keys()
254 254
255 255 class _RepoForm(formencode.Schema):
256 256 allow_extra_fields = True
257 257 filter_extra_fields = False
258 258 repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
259 259 v.SlugifyName(localizer), v.CannotHaveGitSuffix(localizer))
260 260 repo_group = All(v.CanWriteGroup(localizer, old_data),
261 261 v.OneOf(repo_groups, hideList=True))
262 262 repo_type = v.OneOf(supported_backends, required=False,
263 263 if_missing=old_data.get('repo_type'))
264 264 repo_description = v.UnicodeString(strip=True, min=1, not_empty=False)
265 265 repo_private = v.StringBoolean(if_missing=False)
266 266 repo_landing_rev = v.OneOf(landing_revs, hideList=True)
267 267 repo_copy_permissions = v.StringBoolean(if_missing=False)
268 268 clone_uri = All(v.UnicodeString(strip=True, min=1, not_empty=False))
269 269
270 270 repo_enable_statistics = v.StringBoolean(if_missing=False)
271 271 repo_enable_downloads = v.StringBoolean(if_missing=False)
272 272 repo_enable_locking = v.StringBoolean(if_missing=False)
273 273
274 274 if edit:
275 275 # this is repo owner
276 276 user = All(
277 277 v.UnicodeString(not_empty=True),
278 278 v.ValidRepoUser(localizer, allow_disabled))
279 279 clone_uri_change = v.UnicodeString(
280 280 not_empty=False, if_missing=v.Missing)
281 281
282 282 chained_validators = [v.ValidCloneUri(localizer),
283 283 v.ValidRepoName(localizer, edit, old_data)]
284 284 return _RepoForm
285 285
286 286
287 287 def RepoPermsForm(localizer):
288 288 _ = localizer
289 289
290 290 class _RepoPermsForm(formencode.Schema):
291 291 allow_extra_fields = True
292 292 filter_extra_fields = False
293 293 chained_validators = [v.ValidPerms(localizer, type_='repo')]
294 294 return _RepoPermsForm
295 295
296 296
297 297 def RepoGroupPermsForm(localizer, valid_recursive_choices):
298 298 _ = localizer
299 299
300 300 class _RepoGroupPermsForm(formencode.Schema):
301 301 allow_extra_fields = True
302 302 filter_extra_fields = False
303 303 recursive = v.OneOf(valid_recursive_choices)
304 304 chained_validators = [v.ValidPerms(localizer, type_='repo_group')]
305 305 return _RepoGroupPermsForm
306 306
307 307
308 308 def UserGroupPermsForm(localizer):
309 309 _ = localizer
310 310
311 311 class _UserPermsForm(formencode.Schema):
312 312 allow_extra_fields = True
313 313 filter_extra_fields = False
314 314 chained_validators = [v.ValidPerms(localizer, type_='user_group')]
315 315 return _UserPermsForm
316 316
317 317
318 318 def RepoFieldForm(localizer):
319 319 _ = localizer
320 320
321 321 class _RepoFieldForm(formencode.Schema):
322 322 filter_extra_fields = True
323 323 allow_extra_fields = True
324 324
325 325 new_field_key = All(v.FieldKey(localizer),
326 326 v.UnicodeString(strip=True, min=3, not_empty=True))
327 327 new_field_value = v.UnicodeString(not_empty=False, if_missing=u'')
328 328 new_field_type = v.OneOf(['str', 'unicode', 'list', 'tuple'],
329 329 if_missing='str')
330 330 new_field_label = v.UnicodeString(not_empty=False)
331 331 new_field_desc = v.UnicodeString(not_empty=False)
332 332 return _RepoFieldForm
333 333
334 334
335 335 def RepoForkForm(localizer, edit=False, old_data=None,
336 336 supported_backends=BACKENDS.keys(), repo_groups=None,
337 337 landing_revs=None):
338 338 _ = localizer
339 339 old_data = old_data or {}
340 340 repo_groups = repo_groups or []
341 341 landing_revs = landing_revs or []
342 342
343 343 class _RepoForkForm(formencode.Schema):
344 344 allow_extra_fields = True
345 345 filter_extra_fields = False
346 346 repo_name = All(v.UnicodeString(strip=True, min=1, not_empty=True),
347 347 v.SlugifyName(localizer))
348 348 repo_group = All(v.CanWriteGroup(localizer, ),
349 349 v.OneOf(repo_groups, hideList=True))
350 350 repo_type = All(v.ValidForkType(localizer, old_data), v.OneOf(supported_backends))
351 351 description = v.UnicodeString(strip=True, min=1, not_empty=True)
352 352 private = v.StringBoolean(if_missing=False)
353 353 copy_permissions = v.StringBoolean(if_missing=False)
354 354 fork_parent_id = v.UnicodeString()
355 355 chained_validators = [v.ValidForkName(localizer, edit, old_data)]
356 356 landing_rev = v.OneOf(landing_revs, hideList=True)
357 357 return _RepoForkForm
358 358
359 359
360 360 def ApplicationSettingsForm(localizer):
361 361 _ = localizer
362 362
363 363 class _ApplicationSettingsForm(formencode.Schema):
364 364 allow_extra_fields = True
365 365 filter_extra_fields = False
366 366 rhodecode_title = v.UnicodeString(strip=True, max=40, not_empty=False)
367 367 rhodecode_realm = v.UnicodeString(strip=True, min=1, not_empty=True)
368 368 rhodecode_pre_code = v.UnicodeString(strip=True, min=1, not_empty=False)
369 369 rhodecode_post_code = v.UnicodeString(strip=True, min=1, not_empty=False)
370 370 rhodecode_captcha_public_key = v.UnicodeString(strip=True, min=1, not_empty=False)
371 371 rhodecode_captcha_private_key = v.UnicodeString(strip=True, min=1, not_empty=False)
372 372 rhodecode_create_personal_repo_group = v.StringBoolean(if_missing=False)
373 373 rhodecode_personal_repo_group_pattern = v.UnicodeString(strip=True, min=1, not_empty=False)
374 374 return _ApplicationSettingsForm
375 375
376 376
377 377 def ApplicationVisualisationForm(localizer):
378 378 from rhodecode.model.db import Repository
379 379 _ = localizer
380 380
381 381 class _ApplicationVisualisationForm(formencode.Schema):
382 382 allow_extra_fields = True
383 383 filter_extra_fields = False
384 384 rhodecode_show_public_icon = v.StringBoolean(if_missing=False)
385 385 rhodecode_show_private_icon = v.StringBoolean(if_missing=False)
386 386 rhodecode_stylify_metatags = v.StringBoolean(if_missing=False)
387 387
388 388 rhodecode_repository_fields = v.StringBoolean(if_missing=False)
389 389 rhodecode_lightweight_journal = v.StringBoolean(if_missing=False)
390 390 rhodecode_dashboard_items = v.Int(min=5, not_empty=True)
391 391 rhodecode_admin_grid_items = v.Int(min=5, not_empty=True)
392 392 rhodecode_show_version = v.StringBoolean(if_missing=False)
393 393 rhodecode_use_gravatar = v.StringBoolean(if_missing=False)
394 394 rhodecode_markup_renderer = v.OneOf(['markdown', 'rst'])
395 395 rhodecode_gravatar_url = v.UnicodeString(min=3)
396 396 rhodecode_clone_uri_tmpl = v.UnicodeString(not_empty=False, if_empty=Repository.DEFAULT_CLONE_URI)
397 397 rhodecode_clone_uri_ssh_tmpl = v.UnicodeString(not_empty=False, if_empty=Repository.DEFAULT_CLONE_URI_SSH)
398 398 rhodecode_support_url = v.UnicodeString()
399 399 rhodecode_show_revision_number = v.StringBoolean(if_missing=False)
400 400 rhodecode_show_sha_length = v.Int(min=4, not_empty=True)
401 401 return _ApplicationVisualisationForm
402 402
403 403
404 404 class _BaseVcsSettingsForm(formencode.Schema):
405 405
406 406 allow_extra_fields = True
407 407 filter_extra_fields = False
408 408 hooks_changegroup_repo_size = v.StringBoolean(if_missing=False)
409 409 hooks_changegroup_push_logger = v.StringBoolean(if_missing=False)
410 410 hooks_outgoing_pull_logger = v.StringBoolean(if_missing=False)
411 411
412 412 # PR/Code-review
413 413 rhodecode_pr_merge_enabled = v.StringBoolean(if_missing=False)
414 414 rhodecode_use_outdated_comments = v.StringBoolean(if_missing=False)
415 415
416 416 # hg
417 417 extensions_largefiles = v.StringBoolean(if_missing=False)
418 418 extensions_evolve = v.StringBoolean(if_missing=False)
419 419 phases_publish = v.StringBoolean(if_missing=False)
420 420
421 421 rhodecode_hg_use_rebase_for_merging = v.StringBoolean(if_missing=False)
422 422 rhodecode_hg_close_branch_before_merging = v.StringBoolean(if_missing=False)
423 423
424 424 # git
425 425 vcs_git_lfs_enabled = v.StringBoolean(if_missing=False)
426 426 rhodecode_git_use_rebase_for_merging = v.StringBoolean(if_missing=False)
427 427 rhodecode_git_close_branch_before_merging = v.StringBoolean(if_missing=False)
428 428
429 429 # svn
430 430 vcs_svn_proxy_http_requests_enabled = v.StringBoolean(if_missing=False)
431 431 vcs_svn_proxy_http_server_url = v.UnicodeString(strip=True, if_missing=None)
432 432
433 433 # cache
434 434 rhodecode_diff_cache = v.StringBoolean(if_missing=False)
435 435
436 436
437 437 def ApplicationUiSettingsForm(localizer):
438 438 _ = localizer
439 439
440 440 class _ApplicationUiSettingsForm(_BaseVcsSettingsForm):
441 441 web_push_ssl = v.StringBoolean(if_missing=False)
442 442 paths_root_path = All(
443 443 v.ValidPath(localizer),
444 444 v.UnicodeString(strip=True, min=1, not_empty=True)
445 445 )
446 446 largefiles_usercache = All(
447 447 v.ValidPath(localizer),
448 448 v.UnicodeString(strip=True, min=2, not_empty=True))
449 449 vcs_git_lfs_store_location = All(
450 450 v.ValidPath(localizer),
451 451 v.UnicodeString(strip=True, min=2, not_empty=True))
452 452 extensions_hgsubversion = v.StringBoolean(if_missing=False)
453 453 extensions_hggit = v.StringBoolean(if_missing=False)
454 454 new_svn_branch = v.ValidSvnPattern(localizer, section='vcs_svn_branch')
455 455 new_svn_tag = v.ValidSvnPattern(localizer, section='vcs_svn_tag')
456 456 return _ApplicationUiSettingsForm
457 457
458 458
459 459 def RepoVcsSettingsForm(localizer, repo_name):
460 460 _ = localizer
461 461
462 462 class _RepoVcsSettingsForm(_BaseVcsSettingsForm):
463 463 inherit_global_settings = v.StringBoolean(if_missing=False)
464 464 new_svn_branch = v.ValidSvnPattern(localizer,
465 465 section='vcs_svn_branch', repo_name=repo_name)
466 466 new_svn_tag = v.ValidSvnPattern(localizer,
467 467 section='vcs_svn_tag', repo_name=repo_name)
468 468 return _RepoVcsSettingsForm
469 469
470 470
471 471 def LabsSettingsForm(localizer):
472 472 _ = localizer
473 473
474 474 class _LabSettingsForm(formencode.Schema):
475 475 allow_extra_fields = True
476 476 filter_extra_fields = False
477 477 return _LabSettingsForm
478 478
479 479
480 480 def ApplicationPermissionsForm(
481 481 localizer, register_choices, password_reset_choices,
482 482 extern_activate_choices):
483 483 _ = localizer
484 484
485 485 class _DefaultPermissionsForm(formencode.Schema):
486 486 allow_extra_fields = True
487 487 filter_extra_fields = True
488 488
489 489 anonymous = v.StringBoolean(if_missing=False)
490 490 default_register = v.OneOf(register_choices)
491 491 default_register_message = v.UnicodeString()
492 492 default_password_reset = v.OneOf(password_reset_choices)
493 493 default_extern_activate = v.OneOf(extern_activate_choices)
494 494 return _DefaultPermissionsForm
495 495
496 496
497 497 def ObjectPermissionsForm(localizer, repo_perms_choices, group_perms_choices,
498 498 user_group_perms_choices):
499 499 _ = localizer
500 500
501 501 class _ObjectPermissionsForm(formencode.Schema):
502 502 allow_extra_fields = True
503 503 filter_extra_fields = True
504 504 overwrite_default_repo = v.StringBoolean(if_missing=False)
505 505 overwrite_default_group = v.StringBoolean(if_missing=False)
506 506 overwrite_default_user_group = v.StringBoolean(if_missing=False)
507 507 default_repo_perm = v.OneOf(repo_perms_choices)
508 508 default_group_perm = v.OneOf(group_perms_choices)
509 509 default_user_group_perm = v.OneOf(user_group_perms_choices)
510 510 return _ObjectPermissionsForm
511 511
512 512
513 513 def UserPermissionsForm(localizer, create_choices, create_on_write_choices,
514 514 repo_group_create_choices, user_group_create_choices,
515 515 fork_choices, inherit_default_permissions_choices):
516 516 _ = localizer
517 517
518 518 class _DefaultPermissionsForm(formencode.Schema):
519 519 allow_extra_fields = True
520 520 filter_extra_fields = True
521 521
522 522 anonymous = v.StringBoolean(if_missing=False)
523 523
524 524 default_repo_create = v.OneOf(create_choices)
525 525 default_repo_create_on_write = v.OneOf(create_on_write_choices)
526 526 default_user_group_create = v.OneOf(user_group_create_choices)
527 527 default_repo_group_create = v.OneOf(repo_group_create_choices)
528 528 default_fork_create = v.OneOf(fork_choices)
529 529 default_inherit_default_permissions = v.OneOf(inherit_default_permissions_choices)
530 530 return _DefaultPermissionsForm
531 531
532 532
533 533 def UserIndividualPermissionsForm(localizer):
534 534 _ = localizer
535 535
536 536 class _DefaultPermissionsForm(formencode.Schema):
537 537 allow_extra_fields = True
538 538 filter_extra_fields = True
539 539
540 540 inherit_default_permissions = v.StringBoolean(if_missing=False)
541 541 return _DefaultPermissionsForm
542 542
543 543
544 544 def DefaultsForm(localizer, edit=False, old_data=None, supported_backends=BACKENDS.keys()):
545 545 _ = localizer
546 546 old_data = old_data or {}
547 547
548 548 class _DefaultsForm(formencode.Schema):
549 549 allow_extra_fields = True
550 550 filter_extra_fields = True
551 551 default_repo_type = v.OneOf(supported_backends)
552 552 default_repo_private = v.StringBoolean(if_missing=False)
553 553 default_repo_enable_statistics = v.StringBoolean(if_missing=False)
554 554 default_repo_enable_downloads = v.StringBoolean(if_missing=False)
555 555 default_repo_enable_locking = v.StringBoolean(if_missing=False)
556 556 return _DefaultsForm
557 557
558 558
559 559 def AuthSettingsForm(localizer):
560 560 _ = localizer
561 561
562 562 class _AuthSettingsForm(formencode.Schema):
563 563 allow_extra_fields = True
564 564 filter_extra_fields = True
565 565 auth_plugins = All(v.ValidAuthPlugins(localizer),
566 566 v.UniqueListFromString(localizer)(not_empty=True))
567 567 return _AuthSettingsForm
568 568
569 569
570 570 def UserExtraEmailForm(localizer):
571 571 _ = localizer
572 572
573 573 class _UserExtraEmailForm(formencode.Schema):
574 574 email = All(v.UniqSystemEmail(localizer), v.Email(not_empty=True))
575 575 return _UserExtraEmailForm
576 576
577 577
578 578 def UserExtraIpForm(localizer):
579 579 _ = localizer
580 580
581 581 class _UserExtraIpForm(formencode.Schema):
582 582 ip = v.ValidIp(localizer)(not_empty=True)
583 583 return _UserExtraIpForm
584 584
585 585
586 586 def PullRequestForm(localizer, repo_id):
587 587 _ = localizer
588 588
589 589 class ReviewerForm(formencode.Schema):
590 590 user_id = v.Int(not_empty=True)
591 591 reasons = All()
592 592 rules = All(v.UniqueList(localizer, convert=int)())
593 593 mandatory = v.StringBoolean()
594 594
595 595 class _PullRequestForm(formencode.Schema):
596 596 allow_extra_fields = True
597 597 filter_extra_fields = True
598 598
599 599 common_ancestor = v.UnicodeString(strip=True, required=True)
600 600 source_repo = v.UnicodeString(strip=True, required=True)
601 601 source_ref = v.UnicodeString(strip=True, required=True)
602 602 target_repo = v.UnicodeString(strip=True, required=True)
603 603 target_ref = v.UnicodeString(strip=True, required=True)
604 604 revisions = All(#v.NotReviewedRevisions(localizer, repo_id)(),
605 605 v.UniqueList(localizer)(not_empty=True))
606 606 review_members = formencode.ForEach(ReviewerForm())
607 607 pullrequest_title = v.UnicodeString(strip=True, required=True, min=3, max=255)
608 608 pullrequest_desc = v.UnicodeString(strip=True, required=False)
609 description_renderer = v.UnicodeString(strip=True, required=False)
609 610
610 611 return _PullRequestForm
611 612
612 613
613 614 def IssueTrackerPatternsForm(localizer):
614 615 _ = localizer
615 616
616 617 class _IssueTrackerPatternsForm(formencode.Schema):
617 618 allow_extra_fields = True
618 619 filter_extra_fields = False
619 620 chained_validators = [v.ValidPattern(localizer)]
620 621 return _IssueTrackerPatternsForm
@@ -1,1701 +1,1704 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2012-2018 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21
22 22 """
23 23 pull request model for RhodeCode
24 24 """
25 25
26 26
27 27 import json
28 28 import logging
29 29 import datetime
30 30 import urllib
31 31 import collections
32 32
33 33 from pyramid.threadlocal import get_current_request
34 34
35 35 from rhodecode import events
36 36 from rhodecode.translation import lazy_ugettext#, _
37 37 from rhodecode.lib import helpers as h, hooks_utils, diffs
38 38 from rhodecode.lib import audit_logger
39 39 from rhodecode.lib.compat import OrderedDict
40 40 from rhodecode.lib.hooks_daemon import prepare_callback_daemon
41 41 from rhodecode.lib.markup_renderer import (
42 42 DEFAULT_COMMENTS_RENDERER, RstTemplateRenderer)
43 43 from rhodecode.lib.utils2 import safe_unicode, safe_str, md5_safe
44 44 from rhodecode.lib.vcs.backends.base import (
45 45 Reference, MergeResponse, MergeFailureReason, UpdateFailureReason)
46 46 from rhodecode.lib.vcs.conf import settings as vcs_settings
47 47 from rhodecode.lib.vcs.exceptions import (
48 48 CommitDoesNotExistError, EmptyRepositoryError)
49 49 from rhodecode.model import BaseModel
50 50 from rhodecode.model.changeset_status import ChangesetStatusModel
51 51 from rhodecode.model.comment import CommentsModel
52 52 from rhodecode.model.db import (
53 53 or_, PullRequest, PullRequestReviewers, ChangesetStatus,
54 54 PullRequestVersion, ChangesetComment, Repository, RepoReviewRule)
55 55 from rhodecode.model.meta import Session
56 56 from rhodecode.model.notification import NotificationModel, \
57 57 EmailNotificationModel
58 58 from rhodecode.model.scm import ScmModel
59 59 from rhodecode.model.settings import VcsSettingsModel
60 60
61 61
62 62 log = logging.getLogger(__name__)
63 63
64 64
65 65 # Data structure to hold the response data when updating commits during a pull
66 66 # request update.
67 67 UpdateResponse = collections.namedtuple('UpdateResponse', [
68 68 'executed', 'reason', 'new', 'old', 'changes',
69 69 'source_changed', 'target_changed'])
70 70
71 71
72 72 class PullRequestModel(BaseModel):
73 73
74 74 cls = PullRequest
75 75
76 76 DIFF_CONTEXT = 3
77 77
78 78 MERGE_STATUS_MESSAGES = {
79 79 MergeFailureReason.NONE: lazy_ugettext(
80 80 'This pull request can be automatically merged.'),
81 81 MergeFailureReason.UNKNOWN: lazy_ugettext(
82 82 'This pull request cannot be merged because of an unhandled'
83 83 ' exception.'),
84 84 MergeFailureReason.MERGE_FAILED: lazy_ugettext(
85 85 'This pull request cannot be merged because of merge conflicts.'),
86 86 MergeFailureReason.PUSH_FAILED: lazy_ugettext(
87 87 'This pull request could not be merged because push to target'
88 88 ' failed.'),
89 89 MergeFailureReason.TARGET_IS_NOT_HEAD: lazy_ugettext(
90 90 'This pull request cannot be merged because the target is not a'
91 91 ' head.'),
92 92 MergeFailureReason.HG_SOURCE_HAS_MORE_BRANCHES: lazy_ugettext(
93 93 'This pull request cannot be merged because the source contains'
94 94 ' more branches than the target.'),
95 95 MergeFailureReason.HG_TARGET_HAS_MULTIPLE_HEADS: lazy_ugettext(
96 96 'This pull request cannot be merged because the target has'
97 97 ' multiple heads.'),
98 98 MergeFailureReason.TARGET_IS_LOCKED: lazy_ugettext(
99 99 'This pull request cannot be merged because the target repository'
100 100 ' is locked.'),
101 101 MergeFailureReason._DEPRECATED_MISSING_COMMIT: lazy_ugettext(
102 102 'This pull request cannot be merged because the target or the '
103 103 'source reference is missing.'),
104 104 MergeFailureReason.MISSING_TARGET_REF: lazy_ugettext(
105 105 'This pull request cannot be merged because the target '
106 106 'reference is missing.'),
107 107 MergeFailureReason.MISSING_SOURCE_REF: lazy_ugettext(
108 108 'This pull request cannot be merged because the source '
109 109 'reference is missing.'),
110 110 MergeFailureReason.SUBREPO_MERGE_FAILED: lazy_ugettext(
111 111 'This pull request cannot be merged because of conflicts related '
112 112 'to sub repositories.'),
113 113 }
114 114
115 115 UPDATE_STATUS_MESSAGES = {
116 116 UpdateFailureReason.NONE: lazy_ugettext(
117 117 'Pull request update successful.'),
118 118 UpdateFailureReason.UNKNOWN: lazy_ugettext(
119 119 'Pull request update failed because of an unknown error.'),
120 120 UpdateFailureReason.NO_CHANGE: lazy_ugettext(
121 121 'No update needed because the source and target have not changed.'),
122 122 UpdateFailureReason.WRONG_REF_TYPE: lazy_ugettext(
123 123 'Pull request cannot be updated because the reference type is '
124 124 'not supported for an update. Only Branch, Tag or Bookmark is allowed.'),
125 125 UpdateFailureReason.MISSING_TARGET_REF: lazy_ugettext(
126 126 'This pull request cannot be updated because the target '
127 127 'reference is missing.'),
128 128 UpdateFailureReason.MISSING_SOURCE_REF: lazy_ugettext(
129 129 'This pull request cannot be updated because the source '
130 130 'reference is missing.'),
131 131 }
132 132
133 133 def __get_pull_request(self, pull_request):
134 134 return self._get_instance((
135 135 PullRequest, PullRequestVersion), pull_request)
136 136
137 137 def _check_perms(self, perms, pull_request, user, api=False):
138 138 if not api:
139 139 return h.HasRepoPermissionAny(*perms)(
140 140 user=user, repo_name=pull_request.target_repo.repo_name)
141 141 else:
142 142 return h.HasRepoPermissionAnyApi(*perms)(
143 143 user=user, repo_name=pull_request.target_repo.repo_name)
144 144
145 145 def check_user_read(self, pull_request, user, api=False):
146 146 _perms = ('repository.admin', 'repository.write', 'repository.read',)
147 147 return self._check_perms(_perms, pull_request, user, api)
148 148
149 149 def check_user_merge(self, pull_request, user, api=False):
150 150 _perms = ('repository.admin', 'repository.write', 'hg.admin',)
151 151 return self._check_perms(_perms, pull_request, user, api)
152 152
153 153 def check_user_update(self, pull_request, user, api=False):
154 154 owner = user.user_id == pull_request.user_id
155 155 return self.check_user_merge(pull_request, user, api) or owner
156 156
157 157 def check_user_delete(self, pull_request, user):
158 158 owner = user.user_id == pull_request.user_id
159 159 _perms = ('repository.admin',)
160 160 return self._check_perms(_perms, pull_request, user) or owner
161 161
162 162 def check_user_change_status(self, pull_request, user, api=False):
163 163 reviewer = user.user_id in [x.user_id for x in
164 164 pull_request.reviewers]
165 165 return self.check_user_update(pull_request, user, api) or reviewer
166 166
167 167 def check_user_comment(self, pull_request, user):
168 168 owner = user.user_id == pull_request.user_id
169 169 return self.check_user_read(pull_request, user) or owner
170 170
171 171 def get(self, pull_request):
172 172 return self.__get_pull_request(pull_request)
173 173
174 174 def _prepare_get_all_query(self, repo_name, source=False, statuses=None,
175 175 opened_by=None, order_by=None,
176 176 order_dir='desc'):
177 177 repo = None
178 178 if repo_name:
179 179 repo = self._get_repo(repo_name)
180 180
181 181 q = PullRequest.query()
182 182
183 183 # source or target
184 184 if repo and source:
185 185 q = q.filter(PullRequest.source_repo == repo)
186 186 elif repo:
187 187 q = q.filter(PullRequest.target_repo == repo)
188 188
189 189 # closed,opened
190 190 if statuses:
191 191 q = q.filter(PullRequest.status.in_(statuses))
192 192
193 193 # opened by filter
194 194 if opened_by:
195 195 q = q.filter(PullRequest.user_id.in_(opened_by))
196 196
197 197 if order_by:
198 198 order_map = {
199 199 'name_raw': PullRequest.pull_request_id,
200 200 'title': PullRequest.title,
201 201 'updated_on_raw': PullRequest.updated_on,
202 202 'target_repo': PullRequest.target_repo_id
203 203 }
204 204 if order_dir == 'asc':
205 205 q = q.order_by(order_map[order_by].asc())
206 206 else:
207 207 q = q.order_by(order_map[order_by].desc())
208 208
209 209 return q
210 210
211 211 def count_all(self, repo_name, source=False, statuses=None,
212 212 opened_by=None):
213 213 """
214 214 Count the number of pull requests for a specific repository.
215 215
216 216 :param repo_name: target or source repo
217 217 :param source: boolean flag to specify if repo_name refers to source
218 218 :param statuses: list of pull request statuses
219 219 :param opened_by: author user of the pull request
220 220 :returns: int number of pull requests
221 221 """
222 222 q = self._prepare_get_all_query(
223 223 repo_name, source=source, statuses=statuses, opened_by=opened_by)
224 224
225 225 return q.count()
226 226
227 227 def get_all(self, repo_name, source=False, statuses=None, opened_by=None,
228 228 offset=0, length=None, order_by=None, order_dir='desc'):
229 229 """
230 230 Get all pull requests for a specific repository.
231 231
232 232 :param repo_name: target or source repo
233 233 :param source: boolean flag to specify if repo_name refers to source
234 234 :param statuses: list of pull request statuses
235 235 :param opened_by: author user of the pull request
236 236 :param offset: pagination offset
237 237 :param length: length of returned list
238 238 :param order_by: order of the returned list
239 239 :param order_dir: 'asc' or 'desc' ordering direction
240 240 :returns: list of pull requests
241 241 """
242 242 q = self._prepare_get_all_query(
243 243 repo_name, source=source, statuses=statuses, opened_by=opened_by,
244 244 order_by=order_by, order_dir=order_dir)
245 245
246 246 if length:
247 247 pull_requests = q.limit(length).offset(offset).all()
248 248 else:
249 249 pull_requests = q.all()
250 250
251 251 return pull_requests
252 252
253 253 def count_awaiting_review(self, repo_name, source=False, statuses=None,
254 254 opened_by=None):
255 255 """
256 256 Count the number of pull requests for a specific repository that are
257 257 awaiting review.
258 258
259 259 :param repo_name: target or source repo
260 260 :param source: boolean flag to specify if repo_name refers to source
261 261 :param statuses: list of pull request statuses
262 262 :param opened_by: author user of the pull request
263 263 :returns: int number of pull requests
264 264 """
265 265 pull_requests = self.get_awaiting_review(
266 266 repo_name, source=source, statuses=statuses, opened_by=opened_by)
267 267
268 268 return len(pull_requests)
269 269
270 270 def get_awaiting_review(self, repo_name, source=False, statuses=None,
271 271 opened_by=None, offset=0, length=None,
272 272 order_by=None, order_dir='desc'):
273 273 """
274 274 Get all pull requests for a specific repository that are awaiting
275 275 review.
276 276
277 277 :param repo_name: target or source repo
278 278 :param source: boolean flag to specify if repo_name refers to source
279 279 :param statuses: list of pull request statuses
280 280 :param opened_by: author user of the pull request
281 281 :param offset: pagination offset
282 282 :param length: length of returned list
283 283 :param order_by: order of the returned list
284 284 :param order_dir: 'asc' or 'desc' ordering direction
285 285 :returns: list of pull requests
286 286 """
287 287 pull_requests = self.get_all(
288 288 repo_name, source=source, statuses=statuses, opened_by=opened_by,
289 289 order_by=order_by, order_dir=order_dir)
290 290
291 291 _filtered_pull_requests = []
292 292 for pr in pull_requests:
293 293 status = pr.calculated_review_status()
294 294 if status in [ChangesetStatus.STATUS_NOT_REVIEWED,
295 295 ChangesetStatus.STATUS_UNDER_REVIEW]:
296 296 _filtered_pull_requests.append(pr)
297 297 if length:
298 298 return _filtered_pull_requests[offset:offset+length]
299 299 else:
300 300 return _filtered_pull_requests
301 301
302 302 def count_awaiting_my_review(self, repo_name, source=False, statuses=None,
303 303 opened_by=None, user_id=None):
304 304 """
305 305 Count the number of pull requests for a specific repository that are
306 306 awaiting review from a specific user.
307 307
308 308 :param repo_name: target or source repo
309 309 :param source: boolean flag to specify if repo_name refers to source
310 310 :param statuses: list of pull request statuses
311 311 :param opened_by: author user of the pull request
312 312 :param user_id: reviewer user of the pull request
313 313 :returns: int number of pull requests
314 314 """
315 315 pull_requests = self.get_awaiting_my_review(
316 316 repo_name, source=source, statuses=statuses, opened_by=opened_by,
317 317 user_id=user_id)
318 318
319 319 return len(pull_requests)
320 320
321 321 def get_awaiting_my_review(self, repo_name, source=False, statuses=None,
322 322 opened_by=None, user_id=None, offset=0,
323 323 length=None, order_by=None, order_dir='desc'):
324 324 """
325 325 Get all pull requests for a specific repository that are awaiting
326 326 review from a specific user.
327 327
328 328 :param repo_name: target or source repo
329 329 :param source: boolean flag to specify if repo_name refers to source
330 330 :param statuses: list of pull request statuses
331 331 :param opened_by: author user of the pull request
332 332 :param user_id: reviewer user of the pull request
333 333 :param offset: pagination offset
334 334 :param length: length of returned list
335 335 :param order_by: order of the returned list
336 336 :param order_dir: 'asc' or 'desc' ordering direction
337 337 :returns: list of pull requests
338 338 """
339 339 pull_requests = self.get_all(
340 340 repo_name, source=source, statuses=statuses, opened_by=opened_by,
341 341 order_by=order_by, order_dir=order_dir)
342 342
343 343 _my = PullRequestModel().get_not_reviewed(user_id)
344 344 my_participation = []
345 345 for pr in pull_requests:
346 346 if pr in _my:
347 347 my_participation.append(pr)
348 348 _filtered_pull_requests = my_participation
349 349 if length:
350 350 return _filtered_pull_requests[offset:offset+length]
351 351 else:
352 352 return _filtered_pull_requests
353 353
354 354 def get_not_reviewed(self, user_id):
355 355 return [
356 356 x.pull_request for x in PullRequestReviewers.query().filter(
357 357 PullRequestReviewers.user_id == user_id).all()
358 358 ]
359 359
360 360 def _prepare_participating_query(self, user_id=None, statuses=None,
361 361 order_by=None, order_dir='desc'):
362 362 q = PullRequest.query()
363 363 if user_id:
364 364 reviewers_subquery = Session().query(
365 365 PullRequestReviewers.pull_request_id).filter(
366 366 PullRequestReviewers.user_id == user_id).subquery()
367 367 user_filter = or_(
368 368 PullRequest.user_id == user_id,
369 369 PullRequest.pull_request_id.in_(reviewers_subquery)
370 370 )
371 371 q = PullRequest.query().filter(user_filter)
372 372
373 373 # closed,opened
374 374 if statuses:
375 375 q = q.filter(PullRequest.status.in_(statuses))
376 376
377 377 if order_by:
378 378 order_map = {
379 379 'name_raw': PullRequest.pull_request_id,
380 380 'title': PullRequest.title,
381 381 'updated_on_raw': PullRequest.updated_on,
382 382 'target_repo': PullRequest.target_repo_id
383 383 }
384 384 if order_dir == 'asc':
385 385 q = q.order_by(order_map[order_by].asc())
386 386 else:
387 387 q = q.order_by(order_map[order_by].desc())
388 388
389 389 return q
390 390
391 391 def count_im_participating_in(self, user_id=None, statuses=None):
392 392 q = self._prepare_participating_query(user_id, statuses=statuses)
393 393 return q.count()
394 394
395 395 def get_im_participating_in(
396 396 self, user_id=None, statuses=None, offset=0,
397 397 length=None, order_by=None, order_dir='desc'):
398 398 """
399 399 Get all Pull requests that i'm participating in, or i have opened
400 400 """
401 401
402 402 q = self._prepare_participating_query(
403 403 user_id, statuses=statuses, order_by=order_by,
404 404 order_dir=order_dir)
405 405
406 406 if length:
407 407 pull_requests = q.limit(length).offset(offset).all()
408 408 else:
409 409 pull_requests = q.all()
410 410
411 411 return pull_requests
412 412
413 413 def get_versions(self, pull_request):
414 414 """
415 415 returns version of pull request sorted by ID descending
416 416 """
417 417 return PullRequestVersion.query()\
418 418 .filter(PullRequestVersion.pull_request == pull_request)\
419 419 .order_by(PullRequestVersion.pull_request_version_id.asc())\
420 420 .all()
421 421
422 422 def get_pr_version(self, pull_request_id, version=None):
423 423 at_version = None
424 424
425 425 if version and version == 'latest':
426 426 pull_request_ver = PullRequest.get(pull_request_id)
427 427 pull_request_obj = pull_request_ver
428 428 _org_pull_request_obj = pull_request_obj
429 429 at_version = 'latest'
430 430 elif version:
431 431 pull_request_ver = PullRequestVersion.get_or_404(version)
432 432 pull_request_obj = pull_request_ver
433 433 _org_pull_request_obj = pull_request_ver.pull_request
434 434 at_version = pull_request_ver.pull_request_version_id
435 435 else:
436 436 _org_pull_request_obj = pull_request_obj = PullRequest.get_or_404(
437 437 pull_request_id)
438 438
439 439 pull_request_display_obj = PullRequest.get_pr_display_object(
440 440 pull_request_obj, _org_pull_request_obj)
441 441
442 442 return _org_pull_request_obj, pull_request_obj, \
443 443 pull_request_display_obj, at_version
444 444
445 445 def create(self, created_by, source_repo, source_ref, target_repo,
446 446 target_ref, revisions, reviewers, title, description=None,
447 description_renderer=None,
447 448 reviewer_data=None, translator=None, auth_user=None):
448 449 translator = translator or get_current_request().translate
449 450
450 451 created_by_user = self._get_user(created_by)
451 452 auth_user = auth_user or created_by_user
452 453 source_repo = self._get_repo(source_repo)
453 454 target_repo = self._get_repo(target_repo)
454 455
455 456 pull_request = PullRequest()
456 457 pull_request.source_repo = source_repo
457 458 pull_request.source_ref = source_ref
458 459 pull_request.target_repo = target_repo
459 460 pull_request.target_ref = target_ref
460 461 pull_request.revisions = revisions
461 462 pull_request.title = title
462 463 pull_request.description = description
464 pull_request.description_renderer = description_renderer
463 465 pull_request.author = created_by_user
464 466 pull_request.reviewer_data = reviewer_data
465 467
466 468 Session().add(pull_request)
467 469 Session().flush()
468 470
469 471 reviewer_ids = set()
470 472 # members / reviewers
471 473 for reviewer_object in reviewers:
472 474 user_id, reasons, mandatory, rules = reviewer_object
473 475 user = self._get_user(user_id)
474 476
475 477 # skip duplicates
476 478 if user.user_id in reviewer_ids:
477 479 continue
478 480
479 481 reviewer_ids.add(user.user_id)
480 482
481 483 reviewer = PullRequestReviewers()
482 484 reviewer.user = user
483 485 reviewer.pull_request = pull_request
484 486 reviewer.reasons = reasons
485 487 reviewer.mandatory = mandatory
486 488
487 489 # NOTE(marcink): pick only first rule for now
488 490 rule_id = list(rules)[0] if rules else None
489 491 rule = RepoReviewRule.get(rule_id) if rule_id else None
490 492 if rule:
491 493 review_group = rule.user_group_vote_rule()
492 494 if review_group:
493 495 # NOTE(marcink):
494 496 # again, can be that user is member of more,
495 497 # but we pick the first same, as default reviewers algo
496 498 review_group = review_group[0]
497 499
498 500 rule_data = {
499 501 'rule_name':
500 502 rule.review_rule_name,
501 503 'rule_user_group_entry_id':
502 504 review_group.repo_review_rule_users_group_id,
503 505 'rule_user_group_name':
504 506 review_group.users_group.users_group_name,
505 507 'rule_user_group_members':
506 508 [x.user.username for x in review_group.users_group.members],
507 509 }
508 510 # e.g {'vote_rule': -1, 'mandatory': True}
509 511 rule_data.update(review_group.rule_data())
510 512
511 513 reviewer.rule_data = rule_data
512 514
513 515 Session().add(reviewer)
514 516 Session().flush()
515 517
516 518 # Set approval status to "Under Review" for all commits which are
517 519 # part of this pull request.
518 520 ChangesetStatusModel().set_status(
519 521 repo=target_repo,
520 522 status=ChangesetStatus.STATUS_UNDER_REVIEW,
521 523 user=created_by_user,
522 524 pull_request=pull_request
523 525 )
524 526 # we commit early at this point. This has to do with a fact
525 527 # that before queries do some row-locking. And because of that
526 528 # we need to commit and finish transation before below validate call
527 529 # that for large repos could be long resulting in long row locks
528 530 Session().commit()
529 531
530 532 # prepare workspace, and run initial merge simulation
531 533 MergeCheck.validate(
532 534 pull_request, user=created_by_user, translator=translator)
533 535
534 536 self.notify_reviewers(pull_request, reviewer_ids)
535 537 self._trigger_pull_request_hook(
536 538 pull_request, created_by_user, 'create')
537 539
538 540 creation_data = pull_request.get_api_data(with_merge_state=False)
539 541 self._log_audit_action(
540 542 'repo.pull_request.create', {'data': creation_data},
541 543 auth_user, pull_request)
542 544
543 545 return pull_request
544 546
545 547 def _trigger_pull_request_hook(self, pull_request, user, action):
546 548 pull_request = self.__get_pull_request(pull_request)
547 549 target_scm = pull_request.target_repo.scm_instance()
548 550 if action == 'create':
549 551 trigger_hook = hooks_utils.trigger_log_create_pull_request_hook
550 552 elif action == 'merge':
551 553 trigger_hook = hooks_utils.trigger_log_merge_pull_request_hook
552 554 elif action == 'close':
553 555 trigger_hook = hooks_utils.trigger_log_close_pull_request_hook
554 556 elif action == 'review_status_change':
555 557 trigger_hook = hooks_utils.trigger_log_review_pull_request_hook
556 558 elif action == 'update':
557 559 trigger_hook = hooks_utils.trigger_log_update_pull_request_hook
558 560 else:
559 561 return
560 562
561 563 trigger_hook(
562 564 username=user.username,
563 565 repo_name=pull_request.target_repo.repo_name,
564 566 repo_alias=target_scm.alias,
565 567 pull_request=pull_request)
566 568
567 569 def _get_commit_ids(self, pull_request):
568 570 """
569 571 Return the commit ids of the merged pull request.
570 572
571 573 This method is not dealing correctly yet with the lack of autoupdates
572 574 nor with the implicit target updates.
573 575 For example: if a commit in the source repo is already in the target it
574 576 will be reported anyways.
575 577 """
576 578 merge_rev = pull_request.merge_rev
577 579 if merge_rev is None:
578 580 raise ValueError('This pull request was not merged yet')
579 581
580 582 commit_ids = list(pull_request.revisions)
581 583 if merge_rev not in commit_ids:
582 584 commit_ids.append(merge_rev)
583 585
584 586 return commit_ids
585 587
586 588 def merge_repo(self, pull_request, user, extras):
587 589 log.debug("Merging pull request %s", pull_request.pull_request_id)
588 590 merge_state = self._merge_pull_request(pull_request, user, extras)
589 591 if merge_state.executed:
590 592 log.debug(
591 593 "Merge was successful, updating the pull request comments.")
592 594 self._comment_and_close_pr(pull_request, user, merge_state)
593 595
594 596 self._log_audit_action(
595 597 'repo.pull_request.merge',
596 598 {'merge_state': merge_state.__dict__},
597 599 user, pull_request)
598 600
599 601 else:
600 602 log.warn("Merge failed, not updating the pull request.")
601 603 return merge_state
602 604
603 605 def _merge_pull_request(self, pull_request, user, extras, merge_msg=None):
604 606 target_vcs = pull_request.target_repo.scm_instance()
605 607 source_vcs = pull_request.source_repo.scm_instance()
606 608 target_ref = self._refresh_reference(
607 609 pull_request.target_ref_parts, target_vcs)
608 610
609 611 message = merge_msg or (
610 612 'Merge pull request #%(pr_id)s from '
611 613 '%(source_repo)s %(source_ref_name)s\n\n %(pr_title)s') % {
612 614 'pr_id': pull_request.pull_request_id,
613 615 'source_repo': source_vcs.name,
614 616 'source_ref_name': pull_request.source_ref_parts.name,
615 617 'pr_title': pull_request.title
616 618 }
617 619
618 620 workspace_id = self._workspace_id(pull_request)
619 621 repo_id = pull_request.target_repo.repo_id
620 622 use_rebase = self._use_rebase_for_merging(pull_request)
621 623 close_branch = self._close_branch_before_merging(pull_request)
622 624
623 625 callback_daemon, extras = prepare_callback_daemon(
624 626 extras, protocol=vcs_settings.HOOKS_PROTOCOL,
625 627 host=vcs_settings.HOOKS_HOST,
626 628 use_direct_calls=vcs_settings.HOOKS_DIRECT_CALLS)
627 629
628 630 with callback_daemon:
629 631 # TODO: johbo: Implement a clean way to run a config_override
630 632 # for a single call.
631 633 target_vcs.config.set(
632 634 'rhodecode', 'RC_SCM_DATA', json.dumps(extras))
633 635 merge_state = target_vcs.merge(
634 636 repo_id, workspace_id, target_ref, source_vcs,
635 637 pull_request.source_ref_parts,
636 638 user_name=user.username, user_email=user.email,
637 639 message=message, use_rebase=use_rebase,
638 640 close_branch=close_branch)
639 641 return merge_state
640 642
641 643 def _comment_and_close_pr(self, pull_request, user, merge_state, close_msg=None):
642 644 pull_request.merge_rev = merge_state.merge_ref.commit_id
643 645 pull_request.updated_on = datetime.datetime.now()
644 646 close_msg = close_msg or 'Pull request merged and closed'
645 647
646 648 CommentsModel().create(
647 649 text=safe_unicode(close_msg),
648 650 repo=pull_request.target_repo.repo_id,
649 651 user=user.user_id,
650 652 pull_request=pull_request.pull_request_id,
651 653 f_path=None,
652 654 line_no=None,
653 655 closing_pr=True
654 656 )
655 657
656 658 Session().add(pull_request)
657 659 Session().flush()
658 660 # TODO: paris: replace invalidation with less radical solution
659 661 ScmModel().mark_for_invalidation(
660 662 pull_request.target_repo.repo_name)
661 663 self._trigger_pull_request_hook(pull_request, user, 'merge')
662 664
663 665 def has_valid_update_type(self, pull_request):
664 666 source_ref_type = pull_request.source_ref_parts.type
665 667 return source_ref_type in ['book', 'branch', 'tag']
666 668
667 669 def update_commits(self, pull_request):
668 670 """
669 671 Get the updated list of commits for the pull request
670 672 and return the new pull request version and the list
671 673 of commits processed by this update action
672 674 """
673 675 pull_request = self.__get_pull_request(pull_request)
674 676 source_ref_type = pull_request.source_ref_parts.type
675 677 source_ref_name = pull_request.source_ref_parts.name
676 678 source_ref_id = pull_request.source_ref_parts.commit_id
677 679
678 680 target_ref_type = pull_request.target_ref_parts.type
679 681 target_ref_name = pull_request.target_ref_parts.name
680 682 target_ref_id = pull_request.target_ref_parts.commit_id
681 683
682 684 if not self.has_valid_update_type(pull_request):
683 685 log.debug(
684 686 "Skipping update of pull request %s due to ref type: %s",
685 687 pull_request, source_ref_type)
686 688 return UpdateResponse(
687 689 executed=False,
688 690 reason=UpdateFailureReason.WRONG_REF_TYPE,
689 691 old=pull_request, new=None, changes=None,
690 692 source_changed=False, target_changed=False)
691 693
692 694 # source repo
693 695 source_repo = pull_request.source_repo.scm_instance()
694 696 try:
695 697 source_commit = source_repo.get_commit(commit_id=source_ref_name)
696 698 except CommitDoesNotExistError:
697 699 return UpdateResponse(
698 700 executed=False,
699 701 reason=UpdateFailureReason.MISSING_SOURCE_REF,
700 702 old=pull_request, new=None, changes=None,
701 703 source_changed=False, target_changed=False)
702 704
703 705 source_changed = source_ref_id != source_commit.raw_id
704 706
705 707 # target repo
706 708 target_repo = pull_request.target_repo.scm_instance()
707 709 try:
708 710 target_commit = target_repo.get_commit(commit_id=target_ref_name)
709 711 except CommitDoesNotExistError:
710 712 return UpdateResponse(
711 713 executed=False,
712 714 reason=UpdateFailureReason.MISSING_TARGET_REF,
713 715 old=pull_request, new=None, changes=None,
714 716 source_changed=False, target_changed=False)
715 717 target_changed = target_ref_id != target_commit.raw_id
716 718
717 719 if not (source_changed or target_changed):
718 720 log.debug("Nothing changed in pull request %s", pull_request)
719 721 return UpdateResponse(
720 722 executed=False,
721 723 reason=UpdateFailureReason.NO_CHANGE,
722 724 old=pull_request, new=None, changes=None,
723 725 source_changed=target_changed, target_changed=source_changed)
724 726
725 727 change_in_found = 'target repo' if target_changed else 'source repo'
726 728 log.debug('Updating pull request because of change in %s detected',
727 729 change_in_found)
728 730
729 731 # Finally there is a need for an update, in case of source change
730 732 # we create a new version, else just an update
731 733 if source_changed:
732 734 pull_request_version = self._create_version_from_snapshot(pull_request)
733 735 self._link_comments_to_version(pull_request_version)
734 736 else:
735 737 try:
736 738 ver = pull_request.versions[-1]
737 739 except IndexError:
738 740 ver = None
739 741
740 742 pull_request.pull_request_version_id = \
741 743 ver.pull_request_version_id if ver else None
742 744 pull_request_version = pull_request
743 745
744 746 try:
745 747 if target_ref_type in ('tag', 'branch', 'book'):
746 748 target_commit = target_repo.get_commit(target_ref_name)
747 749 else:
748 750 target_commit = target_repo.get_commit(target_ref_id)
749 751 except CommitDoesNotExistError:
750 752 return UpdateResponse(
751 753 executed=False,
752 754 reason=UpdateFailureReason.MISSING_TARGET_REF,
753 755 old=pull_request, new=None, changes=None,
754 756 source_changed=source_changed, target_changed=target_changed)
755 757
756 758 # re-compute commit ids
757 759 old_commit_ids = pull_request.revisions
758 760 pre_load = ["author", "branch", "date", "message"]
759 761 commit_ranges = target_repo.compare(
760 762 target_commit.raw_id, source_commit.raw_id, source_repo, merge=True,
761 763 pre_load=pre_load)
762 764
763 765 ancestor = target_repo.get_common_ancestor(
764 766 target_commit.raw_id, source_commit.raw_id, source_repo)
765 767
766 768 pull_request.source_ref = '%s:%s:%s' % (
767 769 source_ref_type, source_ref_name, source_commit.raw_id)
768 770 pull_request.target_ref = '%s:%s:%s' % (
769 771 target_ref_type, target_ref_name, ancestor)
770 772
771 773 pull_request.revisions = [
772 774 commit.raw_id for commit in reversed(commit_ranges)]
773 775 pull_request.updated_on = datetime.datetime.now()
774 776 Session().add(pull_request)
775 777 new_commit_ids = pull_request.revisions
776 778
777 779 old_diff_data, new_diff_data = self._generate_update_diffs(
778 780 pull_request, pull_request_version)
779 781
780 782 # calculate commit and file changes
781 783 changes = self._calculate_commit_id_changes(
782 784 old_commit_ids, new_commit_ids)
783 785 file_changes = self._calculate_file_changes(
784 786 old_diff_data, new_diff_data)
785 787
786 788 # set comments as outdated if DIFFS changed
787 789 CommentsModel().outdate_comments(
788 790 pull_request, old_diff_data=old_diff_data,
789 791 new_diff_data=new_diff_data)
790 792
791 793 commit_changes = (changes.added or changes.removed)
792 794 file_node_changes = (
793 795 file_changes.added or file_changes.modified or file_changes.removed)
794 796 pr_has_changes = commit_changes or file_node_changes
795 797
796 798 # Add an automatic comment to the pull request, in case
797 799 # anything has changed
798 800 if pr_has_changes:
799 801 update_comment = CommentsModel().create(
800 802 text=self._render_update_message(changes, file_changes),
801 803 repo=pull_request.target_repo,
802 804 user=pull_request.author,
803 805 pull_request=pull_request,
804 806 send_email=False, renderer=DEFAULT_COMMENTS_RENDERER)
805 807
806 808 # Update status to "Under Review" for added commits
807 809 for commit_id in changes.added:
808 810 ChangesetStatusModel().set_status(
809 811 repo=pull_request.source_repo,
810 812 status=ChangesetStatus.STATUS_UNDER_REVIEW,
811 813 comment=update_comment,
812 814 user=pull_request.author,
813 815 pull_request=pull_request,
814 816 revision=commit_id)
815 817
816 818 log.debug(
817 819 'Updated pull request %s, added_ids: %s, common_ids: %s, '
818 820 'removed_ids: %s', pull_request.pull_request_id,
819 821 changes.added, changes.common, changes.removed)
820 822 log.debug(
821 823 'Updated pull request with the following file changes: %s',
822 824 file_changes)
823 825
824 826 log.info(
825 827 "Updated pull request %s from commit %s to commit %s, "
826 828 "stored new version %s of this pull request.",
827 829 pull_request.pull_request_id, source_ref_id,
828 830 pull_request.source_ref_parts.commit_id,
829 831 pull_request_version.pull_request_version_id)
830 832 Session().commit()
831 833 self._trigger_pull_request_hook(
832 834 pull_request, pull_request.author, 'update')
833 835
834 836 return UpdateResponse(
835 837 executed=True, reason=UpdateFailureReason.NONE,
836 838 old=pull_request, new=pull_request_version, changes=changes,
837 839 source_changed=source_changed, target_changed=target_changed)
838 840
839 841 def _create_version_from_snapshot(self, pull_request):
840 842 version = PullRequestVersion()
841 843 version.title = pull_request.title
842 844 version.description = pull_request.description
843 845 version.status = pull_request.status
844 846 version.created_on = datetime.datetime.now()
845 847 version.updated_on = pull_request.updated_on
846 848 version.user_id = pull_request.user_id
847 849 version.source_repo = pull_request.source_repo
848 850 version.source_ref = pull_request.source_ref
849 851 version.target_repo = pull_request.target_repo
850 852 version.target_ref = pull_request.target_ref
851 853
852 854 version._last_merge_source_rev = pull_request._last_merge_source_rev
853 855 version._last_merge_target_rev = pull_request._last_merge_target_rev
854 856 version.last_merge_status = pull_request.last_merge_status
855 857 version.shadow_merge_ref = pull_request.shadow_merge_ref
856 858 version.merge_rev = pull_request.merge_rev
857 859 version.reviewer_data = pull_request.reviewer_data
858 860
859 861 version.revisions = pull_request.revisions
860 862 version.pull_request = pull_request
861 863 Session().add(version)
862 864 Session().flush()
863 865
864 866 return version
865 867
866 868 def _generate_update_diffs(self, pull_request, pull_request_version):
867 869
868 870 diff_context = (
869 871 self.DIFF_CONTEXT +
870 872 CommentsModel.needed_extra_diff_context())
871 873
872 874 source_repo = pull_request_version.source_repo
873 875 source_ref_id = pull_request_version.source_ref_parts.commit_id
874 876 target_ref_id = pull_request_version.target_ref_parts.commit_id
875 877 old_diff = self._get_diff_from_pr_or_version(
876 878 source_repo, source_ref_id, target_ref_id, context=diff_context)
877 879
878 880 source_repo = pull_request.source_repo
879 881 source_ref_id = pull_request.source_ref_parts.commit_id
880 882 target_ref_id = pull_request.target_ref_parts.commit_id
881 883
882 884 new_diff = self._get_diff_from_pr_or_version(
883 885 source_repo, source_ref_id, target_ref_id, context=diff_context)
884 886
885 887 old_diff_data = diffs.DiffProcessor(old_diff)
886 888 old_diff_data.prepare()
887 889 new_diff_data = diffs.DiffProcessor(new_diff)
888 890 new_diff_data.prepare()
889 891
890 892 return old_diff_data, new_diff_data
891 893
892 894 def _link_comments_to_version(self, pull_request_version):
893 895 """
894 896 Link all unlinked comments of this pull request to the given version.
895 897
896 898 :param pull_request_version: The `PullRequestVersion` to which
897 899 the comments shall be linked.
898 900
899 901 """
900 902 pull_request = pull_request_version.pull_request
901 903 comments = ChangesetComment.query()\
902 904 .filter(
903 905 # TODO: johbo: Should we query for the repo at all here?
904 906 # Pending decision on how comments of PRs are to be related
905 907 # to either the source repo, the target repo or no repo at all.
906 908 ChangesetComment.repo_id == pull_request.target_repo.repo_id,
907 909 ChangesetComment.pull_request == pull_request,
908 910 ChangesetComment.pull_request_version == None)\
909 911 .order_by(ChangesetComment.comment_id.asc())
910 912
911 913 # TODO: johbo: Find out why this breaks if it is done in a bulk
912 914 # operation.
913 915 for comment in comments:
914 916 comment.pull_request_version_id = (
915 917 pull_request_version.pull_request_version_id)
916 918 Session().add(comment)
917 919
918 920 def _calculate_commit_id_changes(self, old_ids, new_ids):
919 921 added = [x for x in new_ids if x not in old_ids]
920 922 common = [x for x in new_ids if x in old_ids]
921 923 removed = [x for x in old_ids if x not in new_ids]
922 924 total = new_ids
923 925 return ChangeTuple(added, common, removed, total)
924 926
925 927 def _calculate_file_changes(self, old_diff_data, new_diff_data):
926 928
927 929 old_files = OrderedDict()
928 930 for diff_data in old_diff_data.parsed_diff:
929 931 old_files[diff_data['filename']] = md5_safe(diff_data['raw_diff'])
930 932
931 933 added_files = []
932 934 modified_files = []
933 935 removed_files = []
934 936 for diff_data in new_diff_data.parsed_diff:
935 937 new_filename = diff_data['filename']
936 938 new_hash = md5_safe(diff_data['raw_diff'])
937 939
938 940 old_hash = old_files.get(new_filename)
939 941 if not old_hash:
940 942 # file is not present in old diff, means it's added
941 943 added_files.append(new_filename)
942 944 else:
943 945 if new_hash != old_hash:
944 946 modified_files.append(new_filename)
945 947 # now remove a file from old, since we have seen it already
946 948 del old_files[new_filename]
947 949
948 950 # removed files is when there are present in old, but not in NEW,
949 951 # since we remove old files that are present in new diff, left-overs
950 952 # if any should be the removed files
951 953 removed_files.extend(old_files.keys())
952 954
953 955 return FileChangeTuple(added_files, modified_files, removed_files)
954 956
955 957 def _render_update_message(self, changes, file_changes):
956 958 """
957 959 render the message using DEFAULT_COMMENTS_RENDERER (RST renderer),
958 960 so it's always looking the same disregarding on which default
959 961 renderer system is using.
960 962
961 963 :param changes: changes named tuple
962 964 :param file_changes: file changes named tuple
963 965
964 966 """
965 967 new_status = ChangesetStatus.get_status_lbl(
966 968 ChangesetStatus.STATUS_UNDER_REVIEW)
967 969
968 970 changed_files = (
969 971 file_changes.added + file_changes.modified + file_changes.removed)
970 972
971 973 params = {
972 974 'under_review_label': new_status,
973 975 'added_commits': changes.added,
974 976 'removed_commits': changes.removed,
975 977 'changed_files': changed_files,
976 978 'added_files': file_changes.added,
977 979 'modified_files': file_changes.modified,
978 980 'removed_files': file_changes.removed,
979 981 }
980 982 renderer = RstTemplateRenderer()
981 983 return renderer.render('pull_request_update.mako', **params)
982 984
983 def edit(self, pull_request, title, description, user):
985 def edit(self, pull_request, title, description, description_renderer, user):
984 986 pull_request = self.__get_pull_request(pull_request)
985 987 old_data = pull_request.get_api_data(with_merge_state=False)
986 988 if pull_request.is_closed():
987 989 raise ValueError('This pull request is closed')
988 990 if title:
989 991 pull_request.title = title
990 992 pull_request.description = description
991 993 pull_request.updated_on = datetime.datetime.now()
994 pull_request.description_renderer = description_renderer
992 995 Session().add(pull_request)
993 996 self._log_audit_action(
994 997 'repo.pull_request.edit', {'old_data': old_data},
995 998 user, pull_request)
996 999
997 1000 def update_reviewers(self, pull_request, reviewer_data, user):
998 1001 """
999 1002 Update the reviewers in the pull request
1000 1003
1001 1004 :param pull_request: the pr to update
1002 1005 :param reviewer_data: list of tuples
1003 1006 [(user, ['reason1', 'reason2'], mandatory_flag, [rules])]
1004 1007 """
1005 1008 pull_request = self.__get_pull_request(pull_request)
1006 1009 if pull_request.is_closed():
1007 1010 raise ValueError('This pull request is closed')
1008 1011
1009 1012 reviewers = {}
1010 1013 for user_id, reasons, mandatory, rules in reviewer_data:
1011 1014 if isinstance(user_id, (int, basestring)):
1012 1015 user_id = self._get_user(user_id).user_id
1013 1016 reviewers[user_id] = {
1014 1017 'reasons': reasons, 'mandatory': mandatory}
1015 1018
1016 1019 reviewers_ids = set(reviewers.keys())
1017 1020 current_reviewers = PullRequestReviewers.query()\
1018 1021 .filter(PullRequestReviewers.pull_request ==
1019 1022 pull_request).all()
1020 1023 current_reviewers_ids = set([x.user.user_id for x in current_reviewers])
1021 1024
1022 1025 ids_to_add = reviewers_ids.difference(current_reviewers_ids)
1023 1026 ids_to_remove = current_reviewers_ids.difference(reviewers_ids)
1024 1027
1025 1028 log.debug("Adding %s reviewers", ids_to_add)
1026 1029 log.debug("Removing %s reviewers", ids_to_remove)
1027 1030 changed = False
1028 1031 for uid in ids_to_add:
1029 1032 changed = True
1030 1033 _usr = self._get_user(uid)
1031 1034 reviewer = PullRequestReviewers()
1032 1035 reviewer.user = _usr
1033 1036 reviewer.pull_request = pull_request
1034 1037 reviewer.reasons = reviewers[uid]['reasons']
1035 1038 # NOTE(marcink): mandatory shouldn't be changed now
1036 1039 # reviewer.mandatory = reviewers[uid]['reasons']
1037 1040 Session().add(reviewer)
1038 1041 self._log_audit_action(
1039 1042 'repo.pull_request.reviewer.add', {'data': reviewer.get_dict()},
1040 1043 user, pull_request)
1041 1044
1042 1045 for uid in ids_to_remove:
1043 1046 changed = True
1044 1047 reviewers = PullRequestReviewers.query()\
1045 1048 .filter(PullRequestReviewers.user_id == uid,
1046 1049 PullRequestReviewers.pull_request == pull_request)\
1047 1050 .all()
1048 1051 # use .all() in case we accidentally added the same person twice
1049 1052 # this CAN happen due to the lack of DB checks
1050 1053 for obj in reviewers:
1051 1054 old_data = obj.get_dict()
1052 1055 Session().delete(obj)
1053 1056 self._log_audit_action(
1054 1057 'repo.pull_request.reviewer.delete',
1055 1058 {'old_data': old_data}, user, pull_request)
1056 1059
1057 1060 if changed:
1058 1061 pull_request.updated_on = datetime.datetime.now()
1059 1062 Session().add(pull_request)
1060 1063
1061 1064 self.notify_reviewers(pull_request, ids_to_add)
1062 1065 return ids_to_add, ids_to_remove
1063 1066
1064 1067 def get_url(self, pull_request, request=None, permalink=False):
1065 1068 if not request:
1066 1069 request = get_current_request()
1067 1070
1068 1071 if permalink:
1069 1072 return request.route_url(
1070 1073 'pull_requests_global',
1071 1074 pull_request_id=pull_request.pull_request_id,)
1072 1075 else:
1073 1076 return request.route_url('pullrequest_show',
1074 1077 repo_name=safe_str(pull_request.target_repo.repo_name),
1075 1078 pull_request_id=pull_request.pull_request_id,)
1076 1079
1077 1080 def get_shadow_clone_url(self, pull_request, request=None):
1078 1081 """
1079 1082 Returns qualified url pointing to the shadow repository. If this pull
1080 1083 request is closed there is no shadow repository and ``None`` will be
1081 1084 returned.
1082 1085 """
1083 1086 if pull_request.is_closed():
1084 1087 return None
1085 1088 else:
1086 1089 pr_url = urllib.unquote(self.get_url(pull_request, request=request))
1087 1090 return safe_unicode('{pr_url}/repository'.format(pr_url=pr_url))
1088 1091
1089 1092 def notify_reviewers(self, pull_request, reviewers_ids):
1090 1093 # notification to reviewers
1091 1094 if not reviewers_ids:
1092 1095 return
1093 1096
1094 1097 pull_request_obj = pull_request
1095 1098 # get the current participants of this pull request
1096 1099 recipients = reviewers_ids
1097 1100 notification_type = EmailNotificationModel.TYPE_PULL_REQUEST
1098 1101
1099 1102 pr_source_repo = pull_request_obj.source_repo
1100 1103 pr_target_repo = pull_request_obj.target_repo
1101 1104
1102 1105 pr_url = h.route_url('pullrequest_show',
1103 1106 repo_name=pr_target_repo.repo_name,
1104 1107 pull_request_id=pull_request_obj.pull_request_id,)
1105 1108
1106 1109 # set some variables for email notification
1107 1110 pr_target_repo_url = h.route_url(
1108 1111 'repo_summary', repo_name=pr_target_repo.repo_name)
1109 1112
1110 1113 pr_source_repo_url = h.route_url(
1111 1114 'repo_summary', repo_name=pr_source_repo.repo_name)
1112 1115
1113 1116 # pull request specifics
1114 1117 pull_request_commits = [
1115 1118 (x.raw_id, x.message)
1116 1119 for x in map(pr_source_repo.get_commit, pull_request.revisions)]
1117 1120
1118 1121 kwargs = {
1119 1122 'user': pull_request.author,
1120 1123 'pull_request': pull_request_obj,
1121 1124 'pull_request_commits': pull_request_commits,
1122 1125
1123 1126 'pull_request_target_repo': pr_target_repo,
1124 1127 'pull_request_target_repo_url': pr_target_repo_url,
1125 1128
1126 1129 'pull_request_source_repo': pr_source_repo,
1127 1130 'pull_request_source_repo_url': pr_source_repo_url,
1128 1131
1129 1132 'pull_request_url': pr_url,
1130 1133 }
1131 1134
1132 1135 # pre-generate the subject for notification itself
1133 1136 (subject,
1134 1137 _h, _e, # we don't care about those
1135 1138 body_plaintext) = EmailNotificationModel().render_email(
1136 1139 notification_type, **kwargs)
1137 1140
1138 1141 # create notification objects, and emails
1139 1142 NotificationModel().create(
1140 1143 created_by=pull_request.author,
1141 1144 notification_subject=subject,
1142 1145 notification_body=body_plaintext,
1143 1146 notification_type=notification_type,
1144 1147 recipients=recipients,
1145 1148 email_kwargs=kwargs,
1146 1149 )
1147 1150
1148 1151 def delete(self, pull_request, user):
1149 1152 pull_request = self.__get_pull_request(pull_request)
1150 1153 old_data = pull_request.get_api_data(with_merge_state=False)
1151 1154 self._cleanup_merge_workspace(pull_request)
1152 1155 self._log_audit_action(
1153 1156 'repo.pull_request.delete', {'old_data': old_data},
1154 1157 user, pull_request)
1155 1158 Session().delete(pull_request)
1156 1159
1157 1160 def close_pull_request(self, pull_request, user):
1158 1161 pull_request = self.__get_pull_request(pull_request)
1159 1162 self._cleanup_merge_workspace(pull_request)
1160 1163 pull_request.status = PullRequest.STATUS_CLOSED
1161 1164 pull_request.updated_on = datetime.datetime.now()
1162 1165 Session().add(pull_request)
1163 1166 self._trigger_pull_request_hook(
1164 1167 pull_request, pull_request.author, 'close')
1165 1168
1166 1169 pr_data = pull_request.get_api_data(with_merge_state=False)
1167 1170 self._log_audit_action(
1168 1171 'repo.pull_request.close', {'data': pr_data}, user, pull_request)
1169 1172
1170 1173 def close_pull_request_with_comment(
1171 1174 self, pull_request, user, repo, message=None):
1172 1175
1173 1176 pull_request_review_status = pull_request.calculated_review_status()
1174 1177
1175 1178 if pull_request_review_status == ChangesetStatus.STATUS_APPROVED:
1176 1179 # approved only if we have voting consent
1177 1180 status = ChangesetStatus.STATUS_APPROVED
1178 1181 else:
1179 1182 status = ChangesetStatus.STATUS_REJECTED
1180 1183 status_lbl = ChangesetStatus.get_status_lbl(status)
1181 1184
1182 1185 default_message = (
1183 1186 'Closing with status change {transition_icon} {status}.'
1184 1187 ).format(transition_icon='>', status=status_lbl)
1185 1188 text = message or default_message
1186 1189
1187 1190 # create a comment, and link it to new status
1188 1191 comment = CommentsModel().create(
1189 1192 text=text,
1190 1193 repo=repo.repo_id,
1191 1194 user=user.user_id,
1192 1195 pull_request=pull_request.pull_request_id,
1193 1196 status_change=status_lbl,
1194 1197 status_change_type=status,
1195 1198 closing_pr=True
1196 1199 )
1197 1200
1198 1201 # calculate old status before we change it
1199 1202 old_calculated_status = pull_request.calculated_review_status()
1200 1203 ChangesetStatusModel().set_status(
1201 1204 repo.repo_id,
1202 1205 status,
1203 1206 user.user_id,
1204 1207 comment=comment,
1205 1208 pull_request=pull_request.pull_request_id
1206 1209 )
1207 1210
1208 1211 Session().flush()
1209 1212 events.trigger(events.PullRequestCommentEvent(pull_request, comment))
1210 1213 # we now calculate the status of pull request again, and based on that
1211 1214 # calculation trigger status change. This might happen in cases
1212 1215 # that non-reviewer admin closes a pr, which means his vote doesn't
1213 1216 # change the status, while if he's a reviewer this might change it.
1214 1217 calculated_status = pull_request.calculated_review_status()
1215 1218 if old_calculated_status != calculated_status:
1216 1219 self._trigger_pull_request_hook(
1217 1220 pull_request, user, 'review_status_change')
1218 1221
1219 1222 # finally close the PR
1220 1223 PullRequestModel().close_pull_request(
1221 1224 pull_request.pull_request_id, user)
1222 1225
1223 1226 return comment, status
1224 1227
1225 1228 def merge_status(self, pull_request, translator=None,
1226 1229 force_shadow_repo_refresh=False):
1227 1230 _ = translator or get_current_request().translate
1228 1231
1229 1232 if not self._is_merge_enabled(pull_request):
1230 1233 return False, _('Server-side pull request merging is disabled.')
1231 1234 if pull_request.is_closed():
1232 1235 return False, _('This pull request is closed.')
1233 1236 merge_possible, msg = self._check_repo_requirements(
1234 1237 target=pull_request.target_repo, source=pull_request.source_repo,
1235 1238 translator=_)
1236 1239 if not merge_possible:
1237 1240 return merge_possible, msg
1238 1241
1239 1242 try:
1240 1243 resp = self._try_merge(
1241 1244 pull_request,
1242 1245 force_shadow_repo_refresh=force_shadow_repo_refresh)
1243 1246 log.debug("Merge response: %s", resp)
1244 1247 status = resp.possible, self.merge_status_message(
1245 1248 resp.failure_reason)
1246 1249 except NotImplementedError:
1247 1250 status = False, _('Pull request merging is not supported.')
1248 1251
1249 1252 return status
1250 1253
1251 1254 def _check_repo_requirements(self, target, source, translator):
1252 1255 """
1253 1256 Check if `target` and `source` have compatible requirements.
1254 1257
1255 1258 Currently this is just checking for largefiles.
1256 1259 """
1257 1260 _ = translator
1258 1261 target_has_largefiles = self._has_largefiles(target)
1259 1262 source_has_largefiles = self._has_largefiles(source)
1260 1263 merge_possible = True
1261 1264 message = u''
1262 1265
1263 1266 if target_has_largefiles != source_has_largefiles:
1264 1267 merge_possible = False
1265 1268 if source_has_largefiles:
1266 1269 message = _(
1267 1270 'Target repository large files support is disabled.')
1268 1271 else:
1269 1272 message = _(
1270 1273 'Source repository large files support is disabled.')
1271 1274
1272 1275 return merge_possible, message
1273 1276
1274 1277 def _has_largefiles(self, repo):
1275 1278 largefiles_ui = VcsSettingsModel(repo=repo).get_ui_settings(
1276 1279 'extensions', 'largefiles')
1277 1280 return largefiles_ui and largefiles_ui[0].active
1278 1281
1279 1282 def _try_merge(self, pull_request, force_shadow_repo_refresh=False):
1280 1283 """
1281 1284 Try to merge the pull request and return the merge status.
1282 1285 """
1283 1286 log.debug(
1284 1287 "Trying out if the pull request %s can be merged. Force_refresh=%s",
1285 1288 pull_request.pull_request_id, force_shadow_repo_refresh)
1286 1289 target_vcs = pull_request.target_repo.scm_instance()
1287 1290
1288 1291 # Refresh the target reference.
1289 1292 try:
1290 1293 target_ref = self._refresh_reference(
1291 1294 pull_request.target_ref_parts, target_vcs)
1292 1295 except CommitDoesNotExistError:
1293 1296 merge_state = MergeResponse(
1294 1297 False, False, None, MergeFailureReason.MISSING_TARGET_REF)
1295 1298 return merge_state
1296 1299
1297 1300 target_locked = pull_request.target_repo.locked
1298 1301 if target_locked and target_locked[0]:
1299 1302 log.debug("The target repository is locked.")
1300 1303 merge_state = MergeResponse(
1301 1304 False, False, None, MergeFailureReason.TARGET_IS_LOCKED)
1302 1305 elif force_shadow_repo_refresh or self._needs_merge_state_refresh(
1303 1306 pull_request, target_ref):
1304 1307 log.debug("Refreshing the merge status of the repository.")
1305 1308 merge_state = self._refresh_merge_state(
1306 1309 pull_request, target_vcs, target_ref)
1307 1310 else:
1308 1311 possible = pull_request.\
1309 1312 last_merge_status == MergeFailureReason.NONE
1310 1313 merge_state = MergeResponse(
1311 1314 possible, False, None, pull_request.last_merge_status)
1312 1315
1313 1316 return merge_state
1314 1317
1315 1318 def _refresh_reference(self, reference, vcs_repository):
1316 1319 if reference.type in ('branch', 'book'):
1317 1320 name_or_id = reference.name
1318 1321 else:
1319 1322 name_or_id = reference.commit_id
1320 1323 refreshed_commit = vcs_repository.get_commit(name_or_id)
1321 1324 refreshed_reference = Reference(
1322 1325 reference.type, reference.name, refreshed_commit.raw_id)
1323 1326 return refreshed_reference
1324 1327
1325 1328 def _needs_merge_state_refresh(self, pull_request, target_reference):
1326 1329 return not(
1327 1330 pull_request.revisions and
1328 1331 pull_request.revisions[0] == pull_request._last_merge_source_rev and
1329 1332 target_reference.commit_id == pull_request._last_merge_target_rev)
1330 1333
1331 1334 def _refresh_merge_state(self, pull_request, target_vcs, target_reference):
1332 1335 workspace_id = self._workspace_id(pull_request)
1333 1336 source_vcs = pull_request.source_repo.scm_instance()
1334 1337 repo_id = pull_request.target_repo.repo_id
1335 1338 use_rebase = self._use_rebase_for_merging(pull_request)
1336 1339 close_branch = self._close_branch_before_merging(pull_request)
1337 1340 merge_state = target_vcs.merge(
1338 1341 repo_id, workspace_id,
1339 1342 target_reference, source_vcs, pull_request.source_ref_parts,
1340 1343 dry_run=True, use_rebase=use_rebase,
1341 1344 close_branch=close_branch)
1342 1345
1343 1346 # Do not store the response if there was an unknown error.
1344 1347 if merge_state.failure_reason != MergeFailureReason.UNKNOWN:
1345 1348 pull_request._last_merge_source_rev = \
1346 1349 pull_request.source_ref_parts.commit_id
1347 1350 pull_request._last_merge_target_rev = target_reference.commit_id
1348 1351 pull_request.last_merge_status = merge_state.failure_reason
1349 1352 pull_request.shadow_merge_ref = merge_state.merge_ref
1350 1353 Session().add(pull_request)
1351 1354 Session().commit()
1352 1355
1353 1356 return merge_state
1354 1357
1355 1358 def _workspace_id(self, pull_request):
1356 1359 workspace_id = 'pr-%s' % pull_request.pull_request_id
1357 1360 return workspace_id
1358 1361
1359 1362 def merge_status_message(self, status_code):
1360 1363 """
1361 1364 Return a human friendly error message for the given merge status code.
1362 1365 """
1363 1366 return self.MERGE_STATUS_MESSAGES[status_code]
1364 1367
1365 1368 def generate_repo_data(self, repo, commit_id=None, branch=None,
1366 1369 bookmark=None, translator=None):
1367 1370 from rhodecode.model.repo import RepoModel
1368 1371
1369 1372 all_refs, selected_ref = \
1370 1373 self._get_repo_pullrequest_sources(
1371 1374 repo.scm_instance(), commit_id=commit_id,
1372 1375 branch=branch, bookmark=bookmark, translator=translator)
1373 1376
1374 1377 refs_select2 = []
1375 1378 for element in all_refs:
1376 1379 children = [{'id': x[0], 'text': x[1]} for x in element[0]]
1377 1380 refs_select2.append({'text': element[1], 'children': children})
1378 1381
1379 1382 return {
1380 1383 'user': {
1381 1384 'user_id': repo.user.user_id,
1382 1385 'username': repo.user.username,
1383 1386 'firstname': repo.user.first_name,
1384 1387 'lastname': repo.user.last_name,
1385 1388 'gravatar_link': h.gravatar_url(repo.user.email, 14),
1386 1389 },
1387 1390 'name': repo.repo_name,
1388 1391 'link': RepoModel().get_url(repo),
1389 1392 'description': h.chop_at_smart(repo.description_safe, '\n'),
1390 1393 'refs': {
1391 1394 'all_refs': all_refs,
1392 1395 'selected_ref': selected_ref,
1393 1396 'select2_refs': refs_select2
1394 1397 }
1395 1398 }
1396 1399
1397 1400 def generate_pullrequest_title(self, source, source_ref, target):
1398 1401 return u'{source}#{at_ref} to {target}'.format(
1399 1402 source=source,
1400 1403 at_ref=source_ref,
1401 1404 target=target,
1402 1405 )
1403 1406
1404 1407 def _cleanup_merge_workspace(self, pull_request):
1405 1408 # Merging related cleanup
1406 1409 repo_id = pull_request.target_repo.repo_id
1407 1410 target_scm = pull_request.target_repo.scm_instance()
1408 1411 workspace_id = self._workspace_id(pull_request)
1409 1412
1410 1413 try:
1411 1414 target_scm.cleanup_merge_workspace(repo_id, workspace_id)
1412 1415 except NotImplementedError:
1413 1416 pass
1414 1417
1415 1418 def _get_repo_pullrequest_sources(
1416 1419 self, repo, commit_id=None, branch=None, bookmark=None,
1417 1420 translator=None):
1418 1421 """
1419 1422 Return a structure with repo's interesting commits, suitable for
1420 1423 the selectors in pullrequest controller
1421 1424
1422 1425 :param commit_id: a commit that must be in the list somehow
1423 1426 and selected by default
1424 1427 :param branch: a branch that must be in the list and selected
1425 1428 by default - even if closed
1426 1429 :param bookmark: a bookmark that must be in the list and selected
1427 1430 """
1428 1431 _ = translator or get_current_request().translate
1429 1432
1430 1433 commit_id = safe_str(commit_id) if commit_id else None
1431 1434 branch = safe_str(branch) if branch else None
1432 1435 bookmark = safe_str(bookmark) if bookmark else None
1433 1436
1434 1437 selected = None
1435 1438
1436 1439 # order matters: first source that has commit_id in it will be selected
1437 1440 sources = []
1438 1441 sources.append(('book', repo.bookmarks.items(), _('Bookmarks'), bookmark))
1439 1442 sources.append(('branch', repo.branches.items(), _('Branches'), branch))
1440 1443
1441 1444 if commit_id:
1442 1445 ref_commit = (h.short_id(commit_id), commit_id)
1443 1446 sources.append(('rev', [ref_commit], _('Commit IDs'), commit_id))
1444 1447
1445 1448 sources.append(
1446 1449 ('branch', repo.branches_closed.items(), _('Closed Branches'), branch),
1447 1450 )
1448 1451
1449 1452 groups = []
1450 1453 for group_key, ref_list, group_name, match in sources:
1451 1454 group_refs = []
1452 1455 for ref_name, ref_id in ref_list:
1453 1456 ref_key = '%s:%s:%s' % (group_key, ref_name, ref_id)
1454 1457 group_refs.append((ref_key, ref_name))
1455 1458
1456 1459 if not selected:
1457 1460 if set([commit_id, match]) & set([ref_id, ref_name]):
1458 1461 selected = ref_key
1459 1462
1460 1463 if group_refs:
1461 1464 groups.append((group_refs, group_name))
1462 1465
1463 1466 if not selected:
1464 1467 ref = commit_id or branch or bookmark
1465 1468 if ref:
1466 1469 raise CommitDoesNotExistError(
1467 1470 'No commit refs could be found matching: %s' % ref)
1468 1471 elif repo.DEFAULT_BRANCH_NAME in repo.branches:
1469 1472 selected = 'branch:%s:%s' % (
1470 1473 repo.DEFAULT_BRANCH_NAME,
1471 1474 repo.branches[repo.DEFAULT_BRANCH_NAME]
1472 1475 )
1473 1476 elif repo.commit_ids:
1474 1477 # make the user select in this case
1475 1478 selected = None
1476 1479 else:
1477 1480 raise EmptyRepositoryError()
1478 1481 return groups, selected
1479 1482
1480 1483 def get_diff(self, source_repo, source_ref_id, target_ref_id, context=DIFF_CONTEXT):
1481 1484 return self._get_diff_from_pr_or_version(
1482 1485 source_repo, source_ref_id, target_ref_id, context=context)
1483 1486
1484 1487 def _get_diff_from_pr_or_version(
1485 1488 self, source_repo, source_ref_id, target_ref_id, context):
1486 1489 target_commit = source_repo.get_commit(
1487 1490 commit_id=safe_str(target_ref_id))
1488 1491 source_commit = source_repo.get_commit(
1489 1492 commit_id=safe_str(source_ref_id))
1490 1493 if isinstance(source_repo, Repository):
1491 1494 vcs_repo = source_repo.scm_instance()
1492 1495 else:
1493 1496 vcs_repo = source_repo
1494 1497
1495 1498 # TODO: johbo: In the context of an update, we cannot reach
1496 1499 # the old commit anymore with our normal mechanisms. It needs
1497 1500 # some sort of special support in the vcs layer to avoid this
1498 1501 # workaround.
1499 1502 if (source_commit.raw_id == vcs_repo.EMPTY_COMMIT_ID and
1500 1503 vcs_repo.alias == 'git'):
1501 1504 source_commit.raw_id = safe_str(source_ref_id)
1502 1505
1503 1506 log.debug('calculating diff between '
1504 1507 'source_ref:%s and target_ref:%s for repo `%s`',
1505 1508 target_ref_id, source_ref_id,
1506 1509 safe_unicode(vcs_repo.path))
1507 1510
1508 1511 vcs_diff = vcs_repo.get_diff(
1509 1512 commit1=target_commit, commit2=source_commit, context=context)
1510 1513 return vcs_diff
1511 1514
1512 1515 def _is_merge_enabled(self, pull_request):
1513 1516 return self._get_general_setting(
1514 1517 pull_request, 'rhodecode_pr_merge_enabled')
1515 1518
1516 1519 def _use_rebase_for_merging(self, pull_request):
1517 1520 repo_type = pull_request.target_repo.repo_type
1518 1521 if repo_type == 'hg':
1519 1522 return self._get_general_setting(
1520 1523 pull_request, 'rhodecode_hg_use_rebase_for_merging')
1521 1524 elif repo_type == 'git':
1522 1525 return self._get_general_setting(
1523 1526 pull_request, 'rhodecode_git_use_rebase_for_merging')
1524 1527
1525 1528 return False
1526 1529
1527 1530 def _close_branch_before_merging(self, pull_request):
1528 1531 repo_type = pull_request.target_repo.repo_type
1529 1532 if repo_type == 'hg':
1530 1533 return self._get_general_setting(
1531 1534 pull_request, 'rhodecode_hg_close_branch_before_merging')
1532 1535 elif repo_type == 'git':
1533 1536 return self._get_general_setting(
1534 1537 pull_request, 'rhodecode_git_close_branch_before_merging')
1535 1538
1536 1539 return False
1537 1540
1538 1541 def _get_general_setting(self, pull_request, settings_key, default=False):
1539 1542 settings_model = VcsSettingsModel(repo=pull_request.target_repo)
1540 1543 settings = settings_model.get_general_settings()
1541 1544 return settings.get(settings_key, default)
1542 1545
1543 1546 def _log_audit_action(self, action, action_data, user, pull_request):
1544 1547 audit_logger.store(
1545 1548 action=action,
1546 1549 action_data=action_data,
1547 1550 user=user,
1548 1551 repo=pull_request.target_repo)
1549 1552
1550 1553 def get_reviewer_functions(self):
1551 1554 """
1552 1555 Fetches functions for validation and fetching default reviewers.
1553 1556 If available we use the EE package, else we fallback to CE
1554 1557 package functions
1555 1558 """
1556 1559 try:
1557 1560 from rc_reviewers.utils import get_default_reviewers_data
1558 1561 from rc_reviewers.utils import validate_default_reviewers
1559 1562 except ImportError:
1560 1563 from rhodecode.apps.repository.utils import \
1561 1564 get_default_reviewers_data
1562 1565 from rhodecode.apps.repository.utils import \
1563 1566 validate_default_reviewers
1564 1567
1565 1568 return get_default_reviewers_data, validate_default_reviewers
1566 1569
1567 1570
1568 1571 class MergeCheck(object):
1569 1572 """
1570 1573 Perform Merge Checks and returns a check object which stores information
1571 1574 about merge errors, and merge conditions
1572 1575 """
1573 1576 TODO_CHECK = 'todo'
1574 1577 PERM_CHECK = 'perm'
1575 1578 REVIEW_CHECK = 'review'
1576 1579 MERGE_CHECK = 'merge'
1577 1580
1578 1581 def __init__(self):
1579 1582 self.review_status = None
1580 1583 self.merge_possible = None
1581 1584 self.merge_msg = ''
1582 1585 self.failed = None
1583 1586 self.errors = []
1584 1587 self.error_details = OrderedDict()
1585 1588
1586 1589 def push_error(self, error_type, message, error_key, details):
1587 1590 self.failed = True
1588 1591 self.errors.append([error_type, message])
1589 1592 self.error_details[error_key] = dict(
1590 1593 details=details,
1591 1594 error_type=error_type,
1592 1595 message=message
1593 1596 )
1594 1597
1595 1598 @classmethod
1596 1599 def validate(cls, pull_request, user, translator, fail_early=False,
1597 1600 force_shadow_repo_refresh=False):
1598 1601 _ = translator
1599 1602 merge_check = cls()
1600 1603
1601 1604 # permissions to merge
1602 1605 user_allowed_to_merge = PullRequestModel().check_user_merge(
1603 1606 pull_request, user)
1604 1607 if not user_allowed_to_merge:
1605 1608 log.debug("MergeCheck: cannot merge, approval is pending.")
1606 1609
1607 1610 msg = _('User `{}` not allowed to perform merge.').format(user.username)
1608 1611 merge_check.push_error('error', msg, cls.PERM_CHECK, user.username)
1609 1612 if fail_early:
1610 1613 return merge_check
1611 1614
1612 1615 # review status, must be always present
1613 1616 review_status = pull_request.calculated_review_status()
1614 1617 merge_check.review_status = review_status
1615 1618
1616 1619 status_approved = review_status == ChangesetStatus.STATUS_APPROVED
1617 1620 if not status_approved:
1618 1621 log.debug("MergeCheck: cannot merge, approval is pending.")
1619 1622
1620 1623 msg = _('Pull request reviewer approval is pending.')
1621 1624
1622 1625 merge_check.push_error(
1623 1626 'warning', msg, cls.REVIEW_CHECK, review_status)
1624 1627
1625 1628 if fail_early:
1626 1629 return merge_check
1627 1630
1628 1631 # left over TODOs
1629 1632 todos = CommentsModel().get_unresolved_todos(pull_request)
1630 1633 if todos:
1631 1634 log.debug("MergeCheck: cannot merge, {} "
1632 1635 "unresolved todos left.".format(len(todos)))
1633 1636
1634 1637 if len(todos) == 1:
1635 1638 msg = _('Cannot merge, {} TODO still not resolved.').format(
1636 1639 len(todos))
1637 1640 else:
1638 1641 msg = _('Cannot merge, {} TODOs still not resolved.').format(
1639 1642 len(todos))
1640 1643
1641 1644 merge_check.push_error('warning', msg, cls.TODO_CHECK, todos)
1642 1645
1643 1646 if fail_early:
1644 1647 return merge_check
1645 1648
1646 1649 # merge possible, here is the filesystem simulation + shadow repo
1647 1650 merge_status, msg = PullRequestModel().merge_status(
1648 1651 pull_request, translator=translator,
1649 1652 force_shadow_repo_refresh=force_shadow_repo_refresh)
1650 1653 merge_check.merge_possible = merge_status
1651 1654 merge_check.merge_msg = msg
1652 1655 if not merge_status:
1653 1656 log.debug(
1654 1657 "MergeCheck: cannot merge, pull request merge not possible.")
1655 1658 merge_check.push_error('warning', msg, cls.MERGE_CHECK, None)
1656 1659
1657 1660 if fail_early:
1658 1661 return merge_check
1659 1662
1660 1663 log.debug('MergeCheck: is failed: %s', merge_check.failed)
1661 1664 return merge_check
1662 1665
1663 1666 @classmethod
1664 1667 def get_merge_conditions(cls, pull_request, translator):
1665 1668 _ = translator
1666 1669 merge_details = {}
1667 1670
1668 1671 model = PullRequestModel()
1669 1672 use_rebase = model._use_rebase_for_merging(pull_request)
1670 1673
1671 1674 if use_rebase:
1672 1675 merge_details['merge_strategy'] = dict(
1673 1676 details={},
1674 1677 message=_('Merge strategy: rebase')
1675 1678 )
1676 1679 else:
1677 1680 merge_details['merge_strategy'] = dict(
1678 1681 details={},
1679 1682 message=_('Merge strategy: explicit merge commit')
1680 1683 )
1681 1684
1682 1685 close_branch = model._close_branch_before_merging(pull_request)
1683 1686 if close_branch:
1684 1687 repo_type = pull_request.target_repo.repo_type
1685 1688 if repo_type == 'hg':
1686 1689 close_msg = _('Source branch will be closed after merge.')
1687 1690 elif repo_type == 'git':
1688 1691 close_msg = _('Source branch will be deleted after merge.')
1689 1692
1690 1693 merge_details['close_branch'] = dict(
1691 1694 details={},
1692 1695 message=close_msg
1693 1696 )
1694 1697
1695 1698 return merge_details
1696 1699
1697 1700 ChangeTuple = collections.namedtuple(
1698 1701 'ChangeTuple', ['added', 'common', 'removed', 'total'])
1699 1702
1700 1703 FileChangeTuple = collections.namedtuple(
1701 1704 'FileChangeTuple', ['added', 'modified', 'removed'])
@@ -1,550 +1,551 b''
1 1 // # Copyright (C) 2010-2018 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 var prButtonLockChecks = {
21 21 'compare': false,
22 22 'reviewers': false
23 23 };
24 24
25 25 /**
26 26 * lock button until all checks and loads are made. E.g reviewer calculation
27 27 * should prevent from submitting a PR
28 28 * @param lockEnabled
29 29 * @param msg
30 30 * @param scope
31 31 */
32 32 var prButtonLock = function(lockEnabled, msg, scope) {
33 33 scope = scope || 'all';
34 34 if (scope == 'all'){
35 35 prButtonLockChecks['compare'] = !lockEnabled;
36 36 prButtonLockChecks['reviewers'] = !lockEnabled;
37 37 } else if (scope == 'compare') {
38 38 prButtonLockChecks['compare'] = !lockEnabled;
39 39 } else if (scope == 'reviewers'){
40 40 prButtonLockChecks['reviewers'] = !lockEnabled;
41 41 }
42 42 var checksMeet = prButtonLockChecks.compare && prButtonLockChecks.reviewers;
43 43 if (lockEnabled) {
44 44 $('#pr_submit').attr('disabled', 'disabled');
45 45 }
46 46 else if (checksMeet) {
47 47 $('#pr_submit').removeAttr('disabled');
48 48 }
49 49
50 50 if (msg) {
51 51 $('#pr_open_message').html(msg);
52 52 }
53 53 };
54 54
55 55
56 56 /**
57 57 Generate Title and Description for a PullRequest.
58 58 In case of 1 commits, the title and description is that one commit
59 59 in case of multiple commits, we iterate on them with max N number of commits,
60 60 and build description in a form
61 61 - commitN
62 62 - commitN+1
63 63 ...
64 64
65 65 Title is then constructed from branch names, or other references,
66 66 replacing '-' and '_' into spaces
67 67
68 68 * @param sourceRef
69 69 * @param elements
70 70 * @param limit
71 71 * @returns {*[]}
72 72 */
73 73 var getTitleAndDescription = function(sourceRef, elements, limit) {
74 74 var title = '';
75 75 var desc = '';
76 76
77 77 $.each($(elements).get().reverse().slice(0, limit), function(idx, value) {
78 78 var rawMessage = $(value).find('td.td-description .message').data('messageRaw');
79 79 desc += '- ' + rawMessage.split('\n')[0].replace(/\n+$/, "") + '\n';
80 80 });
81 81 // only 1 commit, use commit message as title
82 82 if (elements.length === 1) {
83 83 title = $(elements[0]).find('td.td-description .message').data('messageRaw').split('\n')[0];
84 84 }
85 85 else {
86 86 // use reference name
87 87 title = sourceRef.replace(/-/g, ' ').replace(/_/g, ' ').capitalizeFirstLetter();
88 88 }
89 89
90 90 return [title, desc]
91 91 };
92 92
93 93
94 94
95 95 ReviewersController = function () {
96 96 var self = this;
97 97 this.$reviewRulesContainer = $('#review_rules');
98 98 this.$rulesList = this.$reviewRulesContainer.find('.pr-reviewer-rules');
99 99 this.forbidReviewUsers = undefined;
100 100 this.$reviewMembers = $('#review_members');
101 101 this.currentRequest = null;
102 102
103 103 this.defaultForbidReviewUsers = function() {
104 104 return [
105 105 {'username': 'default',
106 106 'user_id': templateContext.default_user.user_id}
107 107 ];
108 108 };
109 109
110 110 this.hideReviewRules = function() {
111 111 self.$reviewRulesContainer.hide();
112 112 };
113 113
114 114 this.showReviewRules = function() {
115 115 self.$reviewRulesContainer.show();
116 116 };
117 117
118 118 this.addRule = function(ruleText) {
119 119 self.showReviewRules();
120 120 return '<div>- {0}</div>'.format(ruleText)
121 121 };
122 122
123 123 this.loadReviewRules = function(data) {
124 124 // reset forbidden Users
125 125 this.forbidReviewUsers = self.defaultForbidReviewUsers();
126 126
127 127 // reset state of review rules
128 128 self.$rulesList.html('');
129 129
130 130 if (!data || data.rules === undefined || $.isEmptyObject(data.rules)) {
131 131 // default rule, case for older repo that don't have any rules stored
132 132 self.$rulesList.append(
133 133 self.addRule(
134 134 _gettext('All reviewers must vote.'))
135 135 );
136 136 return self.forbidReviewUsers
137 137 }
138 138
139 139 if (data.rules.voting !== undefined) {
140 140 if (data.rules.voting < 0) {
141 141 self.$rulesList.append(
142 142 self.addRule(
143 143 _gettext('All individual reviewers must vote.'))
144 144 )
145 145 } else if (data.rules.voting === 1) {
146 146 self.$rulesList.append(
147 147 self.addRule(
148 148 _gettext('At least {0} reviewer must vote.').format(data.rules.voting))
149 149 )
150 150
151 151 } else {
152 152 self.$rulesList.append(
153 153 self.addRule(
154 154 _gettext('At least {0} reviewers must vote.').format(data.rules.voting))
155 155 )
156 156 }
157 157 }
158 158
159 159 if (data.rules.voting_groups !== undefined) {
160 160 $.each(data.rules.voting_groups, function(index, rule_data) {
161 161 self.$rulesList.append(
162 162 self.addRule(rule_data.text)
163 163 )
164 164 });
165 165 }
166 166
167 167 if (data.rules.use_code_authors_for_review) {
168 168 self.$rulesList.append(
169 169 self.addRule(
170 170 _gettext('Reviewers picked from source code changes.'))
171 171 )
172 172 }
173 173 if (data.rules.forbid_adding_reviewers) {
174 174 $('#add_reviewer_input').remove();
175 175 self.$rulesList.append(
176 176 self.addRule(
177 177 _gettext('Adding new reviewers is forbidden.'))
178 178 )
179 179 }
180 180 if (data.rules.forbid_author_to_review) {
181 181 self.forbidReviewUsers.push(data.rules_data.pr_author);
182 182 self.$rulesList.append(
183 183 self.addRule(
184 184 _gettext('Author is not allowed to be a reviewer.'))
185 185 )
186 186 }
187 187 if (data.rules.forbid_commit_author_to_review) {
188 188
189 189 if (data.rules_data.forbidden_users) {
190 190 $.each(data.rules_data.forbidden_users, function(index, member_data) {
191 191 self.forbidReviewUsers.push(member_data)
192 192 });
193 193
194 194 }
195 195
196 196 self.$rulesList.append(
197 197 self.addRule(
198 198 _gettext('Commit Authors are not allowed to be a reviewer.'))
199 199 )
200 200 }
201 201
202 202 return self.forbidReviewUsers
203 203 };
204 204
205 205 this.loadDefaultReviewers = function(sourceRepo, sourceRef, targetRepo, targetRef) {
206 206
207 207 if (self.currentRequest) {
208 208 // make sure we cleanup old running requests before triggering this
209 209 // again
210 210 self.currentRequest.abort();
211 211 }
212 212
213 213 $('.calculate-reviewers').show();
214 214 // reset reviewer members
215 215 self.$reviewMembers.empty();
216 216
217 217 prButtonLock(true, null, 'reviewers');
218 218 $('#user').hide(); // hide user autocomplete before load
219 219
220 220 if (sourceRef.length !== 3 || targetRef.length !== 3) {
221 221 // don't load defaults in case we're missing some refs...
222 222 $('.calculate-reviewers').hide();
223 223 return
224 224 }
225 225
226 226 var url = pyroutes.url('repo_default_reviewers_data',
227 227 {
228 228 'repo_name': templateContext.repo_name,
229 229 'source_repo': sourceRepo,
230 230 'source_ref': sourceRef[2],
231 231 'target_repo': targetRepo,
232 232 'target_ref': targetRef[2]
233 233 });
234 234
235 235 self.currentRequest = $.get(url)
236 236 .done(function(data) {
237 237 self.currentRequest = null;
238 238
239 239 // review rules
240 240 self.loadReviewRules(data);
241 241
242 242 for (var i = 0; i < data.reviewers.length; i++) {
243 243 var reviewer = data.reviewers[i];
244 244 self.addReviewMember(
245 245 reviewer, reviewer.reasons, reviewer.mandatory);
246 246 }
247 247 $('.calculate-reviewers').hide();
248 248 prButtonLock(false, null, 'reviewers');
249 249 $('#user').show(); // show user autocomplete after load
250 250 });
251 251 };
252 252
253 253 // check those, refactor
254 254 this.removeReviewMember = function(reviewer_id, mark_delete) {
255 255 var reviewer = $('#reviewer_{0}'.format(reviewer_id));
256 256
257 257 if(typeof(mark_delete) === undefined){
258 258 mark_delete = false;
259 259 }
260 260
261 261 if(mark_delete === true){
262 262 if (reviewer){
263 263 // now delete the input
264 264 $('#reviewer_{0} input'.format(reviewer_id)).remove();
265 265 // mark as to-delete
266 266 var obj = $('#reviewer_{0}_name'.format(reviewer_id));
267 267 obj.addClass('to-delete');
268 268 obj.css({"text-decoration":"line-through", "opacity": 0.5});
269 269 }
270 270 }
271 271 else{
272 272 $('#reviewer_{0}'.format(reviewer_id)).remove();
273 273 }
274 274 };
275 275 this.reviewMemberEntry = function() {
276 276
277 277 };
278 278 this.addReviewMember = function(reviewer_obj, reasons, mandatory) {
279 279 var members = self.$reviewMembers.get(0);
280 280 var id = reviewer_obj.user_id;
281 281 var username = reviewer_obj.username;
282 282
283 283 var reasons = reasons || [];
284 284 var mandatory = mandatory || false;
285 285
286 286 // register IDS to check if we don't have this ID already in
287 287 var currentIds = [];
288 288 var _els = self.$reviewMembers.find('li').toArray();
289 289 for (el in _els){
290 290 currentIds.push(_els[el].id)
291 291 }
292 292
293 293 var userAllowedReview = function(userId) {
294 294 var allowed = true;
295 295 $.each(self.forbidReviewUsers, function(index, member_data) {
296 296 if (parseInt(userId) === member_data['user_id']) {
297 297 allowed = false;
298 298 return false // breaks the loop
299 299 }
300 300 });
301 301 return allowed
302 302 };
303 303
304 304 var userAllowed = userAllowedReview(id);
305 305 if (!userAllowed){
306 306 alert(_gettext('User `{0}` not allowed to be a reviewer').format(username));
307 307 } else {
308 308 // only add if it's not there
309 309 var alreadyReviewer = currentIds.indexOf('reviewer_'+id) != -1;
310 310
311 311 if (alreadyReviewer) {
312 312 alert(_gettext('User `{0}` already in reviewers').format(username));
313 313 } else {
314 314 members.innerHTML += renderTemplate('reviewMemberEntry', {
315 315 'member': reviewer_obj,
316 316 'mandatory': mandatory,
317 317 'allowed_to_update': true,
318 318 'review_status': 'not_reviewed',
319 319 'review_status_label': _gettext('Not Reviewed'),
320 320 'reasons': reasons,
321 321 'create': true
322 322 });
323 323 }
324 324 }
325 325
326 326 };
327 327
328 328 this.updateReviewers = function(repo_name, pull_request_id){
329 329 var postData = $('#reviewers input').serialize();
330 330 _updatePullRequest(repo_name, pull_request_id, postData);
331 331 };
332 332
333 333 };
334 334
335 335
336 336 var _updatePullRequest = function(repo_name, pull_request_id, postData) {
337 337 var url = pyroutes.url(
338 338 'pullrequest_update',
339 339 {"repo_name": repo_name, "pull_request_id": pull_request_id});
340 340 if (typeof postData === 'string' ) {
341 341 postData += '&csrf_token=' + CSRF_TOKEN;
342 342 } else {
343 343 postData.csrf_token = CSRF_TOKEN;
344 344 }
345 345 var success = function(o) {
346 346 window.location.reload();
347 347 };
348 348 ajaxPOST(url, postData, success);
349 349 };
350 350
351 351 /**
352 352 * PULL REQUEST update commits
353 353 */
354 354 var updateCommits = function(repo_name, pull_request_id) {
355 355 var postData = {
356 356 'update_commits': true};
357 357 _updatePullRequest(repo_name, pull_request_id, postData);
358 358 };
359 359
360 360
361 361 /**
362 362 * PULL REQUEST edit info
363 363 */
364 var editPullRequest = function(repo_name, pull_request_id, title, description) {
364 var editPullRequest = function(repo_name, pull_request_id, title, description, renderer) {
365 365 var url = pyroutes.url(
366 366 'pullrequest_update',
367 367 {"repo_name": repo_name, "pull_request_id": pull_request_id});
368 368
369 369 var postData = {
370 370 'title': title,
371 371 'description': description,
372 'description_renderer': renderer,
372 373 'edit_pull_request': true,
373 374 'csrf_token': CSRF_TOKEN
374 375 };
375 376 var success = function(o) {
376 377 window.location.reload();
377 378 };
378 379 ajaxPOST(url, postData, success);
379 380 };
380 381
381 382
382 383 /**
383 384 * Reviewer autocomplete
384 385 */
385 386 var ReviewerAutoComplete = function(inputId) {
386 387 $(inputId).autocomplete({
387 388 serviceUrl: pyroutes.url('user_autocomplete_data'),
388 389 minChars:2,
389 390 maxHeight:400,
390 391 deferRequestBy: 300, //miliseconds
391 392 showNoSuggestionNotice: true,
392 393 tabDisabled: true,
393 394 autoSelectFirst: true,
394 395 params: { user_id: templateContext.rhodecode_user.user_id, user_groups:true, user_groups_expand:true, skip_default_user:true },
395 396 formatResult: autocompleteFormatResult,
396 397 lookupFilter: autocompleteFilterResult,
397 398 onSelect: function(element, data) {
398 399 var mandatory = false;
399 400 var reasons = [_gettext('added manually by "{0}"').format(templateContext.rhodecode_user.username)];
400 401
401 402 // add whole user groups
402 403 if (data.value_type == 'user_group') {
403 404 reasons.push(_gettext('member of "{0}"').format(data.value_display));
404 405
405 406 $.each(data.members, function(index, member_data) {
406 407 var reviewer = member_data;
407 408 reviewer['user_id'] = member_data['id'];
408 409 reviewer['gravatar_link'] = member_data['icon_link'];
409 410 reviewer['user_link'] = member_data['profile_link'];
410 411 reviewer['rules'] = [];
411 412 reviewersController.addReviewMember(reviewer, reasons, mandatory);
412 413 })
413 414 }
414 415 // add single user
415 416 else {
416 417 var reviewer = data;
417 418 reviewer['user_id'] = data['id'];
418 419 reviewer['gravatar_link'] = data['icon_link'];
419 420 reviewer['user_link'] = data['profile_link'];
420 421 reviewer['rules'] = [];
421 422 reviewersController.addReviewMember(reviewer, reasons, mandatory);
422 423 }
423 424
424 425 $(inputId).val('');
425 426 }
426 427 });
427 428 };
428 429
429 430
430 431 VersionController = function () {
431 432 var self = this;
432 433 this.$verSource = $('input[name=ver_source]');
433 434 this.$verTarget = $('input[name=ver_target]');
434 435 this.$showVersionDiff = $('#show-version-diff');
435 436
436 437 this.adjustRadioSelectors = function (curNode) {
437 438 var getVal = function (item) {
438 439 if (item == 'latest') {
439 440 return Number.MAX_SAFE_INTEGER
440 441 }
441 442 else {
442 443 return parseInt(item)
443 444 }
444 445 };
445 446
446 447 var curVal = getVal($(curNode).val());
447 448 var cleared = false;
448 449
449 450 $.each(self.$verSource, function (index, value) {
450 451 var elVal = getVal($(value).val());
451 452
452 453 if (elVal > curVal) {
453 454 if ($(value).is(':checked')) {
454 455 cleared = true;
455 456 }
456 457 $(value).attr('disabled', 'disabled');
457 458 $(value).removeAttr('checked');
458 459 $(value).css({'opacity': 0.1});
459 460 }
460 461 else {
461 462 $(value).css({'opacity': 1});
462 463 $(value).removeAttr('disabled');
463 464 }
464 465 });
465 466
466 467 if (cleared) {
467 468 // if we unchecked an active, set the next one to same loc.
468 469 $(this.$verSource).filter('[value={0}]'.format(
469 470 curVal)).attr('checked', 'checked');
470 471 }
471 472
472 473 self.setLockAction(false,
473 474 $(curNode).data('verPos'),
474 475 $(this.$verSource).filter(':checked').data('verPos')
475 476 );
476 477 };
477 478
478 479
479 480 this.attachVersionListener = function () {
480 481 self.$verTarget.change(function (e) {
481 482 self.adjustRadioSelectors(this)
482 483 });
483 484 self.$verSource.change(function (e) {
484 485 self.adjustRadioSelectors(self.$verTarget.filter(':checked'))
485 486 });
486 487 };
487 488
488 489 this.init = function () {
489 490
490 491 var curNode = self.$verTarget.filter(':checked');
491 492 self.adjustRadioSelectors(curNode);
492 493 self.setLockAction(true);
493 494 self.attachVersionListener();
494 495
495 496 };
496 497
497 498 this.setLockAction = function (state, selectedVersion, otherVersion) {
498 499 var $showVersionDiff = this.$showVersionDiff;
499 500
500 501 if (state) {
501 502 $showVersionDiff.attr('disabled', 'disabled');
502 503 $showVersionDiff.addClass('disabled');
503 504 $showVersionDiff.html($showVersionDiff.data('labelTextLocked'));
504 505 }
505 506 else {
506 507 $showVersionDiff.removeAttr('disabled');
507 508 $showVersionDiff.removeClass('disabled');
508 509
509 510 if (selectedVersion == otherVersion) {
510 511 $showVersionDiff.html($showVersionDiff.data('labelTextShow'));
511 512 } else {
512 513 $showVersionDiff.html($showVersionDiff.data('labelTextDiff'));
513 514 }
514 515 }
515 516
516 517 };
517 518
518 519 this.showVersionDiff = function () {
519 520 var target = self.$verTarget.filter(':checked');
520 521 var source = self.$verSource.filter(':checked');
521 522
522 523 if (target.val() && source.val()) {
523 524 var params = {
524 525 'pull_request_id': templateContext.pull_request_data.pull_request_id,
525 526 'repo_name': templateContext.repo_name,
526 527 'version': target.val(),
527 528 'from_version': source.val()
528 529 };
529 530 window.location = pyroutes.url('pullrequest_show', params)
530 531 }
531 532
532 533 return false;
533 534 };
534 535
535 536 this.toggleVersionView = function (elem) {
536 537
537 538 if (this.$showVersionDiff.is(':visible')) {
538 539 $('.version-pr').hide();
539 540 this.$showVersionDiff.hide();
540 541 $(elem).html($(elem).data('toggleOn'))
541 542 } else {
542 543 $('.version-pr').show();
543 544 this.$showVersionDiff.show();
544 545 $(elem).html($(elem).data('toggleOff'))
545 546 }
546 547
547 548 return false
548 549 }
549 550
550 551 }; No newline at end of file
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now