Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c47cd4d

Browse files
authored
[BitBucket] Add function to get comments of PR. (atlassian-api#773)
1 parent 0dbae0a commit c47cd4d

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

atlassian/bitbucket/cloud/repositories/pullRequests.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def statuses(self):
203203
204204
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/statuses
205205
"""
206-
return self._get_paged("{}/statuses".format(self.url), absolute=True)
206+
return self._get_paged("statuses")
207207

208208
def participants(self):
209209
"""Returns a generator object of participants"""
@@ -227,6 +227,15 @@ def builds(self):
227227

228228
return
229229

230+
def comments(self):
231+
"""
232+
Returns generator object of the comments endpoint
233+
234+
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/comments#get
235+
"""
236+
for comment in self._get_paged("comments"):
237+
yield Comment(comment, **self._new_session_args)
238+
230239
def comment(self, raw_message):
231240
"""
232241
Commenting the pull request in raw format
@@ -357,6 +366,53 @@ def participated_on(self):
357366
return self.get_time("participated_on")
358367

359368

369+
class Comment(BitbucketCloudBase):
370+
def __init__(self, data, *args, **kwargs):
371+
super(Comment, self).__init__(None, None, *args, data=data, expected_type="pullrequest_comment", **kwargs)
372+
373+
@property
374+
def raw(self):
375+
"""The raw comment"""
376+
return self.get_data("content")["raw"]
377+
378+
@property
379+
def html(self):
380+
"""The html comment"""
381+
return self.get_data("content")["html"]
382+
383+
@property
384+
def markup(self):
385+
"""The markup type"""
386+
return self.get_data("content")["markup"]
387+
388+
@property
389+
def user(self):
390+
"""User object with user information of the comment"""
391+
return User(None, self.get_data("user"), **self._new_session_args)
392+
393+
def update(self, **kwargs):
394+
"""
395+
Update the pullrequest properties. Fields not present in the request body are ignored.
396+
397+
:param kwargs: dict: The data to update.
398+
399+
:return: The updated repository
400+
401+
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/comments/%7Bcomment_id%7D#put
402+
"""
403+
return self._update_data(self.put(None, data=kwargs))
404+
405+
def delete(self):
406+
"""
407+
Delete the pullrequest comment.
408+
409+
:return: The response on success
410+
411+
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pullrequests/%7Bpull_request_id%7D/comments/%7Bcomment_id%7D#delete
412+
"""
413+
return super(Comment, self).delete(None)
414+
415+
360416
class Build(BitbucketCloudBase):
361417
STATE_FAILED = "FAILED"
362418
STATE_INPROGRESS = "INPROGRESS"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
responses[None] = {
2+
"pagelen": 10,
3+
"values": [
4+
{
5+
"links": {
6+
"self": {
7+
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/comments/223684280"
8+
},
9+
"html": {
10+
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/_/diff#comment-223684280"
11+
},
12+
},
13+
"deleted": False,
14+
"pullrequest": {
15+
"type": "pullrequest",
16+
"id": 1,
17+
"links": {
18+
"self": {"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1"},
19+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1/pull-requests/1"},
20+
},
21+
"title": "PRTitle",
22+
},
23+
"content": {
24+
"raw": "Test comment 1",
25+
"markup": "markdown",
26+
"html": "<p>Test comment 1</p>",
27+
"type": "rendered",
28+
},
29+
"created_on": "2021-05-08T21:22:05.651662+00:00",
30+
"user": {
31+
"display_name": "User04DisplayName",
32+
"uuid": "{User04UUID}",
33+
"links": {
34+
"self": {"href": "users/%7BUser04UUID%7D"},
35+
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
36+
"avatar": {
37+
"href": "https://secure.gravatar.com/avatar/46a979e676028e3ec208927d6d537a3f?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FTS-2.png"
38+
},
39+
},
40+
"nickname": "User04Nickname",
41+
"type": "user",
42+
"account_id": "User04AccountID",
43+
},
44+
"updated_on": "2021-05-08T21:22:05.653095+00:00",
45+
"type": "pullrequest_comment",
46+
"id": 223684280,
47+
},
48+
{
49+
"links": {
50+
"self": {
51+
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/comments/223684282"
52+
},
53+
"html": {
54+
"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1/_/diff#comment-223684282"
55+
},
56+
},
57+
"deleted": False,
58+
"pullrequest": {
59+
"type": "pullrequest",
60+
"id": 1,
61+
"links": {
62+
"self": {"href": "bitbucket/cloud/2.0/repositories/TestWorkspace1/testrepository1/pullrequests/1"},
63+
"html": {"href": "https://bitbucket.org/TestWorkspace1/testrepository1/pull-requests/1"},
64+
},
65+
"title": "PRTitle",
66+
},
67+
"content": {
68+
"raw": "Test comment 2",
69+
"markup": "markdown",
70+
"html": "<p>Test comment 2</p>",
71+
"type": "rendered",
72+
},
73+
"created_on": "2021-05-08T21:22:12.946004+00:00",
74+
"user": {
75+
"display_name": "User04DisplayName",
76+
"uuid": "{User04UUID}",
77+
"links": {
78+
"self": {"href": "users/%7BUser04UUID%7D"},
79+
"html": {"href": "https://bitbucket.org/%7BUser04UUID%7D/"},
80+
"avatar": {
81+
"href": "https://secure.gravatar.com/avatar/46a979e676028e3ec208927d6d537a3f?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FTS-2.png"
82+
},
83+
},
84+
"nickname": "User04Nickname",
85+
"type": "user",
86+
"account_id": "User04AccountID",
87+
},
88+
"updated_on": "2021-05-08T21:22:12.947344+00:00",
89+
"type": "pullrequest_comment",
90+
"id": 223684282,
91+
},
92+
],
93+
"page": 1,
94+
"size": 2,
95+
}

tests/test_bitbucket_cloud.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from atlassian import Bitbucket
77
from atlassian.bitbucket import Cloud
88
from atlassian.bitbucket.cloud.common.users import User
9-
from atlassian.bitbucket.cloud.repositories.pullRequests import Participant, PullRequest, Build
9+
from atlassian.bitbucket.cloud.repositories.pullRequests import Comment, Participant, PullRequest, Build
1010

1111
BITBUCKET = None
1212
try:
@@ -257,6 +257,17 @@ def test_comment(self, tc1):
257257
assert com["pullrequest"]["id"] == 1
258258
assert not com["deleted"]
259259

260+
def test_comments(self, tc1):
261+
comments = list(tc1.comments())
262+
assert len(comments) == 2
263+
c1, c2 = comments
264+
assert isinstance(c1, Comment)
265+
assert isinstance(c2, Comment)
266+
assert c1.raw == "Test comment 1"
267+
assert isinstance(c1.user, User)
268+
assert c1.user.display_name == "User04DisplayName"
269+
assert c2.html == "<p>Test comment 2</p>"
270+
260271
def test_approve(self, tc1):
261272
ap = tc1.approve()
262273
assert ap["approved"]

0 commit comments

Comments
 (0)