##// END OF EJS Templates
default-reviewers: fixed voting rule calculation on user-group. The previous...
marcink -
r2960:545905e2 default
parent child Browse files
Show More

The requested changes are 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
@@ -1,1704 +1,1707 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 447 description_renderer=None,
448 448 reviewer_data=None, translator=None, auth_user=None):
449 449 translator = translator or get_current_request().translate
450 450
451 451 created_by_user = self._get_user(created_by)
452 452 auth_user = auth_user or created_by_user
453 453 source_repo = self._get_repo(source_repo)
454 454 target_repo = self._get_repo(target_repo)
455 455
456 456 pull_request = PullRequest()
457 457 pull_request.source_repo = source_repo
458 458 pull_request.source_ref = source_ref
459 459 pull_request.target_repo = target_repo
460 460 pull_request.target_ref = target_ref
461 461 pull_request.revisions = revisions
462 462 pull_request.title = title
463 463 pull_request.description = description
464 464 pull_request.description_renderer = description_renderer
465 465 pull_request.author = created_by_user
466 466 pull_request.reviewer_data = reviewer_data
467 467
468 468 Session().add(pull_request)
469 469 Session().flush()
470 470
471 471 reviewer_ids = set()
472 472 # members / reviewers
473 473 for reviewer_object in reviewers:
474 474 user_id, reasons, mandatory, rules = reviewer_object
475 475 user = self._get_user(user_id)
476 476
477 477 # skip duplicates
478 478 if user.user_id in reviewer_ids:
479 479 continue
480 480
481 481 reviewer_ids.add(user.user_id)
482 482
483 483 reviewer = PullRequestReviewers()
484 484 reviewer.user = user
485 485 reviewer.pull_request = pull_request
486 486 reviewer.reasons = reasons
487 487 reviewer.mandatory = mandatory
488 488
489 489 # NOTE(marcink): pick only first rule for now
490 490 rule_id = list(rules)[0] if rules else None
491 491 rule = RepoReviewRule.get(rule_id) if rule_id else None
492 492 if rule:
493 review_group = rule.user_group_vote_rule()
493 review_group = rule.user_group_vote_rule(user_id)
494 # we check if this particular reviewer is member of a voting group
494 495 if review_group:
495 496 # NOTE(marcink):
496 # again, can be that user is member of more,
497 # but we pick the first same, as default reviewers algo
497 # can be that user is member of more but we pick the first same,
498 # same as default reviewers algo
498 499 review_group = review_group[0]
499 500
500 501 rule_data = {
501 502 'rule_name':
502 503 rule.review_rule_name,
503 504 'rule_user_group_entry_id':
504 505 review_group.repo_review_rule_users_group_id,
505 506 'rule_user_group_name':
506 507 review_group.users_group.users_group_name,
507 508 'rule_user_group_members':
508 509 [x.user.username for x in review_group.users_group.members],
510 'rule_user_group_members_id':
511 [x.user.user_id for x in review_group.users_group.members],
509 512 }
510 513 # e.g {'vote_rule': -1, 'mandatory': True}
511 514 rule_data.update(review_group.rule_data())
512 515
513 516 reviewer.rule_data = rule_data
514 517
515 518 Session().add(reviewer)
516 519 Session().flush()
517 520
518 521 # Set approval status to "Under Review" for all commits which are
519 522 # part of this pull request.
520 523 ChangesetStatusModel().set_status(
521 524 repo=target_repo,
522 525 status=ChangesetStatus.STATUS_UNDER_REVIEW,
523 526 user=created_by_user,
524 527 pull_request=pull_request
525 528 )
526 529 # we commit early at this point. This has to do with a fact
527 530 # that before queries do some row-locking. And because of that
528 531 # we need to commit and finish transation before below validate call
529 532 # that for large repos could be long resulting in long row locks
530 533 Session().commit()
531 534
532 535 # prepare workspace, and run initial merge simulation
533 536 MergeCheck.validate(
534 537 pull_request, user=created_by_user, translator=translator)
535 538
536 539 self.notify_reviewers(pull_request, reviewer_ids)
537 540 self._trigger_pull_request_hook(
538 541 pull_request, created_by_user, 'create')
539 542
540 543 creation_data = pull_request.get_api_data(with_merge_state=False)
541 544 self._log_audit_action(
542 545 'repo.pull_request.create', {'data': creation_data},
543 546 auth_user, pull_request)
544 547
545 548 return pull_request
546 549
547 550 def _trigger_pull_request_hook(self, pull_request, user, action):
548 551 pull_request = self.__get_pull_request(pull_request)
549 552 target_scm = pull_request.target_repo.scm_instance()
550 553 if action == 'create':
551 554 trigger_hook = hooks_utils.trigger_log_create_pull_request_hook
552 555 elif action == 'merge':
553 556 trigger_hook = hooks_utils.trigger_log_merge_pull_request_hook
554 557 elif action == 'close':
555 558 trigger_hook = hooks_utils.trigger_log_close_pull_request_hook
556 559 elif action == 'review_status_change':
557 560 trigger_hook = hooks_utils.trigger_log_review_pull_request_hook
558 561 elif action == 'update':
559 562 trigger_hook = hooks_utils.trigger_log_update_pull_request_hook
560 563 else:
561 564 return
562 565
563 566 trigger_hook(
564 567 username=user.username,
565 568 repo_name=pull_request.target_repo.repo_name,
566 569 repo_alias=target_scm.alias,
567 570 pull_request=pull_request)
568 571
569 572 def _get_commit_ids(self, pull_request):
570 573 """
571 574 Return the commit ids of the merged pull request.
572 575
573 576 This method is not dealing correctly yet with the lack of autoupdates
574 577 nor with the implicit target updates.
575 578 For example: if a commit in the source repo is already in the target it
576 579 will be reported anyways.
577 580 """
578 581 merge_rev = pull_request.merge_rev
579 582 if merge_rev is None:
580 583 raise ValueError('This pull request was not merged yet')
581 584
582 585 commit_ids = list(pull_request.revisions)
583 586 if merge_rev not in commit_ids:
584 587 commit_ids.append(merge_rev)
585 588
586 589 return commit_ids
587 590
588 591 def merge_repo(self, pull_request, user, extras):
589 592 log.debug("Merging pull request %s", pull_request.pull_request_id)
590 593 merge_state = self._merge_pull_request(pull_request, user, extras)
591 594 if merge_state.executed:
592 595 log.debug(
593 596 "Merge was successful, updating the pull request comments.")
594 597 self._comment_and_close_pr(pull_request, user, merge_state)
595 598
596 599 self._log_audit_action(
597 600 'repo.pull_request.merge',
598 601 {'merge_state': merge_state.__dict__},
599 602 user, pull_request)
600 603
601 604 else:
602 605 log.warn("Merge failed, not updating the pull request.")
603 606 return merge_state
604 607
605 608 def _merge_pull_request(self, pull_request, user, extras, merge_msg=None):
606 609 target_vcs = pull_request.target_repo.scm_instance()
607 610 source_vcs = pull_request.source_repo.scm_instance()
608 611 target_ref = self._refresh_reference(
609 612 pull_request.target_ref_parts, target_vcs)
610 613
611 614 message = merge_msg or (
612 615 'Merge pull request #%(pr_id)s from '
613 616 '%(source_repo)s %(source_ref_name)s\n\n %(pr_title)s') % {
614 617 'pr_id': pull_request.pull_request_id,
615 618 'source_repo': source_vcs.name,
616 619 'source_ref_name': pull_request.source_ref_parts.name,
617 620 'pr_title': pull_request.title
618 621 }
619 622
620 623 workspace_id = self._workspace_id(pull_request)
621 624 repo_id = pull_request.target_repo.repo_id
622 625 use_rebase = self._use_rebase_for_merging(pull_request)
623 626 close_branch = self._close_branch_before_merging(pull_request)
624 627
625 628 callback_daemon, extras = prepare_callback_daemon(
626 629 extras, protocol=vcs_settings.HOOKS_PROTOCOL,
627 630 host=vcs_settings.HOOKS_HOST,
628 631 use_direct_calls=vcs_settings.HOOKS_DIRECT_CALLS)
629 632
630 633 with callback_daemon:
631 634 # TODO: johbo: Implement a clean way to run a config_override
632 635 # for a single call.
633 636 target_vcs.config.set(
634 637 'rhodecode', 'RC_SCM_DATA', json.dumps(extras))
635 638 merge_state = target_vcs.merge(
636 639 repo_id, workspace_id, target_ref, source_vcs,
637 640 pull_request.source_ref_parts,
638 641 user_name=user.username, user_email=user.email,
639 642 message=message, use_rebase=use_rebase,
640 643 close_branch=close_branch)
641 644 return merge_state
642 645
643 646 def _comment_and_close_pr(self, pull_request, user, merge_state, close_msg=None):
644 647 pull_request.merge_rev = merge_state.merge_ref.commit_id
645 648 pull_request.updated_on = datetime.datetime.now()
646 649 close_msg = close_msg or 'Pull request merged and closed'
647 650
648 651 CommentsModel().create(
649 652 text=safe_unicode(close_msg),
650 653 repo=pull_request.target_repo.repo_id,
651 654 user=user.user_id,
652 655 pull_request=pull_request.pull_request_id,
653 656 f_path=None,
654 657 line_no=None,
655 658 closing_pr=True
656 659 )
657 660
658 661 Session().add(pull_request)
659 662 Session().flush()
660 663 # TODO: paris: replace invalidation with less radical solution
661 664 ScmModel().mark_for_invalidation(
662 665 pull_request.target_repo.repo_name)
663 666 self._trigger_pull_request_hook(pull_request, user, 'merge')
664 667
665 668 def has_valid_update_type(self, pull_request):
666 669 source_ref_type = pull_request.source_ref_parts.type
667 670 return source_ref_type in ['book', 'branch', 'tag']
668 671
669 672 def update_commits(self, pull_request):
670 673 """
671 674 Get the updated list of commits for the pull request
672 675 and return the new pull request version and the list
673 676 of commits processed by this update action
674 677 """
675 678 pull_request = self.__get_pull_request(pull_request)
676 679 source_ref_type = pull_request.source_ref_parts.type
677 680 source_ref_name = pull_request.source_ref_parts.name
678 681 source_ref_id = pull_request.source_ref_parts.commit_id
679 682
680 683 target_ref_type = pull_request.target_ref_parts.type
681 684 target_ref_name = pull_request.target_ref_parts.name
682 685 target_ref_id = pull_request.target_ref_parts.commit_id
683 686
684 687 if not self.has_valid_update_type(pull_request):
685 688 log.debug(
686 689 "Skipping update of pull request %s due to ref type: %s",
687 690 pull_request, source_ref_type)
688 691 return UpdateResponse(
689 692 executed=False,
690 693 reason=UpdateFailureReason.WRONG_REF_TYPE,
691 694 old=pull_request, new=None, changes=None,
692 695 source_changed=False, target_changed=False)
693 696
694 697 # source repo
695 698 source_repo = pull_request.source_repo.scm_instance()
696 699 try:
697 700 source_commit = source_repo.get_commit(commit_id=source_ref_name)
698 701 except CommitDoesNotExistError:
699 702 return UpdateResponse(
700 703 executed=False,
701 704 reason=UpdateFailureReason.MISSING_SOURCE_REF,
702 705 old=pull_request, new=None, changes=None,
703 706 source_changed=False, target_changed=False)
704 707
705 708 source_changed = source_ref_id != source_commit.raw_id
706 709
707 710 # target repo
708 711 target_repo = pull_request.target_repo.scm_instance()
709 712 try:
710 713 target_commit = target_repo.get_commit(commit_id=target_ref_name)
711 714 except CommitDoesNotExistError:
712 715 return UpdateResponse(
713 716 executed=False,
714 717 reason=UpdateFailureReason.MISSING_TARGET_REF,
715 718 old=pull_request, new=None, changes=None,
716 719 source_changed=False, target_changed=False)
717 720 target_changed = target_ref_id != target_commit.raw_id
718 721
719 722 if not (source_changed or target_changed):
720 723 log.debug("Nothing changed in pull request %s", pull_request)
721 724 return UpdateResponse(
722 725 executed=False,
723 726 reason=UpdateFailureReason.NO_CHANGE,
724 727 old=pull_request, new=None, changes=None,
725 728 source_changed=target_changed, target_changed=source_changed)
726 729
727 730 change_in_found = 'target repo' if target_changed else 'source repo'
728 731 log.debug('Updating pull request because of change in %s detected',
729 732 change_in_found)
730 733
731 734 # Finally there is a need for an update, in case of source change
732 735 # we create a new version, else just an update
733 736 if source_changed:
734 737 pull_request_version = self._create_version_from_snapshot(pull_request)
735 738 self._link_comments_to_version(pull_request_version)
736 739 else:
737 740 try:
738 741 ver = pull_request.versions[-1]
739 742 except IndexError:
740 743 ver = None
741 744
742 745 pull_request.pull_request_version_id = \
743 746 ver.pull_request_version_id if ver else None
744 747 pull_request_version = pull_request
745 748
746 749 try:
747 750 if target_ref_type in ('tag', 'branch', 'book'):
748 751 target_commit = target_repo.get_commit(target_ref_name)
749 752 else:
750 753 target_commit = target_repo.get_commit(target_ref_id)
751 754 except CommitDoesNotExistError:
752 755 return UpdateResponse(
753 756 executed=False,
754 757 reason=UpdateFailureReason.MISSING_TARGET_REF,
755 758 old=pull_request, new=None, changes=None,
756 759 source_changed=source_changed, target_changed=target_changed)
757 760
758 761 # re-compute commit ids
759 762 old_commit_ids = pull_request.revisions
760 763 pre_load = ["author", "branch", "date", "message"]
761 764 commit_ranges = target_repo.compare(
762 765 target_commit.raw_id, source_commit.raw_id, source_repo, merge=True,
763 766 pre_load=pre_load)
764 767
765 768 ancestor = target_repo.get_common_ancestor(
766 769 target_commit.raw_id, source_commit.raw_id, source_repo)
767 770
768 771 pull_request.source_ref = '%s:%s:%s' % (
769 772 source_ref_type, source_ref_name, source_commit.raw_id)
770 773 pull_request.target_ref = '%s:%s:%s' % (
771 774 target_ref_type, target_ref_name, ancestor)
772 775
773 776 pull_request.revisions = [
774 777 commit.raw_id for commit in reversed(commit_ranges)]
775 778 pull_request.updated_on = datetime.datetime.now()
776 779 Session().add(pull_request)
777 780 new_commit_ids = pull_request.revisions
778 781
779 782 old_diff_data, new_diff_data = self._generate_update_diffs(
780 783 pull_request, pull_request_version)
781 784
782 785 # calculate commit and file changes
783 786 changes = self._calculate_commit_id_changes(
784 787 old_commit_ids, new_commit_ids)
785 788 file_changes = self._calculate_file_changes(
786 789 old_diff_data, new_diff_data)
787 790
788 791 # set comments as outdated if DIFFS changed
789 792 CommentsModel().outdate_comments(
790 793 pull_request, old_diff_data=old_diff_data,
791 794 new_diff_data=new_diff_data)
792 795
793 796 commit_changes = (changes.added or changes.removed)
794 797 file_node_changes = (
795 798 file_changes.added or file_changes.modified or file_changes.removed)
796 799 pr_has_changes = commit_changes or file_node_changes
797 800
798 801 # Add an automatic comment to the pull request, in case
799 802 # anything has changed
800 803 if pr_has_changes:
801 804 update_comment = CommentsModel().create(
802 805 text=self._render_update_message(changes, file_changes),
803 806 repo=pull_request.target_repo,
804 807 user=pull_request.author,
805 808 pull_request=pull_request,
806 809 send_email=False, renderer=DEFAULT_COMMENTS_RENDERER)
807 810
808 811 # Update status to "Under Review" for added commits
809 812 for commit_id in changes.added:
810 813 ChangesetStatusModel().set_status(
811 814 repo=pull_request.source_repo,
812 815 status=ChangesetStatus.STATUS_UNDER_REVIEW,
813 816 comment=update_comment,
814 817 user=pull_request.author,
815 818 pull_request=pull_request,
816 819 revision=commit_id)
817 820
818 821 log.debug(
819 822 'Updated pull request %s, added_ids: %s, common_ids: %s, '
820 823 'removed_ids: %s', pull_request.pull_request_id,
821 824 changes.added, changes.common, changes.removed)
822 825 log.debug(
823 826 'Updated pull request with the following file changes: %s',
824 827 file_changes)
825 828
826 829 log.info(
827 830 "Updated pull request %s from commit %s to commit %s, "
828 831 "stored new version %s of this pull request.",
829 832 pull_request.pull_request_id, source_ref_id,
830 833 pull_request.source_ref_parts.commit_id,
831 834 pull_request_version.pull_request_version_id)
832 835 Session().commit()
833 836 self._trigger_pull_request_hook(
834 837 pull_request, pull_request.author, 'update')
835 838
836 839 return UpdateResponse(
837 840 executed=True, reason=UpdateFailureReason.NONE,
838 841 old=pull_request, new=pull_request_version, changes=changes,
839 842 source_changed=source_changed, target_changed=target_changed)
840 843
841 844 def _create_version_from_snapshot(self, pull_request):
842 845 version = PullRequestVersion()
843 846 version.title = pull_request.title
844 847 version.description = pull_request.description
845 848 version.status = pull_request.status
846 849 version.created_on = datetime.datetime.now()
847 850 version.updated_on = pull_request.updated_on
848 851 version.user_id = pull_request.user_id
849 852 version.source_repo = pull_request.source_repo
850 853 version.source_ref = pull_request.source_ref
851 854 version.target_repo = pull_request.target_repo
852 855 version.target_ref = pull_request.target_ref
853 856
854 857 version._last_merge_source_rev = pull_request._last_merge_source_rev
855 858 version._last_merge_target_rev = pull_request._last_merge_target_rev
856 859 version.last_merge_status = pull_request.last_merge_status
857 860 version.shadow_merge_ref = pull_request.shadow_merge_ref
858 861 version.merge_rev = pull_request.merge_rev
859 862 version.reviewer_data = pull_request.reviewer_data
860 863
861 864 version.revisions = pull_request.revisions
862 865 version.pull_request = pull_request
863 866 Session().add(version)
864 867 Session().flush()
865 868
866 869 return version
867 870
868 871 def _generate_update_diffs(self, pull_request, pull_request_version):
869 872
870 873 diff_context = (
871 874 self.DIFF_CONTEXT +
872 875 CommentsModel.needed_extra_diff_context())
873 876
874 877 source_repo = pull_request_version.source_repo
875 878 source_ref_id = pull_request_version.source_ref_parts.commit_id
876 879 target_ref_id = pull_request_version.target_ref_parts.commit_id
877 880 old_diff = self._get_diff_from_pr_or_version(
878 881 source_repo, source_ref_id, target_ref_id, context=diff_context)
879 882
880 883 source_repo = pull_request.source_repo
881 884 source_ref_id = pull_request.source_ref_parts.commit_id
882 885 target_ref_id = pull_request.target_ref_parts.commit_id
883 886
884 887 new_diff = self._get_diff_from_pr_or_version(
885 888 source_repo, source_ref_id, target_ref_id, context=diff_context)
886 889
887 890 old_diff_data = diffs.DiffProcessor(old_diff)
888 891 old_diff_data.prepare()
889 892 new_diff_data = diffs.DiffProcessor(new_diff)
890 893 new_diff_data.prepare()
891 894
892 895 return old_diff_data, new_diff_data
893 896
894 897 def _link_comments_to_version(self, pull_request_version):
895 898 """
896 899 Link all unlinked comments of this pull request to the given version.
897 900
898 901 :param pull_request_version: The `PullRequestVersion` to which
899 902 the comments shall be linked.
900 903
901 904 """
902 905 pull_request = pull_request_version.pull_request
903 906 comments = ChangesetComment.query()\
904 907 .filter(
905 908 # TODO: johbo: Should we query for the repo at all here?
906 909 # Pending decision on how comments of PRs are to be related
907 910 # to either the source repo, the target repo or no repo at all.
908 911 ChangesetComment.repo_id == pull_request.target_repo.repo_id,
909 912 ChangesetComment.pull_request == pull_request,
910 913 ChangesetComment.pull_request_version == None)\
911 914 .order_by(ChangesetComment.comment_id.asc())
912 915
913 916 # TODO: johbo: Find out why this breaks if it is done in a bulk
914 917 # operation.
915 918 for comment in comments:
916 919 comment.pull_request_version_id = (
917 920 pull_request_version.pull_request_version_id)
918 921 Session().add(comment)
919 922
920 923 def _calculate_commit_id_changes(self, old_ids, new_ids):
921 924 added = [x for x in new_ids if x not in old_ids]
922 925 common = [x for x in new_ids if x in old_ids]
923 926 removed = [x for x in old_ids if x not in new_ids]
924 927 total = new_ids
925 928 return ChangeTuple(added, common, removed, total)
926 929
927 930 def _calculate_file_changes(self, old_diff_data, new_diff_data):
928 931
929 932 old_files = OrderedDict()
930 933 for diff_data in old_diff_data.parsed_diff:
931 934 old_files[diff_data['filename']] = md5_safe(diff_data['raw_diff'])
932 935
933 936 added_files = []
934 937 modified_files = []
935 938 removed_files = []
936 939 for diff_data in new_diff_data.parsed_diff:
937 940 new_filename = diff_data['filename']
938 941 new_hash = md5_safe(diff_data['raw_diff'])
939 942
940 943 old_hash = old_files.get(new_filename)
941 944 if not old_hash:
942 945 # file is not present in old diff, means it's added
943 946 added_files.append(new_filename)
944 947 else:
945 948 if new_hash != old_hash:
946 949 modified_files.append(new_filename)
947 950 # now remove a file from old, since we have seen it already
948 951 del old_files[new_filename]
949 952
950 953 # removed files is when there are present in old, but not in NEW,
951 954 # since we remove old files that are present in new diff, left-overs
952 955 # if any should be the removed files
953 956 removed_files.extend(old_files.keys())
954 957
955 958 return FileChangeTuple(added_files, modified_files, removed_files)
956 959
957 960 def _render_update_message(self, changes, file_changes):
958 961 """
959 962 render the message using DEFAULT_COMMENTS_RENDERER (RST renderer),
960 963 so it's always looking the same disregarding on which default
961 964 renderer system is using.
962 965
963 966 :param changes: changes named tuple
964 967 :param file_changes: file changes named tuple
965 968
966 969 """
967 970 new_status = ChangesetStatus.get_status_lbl(
968 971 ChangesetStatus.STATUS_UNDER_REVIEW)
969 972
970 973 changed_files = (
971 974 file_changes.added + file_changes.modified + file_changes.removed)
972 975
973 976 params = {
974 977 'under_review_label': new_status,
975 978 'added_commits': changes.added,
976 979 'removed_commits': changes.removed,
977 980 'changed_files': changed_files,
978 981 'added_files': file_changes.added,
979 982 'modified_files': file_changes.modified,
980 983 'removed_files': file_changes.removed,
981 984 }
982 985 renderer = RstTemplateRenderer()
983 986 return renderer.render('pull_request_update.mako', **params)
984 987
985 988 def edit(self, pull_request, title, description, description_renderer, user):
986 989 pull_request = self.__get_pull_request(pull_request)
987 990 old_data = pull_request.get_api_data(with_merge_state=False)
988 991 if pull_request.is_closed():
989 992 raise ValueError('This pull request is closed')
990 993 if title:
991 994 pull_request.title = title
992 995 pull_request.description = description
993 996 pull_request.updated_on = datetime.datetime.now()
994 997 pull_request.description_renderer = description_renderer
995 998 Session().add(pull_request)
996 999 self._log_audit_action(
997 1000 'repo.pull_request.edit', {'old_data': old_data},
998 1001 user, pull_request)
999 1002
1000 1003 def update_reviewers(self, pull_request, reviewer_data, user):
1001 1004 """
1002 1005 Update the reviewers in the pull request
1003 1006
1004 1007 :param pull_request: the pr to update
1005 1008 :param reviewer_data: list of tuples
1006 1009 [(user, ['reason1', 'reason2'], mandatory_flag, [rules])]
1007 1010 """
1008 1011 pull_request = self.__get_pull_request(pull_request)
1009 1012 if pull_request.is_closed():
1010 1013 raise ValueError('This pull request is closed')
1011 1014
1012 1015 reviewers = {}
1013 1016 for user_id, reasons, mandatory, rules in reviewer_data:
1014 1017 if isinstance(user_id, (int, basestring)):
1015 1018 user_id = self._get_user(user_id).user_id
1016 1019 reviewers[user_id] = {
1017 1020 'reasons': reasons, 'mandatory': mandatory}
1018 1021
1019 1022 reviewers_ids = set(reviewers.keys())
1020 1023 current_reviewers = PullRequestReviewers.query()\
1021 1024 .filter(PullRequestReviewers.pull_request ==
1022 1025 pull_request).all()
1023 1026 current_reviewers_ids = set([x.user.user_id for x in current_reviewers])
1024 1027
1025 1028 ids_to_add = reviewers_ids.difference(current_reviewers_ids)
1026 1029 ids_to_remove = current_reviewers_ids.difference(reviewers_ids)
1027 1030
1028 1031 log.debug("Adding %s reviewers", ids_to_add)
1029 1032 log.debug("Removing %s reviewers", ids_to_remove)
1030 1033 changed = False
1031 1034 for uid in ids_to_add:
1032 1035 changed = True
1033 1036 _usr = self._get_user(uid)
1034 1037 reviewer = PullRequestReviewers()
1035 1038 reviewer.user = _usr
1036 1039 reviewer.pull_request = pull_request
1037 1040 reviewer.reasons = reviewers[uid]['reasons']
1038 1041 # NOTE(marcink): mandatory shouldn't be changed now
1039 1042 # reviewer.mandatory = reviewers[uid]['reasons']
1040 1043 Session().add(reviewer)
1041 1044 self._log_audit_action(
1042 1045 'repo.pull_request.reviewer.add', {'data': reviewer.get_dict()},
1043 1046 user, pull_request)
1044 1047
1045 1048 for uid in ids_to_remove:
1046 1049 changed = True
1047 1050 reviewers = PullRequestReviewers.query()\
1048 1051 .filter(PullRequestReviewers.user_id == uid,
1049 1052 PullRequestReviewers.pull_request == pull_request)\
1050 1053 .all()
1051 1054 # use .all() in case we accidentally added the same person twice
1052 1055 # this CAN happen due to the lack of DB checks
1053 1056 for obj in reviewers:
1054 1057 old_data = obj.get_dict()
1055 1058 Session().delete(obj)
1056 1059 self._log_audit_action(
1057 1060 'repo.pull_request.reviewer.delete',
1058 1061 {'old_data': old_data}, user, pull_request)
1059 1062
1060 1063 if changed:
1061 1064 pull_request.updated_on = datetime.datetime.now()
1062 1065 Session().add(pull_request)
1063 1066
1064 1067 self.notify_reviewers(pull_request, ids_to_add)
1065 1068 return ids_to_add, ids_to_remove
1066 1069
1067 1070 def get_url(self, pull_request, request=None, permalink=False):
1068 1071 if not request:
1069 1072 request = get_current_request()
1070 1073
1071 1074 if permalink:
1072 1075 return request.route_url(
1073 1076 'pull_requests_global',
1074 1077 pull_request_id=pull_request.pull_request_id,)
1075 1078 else:
1076 1079 return request.route_url('pullrequest_show',
1077 1080 repo_name=safe_str(pull_request.target_repo.repo_name),
1078 1081 pull_request_id=pull_request.pull_request_id,)
1079 1082
1080 1083 def get_shadow_clone_url(self, pull_request, request=None):
1081 1084 """
1082 1085 Returns qualified url pointing to the shadow repository. If this pull
1083 1086 request is closed there is no shadow repository and ``None`` will be
1084 1087 returned.
1085 1088 """
1086 1089 if pull_request.is_closed():
1087 1090 return None
1088 1091 else:
1089 1092 pr_url = urllib.unquote(self.get_url(pull_request, request=request))
1090 1093 return safe_unicode('{pr_url}/repository'.format(pr_url=pr_url))
1091 1094
1092 1095 def notify_reviewers(self, pull_request, reviewers_ids):
1093 1096 # notification to reviewers
1094 1097 if not reviewers_ids:
1095 1098 return
1096 1099
1097 1100 pull_request_obj = pull_request
1098 1101 # get the current participants of this pull request
1099 1102 recipients = reviewers_ids
1100 1103 notification_type = EmailNotificationModel.TYPE_PULL_REQUEST
1101 1104
1102 1105 pr_source_repo = pull_request_obj.source_repo
1103 1106 pr_target_repo = pull_request_obj.target_repo
1104 1107
1105 1108 pr_url = h.route_url('pullrequest_show',
1106 1109 repo_name=pr_target_repo.repo_name,
1107 1110 pull_request_id=pull_request_obj.pull_request_id,)
1108 1111
1109 1112 # set some variables for email notification
1110 1113 pr_target_repo_url = h.route_url(
1111 1114 'repo_summary', repo_name=pr_target_repo.repo_name)
1112 1115
1113 1116 pr_source_repo_url = h.route_url(
1114 1117 'repo_summary', repo_name=pr_source_repo.repo_name)
1115 1118
1116 1119 # pull request specifics
1117 1120 pull_request_commits = [
1118 1121 (x.raw_id, x.message)
1119 1122 for x in map(pr_source_repo.get_commit, pull_request.revisions)]
1120 1123
1121 1124 kwargs = {
1122 1125 'user': pull_request.author,
1123 1126 'pull_request': pull_request_obj,
1124 1127 'pull_request_commits': pull_request_commits,
1125 1128
1126 1129 'pull_request_target_repo': pr_target_repo,
1127 1130 'pull_request_target_repo_url': pr_target_repo_url,
1128 1131
1129 1132 'pull_request_source_repo': pr_source_repo,
1130 1133 'pull_request_source_repo_url': pr_source_repo_url,
1131 1134
1132 1135 'pull_request_url': pr_url,
1133 1136 }
1134 1137
1135 1138 # pre-generate the subject for notification itself
1136 1139 (subject,
1137 1140 _h, _e, # we don't care about those
1138 1141 body_plaintext) = EmailNotificationModel().render_email(
1139 1142 notification_type, **kwargs)
1140 1143
1141 1144 # create notification objects, and emails
1142 1145 NotificationModel().create(
1143 1146 created_by=pull_request.author,
1144 1147 notification_subject=subject,
1145 1148 notification_body=body_plaintext,
1146 1149 notification_type=notification_type,
1147 1150 recipients=recipients,
1148 1151 email_kwargs=kwargs,
1149 1152 )
1150 1153
1151 1154 def delete(self, pull_request, user):
1152 1155 pull_request = self.__get_pull_request(pull_request)
1153 1156 old_data = pull_request.get_api_data(with_merge_state=False)
1154 1157 self._cleanup_merge_workspace(pull_request)
1155 1158 self._log_audit_action(
1156 1159 'repo.pull_request.delete', {'old_data': old_data},
1157 1160 user, pull_request)
1158 1161 Session().delete(pull_request)
1159 1162
1160 1163 def close_pull_request(self, pull_request, user):
1161 1164 pull_request = self.__get_pull_request(pull_request)
1162 1165 self._cleanup_merge_workspace(pull_request)
1163 1166 pull_request.status = PullRequest.STATUS_CLOSED
1164 1167 pull_request.updated_on = datetime.datetime.now()
1165 1168 Session().add(pull_request)
1166 1169 self._trigger_pull_request_hook(
1167 1170 pull_request, pull_request.author, 'close')
1168 1171
1169 1172 pr_data = pull_request.get_api_data(with_merge_state=False)
1170 1173 self._log_audit_action(
1171 1174 'repo.pull_request.close', {'data': pr_data}, user, pull_request)
1172 1175
1173 1176 def close_pull_request_with_comment(
1174 1177 self, pull_request, user, repo, message=None):
1175 1178
1176 1179 pull_request_review_status = pull_request.calculated_review_status()
1177 1180
1178 1181 if pull_request_review_status == ChangesetStatus.STATUS_APPROVED:
1179 1182 # approved only if we have voting consent
1180 1183 status = ChangesetStatus.STATUS_APPROVED
1181 1184 else:
1182 1185 status = ChangesetStatus.STATUS_REJECTED
1183 1186 status_lbl = ChangesetStatus.get_status_lbl(status)
1184 1187
1185 1188 default_message = (
1186 1189 'Closing with status change {transition_icon} {status}.'
1187 1190 ).format(transition_icon='>', status=status_lbl)
1188 1191 text = message or default_message
1189 1192
1190 1193 # create a comment, and link it to new status
1191 1194 comment = CommentsModel().create(
1192 1195 text=text,
1193 1196 repo=repo.repo_id,
1194 1197 user=user.user_id,
1195 1198 pull_request=pull_request.pull_request_id,
1196 1199 status_change=status_lbl,
1197 1200 status_change_type=status,
1198 1201 closing_pr=True
1199 1202 )
1200 1203
1201 1204 # calculate old status before we change it
1202 1205 old_calculated_status = pull_request.calculated_review_status()
1203 1206 ChangesetStatusModel().set_status(
1204 1207 repo.repo_id,
1205 1208 status,
1206 1209 user.user_id,
1207 1210 comment=comment,
1208 1211 pull_request=pull_request.pull_request_id
1209 1212 )
1210 1213
1211 1214 Session().flush()
1212 1215 events.trigger(events.PullRequestCommentEvent(pull_request, comment))
1213 1216 # we now calculate the status of pull request again, and based on that
1214 1217 # calculation trigger status change. This might happen in cases
1215 1218 # that non-reviewer admin closes a pr, which means his vote doesn't
1216 1219 # change the status, while if he's a reviewer this might change it.
1217 1220 calculated_status = pull_request.calculated_review_status()
1218 1221 if old_calculated_status != calculated_status:
1219 1222 self._trigger_pull_request_hook(
1220 1223 pull_request, user, 'review_status_change')
1221 1224
1222 1225 # finally close the PR
1223 1226 PullRequestModel().close_pull_request(
1224 1227 pull_request.pull_request_id, user)
1225 1228
1226 1229 return comment, status
1227 1230
1228 1231 def merge_status(self, pull_request, translator=None,
1229 1232 force_shadow_repo_refresh=False):
1230 1233 _ = translator or get_current_request().translate
1231 1234
1232 1235 if not self._is_merge_enabled(pull_request):
1233 1236 return False, _('Server-side pull request merging is disabled.')
1234 1237 if pull_request.is_closed():
1235 1238 return False, _('This pull request is closed.')
1236 1239 merge_possible, msg = self._check_repo_requirements(
1237 1240 target=pull_request.target_repo, source=pull_request.source_repo,
1238 1241 translator=_)
1239 1242 if not merge_possible:
1240 1243 return merge_possible, msg
1241 1244
1242 1245 try:
1243 1246 resp = self._try_merge(
1244 1247 pull_request,
1245 1248 force_shadow_repo_refresh=force_shadow_repo_refresh)
1246 1249 log.debug("Merge response: %s", resp)
1247 1250 status = resp.possible, self.merge_status_message(
1248 1251 resp.failure_reason)
1249 1252 except NotImplementedError:
1250 1253 status = False, _('Pull request merging is not supported.')
1251 1254
1252 1255 return status
1253 1256
1254 1257 def _check_repo_requirements(self, target, source, translator):
1255 1258 """
1256 1259 Check if `target` and `source` have compatible requirements.
1257 1260
1258 1261 Currently this is just checking for largefiles.
1259 1262 """
1260 1263 _ = translator
1261 1264 target_has_largefiles = self._has_largefiles(target)
1262 1265 source_has_largefiles = self._has_largefiles(source)
1263 1266 merge_possible = True
1264 1267 message = u''
1265 1268
1266 1269 if target_has_largefiles != source_has_largefiles:
1267 1270 merge_possible = False
1268 1271 if source_has_largefiles:
1269 1272 message = _(
1270 1273 'Target repository large files support is disabled.')
1271 1274 else:
1272 1275 message = _(
1273 1276 'Source repository large files support is disabled.')
1274 1277
1275 1278 return merge_possible, message
1276 1279
1277 1280 def _has_largefiles(self, repo):
1278 1281 largefiles_ui = VcsSettingsModel(repo=repo).get_ui_settings(
1279 1282 'extensions', 'largefiles')
1280 1283 return largefiles_ui and largefiles_ui[0].active
1281 1284
1282 1285 def _try_merge(self, pull_request, force_shadow_repo_refresh=False):
1283 1286 """
1284 1287 Try to merge the pull request and return the merge status.
1285 1288 """
1286 1289 log.debug(
1287 1290 "Trying out if the pull request %s can be merged. Force_refresh=%s",
1288 1291 pull_request.pull_request_id, force_shadow_repo_refresh)
1289 1292 target_vcs = pull_request.target_repo.scm_instance()
1290 1293
1291 1294 # Refresh the target reference.
1292 1295 try:
1293 1296 target_ref = self._refresh_reference(
1294 1297 pull_request.target_ref_parts, target_vcs)
1295 1298 except CommitDoesNotExistError:
1296 1299 merge_state = MergeResponse(
1297 1300 False, False, None, MergeFailureReason.MISSING_TARGET_REF)
1298 1301 return merge_state
1299 1302
1300 1303 target_locked = pull_request.target_repo.locked
1301 1304 if target_locked and target_locked[0]:
1302 1305 log.debug("The target repository is locked.")
1303 1306 merge_state = MergeResponse(
1304 1307 False, False, None, MergeFailureReason.TARGET_IS_LOCKED)
1305 1308 elif force_shadow_repo_refresh or self._needs_merge_state_refresh(
1306 1309 pull_request, target_ref):
1307 1310 log.debug("Refreshing the merge status of the repository.")
1308 1311 merge_state = self._refresh_merge_state(
1309 1312 pull_request, target_vcs, target_ref)
1310 1313 else:
1311 1314 possible = pull_request.\
1312 1315 last_merge_status == MergeFailureReason.NONE
1313 1316 merge_state = MergeResponse(
1314 1317 possible, False, None, pull_request.last_merge_status)
1315 1318
1316 1319 return merge_state
1317 1320
1318 1321 def _refresh_reference(self, reference, vcs_repository):
1319 1322 if reference.type in ('branch', 'book'):
1320 1323 name_or_id = reference.name
1321 1324 else:
1322 1325 name_or_id = reference.commit_id
1323 1326 refreshed_commit = vcs_repository.get_commit(name_or_id)
1324 1327 refreshed_reference = Reference(
1325 1328 reference.type, reference.name, refreshed_commit.raw_id)
1326 1329 return refreshed_reference
1327 1330
1328 1331 def _needs_merge_state_refresh(self, pull_request, target_reference):
1329 1332 return not(
1330 1333 pull_request.revisions and
1331 1334 pull_request.revisions[0] == pull_request._last_merge_source_rev and
1332 1335 target_reference.commit_id == pull_request._last_merge_target_rev)
1333 1336
1334 1337 def _refresh_merge_state(self, pull_request, target_vcs, target_reference):
1335 1338 workspace_id = self._workspace_id(pull_request)
1336 1339 source_vcs = pull_request.source_repo.scm_instance()
1337 1340 repo_id = pull_request.target_repo.repo_id
1338 1341 use_rebase = self._use_rebase_for_merging(pull_request)
1339 1342 close_branch = self._close_branch_before_merging(pull_request)
1340 1343 merge_state = target_vcs.merge(
1341 1344 repo_id, workspace_id,
1342 1345 target_reference, source_vcs, pull_request.source_ref_parts,
1343 1346 dry_run=True, use_rebase=use_rebase,
1344 1347 close_branch=close_branch)
1345 1348
1346 1349 # Do not store the response if there was an unknown error.
1347 1350 if merge_state.failure_reason != MergeFailureReason.UNKNOWN:
1348 1351 pull_request._last_merge_source_rev = \
1349 1352 pull_request.source_ref_parts.commit_id
1350 1353 pull_request._last_merge_target_rev = target_reference.commit_id
1351 1354 pull_request.last_merge_status = merge_state.failure_reason
1352 1355 pull_request.shadow_merge_ref = merge_state.merge_ref
1353 1356 Session().add(pull_request)
1354 1357 Session().commit()
1355 1358
1356 1359 return merge_state
1357 1360
1358 1361 def _workspace_id(self, pull_request):
1359 1362 workspace_id = 'pr-%s' % pull_request.pull_request_id
1360 1363 return workspace_id
1361 1364
1362 1365 def merge_status_message(self, status_code):
1363 1366 """
1364 1367 Return a human friendly error message for the given merge status code.
1365 1368 """
1366 1369 return self.MERGE_STATUS_MESSAGES[status_code]
1367 1370
1368 1371 def generate_repo_data(self, repo, commit_id=None, branch=None,
1369 1372 bookmark=None, translator=None):
1370 1373 from rhodecode.model.repo import RepoModel
1371 1374
1372 1375 all_refs, selected_ref = \
1373 1376 self._get_repo_pullrequest_sources(
1374 1377 repo.scm_instance(), commit_id=commit_id,
1375 1378 branch=branch, bookmark=bookmark, translator=translator)
1376 1379
1377 1380 refs_select2 = []
1378 1381 for element in all_refs:
1379 1382 children = [{'id': x[0], 'text': x[1]} for x in element[0]]
1380 1383 refs_select2.append({'text': element[1], 'children': children})
1381 1384
1382 1385 return {
1383 1386 'user': {
1384 1387 'user_id': repo.user.user_id,
1385 1388 'username': repo.user.username,
1386 1389 'firstname': repo.user.first_name,
1387 1390 'lastname': repo.user.last_name,
1388 1391 'gravatar_link': h.gravatar_url(repo.user.email, 14),
1389 1392 },
1390 1393 'name': repo.repo_name,
1391 1394 'link': RepoModel().get_url(repo),
1392 1395 'description': h.chop_at_smart(repo.description_safe, '\n'),
1393 1396 'refs': {
1394 1397 'all_refs': all_refs,
1395 1398 'selected_ref': selected_ref,
1396 1399 'select2_refs': refs_select2
1397 1400 }
1398 1401 }
1399 1402
1400 1403 def generate_pullrequest_title(self, source, source_ref, target):
1401 1404 return u'{source}#{at_ref} to {target}'.format(
1402 1405 source=source,
1403 1406 at_ref=source_ref,
1404 1407 target=target,
1405 1408 )
1406 1409
1407 1410 def _cleanup_merge_workspace(self, pull_request):
1408 1411 # Merging related cleanup
1409 1412 repo_id = pull_request.target_repo.repo_id
1410 1413 target_scm = pull_request.target_repo.scm_instance()
1411 1414 workspace_id = self._workspace_id(pull_request)
1412 1415
1413 1416 try:
1414 1417 target_scm.cleanup_merge_workspace(repo_id, workspace_id)
1415 1418 except NotImplementedError:
1416 1419 pass
1417 1420
1418 1421 def _get_repo_pullrequest_sources(
1419 1422 self, repo, commit_id=None, branch=None, bookmark=None,
1420 1423 translator=None):
1421 1424 """
1422 1425 Return a structure with repo's interesting commits, suitable for
1423 1426 the selectors in pullrequest controller
1424 1427
1425 1428 :param commit_id: a commit that must be in the list somehow
1426 1429 and selected by default
1427 1430 :param branch: a branch that must be in the list and selected
1428 1431 by default - even if closed
1429 1432 :param bookmark: a bookmark that must be in the list and selected
1430 1433 """
1431 1434 _ = translator or get_current_request().translate
1432 1435
1433 1436 commit_id = safe_str(commit_id) if commit_id else None
1434 1437 branch = safe_str(branch) if branch else None
1435 1438 bookmark = safe_str(bookmark) if bookmark else None
1436 1439
1437 1440 selected = None
1438 1441
1439 1442 # order matters: first source that has commit_id in it will be selected
1440 1443 sources = []
1441 1444 sources.append(('book', repo.bookmarks.items(), _('Bookmarks'), bookmark))
1442 1445 sources.append(('branch', repo.branches.items(), _('Branches'), branch))
1443 1446
1444 1447 if commit_id:
1445 1448 ref_commit = (h.short_id(commit_id), commit_id)
1446 1449 sources.append(('rev', [ref_commit], _('Commit IDs'), commit_id))
1447 1450
1448 1451 sources.append(
1449 1452 ('branch', repo.branches_closed.items(), _('Closed Branches'), branch),
1450 1453 )
1451 1454
1452 1455 groups = []
1453 1456 for group_key, ref_list, group_name, match in sources:
1454 1457 group_refs = []
1455 1458 for ref_name, ref_id in ref_list:
1456 1459 ref_key = '%s:%s:%s' % (group_key, ref_name, ref_id)
1457 1460 group_refs.append((ref_key, ref_name))
1458 1461
1459 1462 if not selected:
1460 1463 if set([commit_id, match]) & set([ref_id, ref_name]):
1461 1464 selected = ref_key
1462 1465
1463 1466 if group_refs:
1464 1467 groups.append((group_refs, group_name))
1465 1468
1466 1469 if not selected:
1467 1470 ref = commit_id or branch or bookmark
1468 1471 if ref:
1469 1472 raise CommitDoesNotExistError(
1470 1473 'No commit refs could be found matching: %s' % ref)
1471 1474 elif repo.DEFAULT_BRANCH_NAME in repo.branches:
1472 1475 selected = 'branch:%s:%s' % (
1473 1476 repo.DEFAULT_BRANCH_NAME,
1474 1477 repo.branches[repo.DEFAULT_BRANCH_NAME]
1475 1478 )
1476 1479 elif repo.commit_ids:
1477 1480 # make the user select in this case
1478 1481 selected = None
1479 1482 else:
1480 1483 raise EmptyRepositoryError()
1481 1484 return groups, selected
1482 1485
1483 1486 def get_diff(self, source_repo, source_ref_id, target_ref_id, context=DIFF_CONTEXT):
1484 1487 return self._get_diff_from_pr_or_version(
1485 1488 source_repo, source_ref_id, target_ref_id, context=context)
1486 1489
1487 1490 def _get_diff_from_pr_or_version(
1488 1491 self, source_repo, source_ref_id, target_ref_id, context):
1489 1492 target_commit = source_repo.get_commit(
1490 1493 commit_id=safe_str(target_ref_id))
1491 1494 source_commit = source_repo.get_commit(
1492 1495 commit_id=safe_str(source_ref_id))
1493 1496 if isinstance(source_repo, Repository):
1494 1497 vcs_repo = source_repo.scm_instance()
1495 1498 else:
1496 1499 vcs_repo = source_repo
1497 1500
1498 1501 # TODO: johbo: In the context of an update, we cannot reach
1499 1502 # the old commit anymore with our normal mechanisms. It needs
1500 1503 # some sort of special support in the vcs layer to avoid this
1501 1504 # workaround.
1502 1505 if (source_commit.raw_id == vcs_repo.EMPTY_COMMIT_ID and
1503 1506 vcs_repo.alias == 'git'):
1504 1507 source_commit.raw_id = safe_str(source_ref_id)
1505 1508
1506 1509 log.debug('calculating diff between '
1507 1510 'source_ref:%s and target_ref:%s for repo `%s`',
1508 1511 target_ref_id, source_ref_id,
1509 1512 safe_unicode(vcs_repo.path))
1510 1513
1511 1514 vcs_diff = vcs_repo.get_diff(
1512 1515 commit1=target_commit, commit2=source_commit, context=context)
1513 1516 return vcs_diff
1514 1517
1515 1518 def _is_merge_enabled(self, pull_request):
1516 1519 return self._get_general_setting(
1517 1520 pull_request, 'rhodecode_pr_merge_enabled')
1518 1521
1519 1522 def _use_rebase_for_merging(self, pull_request):
1520 1523 repo_type = pull_request.target_repo.repo_type
1521 1524 if repo_type == 'hg':
1522 1525 return self._get_general_setting(
1523 1526 pull_request, 'rhodecode_hg_use_rebase_for_merging')
1524 1527 elif repo_type == 'git':
1525 1528 return self._get_general_setting(
1526 1529 pull_request, 'rhodecode_git_use_rebase_for_merging')
1527 1530
1528 1531 return False
1529 1532
1530 1533 def _close_branch_before_merging(self, pull_request):
1531 1534 repo_type = pull_request.target_repo.repo_type
1532 1535 if repo_type == 'hg':
1533 1536 return self._get_general_setting(
1534 1537 pull_request, 'rhodecode_hg_close_branch_before_merging')
1535 1538 elif repo_type == 'git':
1536 1539 return self._get_general_setting(
1537 1540 pull_request, 'rhodecode_git_close_branch_before_merging')
1538 1541
1539 1542 return False
1540 1543
1541 1544 def _get_general_setting(self, pull_request, settings_key, default=False):
1542 1545 settings_model = VcsSettingsModel(repo=pull_request.target_repo)
1543 1546 settings = settings_model.get_general_settings()
1544 1547 return settings.get(settings_key, default)
1545 1548
1546 1549 def _log_audit_action(self, action, action_data, user, pull_request):
1547 1550 audit_logger.store(
1548 1551 action=action,
1549 1552 action_data=action_data,
1550 1553 user=user,
1551 1554 repo=pull_request.target_repo)
1552 1555
1553 1556 def get_reviewer_functions(self):
1554 1557 """
1555 1558 Fetches functions for validation and fetching default reviewers.
1556 1559 If available we use the EE package, else we fallback to CE
1557 1560 package functions
1558 1561 """
1559 1562 try:
1560 1563 from rc_reviewers.utils import get_default_reviewers_data
1561 1564 from rc_reviewers.utils import validate_default_reviewers
1562 1565 except ImportError:
1563 1566 from rhodecode.apps.repository.utils import \
1564 1567 get_default_reviewers_data
1565 1568 from rhodecode.apps.repository.utils import \
1566 1569 validate_default_reviewers
1567 1570
1568 1571 return get_default_reviewers_data, validate_default_reviewers
1569 1572
1570 1573
1571 1574 class MergeCheck(object):
1572 1575 """
1573 1576 Perform Merge Checks and returns a check object which stores information
1574 1577 about merge errors, and merge conditions
1575 1578 """
1576 1579 TODO_CHECK = 'todo'
1577 1580 PERM_CHECK = 'perm'
1578 1581 REVIEW_CHECK = 'review'
1579 1582 MERGE_CHECK = 'merge'
1580 1583
1581 1584 def __init__(self):
1582 1585 self.review_status = None
1583 1586 self.merge_possible = None
1584 1587 self.merge_msg = ''
1585 1588 self.failed = None
1586 1589 self.errors = []
1587 1590 self.error_details = OrderedDict()
1588 1591
1589 1592 def push_error(self, error_type, message, error_key, details):
1590 1593 self.failed = True
1591 1594 self.errors.append([error_type, message])
1592 1595 self.error_details[error_key] = dict(
1593 1596 details=details,
1594 1597 error_type=error_type,
1595 1598 message=message
1596 1599 )
1597 1600
1598 1601 @classmethod
1599 1602 def validate(cls, pull_request, user, translator, fail_early=False,
1600 1603 force_shadow_repo_refresh=False):
1601 1604 _ = translator
1602 1605 merge_check = cls()
1603 1606
1604 1607 # permissions to merge
1605 1608 user_allowed_to_merge = PullRequestModel().check_user_merge(
1606 1609 pull_request, user)
1607 1610 if not user_allowed_to_merge:
1608 1611 log.debug("MergeCheck: cannot merge, approval is pending.")
1609 1612
1610 1613 msg = _('User `{}` not allowed to perform merge.').format(user.username)
1611 1614 merge_check.push_error('error', msg, cls.PERM_CHECK, user.username)
1612 1615 if fail_early:
1613 1616 return merge_check
1614 1617
1615 1618 # review status, must be always present
1616 1619 review_status = pull_request.calculated_review_status()
1617 1620 merge_check.review_status = review_status
1618 1621
1619 1622 status_approved = review_status == ChangesetStatus.STATUS_APPROVED
1620 1623 if not status_approved:
1621 1624 log.debug("MergeCheck: cannot merge, approval is pending.")
1622 1625
1623 1626 msg = _('Pull request reviewer approval is pending.')
1624 1627
1625 1628 merge_check.push_error(
1626 1629 'warning', msg, cls.REVIEW_CHECK, review_status)
1627 1630
1628 1631 if fail_early:
1629 1632 return merge_check
1630 1633
1631 1634 # left over TODOs
1632 1635 todos = CommentsModel().get_unresolved_todos(pull_request)
1633 1636 if todos:
1634 1637 log.debug("MergeCheck: cannot merge, {} "
1635 1638 "unresolved todos left.".format(len(todos)))
1636 1639
1637 1640 if len(todos) == 1:
1638 1641 msg = _('Cannot merge, {} TODO still not resolved.').format(
1639 1642 len(todos))
1640 1643 else:
1641 1644 msg = _('Cannot merge, {} TODOs still not resolved.').format(
1642 1645 len(todos))
1643 1646
1644 1647 merge_check.push_error('warning', msg, cls.TODO_CHECK, todos)
1645 1648
1646 1649 if fail_early:
1647 1650 return merge_check
1648 1651
1649 1652 # merge possible, here is the filesystem simulation + shadow repo
1650 1653 merge_status, msg = PullRequestModel().merge_status(
1651 1654 pull_request, translator=translator,
1652 1655 force_shadow_repo_refresh=force_shadow_repo_refresh)
1653 1656 merge_check.merge_possible = merge_status
1654 1657 merge_check.merge_msg = msg
1655 1658 if not merge_status:
1656 1659 log.debug(
1657 1660 "MergeCheck: cannot merge, pull request merge not possible.")
1658 1661 merge_check.push_error('warning', msg, cls.MERGE_CHECK, None)
1659 1662
1660 1663 if fail_early:
1661 1664 return merge_check
1662 1665
1663 1666 log.debug('MergeCheck: is failed: %s', merge_check.failed)
1664 1667 return merge_check
1665 1668
1666 1669 @classmethod
1667 1670 def get_merge_conditions(cls, pull_request, translator):
1668 1671 _ = translator
1669 1672 merge_details = {}
1670 1673
1671 1674 model = PullRequestModel()
1672 1675 use_rebase = model._use_rebase_for_merging(pull_request)
1673 1676
1674 1677 if use_rebase:
1675 1678 merge_details['merge_strategy'] = dict(
1676 1679 details={},
1677 1680 message=_('Merge strategy: rebase')
1678 1681 )
1679 1682 else:
1680 1683 merge_details['merge_strategy'] = dict(
1681 1684 details={},
1682 1685 message=_('Merge strategy: explicit merge commit')
1683 1686 )
1684 1687
1685 1688 close_branch = model._close_branch_before_merging(pull_request)
1686 1689 if close_branch:
1687 1690 repo_type = pull_request.target_repo.repo_type
1688 1691 if repo_type == 'hg':
1689 1692 close_msg = _('Source branch will be closed after merge.')
1690 1693 elif repo_type == 'git':
1691 1694 close_msg = _('Source branch will be deleted after merge.')
1692 1695
1693 1696 merge_details['close_branch'] = dict(
1694 1697 details={},
1695 1698 message=close_msg
1696 1699 )
1697 1700
1698 1701 return merge_details
1699 1702
1700 1703 ChangeTuple = collections.namedtuple(
1701 1704 'ChangeTuple', ['added', 'common', 'removed', 'total'])
1702 1705
1703 1706 FileChangeTuple = collections.namedtuple(
1704 1707 'FileChangeTuple', ['added', 'modified', 'removed'])
General Comments 0
You need to be logged in to leave comments. Login now