How to get comments from Jira using Python

Jira, a popular project management and ticketing platform, presents users with robust APIs, which provide the ability to engage with the platform in a programmatic manner. 

This opens up the potential for automating various tasks—and pulling comments from tickets might be at the top of your list.

Pulling comments and adding them to other applications can help your teams stay on top of customer feedback, track issues, and monitor progress in real-time, leading to faster and more effective responses. 

To help you get comments from Jira, we’ll walk through how you can set up authentication for the Jira API and make get requests in Python.

Authenticating with Jira

Before you can pull comments from the Jira API, you'll need to authenticate your requests. 

To do this, you’ll need to include an `Authorization` header in the format `Authorization: Basic {Email Address}:{API-KEY}` in your request. 

Using this format, `{Email Address}` should be replaced with the email address you use for your Jira account, and `{API-KEY}` should be replaced with your Jira API key. Both of these values should be encoded in Base64.

Related: What you need to do to get attachments from the Jira API

Pulling comments from Jira

To pull comments from the Jira API via Python, you’ll need to make two separate API calls. One to fetch the ticket ids and then another to fetch the comments related to those tickets. 

The following script first fetches the ticket ids by sending a GET request to the Jira search API. It then iterates over the returned ticket ids to fetch the comments related to each ticket by sending a GET request to the Jira issue comment API. The comments are then printed to the console.

```python import requests import base64 import json # Authentication email = 'YOUR_EMAIL' api_key = 'YOUR_API_KEY' domain = 'YOUR_DOMAIN' auth_str = '{}:{}'.format(email, api_key) base64_auth_str = base64.b64encode(auth_str.encode()).decode() headers = { 'Authorization': 'Basic {}'.format(base64_auth_str), 'Accept': 'application/json' } # Fetch tickets url = 'https://{}.atlassian.net/rest/api/3/search'.format(domain) query = {'fields': 'comment', 'maxResults': 50, 'startAt': 0} response = requests.get(url, headers=headers, params=query) tickets = json.loads(response.text)['issues'] # Fetch comments for ticket in tickets: ticket_id = ticket['id'] url = 'https://{}.atlassian.net/rest/api/3/issue/{}/comment'.format(domain, ticket_id) query = {'expand': 'renderedBody', 'maxResults': 50, 'startAt': 0} response = requests.get(url, headers=headers, params=query) comments = json.loads(response.text)['comments'] for comment in comments: print(comment['body']) ```

Replace `'YOUR_EMAIL'`, `'YOUR_API_KEY'`, and `'YOUR_DOMAIN'` with your actual email, API key, and domain associated with your Jira account. The `maxResults` query parameter is set to 50 as per the instructions, and `startAt` is initially set to 0 and should be incremented by 50 for each subsequent request to handle pagination.

Here’s an example of an individual item returned by this API endpoint:

{
    "self": "https://jira.example.com/rest/api/2/issue/10001/comment/1",
    "id": "1",
    "author": {
        "self": "https://jira.example.com/rest/api/2/user?username=jsmith",
        "accountId": "5b109f2e9729b51fdf808842",
        "emailAddress": "jsmith@example.com",
        "avatarUrls": {
            "48x48": "https://avatar.example.com/user/avatar/48",
            "24x24": "https://avatar.example.com/user/avatar/24",
            "16x16": "https://avatar.example.com/user/avatar/16",
            "32x32": "https://avatar.example.com/user/avatar/32"
        },
        "displayName": "John Smith",
        "active": true,
        "timeZone": "America/Los_Angeles",
        "accountType": "atlassian"
    },
    "body": {
        "version": 1,
        "type": "doc",
        "content": [
            {
                "type": "paragraph",
                "content": [
                    {
                        "type": "text",
                        "text": "This is a comment on the issue"
                    }
                ]
            }
        ]
    },
    "renderedBody": "

This is a comment on the issue

", "updateAuthor": { "self": "https://jira.example.com/rest/api/2/user?username=jsmith", "accountId": "5b109f2e9729b51fdf808842", "emailAddress": "jsmith@example.com", "avatarUrls": { "48x48": "https://avatar.example.com/user/avatar/48", "24x24": "https://avatar.example.com/user/avatar/24", "16x16": "https://avatar.example.com/user/avatar/16", "32x32": "https://avatar.example.com/user/avatar/32" }, "displayName": "John Smith", "active": true, "timeZone": "America/Los_Angeles", "accountType": "atlassian" }, "created": "2019-05-15T14:00:00.000+0000", "updated": "2019-05-15T14:00:00.000+0000", "jsdPublic": true }

Related: The steps for fetching projects from the Jira API

Best practices for testing your Jira integration

Even when everything seems perfect with your Jira integration in the development stage, unexpected challenges can emerge when it's deployed in a production environment. These challenges could vary widely, from connectivity issues to permission conflicts to unexpected data formats.

To proactively identify and address these potential pitfalls, consider implementing these strategies:

  • Set up a testing environment. Creating a dedicated testing environment is crucial to mimic real-world scenarios without affecting your production system. You can set this up by duplicating your production environment, ensuring a realistic and controlled testing platform.
  • Perform a variety of test cases: It’s essential to cover all bases by testing various scenarios, including edge cases and typical user behaviors. For instance, test how the integration handles large data sets or reacts to invalid input.
  • Leverage API testing solutions: Integrating robust API testing tools is key to ensuring smooth operation. Explore options like Postman or SoapUI, which are popular for their comprehensive testing capabilities.
  • Automate the tests: Automation not only saves time but also ensures consistency in testing. Utilize scripting or tools like Selenium to automate your test cases, which allows for frequent and efficient testing cycles.
  • Test for security and compliance: Ensuring that your integration adheres to security standards and compliance regulations is non-negotiable. Regularly test for vulnerabilities and compliance to safeguard your data and operations.

Final thoughts

Your clients might utilize ticketing systems that extend beyond Jira.

You have the flexibility to integrate with numerous other ticketing platforms, such as Asana, Zendesk, ClickUp, ServiceNow, by building to Merge’s Ticketing Unified API

Learn more about Merge’s Ticketing Unified API, the platform’s management and maintenance capabilities, and much more by scheduling a demo with our team of integration specialists.