Skip to content

Instantly share code, notes, and snippets.

@jitsejan
Created September 5, 2020 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitsejan/0d841dba8f1efeb6bbef10ae7149cc1f to your computer and use it in GitHub Desktop.
Save jitsejan/0d841dba8f1efeb6bbef10ae7149cc1f to your computer and use it in GitHub Desktop.
Jira lead time extraction
import pandas as pd
import requests
JIRA_TOKEN = "abcdefg12345"
JIRA_URL = "https://jitsejan.atlassian.net/"
JIRA_USER = "mail@jitsejan.com"
# Setup a requests session
rsession = requests.Session()
# New Jira instances authenticate through a token
rsession.auth = (JIRA_USER, JIRA_TOKEN)
# Jira query
jql = "project = BT"
# Parameters with the changelog enabled
params = {
'jql': jql,
'expand': 'changelog',
}
# Execute the Jira query and get the issues field from the JSON response.
issues = rsession.get(f"{JIRA_URL}/rest/api/2/search", params=params).json()['issues']
# Get the base data
base = pd.json_normalize(issues)[['key', 'fields.created']]
base.rename(columns={'fields.created': 'created'}, inplace=True)
# Get the transitions
transitions = pd.json_normalize(data=issues,
record_path=['changelog', 'histories'],
meta=['fields', 'key'])[['created', 'items', 'key']]
# Filter and transform the status transitions
transitions = transitions\
.join(transitions['items']\
.explode()
.apply(pd.Series)
)\
.query("field == 'status'")\
.drop("items", axis=1)[['created', 'key', 'toString']]
# Create the backlog transitions
create_transitions = base.copy()
create_transitions['toString'] = 'Backlog'
# Combine the transitions and the Backlog dates
transitions = pd.concat([transitions, create_transitions])
# Pivot the statuses
statuses = transitions.pivot(index='key', values='created', columns='toString')
# Make sure all fields are of datetime
statuses = statuses.astype('datetime64[ns]')
statuses.rename(columns={'toString': 'Status'}, inplace=True)
# Calculate the lead time (in seconds) by calculating the timedelta
statuses['Duration'] = (statuses['Done'] - statuses['Backlog']).apply(lambda x: x.seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment