Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to fetch data from Jira in Python?

How to fetch data from Jira in Python?

Jira is an agile, project management tool, developed by Atlassian, primarily used for, tracking project bugs, and, issues. It has gradually developed, into a powerful, work management tool, that can handle, all stages of agile methodology. In this article, we will learn, how to fetch data, from Jira, using Python.

There are two ways to get the data:

  1. Using JIRA library for Python.
  2. Using the JIRA Rest API.

The configuration, required, in the Jira software tool, is as follows:

  1. Create a Jira user account.
  2. Create a domain name, add a project, and, record some issues, or, bugs. Our task is to fetch, issues data, using Python code.
  3. We need to have, a valid token, for authentication, which can be obtained, from link  https://id.atlassian.com/manage/api-tokens.

Issues recorded in JIRA tool for project “MedicineAppBugs”

JQL: JQL stands for Jira Query Language. It is an efficient way, of fetching, JIRA-related data. It can be used, in both, JIRA library, and, API approach, for obtaining data. This involves, forming queries, to filter information, regarding, relevant Bugs, Projects, Issues etc. One can use, combinations, of different operators, and, keywords, in the query.

Fetch data using Jira library for Python

JIRA, is a Python library, for connecting, with the JIRA tool. This library is easy to use, as compared, to the API method, for fetching data, related to Issues, Projects, Worklogs etc. The library, requires, a Python version, greater than 3.5.

Install jira using the command:

pip install jira

Approach:

  • Import the jira module.
  • Construct, a Jira client instance, using the following –
    • The server key, which is your domain name, is created on Atlassian account.
    • Basic authentication parameters, your registered emailID, and, the unique token received.
  • Get a JIRA client instance bypassing, Authentication parameters.
  • Search all issues mentioned against a project name(Display the details, like Issue Key, Summary, Reporter Name, using the print statement.).

Below is the implementation:

Python




# import the installed Jira library
from jira import JIRA
  
# Specify a server key. It should be your
# domain name link. yourdomainname.atlassian.net
jiraOptions = {'server': "https://txxxxxxpython.atlassian.net"}
  
# Get a JIRA client instance, pass,
# Authentication parameters
# and the Server name.
# emailID = your emailID
# token = token you receive after registration
jira = JIRA(options=jiraOptions, basic_auth=(
    "prxxxxxxh@gmail.com", "bj9xxxxxxxxxxxxxxxxxxx5A"))
  
# Search all issues mentioned against a project name.
for singleIssue in jira.search_issues(jql_str='project = MedicineAppBugs'):
    print('{}: {}:{}'.format(singleIssue.key, singleIssue.fields.summary,
                             singleIssue.fields.reporter.displayName))


Output:

Issues data output using JIRA library.

Using the Jira library, we can, also, fetch details, of a single issue.

 The Key is a unique ID, of the Issue, details of which, we require. It is obtained, after adding an Issue, for a project, on the platform, while fetching details of a single issue, pass its UniqueID or Key.

Python




# import the installed Jira library
from jira import JIRA
  
# Specify a server key. It is your  domain 
# name link.
jiraOptions = {'server': "https://txxxxxxpython.atlassian.net"}
  
# Get a JIRA client instance, Pass 
# Authentication parameters
# and  Server name.
# emailID = your emailID
# token = token you receive after registration
jira = JIRA(options = jiraOptions, 
            basic_auth = ("prxxxxxxh@gmail.com",
                          "bj9xxxxxxxxxxxxxxxxxxx5A"))
  
# While fetching details of a single issue,
# pass its UniqueID or Key.
singleIssue = jira.issue('MED-1')
print('{}: {}:{}'.format(singleIssue.key,
                         singleIssue.fields.summary,
                         singleIssue.fields.reporter.displayName))


Output:

Details of one issue using the JIRA library

 Fetch data using Jira Rest API

The JIRA server platform, provides the REST API, for issues and workflows. It allows us, to perform, CRUD operations, on Issues, Groups, Dashboards, etc. The developer platform, for Jira Rest API, is well-documented and can be referred to at https://developer.atlassian.com/cloud/jira/platform/rest/v2/intro/. Based on our requirement, we need to look, for the specific URI, on the platform. In, the code snippet below, we are fetching, all Issues, present, against, our mentioned project ‘MedicineAppBugs’. 

Python libraries required:

  1. Library JSON is available in python distribution package.
  2. Install requests using command – pip install requests.
  3. Install pandas using command – pip install pandas.

Get API link

  • Visit the developer API.
  • One can find, a wide range, of API options, available, for developers, on the left panel. For instance, there are APIs, for performing CRUD operations, on “Users” , “Projects” apart from Issues.
  • In this article, we are fetching, all issues, hence, we will select the option, “Issue search”. We will select, sub-option, “Search for issues using JQL(GET)” method.
  • On selecting this option, the URI  “GET /rest/api/2/search” , is displayed, along with, the format, of request parameters, permitted.
  • Append the above link, with your domain name as – “https://your-domain.atlassian.net/rest/api/2/search”. This final URL, will help, to fetch, all Issues, against our project.

