Ever since I came out with jiraone, I have always asked myself what extra functions can I make that will help those users who are trying to get meaningful data out of their Jira instances. Today, I will be discussing how you can extract the time in status within your Jira project.
This article focuses on using python v3.6.x and above and a package called jiraone which can be downloaded using pip install jiraone
if you’re in a virtual environment or you can make the call from python3 -m pip install jiraone
To start with, time in status is something that every user would want a report on. If you’re an IT team, you will want to have a report that lets you know how long a ticket has been on a status. You can be able to conveniently get this information from any Atlassian marketplace addon available to display this information via the UI. However, if you’re keen on using API and generating a report that you can use to work with your little team or push to another application then this article is right for you.
We can begin by writing a small script in python that will make the request for us and we can generate the report either in CSV or JSON file format all in UTF-8 character encoding.
For this use case, we’re using a configuration file in JSON format to house our login credentials. eg. config.json
{
"user": "prince.nyeche@example.com","password": "apitoken",
"url": "https://your-instance.atlassian.net"}
The above file should be saved as a config.json extension in the same directory as our python file. Which can be given any name. eg. time_status.py
from jiraone import LOGIN, PROJECT, file_reader from jiraone.module import time_in_status import json
config = json.load(open('config.json'))
LOGIN(**config)
key = ["COM-12", "COM-14"]
if __name__ == "__main__":
time_in_status(PROJECT, key, file_reader, pprint=True, is_printable=False,
output_format="json", report_folder="STATUSPAGE", report_file="time.csv",
status="In progress", login=LOGIN, output_filename="result")
The function we’re using to output the time in status is literally called “time_in_status”. This function has the ability to generate the time an issue has stayed in a particular status or it can generate all the time it stays in each and every status that exists within a Jira issue. I’ll explain what each argument within the function does, so you can get a clear picture of how to use it. The standard way to call this function is the way it is shown above. First, the PROJECT alias is used as a required positional argument and within the function calls the change_log() method. The second argument requires an issue key. Now you can be able to pass the issue key in various formats such as below
key = "COM-12" # as a string
key = "COM-12,COM-14" # a string separated by commakey = 10034 # an integer denoting the issueid
key = ["COM-12", "COM-114", "TPS-14", 10024] # a list of issue keys or issue ids
key = {"jql": "project = COM ORDER BY created DESC"} # a dict using JQL
The third argument is file_reader function which you will need to pass or you can pass as a keyword argument as reader=file_reader. The remaining arguments can be passed as keyword arguments, pprint enables you to print out the time in status in Jira’s pretty format e.g. 13d 11h 22m 15s if it is set to True otherwise if it is not set at all, you will get the DateTime output as 13 days, 11:22:15.913 which is a time delta string of the DateTime string collected from the issue history. The output_format argument enables you to generate a report file either in CSV or JSON format. The words have to be strings and are case insensitive. E.g cSV or JsoN will output the correct file. The output_file argument basically just allows you to name the file, avoid using any extension as this will be automatically added based on the output_format. The status argument allows you to only output statuses that have that status name. For example, you want a report of only “In progress” status, then you should write the name "In progress" as the value to the status argument. If left blank, the result will be all the statuses within the issues being searched. Therefore, if you want the time in status for all the statuses that exist within the Jira issues, do not set the status argument. The login argument is essential to the function as it is required for authenticating your API to the Jira issues. The report_file basically helps within the history generation, you do not have to set this as it is optional. The same goes for report_folder you do not have to set this as it is optional.
Once you run the script, you will end up with a report that looks like the one below as the output
[
{
"author": "Prince Nyeche",
"issueKey": "COM-12",
"status": "To Do",
"summary": "Workflow test 3",
"timeStatus": "0h 00m 19s"
},
{
"author": "Prince Nyeche",
"issueKey": "COM-14",
"status": "In Progress",
"summary": "Workflow test 3",
"timeStatus": "8d 6h 32m 52s"
}
]
That’s how easy you can to generate a report of the time in status of a Jira issue using API.
Prince Nyeche
8 comments