How to Get Issue Comments in Jira With Python

In this short guide, I’ll show you how to get issue comments in Jira with Python API.

In the next section, I’ll review the steps to connect to Jira, filter tickets and get comments from those issues.

Step 1: Connect to Jira with Python

To connect to Jira with Python you need to things:

  • install Python library - jira
  • generate Jira token

To** install the Python jira library** you can use the next syntax:

pip install jira

To generate Jira token you can follow the next steps:

  • Open Jira api-tokens and login
  • Click Create API token.
  • Enter a Label for your token
  • Create.
  • Click Copy to clipboard
  • Use the token to your script

Note: it's not possible to retrieve the token later - so saved it somewhere if you needed.

Note 2: Older token might cause errors or not work at all. It's a good idea to test with a new Jira token in case of errors.

Finally you can connect to Jira in your Python code by:

import pandas as pd
from jira import JIRA

jira = JIRA({"server": "https://xxx.atlassian.net/"}, basic_auth=("xxx@xxx.com", "xxxx_token_xxxx"))

where:

  • https://xxx.atlassian.net/ - is the target Jira server
  • xxx@xxx.com - is your email or ID
  • "xxxx_token_xxxx" - the above generated token

Step 2: Filter issues with Python and JQL

In this step we are going to list all tickets with JQL. Let's say that we are interested in New Tickets without Assignee.

We are going use method jira.search_issues and setup max results to 100:

new_issues = jira.search_issues("project = 'Project_X' and status = 'New' and assignee is EMPTY ORDER BY created DESC", maxResults=100)

This will return Jira issues objects which we will use in the next steps.

Step 3: Get issue comments from Jira

To get the comments we will iterate through all issues and get the additional information by:

for issue in new_issues:
    comment_dict = {}
    print(issue.key)
    issue_num = issue.key

    issue = jira.issue(issue_num)

    comments =  issue.fields.comment.comments

The comment information will be extracted with the following information:

  • comment.body - the comment text
  • comment.author.displayName - the comment author
  • comment.created - the creation date
    for comment in comments:
        print("Comment text : ",comment.body)
        print("Comment author : ",comment.author.displayName)
        print("Comment time : ",comment.created)

        comment_dict['author'] = comment.author.displayName
        comment_dict['body'] = comment.body
        comment_dict['date'] = comment.created
        comment_dict['key'] = issue.key
        df_comments.append(comment_dict)
        
pd.DataFrame(df_comments).to_csv('jira_comments.csv')        

Finally the code will save all the comments and authors to a CSV file.

Step 4: Full code: get Jira comments with Python

Finally let's read the export CSV file and analyze the Jira comments with Pandas:

import pandas as pd
df = pd.read_csv('jira_comments.csv')

df['date'] = pd.to_datetime(df['date'])

df.tail(20)

In order to extract links with regex from the comments we can use the next syntax:

df['link'] = df['body'].str.extract("\[https://(.*)\|.*\]")

To find the latest link shared in a Jira comment with Pandas we can do:

df.groupby('key').link.max().to_dict()

Step 5: Full code: get Jira comments with Python

You can find the full code for comment extraction from Jira below:

import pandas as pd
from jira import JIRA

jira = JIRA({"server": "https://xxx.atlassian.net/"}, basic_auth=("xxx@xxx.com", "xxxx"))


new_issues = jira.search_issues("project = 'Project_X' and status = 'New' and assignee is EMPTY ORDER BY created DESC", maxResults=100)

df_comments = []

for issue in new_issues:
    comment_dict = {}
    print(issue.key)
    issue_num = issue.key

    issue = jira.issue(issue_num)

    comments =  issue.fields.comment.comments


    for comment in comments:
        print("Comment text : ",comment.body)
        print("Comment author : ",comment.author.displayName)
        print("Comment time : ",comment.created)

        comment_dict['author'] = comment.author.displayName
        comment_dict['body'] = comment.body
        comment_dict['date'] = comment.created
        comment_dict['key'] = issue.key
        df_comments.append(comment_dict)

pd.DataFrame(df_comments).to_csv('jira_comments.csv')