##// 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 expected = {
58 expected = {
59 'pull_request_id': pull_request.pull_request_id,
59 'pull_request_id': pull_request.pull_request_id,
60 'comment_id': comments[-1].comment_id,
60 'comment_id': comments[-1].comment_id,
61 'status': None
61 'status': {'given': None, 'was_changed': None}
62 }
62 }
63 assert_ok(id_, expected, response.body)
63 assert_ok(id_, expected, response.body)
64
64
@@ -88,7 +88,56 b' class TestCommentPullRequest(object):'
88 expected = {
88 expected = {
89 'pull_request_id': pull_request.pull_request_id,
89 'pull_request_id': pull_request.pull_request_id,
90 'comment_id': comments[-1].comment_id,
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 assert_ok(id_, expected, response.body)
142 assert_ok(id_, expected, response.body)
94
143
@@ -103,7 +152,7 b' class TestCommentPullRequest(object):'
103 pullrequestid=pull_request_id)
152 pullrequestid=pull_request_id)
104 response = api_call(self.app, params)
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 assert_error(id_, expected, given=response.body)
156 assert_error(id_, expected, given=response.body)
108
157
109 @pytest.mark.backends("git", "hg")
158 @pytest.mark.backends("git", "hg")
@@ -118,7 +167,7 b' class TestCommentPullRequest(object):'
118 status='42')
167 status='42')
119 response = api_call(self.app, params)
168 response = api_call(self.app, params)
120
169
121 expected = 'unknown comment status`42`'
170 expected = 'Unknown comment status: `42`'
122 assert_error(id_, expected, given=response.body)
171 assert_error(id_, expected, given=response.body)
123
172
124 @pytest.mark.backends("git", "hg")
173 @pytest.mark.backends("git", "hg")
@@ -144,3 +193,17 b' class TestCommentPullRequest(object):'
144
193
145 expected = 'userid is not the same as your user'
194 expected = 'userid is not the same as your user'
146 assert_error(id_, expected, given=response.body)
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 @jsonrpc_method()
361 @jsonrpc_method()
362 def comment_pull_request(request, apiuser, repoid, pullrequestid,
362 def comment_pull_request(request, apiuser, repoid, pullrequestid,
363 message=Optional(None), status=Optional(None),
363 message=Optional(None), status=Optional(None),
364 commit_id=Optional(None),
364 userid=Optional(OAttr('apiuser'))):
365 userid=Optional(OAttr('apiuser'))):
365 """
366 """
366 Comment on the pull request specified with the `pullrequestid`,
367 Comment on the pull request specified with the `pullrequestid`,
@@ -382,6 +383,10 b' def comment_pull_request(request, apiuse'
382 * rejected
383 * rejected
383 * under_review
384 * under_review
384 :type status: str
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 :param userid: Comment on the pull request as this user
390 :param userid: Comment on the pull request as this user
386 :type userid: Optional(str or int)
391 :type userid: Optional(str or int)
387
392
@@ -393,7 +398,9 b' def comment_pull_request(request, apiuse'
393 result :
398 result :
394 {
399 {
395 "pull_request_id": "<Integer>",
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 error : null
405 error : null
399 """
406 """
@@ -412,24 +419,42 b' def comment_pull_request(request, apiuse'
412 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
419 raise JSONRPCError('repository `%s` does not exist' % (repoid,))
413 message = Optional.extract(message)
420 message = Optional.extract(message)
414 status = Optional.extract(status)
421 status = Optional.extract(status)
422 commit_id = Optional.extract(commit_id)
423
415 if not message and not status:
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 if (status not in (st[0] for st in ChangesetStatus.STATUSES) and
429 if (status not in (st[0] for st in ChangesetStatus.STATUSES) and
419 status is not None):
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 allowed_to_change_status = PullRequestModel().check_user_change_status(
437 allowed_to_change_status = PullRequestModel().check_user_change_status(
423 pull_request, apiuser)
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 text = message
447 text = message
448 status_label = ChangesetStatus.get_status_lbl(status)
425 if status and allowed_to_change_status:
449 if status and allowed_to_change_status:
426 st_message = (('Status change %(transition_icon)s %(status)s')
450 st_message = ('Status change %(transition_icon)s %(status)s'
427 % {'transition_icon': '>',
451 % {'transition_icon': '>', 'status': status_label})
428 'status': ChangesetStatus.get_status_lbl(status)})
429 text = message or st_message
452 text = message or st_message
430
453
431 rc_config = SettingsModel().get_all_settings()
454 rc_config = SettingsModel().get_all_settings()
432 renderer = rc_config.get('rhodecode_markup_renderer', 'rst')
455 renderer = rc_config.get('rhodecode_markup_renderer', 'rst')
456
457 status_change = status and allowed_to_change_status
433 comment = ChangesetCommentsModel().create(
458 comment = ChangesetCommentsModel().create(
434 text=text,
459 text=text,
435 repo=pull_request.target_repo.repo_id,
460 repo=pull_request.target_repo.repo_id,
@@ -437,10 +462,8 b' def comment_pull_request(request, apiuse'
437 pull_request=pull_request.pull_request_id,
462 pull_request=pull_request.pull_request_id,
438 f_path=None,
463 f_path=None,
439 line_no=None,
464 line_no=None,
440 status_change=(ChangesetStatus.get_status_lbl(status)
465 status_change=(status_label if status_change else None),
441 if status and allowed_to_change_status else None),
466 status_change_type=(status if status_change else None),
442 status_change_type=(status
443 if status and allowed_to_change_status else None),
444 closing_pr=False,
467 closing_pr=False,
445 renderer=renderer
468 renderer=renderer
446 )
469 )
@@ -458,8 +481,8 b' def comment_pull_request(request, apiuse'
458 Session().commit()
481 Session().commit()
459 data = {
482 data = {
460 'pull_request_id': pull_request.pull_request_id,
483 'pull_request_id': pull_request.pull_request_id,
461 'comment_id': comment.comment_id,
484 'comment_id': comment.comment_id if comment else None,
462 'status': status
485 'status': {'given': status, 'was_changed': status_change},
463 }
486 }
464 return data
487 return data
465
488
General Comments 0
You need to be logged in to leave comments. Login now