Show More
@@ -0,0 +1,82 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | ||||
|
3 | # Copyright (C) 2010-2017 RhodeCode GmbH | |||
|
4 | # | |||
|
5 | # This program is free software: you can redistribute it and/or modify | |||
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |||
|
7 | # (only), as published by the Free Software Foundation. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
14 | # You should have received a copy of the GNU Affero General Public License | |||
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
|
16 | # | |||
|
17 | # This program is dual-licensed. If you wish to learn more about the | |||
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |||
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
|
20 | ||||
|
21 | ||||
|
22 | import pytest | |||
|
23 | import urlobject | |||
|
24 | ||||
|
25 | from rhodecode.api.tests.utils import ( | |||
|
26 | build_data, api_call, assert_error, assert_ok) | |||
|
27 | from rhodecode.lib import helpers as h | |||
|
28 | from rhodecode.lib.utils2 import safe_unicode | |||
|
29 | ||||
|
30 | pytestmark = pytest.mark.backends("git", "hg") | |||
|
31 | ||||
|
32 | ||||
|
33 | @pytest.mark.usefixtures("testuser_api", "app") | |||
|
34 | class TestGetPullRequestComments(object): | |||
|
35 | ||||
|
36 | def test_api_get_pull_request_comments(self, pr_util, http_host_only_stub): | |||
|
37 | from rhodecode.model.pull_request import PullRequestModel | |||
|
38 | ||||
|
39 | pull_request = pr_util.create_pull_request(mergeable=True) | |||
|
40 | id_, params = build_data( | |||
|
41 | self.apikey, 'get_pull_request_comments', | |||
|
42 | pullrequestid=pull_request.pull_request_id) | |||
|
43 | ||||
|
44 | response = api_call(self.app, params) | |||
|
45 | ||||
|
46 | assert response.status == '200 OK' | |||
|
47 | resp_date = response.json['result'][0]['comment_created_on'] | |||
|
48 | resp_comment_id = response.json['result'][0]['comment_id'] | |||
|
49 | ||||
|
50 | expected = [ | |||
|
51 | {'comment_author': {'active': True, | |||
|
52 | 'full_name_or_username': 'RhodeCode Admin', | |||
|
53 | 'username': 'test_admin'}, | |||
|
54 | 'comment_created_on': resp_date, | |||
|
55 | 'comment_f_path': None, | |||
|
56 | 'comment_id': resp_comment_id, | |||
|
57 | 'comment_lineno': None, | |||
|
58 | 'comment_status': {'status': 'under_review', | |||
|
59 | 'status_lbl': 'Under Review'}, | |||
|
60 | 'comment_text': 'Auto status change to |new_status|\n\n.. |new_status| replace:: *"Under Review"*', | |||
|
61 | 'comment_type': 'note', | |||
|
62 | 'pull_request_version': None} | |||
|
63 | ] | |||
|
64 | assert_ok(id_, expected, response.body) | |||
|
65 | ||||
|
66 | def test_api_get_pull_request_comments_repo_error(self, pr_util): | |||
|
67 | pull_request = pr_util.create_pull_request() | |||
|
68 | id_, params = build_data( | |||
|
69 | self.apikey, 'get_pull_request_comments', | |||
|
70 | repoid=666, pullrequestid=pull_request.pull_request_id) | |||
|
71 | response = api_call(self.app, params) | |||
|
72 | ||||
|
73 | expected = 'repository `666` does not exist' | |||
|
74 | assert_error(id_, expected, given=response.body) | |||
|
75 | ||||
|
76 | def test_api_get_pull_request_comments_pull_request_error(self): | |||
|
77 | id_, params = build_data( | |||
|
78 | self.apikey, 'get_pull_request_comments', pullrequestid=666) | |||
|
79 | response = api_call(self.app, params) | |||
|
80 | ||||
|
81 | expected = 'pull request `666` does not exist' | |||
|
82 | assert_error(id_, expected, given=response.body) |
@@ -311,6 +311,109 b' def merge_pull_request(' | |||||
311 |
|
311 | |||
312 |
|
312 | |||
313 | @jsonrpc_method() |
|
313 | @jsonrpc_method() | |
|
314 | def get_pull_request_comments( | |||
|
315 | request, apiuser, pullrequestid, repoid=Optional(None)): | |||
|
316 | """ | |||
|
317 | Get all comments of pull request specified with the `pullrequestid` | |||
|
318 | ||||
|
319 | :param apiuser: This is filled automatically from the |authtoken|. | |||
|
320 | :type apiuser: AuthUser | |||
|
321 | :param repoid: Optional repository name or repository ID. | |||
|
322 | :type repoid: str or int | |||
|
323 | :param pullrequestid: The pull request ID. | |||
|
324 | :type pullrequestid: int | |||
|
325 | ||||
|
326 | Example output: | |||
|
327 | ||||
|
328 | .. code-block:: bash | |||
|
329 | ||||
|
330 | id : <id_given_in_input> | |||
|
331 | result : [ | |||
|
332 | { | |||
|
333 | "comment_author": { | |||
|
334 | "active": true, | |||
|
335 | "full_name_or_username": "Tom Gore", | |||
|
336 | "username": "admin" | |||
|
337 | }, | |||
|
338 | "comment_created_on": "2017-01-02T18:43:45.533", | |||
|
339 | "comment_f_path": null, | |||
|
340 | "comment_id": 25, | |||
|
341 | "comment_lineno": null, | |||
|
342 | "comment_status": { | |||
|
343 | "status": "under_review", | |||
|
344 | "status_lbl": "Under Review" | |||
|
345 | }, | |||
|
346 | "comment_text": "Example text", | |||
|
347 | "comment_type": null, | |||
|
348 | "pull_request_version": null | |||
|
349 | } | |||
|
350 | ], | |||
|
351 | error : null | |||
|
352 | """ | |||
|
353 | ||||
|
354 | pull_request = get_pull_request_or_error(pullrequestid) | |||
|
355 | if Optional.extract(repoid): | |||
|
356 | repo = get_repo_or_error(repoid) | |||
|
357 | else: | |||
|
358 | repo = pull_request.target_repo | |||
|
359 | ||||
|
360 | if not PullRequestModel().check_user_read( | |||
|
361 | pull_request, apiuser, api=True): | |||
|
362 | raise JSONRPCError('repository `%s` or pull request `%s` ' | |||
|
363 | 'does not exist' % (repoid, pullrequestid)) | |||
|
364 | ||||
|
365 | (pull_request_latest, | |||
|
366 | pull_request_at_ver, | |||
|
367 | pull_request_display_obj, | |||
|
368 | at_version) = PullRequestModel().get_pr_version( | |||
|
369 | pull_request.pull_request_id, version=None) | |||
|
370 | ||||
|
371 | versions = pull_request_display_obj.versions() | |||
|
372 | ver_map = { | |||
|
373 | ver.pull_request_version_id: cnt | |||
|
374 | for cnt, ver in enumerate(versions, 1) | |||
|
375 | } | |||
|
376 | ||||
|
377 | # GENERAL COMMENTS with versions # | |||
|
378 | q = CommentsModel()._all_general_comments_of_pull_request(pull_request) | |||
|
379 | q = q.order_by(ChangesetComment.comment_id.asc()) | |||
|
380 | general_comments = q.all() | |||
|
381 | ||||
|
382 | # INLINE COMMENTS with versions # | |||
|
383 | q = CommentsModel()._all_inline_comments_of_pull_request(pull_request) | |||
|
384 | q = q.order_by(ChangesetComment.comment_id.asc()) | |||
|
385 | inline_comments = q.all() | |||
|
386 | ||||
|
387 | data = [] | |||
|
388 | for comment in inline_comments + general_comments: | |||
|
389 | full_data = comment.get_api_data() | |||
|
390 | pr_version_id = None | |||
|
391 | if comment.pull_request_version_id: | |||
|
392 | pr_version_id = 'v{}'.format( | |||
|
393 | ver_map[comment.pull_request_version_id]) | |||
|
394 | ||||
|
395 | # sanitize some entries | |||
|
396 | ||||
|
397 | full_data['pull_request_version'] = pr_version_id | |||
|
398 | full_data['comment_author'] = { | |||
|
399 | 'username': full_data['comment_author'].username, | |||
|
400 | 'full_name_or_username': full_data['comment_author'].full_name_or_username, | |||
|
401 | 'active': full_data['comment_author'].active, | |||
|
402 | } | |||
|
403 | ||||
|
404 | if full_data['comment_status']: | |||
|
405 | full_data['comment_status'] = { | |||
|
406 | 'status': full_data['comment_status'][0].status, | |||
|
407 | 'status_lbl': full_data['comment_status'][0].status_lbl, | |||
|
408 | } | |||
|
409 | else: | |||
|
410 | full_data['comment_status'] = {} | |||
|
411 | ||||
|
412 | data.append(full_data) | |||
|
413 | return data | |||
|
414 | ||||
|
415 | ||||
|
416 | @jsonrpc_method() | |||
314 | def comment_pull_request( |
|
417 | def comment_pull_request( | |
315 | request, apiuser, repoid, pullrequestid, message=Optional(None), |
|
418 | request, apiuser, repoid, pullrequestid, message=Optional(None), | |
316 | commit_id=Optional(None), status=Optional(None), |
|
419 | commit_id=Optional(None), status=Optional(None), |
General Comments 0
You need to be logged in to leave comments.
Login now