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. Over time, it has evolved into a work management platform capable of handling all stages of the agile methodology. There are two ways to get data from Jira:

Prerequisites in Jira

Issues recorded in JIRA tool for project "MedicineAppBugs"

**JQL: Jira Query Language (JQL) is an efficient way to fetch Jira data. One can filter issues, projects, or bugs using keywords and operators. JQL works with both the Jira library and REST API.

Fetch Data Using Jira Python Library

The jira library is an easy-to-use Python library to connect with Jira. It requires Python 3.5+.To install Jira Library use below command in command prompt or terminal:

pip install jira

**Example: Fetch All Issues from a Project

Python `

from jira import JIRA

jiraOptions = {'server': "https://yourdomain.atlassian.net/"} jira = JIRA(options=jiraOptions, basic_auth=("youremail@gmail.com", "your_api_token"))

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.

**Explanation:

**Example: Fetch a Single Issue

Python `

from jira import JIRA

jiraOptions = {'server': "https://yourdomain.atlassian.net/"} jira = JIRA(options=jiraOptions, basic_auth=("youremail@gmail.com", "your_api_token"))

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

**Explanation: jira.issue('MED-1') Retrieves a single issue from Jira using its unique key (MED-1).

Fetch Data Using Jira REST API

The Jira platform provides a REST API that allows you to interact with Jira programmatically. Using the API, you can perform CRUD operations on issues, projects, dashboards, users, and more.

Here, we focus on fetching all issues for a specific project (MedicineAppBugs) using the REST API. This approach gives you full control over the data and allows you to filter, sort or process issues according to your requirements.

**Python Libraries Required

Getting the API URL

To fetch issues, we will use Jira’s Issue Search API.

This URL allows you to retrieve all issues for your project or filter them using JQL queries.

Python `

import requests from requests.auth import HTTPBasicAuth import json import pandas as pd

url = "https://yourdomain.atlassian.net/rest/api/2/search" auth = HTTPBasicAuth("youremail@gmail.com", "your_api_token") headers = {"Accept": "application/json"} query = {'jql': 'project = MedicineAppBugs'}

response = requests.get(url, headers=headers, auth=auth, params=query)

dictProjectIssues = response.json() listAllIssues = []

def iterateDictIssues(oIssues, listInner): for key, values in oIssues.items(): if key == "fields": iterateDictIssues(values, listInner) elif key == "reporter": iterateDictIssues(values, listInner) elif key == "key": listInner.append(values) elif key == "summary": listInner.append(values) elif key == "displayName": listInner.append(values)

for key, value in dictProjectIssues.items(): if key == "issues": for eachIssue in value: listInner = [] iterateDictIssues(eachIssue, listInner) listAllIssues.append(listInner)

dfIssues = pd.DataFrame(listAllIssues, columns=["Key", "Summary", "Reporter"]) print(dfIssues)

`