##// END OF EJS Templates
api: comment_pull_request, added commit_id parameter to validate status changed on particular commit....
marcink -
r1269:26e59d48 default
parent child Browse files
Show More
@@ -58,7 +58,7 b' class TestCommentPullRequest(object):'
58 58 expected = {
59 59 'pull_request_id': pull_request.pull_request_id,
60 60 'comment_id': comments[-1].comment_id,
61 'status': None
61 'status': {'given': None, 'was_changed': None}
62 62 }
63 63 assert_ok(id_, expected, response.body)
64 64
@@ -88,7 +88,56 b' class TestCommentPullRequest(object):'
88 88 expected = {
89 89 'pull_request_id': pull_request.pull_request_id,
90 90 'comment_id': comments[-1].comment_id,
91 'status': 'rejected'
91 'status': {'given': 'rejected', 'was_changed': True}
92 }
93 assert_ok(id_, expected, response.body)
94
95 @pytest.mark.backends("git", "hg")
96 def test_api_comment_pull_request_change_status_with_specific_commit_id(
97 self, pr_util, no_notifications):
98 pull_request = pr_util.create_pull_request()
99 pull_request_id = pull_request.pull_request_id
100 latest_commit_id = 'test_commit'
101 # inject additional revision, to fail test the status change on
102 # non-latest commit
103 pull_request.revisions = pull_request.revisions + ['test_commit']
104
105 id_, params = build_data(
106 self.apikey, 'comment_pull_request',
107 repoid=pull_request.target_repo.repo_name,
108 pullrequestid=pull_request.pull_request_id,
109 status='approved', commit_id=latest_commit_id)
110 response = api_call(self.app, params)
111 pull_request = PullRequestModel().get(pull_request_id)
112
113 expected = {
114 'pull_request_id': pull_request.pull_request_id,
115 'comment_id': None,
116 'status': {'given': 'approved', 'was_changed': False}
117 }
118 assert_ok(id_, expected, response.body)
119
120 @pytest.mark.backends("git", "hg")
121 def test_api_comment_pull_request_change_status_with_specific_commit_id(
122 self, pr_util, no_notifications):
123 pull_request = pr_util.create_pull_request()
124 pull_request_id = pull_request.pull_request_id
125 latest_commit_id = pull_request.revisions[0]
126
127 id_, params = build_data(
128 self.apikey, 'comment_pull_request',
129 repoid=pull_request.target_repo.repo_name,
130 pullrequestid=pull_request.pull_request_id,
131 status='approved', commit_id=latest_commit_id)
132 response = api_call(self.app, params)
133 pull_request = PullRequestModel().get(pull_request_id)
134
135 comments = ChangesetCommentsModel().get_comments(
136 pull_request.target_repo.repo_id, pull_request=pull_request)
137 expected = {
138 'pull_request_id': pull_request.pull_request_id,
139 'comment_id': comments[-1].comment_id,
140 'status': {'given': 'approved', 'was_changed': True}
92 141 }
93 142 assert_ok(id_, expected, response.body)
94 143
@@ -103,7 +152,7 b' class TestCommentPullRequest(object):'
103 152 pullrequestid=pull_request_id)
104 153 response = api_call(self.app, params)
105 154
106 expected = 'message and status parameter missing'
155 expected = 'Both message and status parameters are missing. At least one is required.'
107 156 assert_error(id_, expected, given=response.body)
108 157
109 158 @pytest.mark.backends("git", "hg")
@@ -118,7 +167,7 b' class TestCommentPullRequest(object):'
118 167 status='42')
119 168 response = api_call(self.app, params)
120 169
121 expected = 'unknown comment status`42`'
170 expected = 'Unknown comment status: `42`'
122 171 assert_error(id_, expected, given=response.body)
123 172
124 173 @pytest.mark.backends("git", "hg")
@@ -144,3 +193,17 b' class TestCommentPullRequest(object):'
144 193
145 194 expected = 'userid is not the same as your user'
146 195 assert_error(id_, expected, given=response.body)
196
197 @pytest.mark.backends("git", "hg")
198 def test_api_comment_pull_request_wrong_commit_id_error(self, pr_util):
199 pull_request = pr_util.create_pull_request()
200 id_, params = build_data(
201 self.apikey_regular, 'comment_pull_request',
202 repoid=pull_request.target_repo.repo_name,
203 status='approved',
204 pullrequestid=pull_request.pull_request_id,
205 commit_id='XXX')
206 response = api_call(self.app, params)
207
208 expected = 'Invalid commit_id `XXX` for this pull request.'
209 assert_error(id_, expected, given=response.body)
@@ -361,6 +361,7 b' def close_pull_request(request, apiuser,'
361 361 @jsonrpc_method()
362 362 def comment_pull_request(request, apiuser, repoid, pullrequestid,
363 363 message=Optional(None), status=Optional(None),
364 commit_id=Optional(None),
364 365 userid=Optional(OAttr('apiuser'))):
365 366 """
366 367 Comment on the pull request specified with the `pullrequestid`,
@@ -382,6 +383,10 b' def comment_pull_request(request, apiuse'
382 383 * rejected
383 384 * under_review
384 385 :type status: str
386 :param commit_id: Specify the commit_id for which to set a comment. If
387 given commit_id is different than latest in the PR status
388 change won't be performed.
389 :type commit_id: str
385 390 :param userid: Comment on the pull request as this user
386 391 :type userid: Optional(str or int)
387 392
@@ -393,7 +398,9 b' def comment_pull_request(request, apiuse'
393 398 result :
394 399 {
395 400 "pull_request_id": "<Integer>",
396 "comment_id": "<Integer>"
401 "comment_id": "<Integer>",
402 "status": {"given": <given_status>,
403 "was_changed": <bool status_was_actually_changed> },
397 404 }
398 405 error : null
399 406 """
@@ -412,24 +419,42 b' def comment_pull_request(request, apiuse'
412 419 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
413 420 message = Optional.extract(message)
414 421 status = Optional.extract(status)
422 commit_id = Optional.extract(commit_id)
423
415 424 if not message and not status:
416 raise JSONRPCError('message and status parameter missing')
425 raise JSONRPCError(
426 'Both message and status parameters are missing. '
427 'At least one is required.')
417 428
418 429 if (status not in (st[0] for st in ChangesetStatus.STATUSES) and
419 430 status is not None):
420 raise JSONRPCError('unknown comment status`%s`' % status)
431 raise JSONRPCError('Unknown comment status: `%s`' % status)
432
433 if commit_id and commit_id not in pull_request.revisions:
434 raise JSONRPCError(
435 'Invalid commit_id `%s` for this pull request.' % commit_id)
421 436
422 437 allowed_to_change_status = PullRequestModel().check_user_change_status(
423 438 pull_request, apiuser)
439
440 # if commit_id is passed re-validated if user is allowed to change status
441 # based on latest commit_id from the PR
442 if commit_id:
443 commit_idx = pull_request.revisions.index(commit_id)
444 if commit_idx != 0:
445 allowed_to_change_status = False
446
424 447 text = message
448 status_label = ChangesetStatus.get_status_lbl(status)
425 449 if status and allowed_to_change_status:
426 st_message = (('Status change %(transition_icon)s %(status)s')
427 % {'transition_icon': '>',
428 'status': ChangesetStatus.get_status_lbl(status)})
450 st_message = ('Status change %(transition_icon)s %(status)s'
451 % {'transition_icon': '>', 'status': status_label})
429 452 text = message or st_message
430 453
431 454 rc_config = SettingsModel().get_all_settings()
432 455 renderer = rc_config.get('rhodecode_markup_renderer', 'rst')
456
457 status_change = status and allowed_to_change_status
433 458 comment = ChangesetCommentsModel().create(
434 459 text=text,
435 460 repo=pull_request.target_repo.repo_id,
@@ -437,10 +462,8 b' def comment_pull_request(request, apiuse'
437 462 pull_request=pull_request.pull_request_id,
438 463 f_path=None,
439 464 line_no=None,
440 status_change=(ChangesetStatus.get_status_lbl(status)
441 if status and allowed_to_change_status else None),
442 status_change_type=(status
443 if status and allowed_to_change_status else None),
465 status_change=(status_label if status_change else None),
466 status_change_type=(status if status_change else None),
444 467 closing_pr=False,
445 468 renderer=renderer
446 469 )
@@ -458,8 +481,8 b' def comment_pull_request(request, apiuse'
458 481 Session().commit()
459 482 data = {
460 483 'pull_request_id': pull_request.pull_request_id,
461 'comment_id': comment.comment_id,
462 'status': status
484 'comment_id': comment.comment_id if comment else None,
485 'status': {'given': status, 'was_changed': status_change},
463 486 }
464 487 return data
465 488
General Comments 0
You need to be logged in to leave comments. Login now