Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jira: Add functionality to get comments on an issue #1064

Merged
merged 5 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions atlassian/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,46 @@ def get_cluster_zdu_state(self):
url = self.resource_url("cluster/zdu/state")
return self.get(url)

# Issue Comments
def issue_get_comments(self, issue_id):
"""
Get Comments on an Issue
:param issue_id: Issue ID
:raises: requests.exceptions.HTTPError
:return:
"""
base_url = self.resource_url("issue")
url = "{base_url}/{issue_id}/comment".format(base_url=base_url, issue_id=issue_id)
return self.get(url)

def issues_get_comments_by_id(self, *args):
"""
Get Comments on Multiple Issues
:param *args: int Issue ID's
:raises: requests.exceptions.HTTPError
:return:
"""
if not all([isinstance(i, int) for i in args]):
raise TypeError("Arguments to `issues_get_comments_by_id` must be int")
data = {"ids": list(args)}
base_url = self.resource_url("comment")
url = "{base_url}/list".format(base_url=base_url)
return self.post(url, data=data)

def issue_get_comment(self, issue_id, comment_id):
"""
Get a single comment
:param issue_id: int or str
:param comment_id: int
:raises: requests.exceptions.HTTPError
:return:
"""
base_url = self.resource_url("issue")
url = "{base_url}/{issue_id}/comment/{comment_id}".format(
base_url=base_url, issue_id=issue_id, comment_id=comment_id
)
return self.get(url)

"""
Comments properties
Reference: https://docs.atlassian.com/software/jira/docs/api/REST/8.5.0/#api/2/comment/{commentId}/properties
Expand Down
8 changes: 8 additions & 0 deletions docs/jira.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ Manage issues
# Restore an issue
issue_restore(issue_id_or_key)

# Issue Comments
jira.issue_get_comments(issue_id_or_key)

jira.issue_get_comment(issue_id_or_key, comment_id)

jira.issues_get_comments_by_id(comment_id, [comment_id...])


Epic Issues
-------------

Expand Down
24 changes: 24 additions & 0 deletions examples/jira/jira_get_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
""" Example: Get Comments on a Jira Issue
"""
# coding=utf-8
from atlassian import Jira


def main():
jira = Jira(url="https://atlassian-python.atlassian.net/", username="martyn.bristow@gmail.com", password="...")

print("\n*Get Comments on INT-1*")
comments = jira.issue_get_comments("INT-1")
print(comments)

print("\n*Get a single comment on INT-1*")
comments = jira.issue_get_comment(10000, 10001)
print(comments)

print("\n*Get a set of comments*")
comments = jira.issues_get_comments_by_id(10000, 10002)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a suggestion for the naming of function,
keeping in line with the single comment function naming convention,
could we keep multiple comments for the single issue named as,
issue_get_comments_selective rather than issues_get_comments_by_id
to help us understand that it is multiple selected comments for single issue.
Or some other better name? because i believe the filtering would always be based on the comment_id so keeping by_id mite be redundant here.

because it would always be comments drawn for the single issue and not multiple one if i understand correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm happy to change it to this. My only reason for choosing: issues_get_comments_by_id was to be consistent with the Atlassian JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-comment-list-post

issues_get_comments_by_ids is still a bit weak, so I'll consider selective unless I have an idea

I also chose: issues_get_comments rather than get_issue_comments to match some existing code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds fine then, looks good to me for merge, thanks!

print(comments)


if __name__ == "__main__":
main()
84 changes: 84 additions & 0 deletions tests/responses/jira/rest/api/2/comment/list/POST
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
responses["fields=*all"] = {
"maxResults": 1048576,
"startAt": 0,
"total": 2,
"isLast": true,
"values": [
{
"self": "https://atlassian-python.atlassian.net/rest/api/2/issue/10000/comment/10000",
"id": "10000",
"author": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"body": "Some Text comment",
"updateAuthor": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"created": "2022-10-25T20:21:33.277+0100",
"updated": "2022-10-25T20:21:33.277+0100",
"jsdPublic": true,
},
{
"self": "https://atlassian-python.atlassian.net/rest/api/2/issue/10000/comment/10001",
"id": "10001",
"author": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"body": "Second Comment",
"updateAuthor": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"created": "2022-10-25T20:21:40.840+0100",
"updated": "2022-10-25T20:21:40.840+0100",
"jsdPublic": true,
},
],
}
38 changes: 38 additions & 0 deletions tests/responses/jira/rest/api/2/issue/FOO-123/comment/10000/GET
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
responses[None] = {
"self": "https://atlassian-python.atlassian.net/rest/api/2/issue/10000/comment/10000",
"id": "10000",
"author": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"body": "Some Text comment",
"updateAuthor": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"created": "2022-10-25T20:21:33.277+0100",
"updated": "2022-10-25T20:21:33.277+0100",
"jsdPublic": true,
}
83 changes: 83 additions & 0 deletions tests/responses/jira/rest/api/2/issue/FOO-123/comment/GET
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
responses[None] = {
"startAt": 0,
"maxResults": 1048576,
"total": 2,
"comments": [
{
"self": "https://atlassian-python.atlassian.net/rest/api/2/issue/10000/comment/10000",
"id": "10000",
"author": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"body": "Some Text comment",
"updateAuthor": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"created": "2022-10-25T20:21:33.277+0100",
"updated": "2022-10-25T20:21:33.277+0100",
"jsdPublic": true,
},
{
"self": "https://atlassian-python.atlassian.net/rest/api/2/issue/10000/comment/10001",
"id": "10001",
"author": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"body": "Second Comment",
"updateAuthor": {
"self": "https://atlassian-python.atlassian.net/rest/api/2/user?accountId=557058%3Adc675182-d776-46f5-90fb-fe5637b71397",
"accountId": "557058:dc675182-d776-46f5-90fb-fe5637b71397",
"emailAddress": "martyn.bristow@gmail.com",
"avatarUrls": {
"48x48": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"24x24": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"16x16": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
"32x32": "https://secure.gravatar.com/avatar/6f1cc3b97033de549f9d156d0eea4a34?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FMB-4.png",
},
"displayName": "Martyn Bristow",
"active": true,
"timeZone": "Europe/London",
"accountType": "atlassian",
},
"created": "2022-10-25T20:21:40.840+0100",
"updated": "2022-10-25T20:21:40.840+0100",
"jsdPublic": true,
},
],
}
17 changes: 17 additions & 0 deletions tests/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ def test_get_epic_issues(self):
def test_get_epic_issues_not_found(self):
with self.assertRaises(HTTPError):
self.jira.epic_issues("BAR-11")

def test_get_issue_comments(self):
"""Can retrieve issue comments"""
resp = self.jira.issue_get_comments("FOO-123")
self.assertEqual(len(resp["comments"]), 2)
self.assertEqual(resp["total"], 2)

def test_get_issue_comment(self):
"""Can retrieve issue comments"""
resp = self.jira.issue_get_comment("FOO-123", 10000)
self.assertEqual(resp["body"], "Some Text comment")
self.assertEqual(resp["id"], "10000")

def test_get_issue_comment_not_found(self):
"""Get comment on issue by id, but not found"""
with self.assertRaises(HTTPError):
self.jira.epic_issues("BAR-11")