Script Field to Calculate Age of Issue (Jira Cloud)

Edited

Hi all,

I'm migrating Jira to the Cloud and I had a calculated field from Jira Misc Tools Add On. Because this Add on does not exist in the Cloud, I'm trying to use Script Runner to do the same.

The code that I had in the Jira Misc Calculated field was:

issue.get("updated")==null ? null : ((new Date().getTime()) - issue.get("updated").getTime()) / 1000 / 3600

Can you help me converting it to be used as a script listener or scripted field? 

Thanks in advance,

Hugo

3 answers

Here is the same code posted by @Hugo Noronha but just generalized so that it turns the issuekey from a fixed label to a variable and then you can just plug it into the Scripted Fields and it works right out of the box / no error messages!

Enjoy

import java.sql.Timestamp
import java.time.*
import java.text.SimpleDateFormat
import java.text.DateFormat
import java.util.Date
import java.util.concurrent.TimeUnit


def issueKey = issue.key
def created = "created"
long ElapsedDays = 0
def today = new Date()

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

def getresult = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
if (getresult.status == 200){
created = getresult.body.fields[created]
if (created != null) {
Date created_date = sdf.parse(created.toString());
ElapsedDays = (today - created_date) // return days
return ElapsedDays
}
} else {
return "Failed to find issue: Status: ${getresult.status} ${getresult.body}"
}

Meanwhile I found a way to do it

import java.sql.Timestamp
import java.time.*
import java.text.SimpleDateFormat
import java.text.DateFormat
import java.util.Date
import java.util.concurrent.TimeUnit

def issueKey = 'SomeIssue'
def updated = "updated"
long ElapsedDays = 0
def today = new Date()

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");

def getresult = get("/rest/api/2/issue/${issueKey}")
.header('Content-Type', 'application/json')
.asObject(Map)
if (getresult.status == 200){
    updated = getresult.body.fields[updated]
    if (updated != null) {
        Date updated_date = sdf.parse(updated.toString());
        ElapsedDays = (today - updated_date) // return days
        return ElapsedDays
    }
} else {
    return "Failed to find issue: Status: ${getresult.status} ${getresult.body}"
}

0 votes
Emre Toptancı _OBSS_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
Feb 25, 2022

Hello Hugo,

I know you are asking about a Script Runner field but you may want to change it to a ready-made solution that offers much more features and flexibility.

 

Our team at OBSS built Time in Status for this exact need. It is available for Jira Server, Cloud, and Data Center. 

Time in Status basically allows you to see how much time each issue spent on each status and on each assignee. You can see this data in the app's main reporting page, Time in Status tab on issue view pages and also dashboard gadgets.

tisCloud_StatusDuration_LeadTime_with Estimates.png  tisCloud_AssigneeDuration.png 

The app has Consolidated Columns feature. This feature allows you to combine the duration for multiple statuses into a single column and exclude unwanted ones. It is the most flexible way to get any measurement you might want. Measurements like Issue Age, Cycle Time, Lead Time, Resolution Time etc.

For all numeric report types, you can calculate averages and sums of those durations grouped by the issue fields you select. For example total in-progress time per customer (organization) or average resolution time per week, month, issuetype, request type, etc. The ability to group by parts of dates (year, month, week, day, hour) is particularly useful here since it allows you to compare different time periods or see the trend.

tisCloud_StatusDuration_LeadTime_Average_TimeGrouped.png

The app calculates its reports using already existing Jira issue histories so when you install the app, you don't need to add anything to your issue workflows and you can get reports on your past issues as well. It supports both Company Managed and Team Managed projects.

Time in Status reports can be accessed through its own reporting page, dashboard gadgets, and issue view screen tabs. All these options can provide both calculated data tables and charts. And the app has a REST API so you can get the reports from Jira UI or via REST.

Gadget_AverageStatusDurationByComponent.png  tisCloud_StatusDuration_LeadTime_Chart.png

Using Time in Status you can:

  • See how much time each issue spent on each status, assignee, user group and also see dates of status transitions.
  • Calculate averages and sums of those durations grouped by issue fields you select. (For example, see average InProgress time per project and per issue type.)
  • Export your data as XLS, XLSX, or CSV.
  • Access data via REST API. (for integrations)
  • Visualize data with various chart types.
  • See Time in Status reports on Jira Dashboard gadgets

https://marketplace.atlassian.com/apps/1211756/

EmreT

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events