How to fetch data from Jira in Python? (original) (raw)

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:

Below is the implementation:

Python

from jira import JIRA

jira = JIRA(options = jiraOptions, basic_auth = (

`` "prxxxxxxh@gmail.com" , "bj9xxxxxxxxxxxxxxxxxxx5A" ))

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

from jira import JIRA

jira = JIRA(options = jiraOptions,

`` basic_auth = ( "prxxxxxxh@gmail.com" ,

`` "bj9xxxxxxxxxxxxxxxxxxx5A" ))

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.

Approach:

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 requests

from requests.auth import HTTPBasicAuth

import json

import pandas as pd

auth = HTTPBasicAuth( "prxxxxxxh@gmail.com" ,

`` "bj9xxxxxxxxxxxxxxxxxxx5A" )

headers = {

`` "Accept" : "application/json"

}

query = {

`` 'jql' : 'project =MedicineAppBugs '

}

response = requests.request(

`` "GET" ,

`` url,

`` headers = headers,

`` auth = auth,

`` params = query

)

projectIssues = json.dumps(json.loads(response.text),

`` sort_keys = True ,

`` indent = 4 ,

`` separators = ( "," , ": " ))

dictProjectIssues = json.loads(projectIssues)

listAllIssues = []

keyIssue, keySummary, keyReporter = " ", " ", " "

def iterateDictIssues(oIssues, listInner):

`` for key, values in oIssues.items():

`` if (key = = "fields" ):

`` fieldsDict = dict (values)

`` iterateDictIssues(fieldsDict, listInner)

`` elif (key = = "reporter" ):

`` reporterDict = dict (values)

`` iterateDictIssues(reporterDict, listInner)

`` elif (key = = 'key' ):

`` keyIssue = values

`` listInner.append(keyIssue)

`` elif (key = = 'summary' ):

`` keySummary = values

`` listInner.append(keySummary)

`` elif (key = = "displayName" ):

`` keyReporter = values

`` listInner.append(keyReporter)

for key, value in dictProjectIssues.items():

`` if (key = = "issues" ):

`` totalIssues = len (value)

`` for eachIssue in range (totalIssues):

`` listInner = []

`` iterateDictIssues(value[eachIssue], listInner)

`` listAllIssues.append(listInner)

dfIssues = pd.DataFrame(listAllIssues, columns = [ "Reporter" ,

`` "Summary" ,

`` "Key" ])

columnTiles = [ "Key" , "Summary" , "Reporter" ]

dfIssues = dfIssues.reindex(columns = columnTiles)

print (dfIssues)

Output:

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