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
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}"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.
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.
Using Time in Status you can:
https://marketplace.atlassian.com/apps/1211756/
EmreT
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.