Approach:

  • Import the required modules.
  • Prepare URL, to search, all issues.
  • Create an authentication object, using registered emailID, and, token received.
  • Pass the project name, in, JQL query. If you, omit JQL query, then, Issues, present, against all projects, on your domain, are obtained.
  • Create and send, a request object, using authentication, header objects, and, JQL query.
  • Use, JSON loads method, to convert the JSON response, into a Python dictionary object.
  • All Issues are present, as list elements, against the key ‘issues’, in the main API output. Hence, loop through each element.
  • As, a single Issue, individually, is a further, nested dictionary object, use “iterateDictIssues” function, to get the required keys.
  • Finally, append the output list, to a Pandas’ data frame, and, display it.

Note: Please study, the API output carefully, to check the placement, and, type, of fields, you require. They can be, nested dictionaries, or list objects, and, one needs to decide, the function logic, accordingly.

Below is the implementation:

Python




# Import the required libraries
import requests
from requests.auth import HTTPBasicAuth
import json
import pandas as pd
  
# URL to Search all issues.
  
# Create an authentication object,using
# registered emailID, and, token received.
auth = HTTPBasicAuth("prxxxxxxh@gmail.com",
                     "bj9xxxxxxxxxxxxxxxxxxx5A")
  
# The Header parameter, should mention, the
# desired format of data.
headers = {
    "Accept": "application/json"
}
# Mention the JQL query.
# Here, all issues, of a project, are
# fetched,as,no criteria is mentioned.
query = {
    'jql': 'project =MedicineAppBugs '
}
  
# Create a request object with above parameters.
response = requests.request(
    "GET",
    url,
    headers=headers,
    auth=auth,
    params=query
)
  
# Get all project issues,by using the
# json loads method.
projectIssues = json.dumps(json.loads(response.text),
                           sort_keys=True,
                           indent=4,
                           separators=(",", ": "))
  
# The JSON response received, using
# the requests object,
# is an intricate nested object.
# Convert the output to a dictionary object.
dictProjectIssues = json.loads(projectIssues)
  
# We will append,all issues,in a list object.
listAllIssues = []
  
# The Issue details, we are interested in,
# are "Key" , "Summary" and "Reporter Name"
keyIssue, keySummary, keyReporter = "", "", ""
  
  
def iterateDictIssues(oIssues, listInner):
  
    # Now,the details for each Issue, maybe
    # directly accessible, or present further,
    # in nested dictionary objects.
    for key, values in oIssues.items():
  
        # If key is 'fields', get its value,
        # to fetch the 'summary' of issue.
        if(key == "fields"):
  
            # Since type of object is Json str,
            # convert to dictionary object.
            fieldsDict = dict(values)
  
            # The 'summary' field, we want, is 
            # present in, further,nested dictionary
            # object. Hence,recursive call to 
            # function 'iterateDictIssues'.
            iterateDictIssues(fieldsDict, listInner)
  
        # If key is 'reporter',get its value,
        # to fetch the 'reporter name' of issue.
        elif (key == "reporter"):
  
            # Since type of object is Json str 
            # convert to dictionary object.
            reporterDict = dict(values)
  
            # The 'displayName', we want,is present
            # in,further, nested dictionary object.
            # Hence,recursive call to function 'iterateDictIssues'.
            iterateDictIssues(reporterDict, listInner)
  
        # Issue keyID 'key' is directly accessible.
        # Get the value of key "key" and append
        # to temporary list object.
        elif(key == 'key'):
            keyIssue = values
            listInner.append(keyIssue)
  
        # Get the value of key "summary",and,
        # append to temporary list object, once matched.
        elif(key == 'summary'):
            keySummary = values
            listInner.append(keySummary)
  
        # Get the value of key "displayName",and,
        # append to temporary list object,once matched.
        elif(key == "displayName"):
            keyReporter = values
            listInner.append(keyReporter)
  
  
# Iterate through the API output and look
# for key 'issues'.
for key, value in dictProjectIssues.items():
  
    # Issues fetched, are present as list elements,
    # against the key "issues".
    if(key == "issues"):
  
        # Get the total number of issues present
        # for our project.
        totalIssues = len(value)
  
        # Iterate through each issue,and,
        # extract needed details-Key, Summary,
        # Reporter Name.
        for eachIssue in range(totalIssues):
            listInner = []
  
            # Issues related data,is nested 
            # dictionary object.
            iterateDictIssues(value[eachIssue], listInner)
  
            # We append, the temporary list fields,
            # to a final list.
            listAllIssues.append(listInner)
  
# Prepare a dataframe object,with the final 
# list of values fetched.
dfIssues = pd.DataFrame(listAllIssues, columns=["Reporter",
                                                "Summary",
                                                "Key"])
  
# Reframing the columns to get proper 
# sequence in output.
columnTiles = ["Key", "Summary", "Reporter"]
dfIssues = dfIssues.reindex(columns=columnTiles)
print(dfIssues)


Output:

Issues received from JIRA tool using JIRA REST API in python code

RELATED ARTICLES

Most Popular

Recent Comments