Skip to content

Commit a3ede33

Browse files
expand issue comments method to include all available API options (#1939)
1 parent f60cf71 commit a3ede33

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

jira/client.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2337,19 +2337,35 @@ def assign_issue(self, issue: int | str, assignee: str | None) -> bool:
23372337
return True
23382338

23392339
@translate_resource_args
2340-
def comments(self, issue: int | str, expand: str | None = None) -> list[Comment]:
2340+
def comments(
2341+
self,
2342+
issue: int | str,
2343+
expand: str | None = None,
2344+
start_at: int | None = None,
2345+
max_results: int | None = None,
2346+
order_by: str | None = None,
2347+
) -> list[Comment]:
23412348
"""Get a list of comment Resources of the issue provided.
23422349
23432350
Args:
23442351
issue (Union[int, str]): the issue ID or key to get the comments from
23452352
expand (Optional[str]): extra information to fetch for each comment such as renderedBody and properties.
2353+
start_at (Optional[int]): index of the first comment to return (page offset)
2354+
max_results (Optional[int]): maximum number of comments to return
2355+
order_by (Optional[str]): order of the comments to return; should be 'created', '+created' or '-created'.
23462356
23472357
Returns:
23482358
List[Comment]
23492359
"""
23502360
params = {}
23512361
if expand is not None:
23522362
params["expand"] = expand
2363+
if start_at is not None:
2364+
params["startAt"] = str(start_at)
2365+
if max_results is not None:
2366+
params["maxResults"] = str(max_results)
2367+
if order_by is not None:
2368+
params["orderBy"] = order_by
23532369
r_json = self._get_json(f"issue/{issue}/comment", params=params)
23542370

23552371
comments = [

tests/resources/test_comment.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,51 @@ def test_comments(self):
2828
comments = self.jira.comments(issue)
2929
assert len(comments) == 0
3030

31+
def test_comments_start_at(self):
32+
comments_created = []
33+
for i in range(10):
34+
comments_created.append(
35+
self.jira.add_comment(self.issue_1_key, f"Comment #{i+1}")
36+
)
37+
comments = self.jira.comments(self.issue_1_key, start_at=5)
38+
self.assertEqual(len(comments), 5)
39+
self.assertEqual(comments[0].body, "Comment #6")
40+
for comment in comments_created:
41+
comment.delete()
42+
comments = self.jira.comments(self.issue_1_key)
43+
assert len(comments) == 0
44+
45+
def test_comments_max_results(self):
46+
comments_created = []
47+
for i in range(10):
48+
comments_created.append(
49+
self.jira.add_comment(self.issue_1_key, f"Comment #{i+1}")
50+
)
51+
comments = self.jira.comments(self.issue_1_key, max_results=4)
52+
self.assertEqual(len(comments), 4)
53+
self.assertEqual(comments[0].body, "Comment #1")
54+
for comment in comments_created:
55+
comment.delete()
56+
comments = self.jira.comments(self.issue_1_key)
57+
assert len(comments) == 0
58+
59+
def test_comments_order_by(self):
60+
comments_created = []
61+
for i in range(10):
62+
comments_created.append(
63+
self.jira.add_comment(self.issue_1_key, f"Comment #{i+1}")
64+
)
65+
comments = self.jira.comments(self.issue_1_key, order_by="created")
66+
self.assertEqual(comments[0].body, "Comment #1")
67+
comments = self.jira.comments(self.issue_1_key, order_by="+created")
68+
self.assertEqual(comments[0].body, "Comment #1")
69+
comments = self.jira.comments(self.issue_1_key, order_by="-created")
70+
self.assertEqual(comments[0].body, "Comment #10")
71+
for comment in comments_created:
72+
comment.delete()
73+
comments = self.jira.comments(self.issue_1_key)
74+
assert len(comments) == 0
75+
3176
def test_expanded_comments(self):
3277
comment1 = self.jira.add_comment(self.issue_1_key, "First comment")
3378
comment2 = self.jira.add_comment(self.issue_1_key, "Second comment")

0 commit comments

Comments
 (0)