##// END OF EJS Templates
tests: Adapt tests to return value change.
Martin Bornhold -
r1077:b67a4d33 default
parent child Browse files
Show More
@@ -1,691 +1,691 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2011-2016 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 import logging
23 23
24 24 from rhodecode.api import jsonrpc_method, JSONRPCError
25 25 from rhodecode.api.utils import (
26 26 has_superadmin_permission, Optional, OAttr, get_repo_or_error,
27 27 get_pull_request_or_error, get_commit_or_error, get_user_or_error,
28 28 has_repo_permissions, resolve_ref_or_error)
29 29 from rhodecode.lib.auth import (HasRepoPermissionAnyApi)
30 30 from rhodecode.lib.base import vcs_operation_context
31 31 from rhodecode.lib.utils2 import str2bool
32 32 from rhodecode.model.changeset_status import ChangesetStatusModel
33 33 from rhodecode.model.comment import ChangesetCommentsModel
34 34 from rhodecode.model.db import Session, ChangesetStatus
35 35 from rhodecode.model.pull_request import PullRequestModel
36 36 from rhodecode.model.settings import SettingsModel
37 37
38 38 log = logging.getLogger(__name__)
39 39
40 40
41 41 @jsonrpc_method()
42 42 def get_pull_request(request, apiuser, repoid, pullrequestid):
43 43 """
44 44 Get a pull request based on the given ID.
45 45
46 46 :param apiuser: This is filled automatically from the |authtoken|.
47 47 :type apiuser: AuthUser
48 48 :param repoid: Repository name or repository ID from where the pull
49 49 request was opened.
50 50 :type repoid: str or int
51 51 :param pullrequestid: ID of the requested pull request.
52 52 :type pullrequestid: int
53 53
54 54 Example output:
55 55
56 56 .. code-block:: bash
57 57
58 58 "id": <id_given_in_input>,
59 59 "result":
60 60 {
61 61 "pull_request_id": "<pull_request_id>",
62 62 "url": "<url>",
63 63 "title": "<title>",
64 64 "description": "<description>",
65 65 "status" : "<status>",
66 66 "created_on": "<date_time_created>",
67 67 "updated_on": "<date_time_updated>",
68 68 "commit_ids": [
69 69 ...
70 70 "<commit_id>",
71 71 "<commit_id>",
72 72 ...
73 73 ],
74 74 "review_status": "<review_status>",
75 75 "mergeable": {
76 76 "status": "<bool>",
77 77 "message": "<message>",
78 78 },
79 79 "source": {
80 80 "clone_url": "<clone_url>",
81 81 "repository": "<repository_name>",
82 82 "reference":
83 83 {
84 84 "name": "<name>",
85 85 "type": "<type>",
86 86 "commit_id": "<commit_id>",
87 87 }
88 88 },
89 89 "target": {
90 90 "clone_url": "<clone_url>",
91 91 "repository": "<repository_name>",
92 92 "reference":
93 93 {
94 94 "name": "<name>",
95 95 "type": "<type>",
96 96 "commit_id": "<commit_id>",
97 97 }
98 98 },
99 99 "merge": {
100 100 "clone_url": "<clone_url>",
101 101 "reference":
102 102 {
103 103 "name": "<name>",
104 104 "type": "<type>",
105 105 "commit_id": "<commit_id>",
106 106 }
107 107 },
108 108 "author": <user_obj>,
109 109 "reviewers": [
110 110 ...
111 111 {
112 112 "user": "<user_obj>",
113 113 "review_status": "<review_status>",
114 114 }
115 115 ...
116 116 ]
117 117 },
118 118 "error": null
119 119 """
120 120 get_repo_or_error(repoid)
121 121 pull_request = get_pull_request_or_error(pullrequestid)
122 122 if not PullRequestModel().check_user_read(
123 123 pull_request, apiuser, api=True):
124 124 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
125 125 data = pull_request.get_api_data()
126 126 return data
127 127
128 128
129 129 @jsonrpc_method()
130 130 def get_pull_requests(request, apiuser, repoid, status=Optional('new')):
131 131 """
132 132 Get all pull requests from the repository specified in `repoid`.
133 133
134 134 :param apiuser: This is filled automatically from the |authtoken|.
135 135 :type apiuser: AuthUser
136 136 :param repoid: Repository name or repository ID.
137 137 :type repoid: str or int
138 138 :param status: Only return pull requests with the specified status.
139 139 Valid options are.
140 140 * ``new`` (default)
141 141 * ``open``
142 142 * ``closed``
143 143 :type status: str
144 144
145 145 Example output:
146 146
147 147 .. code-block:: bash
148 148
149 149 "id": <id_given_in_input>,
150 150 "result":
151 151 [
152 152 ...
153 153 {
154 154 "pull_request_id": "<pull_request_id>",
155 155 "url": "<url>",
156 156 "title" : "<title>",
157 157 "description": "<description>",
158 158 "status": "<status>",
159 159 "created_on": "<date_time_created>",
160 160 "updated_on": "<date_time_updated>",
161 161 "commit_ids": [
162 162 ...
163 163 "<commit_id>",
164 164 "<commit_id>",
165 165 ...
166 166 ],
167 167 "review_status": "<review_status>",
168 168 "mergeable": {
169 169 "status": "<bool>",
170 170 "message: "<message>",
171 171 },
172 172 "source": {
173 173 "clone_url": "<clone_url>",
174 174 "reference":
175 175 {
176 176 "name": "<name>",
177 177 "type": "<type>",
178 178 "commit_id": "<commit_id>",
179 179 }
180 180 },
181 181 "target": {
182 182 "clone_url": "<clone_url>",
183 183 "reference":
184 184 {
185 185 "name": "<name>",
186 186 "type": "<type>",
187 187 "commit_id": "<commit_id>",
188 188 }
189 189 },
190 190 "merge": {
191 191 "clone_url": "<clone_url>",
192 192 "reference":
193 193 {
194 194 "name": "<name>",
195 195 "type": "<type>",
196 196 "commit_id": "<commit_id>",
197 197 }
198 198 },
199 199 "author": <user_obj>,
200 200 "reviewers": [
201 201 ...
202 202 {
203 203 "user": "<user_obj>",
204 204 "review_status": "<review_status>",
205 205 }
206 206 ...
207 207 ]
208 208 }
209 209 ...
210 210 ],
211 211 "error": null
212 212
213 213 """
214 214 repo = get_repo_or_error(repoid)
215 215 if not has_superadmin_permission(apiuser):
216 216 _perms = (
217 217 'repository.admin', 'repository.write', 'repository.read',)
218 218 has_repo_permissions(apiuser, repoid, repo, _perms)
219 219
220 220 status = Optional.extract(status)
221 221 pull_requests = PullRequestModel().get_all(repo, statuses=[status])
222 222 data = [pr.get_api_data() for pr in pull_requests]
223 223 return data
224 224
225 225
226 226 @jsonrpc_method()
227 227 def merge_pull_request(request, apiuser, repoid, pullrequestid,
228 228 userid=Optional(OAttr('apiuser'))):
229 229 """
230 230 Merge the pull request specified by `pullrequestid` into its target
231 231 repository.
232 232
233 233 :param apiuser: This is filled automatically from the |authtoken|.
234 234 :type apiuser: AuthUser
235 235 :param repoid: The Repository name or repository ID of the
236 236 target repository to which the |pr| is to be merged.
237 237 :type repoid: str or int
238 238 :param pullrequestid: ID of the pull request which shall be merged.
239 239 :type pullrequestid: int
240 240 :param userid: Merge the pull request as this user.
241 241 :type userid: Optional(str or int)
242 242
243 243 Example output:
244 244
245 245 .. code-block:: bash
246 246
247 247 "id": <id_given_in_input>,
248 248 "result":
249 249 {
250 250 "executed": "<bool>",
251 251 "failure_reason": "<int>",
252 252 "merge_commit_id": "<merge_commit_id>",
253 253 "possible": "<bool>",
254 254 "merge_ref": {
255 255 "commit_id": "<commit_id>",
256 256 "type": "<type>",
257 257 "name": "<name>"
258 258 }
259 259 },
260 260 "error": null
261 261
262 262 """
263 263 repo = get_repo_or_error(repoid)
264 264 if not isinstance(userid, Optional):
265 265 if (has_superadmin_permission(apiuser) or
266 266 HasRepoPermissionAnyApi('repository.admin')(
267 267 user=apiuser, repo_name=repo.repo_name)):
268 268 apiuser = get_user_or_error(userid)
269 269 else:
270 270 raise JSONRPCError('userid is not the same as your user')
271 271
272 272 pull_request = get_pull_request_or_error(pullrequestid)
273 273 if not PullRequestModel().check_user_merge(
274 274 pull_request, apiuser, api=True):
275 275 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
276 276 if pull_request.is_closed():
277 277 raise JSONRPCError(
278 278 'pull request `%s` merge failed, pull request is closed' % (
279 279 pullrequestid,))
280 280
281 281 target_repo = pull_request.target_repo
282 282 extras = vcs_operation_context(
283 283 request.environ, repo_name=target_repo.repo_name,
284 284 username=apiuser.username, action='push',
285 285 scm=target_repo.repo_type)
286 286 merge_response = PullRequestModel().merge(
287 287 pull_request, apiuser, extras=extras)
288 288 if merge_response.executed:
289 289 PullRequestModel().close_pull_request(
290 290 pull_request.pull_request_id, apiuser)
291 291
292 292 Session().commit()
293 293
294 294 # In previous versions the merge response directly contained the merge
295 295 # commit id. It is now contained in the merge reference object. To be
296 296 # backwards compatible we have to extract it again.
297 297 merge_response = merge_response._asdict()
298 298 merge_response['merge_commit_id'] = merge_response['merge_ref'].commit_id
299 299
300 300 return merge_response
301 301
302 302
303 303 @jsonrpc_method()
304 304 def close_pull_request(request, apiuser, repoid, pullrequestid,
305 305 userid=Optional(OAttr('apiuser'))):
306 306 """
307 307 Close the pull request specified by `pullrequestid`.
308 308
309 309 :param apiuser: This is filled automatically from the |authtoken|.
310 310 :type apiuser: AuthUser
311 311 :param repoid: Repository name or repository ID to which the pull
312 312 request belongs.
313 313 :type repoid: str or int
314 314 :param pullrequestid: ID of the pull request to be closed.
315 315 :type pullrequestid: int
316 316 :param userid: Close the pull request as this user.
317 317 :type userid: Optional(str or int)
318 318
319 319 Example output:
320 320
321 321 .. code-block:: bash
322 322
323 323 "id": <id_given_in_input>,
324 324 "result":
325 325 {
326 326 "pull_request_id": "<int>",
327 327 "closed": "<bool>"
328 328 },
329 329 "error": null
330 330
331 331 """
332 332 repo = get_repo_or_error(repoid)
333 333 if not isinstance(userid, Optional):
334 334 if (has_superadmin_permission(apiuser) or
335 335 HasRepoPermissionAnyApi('repository.admin')(
336 336 user=apiuser, repo_name=repo.repo_name)):
337 337 apiuser = get_user_or_error(userid)
338 338 else:
339 339 raise JSONRPCError('userid is not the same as your user')
340 340
341 341 pull_request = get_pull_request_or_error(pullrequestid)
342 342 if not PullRequestModel().check_user_update(
343 343 pull_request, apiuser, api=True):
344 344 raise JSONRPCError(
345 345 'pull request `%s` close failed, no permission to close.' % (
346 346 pullrequestid,))
347 347 if pull_request.is_closed():
348 348 raise JSONRPCError(
349 349 'pull request `%s` is already closed' % (pullrequestid,))
350 350
351 351 PullRequestModel().close_pull_request(
352 352 pull_request.pull_request_id, apiuser)
353 353 Session().commit()
354 354 data = {
355 355 'pull_request_id': pull_request.pull_request_id,
356 356 'closed': True,
357 357 }
358 358 return data
359 359
360 360
361 361 @jsonrpc_method()
362 362 def comment_pull_request(request, apiuser, repoid, pullrequestid,
363 363 message=Optional(None), status=Optional(None),
364 364 userid=Optional(OAttr('apiuser'))):
365 365 """
366 366 Comment on the pull request specified with the `pullrequestid`,
367 367 in the |repo| specified by the `repoid`, and optionally change the
368 368 review status.
369 369
370 370 :param apiuser: This is filled automatically from the |authtoken|.
371 371 :type apiuser: AuthUser
372 372 :param repoid: The repository name or repository ID.
373 373 :type repoid: str or int
374 374 :param pullrequestid: The pull request ID.
375 375 :type pullrequestid: int
376 376 :param message: The text content of the comment.
377 377 :type message: str
378 378 :param status: (**Optional**) Set the approval status of the pull
379 379 request. Valid options are:
380 380 * not_reviewed
381 381 * approved
382 382 * rejected
383 383 * under_review
384 384 :type status: str
385 385 :param userid: Comment on the pull request as this user
386 386 :type userid: Optional(str or int)
387 387
388 388 Example output:
389 389
390 390 .. code-block:: bash
391 391
392 392 id : <id_given_in_input>
393 393 result :
394 394 {
395 395 "pull_request_id": "<Integer>",
396 396 "comment_id": "<Integer>"
397 397 }
398 398 error : null
399 399 """
400 400 repo = get_repo_or_error(repoid)
401 401 if not isinstance(userid, Optional):
402 402 if (has_superadmin_permission(apiuser) or
403 403 HasRepoPermissionAnyApi('repository.admin')(
404 404 user=apiuser, repo_name=repo.repo_name)):
405 405 apiuser = get_user_or_error(userid)
406 406 else:
407 407 raise JSONRPCError('userid is not the same as your user')
408 408
409 409 pull_request = get_pull_request_or_error(pullrequestid)
410 410 if not PullRequestModel().check_user_read(
411 411 pull_request, apiuser, api=True):
412 412 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
413 413 message = Optional.extract(message)
414 414 status = Optional.extract(status)
415 415 if not message and not status:
416 416 raise JSONRPCError('message and status parameter missing')
417 417
418 418 if (status not in (st[0] for st in ChangesetStatus.STATUSES) and
419 419 status is not None):
420 420 raise JSONRPCError('unknown comment status`%s`' % status)
421 421
422 422 allowed_to_change_status = PullRequestModel().check_user_change_status(
423 423 pull_request, apiuser)
424 424 text = message
425 425 if status and allowed_to_change_status:
426 426 st_message = (('Status change %(transition_icon)s %(status)s')
427 427 % {'transition_icon': '>',
428 428 'status': ChangesetStatus.get_status_lbl(status)})
429 429 text = message or st_message
430 430
431 431 rc_config = SettingsModel().get_all_settings()
432 432 renderer = rc_config.get('rhodecode_markup_renderer', 'rst')
433 433 comment = ChangesetCommentsModel().create(
434 434 text=text,
435 435 repo=pull_request.target_repo.repo_id,
436 436 user=apiuser.user_id,
437 437 pull_request=pull_request.pull_request_id,
438 438 f_path=None,
439 439 line_no=None,
440 440 status_change=(ChangesetStatus.get_status_lbl(status)
441 441 if status and allowed_to_change_status else None),
442 442 status_change_type=(status
443 443 if status and allowed_to_change_status else None),
444 444 closing_pr=False,
445 445 renderer=renderer
446 446 )
447 447
448 448 if allowed_to_change_status and status:
449 449 ChangesetStatusModel().set_status(
450 450 pull_request.target_repo.repo_id,
451 451 status,
452 452 apiuser.user_id,
453 453 comment,
454 454 pull_request=pull_request.pull_request_id
455 455 )
456 456 Session().flush()
457 457
458 458 Session().commit()
459 459 data = {
460 460 'pull_request_id': pull_request.pull_request_id,
461 461 'comment_id': comment.comment_id,
462 462 'status': status
463 463 }
464 464 return data
465 465
466 466
467 467 @jsonrpc_method()
468 468 def create_pull_request(
469 469 request, apiuser, source_repo, target_repo, source_ref, target_ref,
470 470 title, description=Optional(''), reviewers=Optional(None)):
471 471 """
472 472 Creates a new pull request.
473 473
474 474 Accepts refs in the following formats:
475 475
476 476 * branch:<branch_name>:<sha>
477 477 * branch:<branch_name>
478 478 * bookmark:<bookmark_name>:<sha> (Mercurial only)
479 479 * bookmark:<bookmark_name> (Mercurial only)
480 480
481 481 :param apiuser: This is filled automatically from the |authtoken|.
482 482 :type apiuser: AuthUser
483 483 :param source_repo: Set the source repository name.
484 484 :type source_repo: str
485 485 :param target_repo: Set the target repository name.
486 486 :type target_repo: str
487 487 :param source_ref: Set the source ref name.
488 488 :type source_ref: str
489 489 :param target_ref: Set the target ref name.
490 490 :type target_ref: str
491 491 :param title: Set the pull request title.
492 492 :type title: str
493 493 :param description: Set the pull request description.
494 494 :type description: Optional(str)
495 495 :param reviewers: Set the new pull request reviewers list.
496 496 :type reviewers: Optional(list)
497 497 Accepts username strings or objects of the format:
498 498 {
499 499 'username': 'nick', 'reasons': ['original author']
500 500 }
501 501 """
502 502
503 503 source = get_repo_or_error(source_repo)
504 504 target = get_repo_or_error(target_repo)
505 505 if not has_superadmin_permission(apiuser):
506 506 _perms = ('repository.admin', 'repository.write', 'repository.read',)
507 507 has_repo_permissions(apiuser, source_repo, source, _perms)
508 508
509 509 full_source_ref = resolve_ref_or_error(source_ref, source)
510 510 full_target_ref = resolve_ref_or_error(target_ref, target)
511 511 source_commit = get_commit_or_error(full_source_ref, source)
512 512 target_commit = get_commit_or_error(full_target_ref, target)
513 513 source_scm = source.scm_instance()
514 514 target_scm = target.scm_instance()
515 515
516 516 commit_ranges = target_scm.compare(
517 517 target_commit.raw_id, source_commit.raw_id, source_scm,
518 518 merge=True, pre_load=[])
519 519
520 520 ancestor = target_scm.get_common_ancestor(
521 521 target_commit.raw_id, source_commit.raw_id, source_scm)
522 522
523 523 if not commit_ranges:
524 524 raise JSONRPCError('no commits found')
525 525
526 526 if not ancestor:
527 527 raise JSONRPCError('no common ancestor found')
528 528
529 529 reviewer_objects = Optional.extract(reviewers) or []
530 530 if not isinstance(reviewer_objects, list):
531 531 raise JSONRPCError('reviewers should be specified as a list')
532 532
533 533 reviewers_reasons = []
534 534 for reviewer_object in reviewer_objects:
535 535 reviewer_reasons = []
536 536 if isinstance(reviewer_object, (basestring, int)):
537 537 reviewer_username = reviewer_object
538 538 else:
539 539 reviewer_username = reviewer_object['username']
540 540 reviewer_reasons = reviewer_object.get('reasons', [])
541 541
542 542 user = get_user_or_error(reviewer_username)
543 543 reviewers_reasons.append((user.user_id, reviewer_reasons))
544 544
545 545 pull_request_model = PullRequestModel()
546 546 pull_request = pull_request_model.create(
547 547 created_by=apiuser.user_id,
548 548 source_repo=source_repo,
549 549 source_ref=full_source_ref,
550 550 target_repo=target_repo,
551 551 target_ref=full_target_ref,
552 552 revisions=reversed(
553 553 [commit.raw_id for commit in reversed(commit_ranges)]),
554 554 reviewers=reviewers_reasons,
555 555 title=title,
556 556 description=Optional.extract(description)
557 557 )
558 558
559 559 Session().commit()
560 560 data = {
561 561 'msg': 'Created new pull request `{}`'.format(title),
562 562 'pull_request_id': pull_request.pull_request_id,
563 563 }
564 564 return data
565 565
566 566
567 567 @jsonrpc_method()
568 568 def update_pull_request(
569 569 request, apiuser, repoid, pullrequestid, title=Optional(''),
570 570 description=Optional(''), reviewers=Optional(None),
571 571 update_commits=Optional(None), close_pull_request=Optional(None)):
572 572 """
573 573 Updates a pull request.
574 574
575 575 :param apiuser: This is filled automatically from the |authtoken|.
576 576 :type apiuser: AuthUser
577 577 :param repoid: The repository name or repository ID.
578 578 :type repoid: str or int
579 579 :param pullrequestid: The pull request ID.
580 580 :type pullrequestid: int
581 581 :param title: Set the pull request title.
582 582 :type title: str
583 583 :param description: Update pull request description.
584 584 :type description: Optional(str)
585 585 :param reviewers: Update pull request reviewers list with new value.
586 586 :type reviewers: Optional(list)
587 587 :param update_commits: Trigger update of commits for this pull request
588 588 :type: update_commits: Optional(bool)
589 589 :param close_pull_request: Close this pull request with rejected state
590 590 :type: close_pull_request: Optional(bool)
591 591
592 592 Example output:
593 593
594 594 .. code-block:: bash
595 595
596 596 id : <id_given_in_input>
597 597 result :
598 598 {
599 599 "msg": "Updated pull request `63`",
600 600 "pull_request": <pull_request_object>,
601 601 "updated_reviewers": {
602 602 "added": [
603 603 "username"
604 604 ],
605 605 "removed": []
606 606 },
607 607 "updated_commits": {
608 608 "added": [
609 609 "<sha1_hash>"
610 610 ],
611 611 "common": [
612 612 "<sha1_hash>",
613 613 "<sha1_hash>",
614 614 ],
615 615 "removed": []
616 616 }
617 617 }
618 618 error : null
619 619 """
620 620
621 621 repo = get_repo_or_error(repoid)
622 622 pull_request = get_pull_request_or_error(pullrequestid)
623 623 if not PullRequestModel().check_user_update(
624 624 pull_request, apiuser, api=True):
625 625 raise JSONRPCError(
626 626 'pull request `%s` update failed, no permission to update.' % (
627 627 pullrequestid,))
628 628 if pull_request.is_closed():
629 629 raise JSONRPCError(
630 630 'pull request `%s` update failed, pull request is closed' % (
631 631 pullrequestid,))
632 632
633 633 reviewer_objects = Optional.extract(reviewers) or []
634 634 if not isinstance(reviewer_objects, list):
635 635 raise JSONRPCError('reviewers should be specified as a list')
636 636
637 637 reviewers_reasons = []
638 638 reviewer_ids = set()
639 639 for reviewer_object in reviewer_objects:
640 640 reviewer_reasons = []
641 641 if isinstance(reviewer_object, (int, basestring)):
642 642 reviewer_username = reviewer_object
643 643 else:
644 644 reviewer_username = reviewer_object['username']
645 645 reviewer_reasons = reviewer_object.get('reasons', [])
646 646
647 647 user = get_user_or_error(reviewer_username)
648 648 reviewer_ids.add(user.user_id)
649 649 reviewers_reasons.append((user.user_id, reviewer_reasons))
650 650
651 651 title = Optional.extract(title)
652 652 description = Optional.extract(description)
653 653 if title or description:
654 654 PullRequestModel().edit(
655 655 pull_request, title or pull_request.title,
656 656 description or pull_request.description)
657 657 Session().commit()
658 658
659 659 commit_changes = {"added": [], "common": [], "removed": []}
660 660 if str2bool(Optional.extract(update_commits)):
661 661 if PullRequestModel().has_valid_update_type(pull_request):
662 _version, _commit_changes = PullRequestModel().update_commits(
662 update_response = PullRequestModel().update_commits(
663 663 pull_request)
664 commit_changes = _commit_changes or commit_changes
664 commit_changes = update_response.changes or commit_changes
665 665 Session().commit()
666 666
667 667 reviewers_changes = {"added": [], "removed": []}
668 668 if reviewer_ids:
669 669 added_reviewers, removed_reviewers = \
670 670 PullRequestModel().update_reviewers(pull_request, reviewers_reasons)
671 671
672 672 reviewers_changes['added'] = sorted(
673 673 [get_user_or_error(n).username for n in added_reviewers])
674 674 reviewers_changes['removed'] = sorted(
675 675 [get_user_or_error(n).username for n in removed_reviewers])
676 676 Session().commit()
677 677
678 678 if str2bool(Optional.extract(close_pull_request)):
679 679 PullRequestModel().close_pull_request_with_comment(
680 680 pull_request, apiuser, repo)
681 681 Session().commit()
682 682
683 683 data = {
684 684 'msg': 'Updated pull request `{}`'.format(
685 685 pull_request.pull_request_id),
686 686 'pull_request': pull_request.get_api_data(),
687 687 'updated_commits': commit_changes,
688 688 'updated_reviewers': reviewers_changes
689 689 }
690
690 691 return data
691
@@ -1,178 +1,178 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 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 pytest
22 22
23 23 from rhodecode.model import db
24 24 from rhodecode.model.changeset_status import ChangesetStatusModel
25 25 from rhodecode.model.pull_request import PullRequestModel
26 26
27 27
28 28 pytestmark = [
29 29 pytest.mark.backends("git", "hg"),
30 30 ]
31 31
32 32
33 33 def test_new_pull_request_is_under_review(pr_util):
34 34 pull_request = pr_util.create_pull_request()
35 35
36 36 # Expect that review status "Under Review"
37 37 expected_review_status = db.ChangesetStatus.STATUS_UNDER_REVIEW
38 38 assert pull_request.calculated_review_status() == expected_review_status
39 39
40 40
41 41 @pytest.mark.parametrize("voted_status", [
42 42 db.ChangesetStatus.STATUS_APPROVED,
43 43 db.ChangesetStatus.STATUS_REJECTED,
44 44 db.ChangesetStatus.STATUS_UNDER_REVIEW,
45 45 ])
46 46 def test_pull_request_under_review_if_one_reviewer_voted(
47 47 pr_util, voted_status):
48 48 pull_request = pr_util.create_pull_request()
49 49 pr_util.create_status_votes(
50 50 voted_status, pull_request.reviewers[0])
51 51
52 52 # Expect that review status "Under Review"
53 53 expected_review_status = db.ChangesetStatus.STATUS_UNDER_REVIEW
54 54 assert pull_request.calculated_review_status() == expected_review_status
55 55
56 56
57 57 @pytest.mark.parametrize("voted_status", [
58 58 db.ChangesetStatus.STATUS_APPROVED,
59 59 db.ChangesetStatus.STATUS_REJECTED,
60 60 db.ChangesetStatus.STATUS_UNDER_REVIEW,
61 61 ])
62 62 def test_pull_request_has_voted_status_if_all_voted(pr_util, voted_status):
63 63 pull_request = pr_util.create_pull_request()
64 64 pr_util.create_status_votes(
65 65 voted_status, *pull_request.reviewers)
66 66
67 67 # Expect that review status is the voted_status
68 68 expected_review_status = voted_status
69 69 assert pull_request.calculated_review_status() == expected_review_status
70 70
71 71
72 72 @pytest.mark.parametrize("voted_status", [
73 73 db.ChangesetStatus.STATUS_APPROVED,
74 74 db.ChangesetStatus.STATUS_REJECTED,
75 75 db.ChangesetStatus.STATUS_UNDER_REVIEW,
76 76 ])
77 77 def test_pull_request_stays_if_update_without_change(pr_util, voted_status):
78 78 pull_request = pr_util.create_pull_request()
79 79 pr_util.create_status_votes(
80 80 voted_status, *pull_request.reviewers)
81 81
82 82 # Update, without change
83 version, changes = PullRequestModel().update_commits(pull_request)
83 PullRequestModel().update_commits(pull_request)
84 84
85 85 # Expect that review status is the voted_status
86 86 expected_review_status = voted_status
87 87 assert pull_request.calculated_review_status() == expected_review_status
88 88
89 89
90 90 @pytest.mark.parametrize("voted_status", [
91 91 db.ChangesetStatus.STATUS_APPROVED,
92 92 db.ChangesetStatus.STATUS_REJECTED,
93 93 db.ChangesetStatus.STATUS_UNDER_REVIEW,
94 94 ])
95 95 def test_pull_request_under_review_if_update(pr_util, voted_status):
96 96 pull_request = pr_util.create_pull_request()
97 97 pr_util.create_status_votes(
98 98 voted_status, *pull_request.reviewers)
99 99
100 100 # Update, with change
101 101 pr_util.update_source_repository()
102 version, changes = PullRequestModel().update_commits(pull_request)
102 PullRequestModel().update_commits(pull_request)
103 103
104 104 # Expect that review status is the voted_status
105 105 expected_review_status = db.ChangesetStatus.STATUS_UNDER_REVIEW
106 106 assert pull_request.calculated_review_status() == expected_review_status
107 107
108 108
109 109 def test_commit_under_review_if_part_of_new_pull_request(pr_util):
110 110 pull_request = pr_util.create_pull_request()
111 111 for commit_id in pull_request.revisions:
112 112 status = ChangesetStatusModel().get_status(
113 113 repo=pr_util.source_repository, revision=commit_id)
114 114 assert status == db.ChangesetStatus.STATUS_UNDER_REVIEW
115 115
116 116
117 117 @pytest.mark.parametrize("voted_status", [
118 118 db.ChangesetStatus.STATUS_APPROVED,
119 119 db.ChangesetStatus.STATUS_REJECTED,
120 120 db.ChangesetStatus.STATUS_UNDER_REVIEW,
121 121 ])
122 122 def test_commit_has_voted_status_after_vote_on_pull_request(
123 123 pr_util, voted_status):
124 124 pull_request = pr_util.create_pull_request()
125 125 pr_util.create_status_votes(
126 126 voted_status, pull_request.reviewers[0])
127 127 for commit_id in pull_request.revisions:
128 128 status = ChangesetStatusModel().get_status(
129 129 repo=pr_util.source_repository, revision=commit_id)
130 130 assert status == voted_status
131 131
132 132
133 133 def test_commit_under_review_if_added_to_pull_request(pr_util):
134 134 pull_request = pr_util.create_pull_request()
135 135 pr_util.create_status_votes(
136 136 db.ChangesetStatus.STATUS_APPROVED, pull_request.reviewers[0])
137 137 added_commit_id = pr_util.add_one_commit()
138 138
139 139 status = ChangesetStatusModel().get_status(
140 140 repo=pr_util.source_repository, revision=added_commit_id)
141 141 assert status == db.ChangesetStatus.STATUS_UNDER_REVIEW
142 142
143 143
144 144 @pytest.mark.parametrize("voted_status", [
145 145 db.ChangesetStatus.STATUS_APPROVED,
146 146 db.ChangesetStatus.STATUS_REJECTED,
147 147 db.ChangesetStatus.STATUS_UNDER_REVIEW,
148 148 ])
149 149 def test_commit_keeps_status_if_removed_from_pull_request(
150 150 pr_util, voted_status):
151 151 pull_request = pr_util.create_pull_request()
152 152 pr_util.add_one_commit()
153 153 pr_util.create_status_votes(voted_status, pull_request.reviewers[0])
154 154
155 155 removed_commit_id = pr_util.remove_one_commit()
156 156
157 157 status = ChangesetStatusModel().get_status(
158 158 repo=pr_util.source_repository, revision=removed_commit_id)
159 159 assert status == voted_status
160 160
161 161
162 162 @pytest.mark.parametrize("voted_status", [
163 163 db.ChangesetStatus.STATUS_APPROVED,
164 164 db.ChangesetStatus.STATUS_REJECTED,
165 165 db.ChangesetStatus.STATUS_UNDER_REVIEW,
166 166 ])
167 167 def test_commit_keeps_status_if_unchanged_after_update_of_pull_request(
168 168 pr_util, voted_status):
169 169 pull_request = pr_util.create_pull_request()
170 170 commit_id = pull_request.revisions[-1]
171 171 pr_util.create_status_votes(voted_status, pull_request.reviewers[0])
172 172 pr_util.update_source_repository()
173 173 PullRequestModel().update_commits(pull_request)
174 174 assert pull_request.revisions[-1] == commit_id
175 175
176 176 status = ChangesetStatusModel().get_status(
177 177 repo=pr_util.source_repository, revision=commit_id)
178 178 assert status == voted_status
General Comments 0
You need to be logged in to leave comments. Login now