Scriptrunner: Finding the min and max dates of a group of stories related to an epic

J C November 21, 2017

Hi,

I'm trying to find the best way to find the min and max dates of two custom fields of a group of stories related to an epic.  We have two custom fields that we would like to get the earliest date and the latest date of and have the scripted field update in the epic it is related to.  Any references/thoughts/suggestions of a ScriptRunner flow that would assist with that task?

Thanks in advance!

2 answers

1 accepted

1 vote
Answer accepted
Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2017

Hey James!

So, to clarify, are you saying that you want to pull two custom field values from an epic (which both consist of a list of stories related to that epic), and then, for each custom field, get the stories with the minimum and maximum dates? Then, once you find those, you want to output those stories to a script field? Additionally, what are the dates that you're referring to? Resolution dates, creation dates, etc...?

I can give you a better answer after you confirm the above, but conceptually, in a script field you could grab the values from those custom fields (assuming they are issue keys or something), get their associated issues, loop through the issues, and then do some sort of comparison using the java built-ins for Date objects: Date.before(Date), Date.after(Date), etc... While looping through, you could store the issues with the lowest and highest dates in variables to be returned later.

Again, I could probably code something up as an example after you answer those questions! :D

Thanks!

Aidan

J C November 21, 2017

Hi Aidan,

 

thank you you for the quick reply!  I would like to roll up max and min dates of target dates of the stories that are associated to an Epic.  So I would like to get the stories of an epic and then find all of the results of a custom date field to find the max or min to update a scripted field in the Epic.  Let me know if that clears things up.  Thank you again!

Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2017

No problem! :)

So you're getting all of the stories of an epic, and for each of those stories you're getting the values from one of their custom date fields. From those values you want to find the max and min. Then you want to use that collection of maximums and minimums in some way to update the script field of the epic. Correct?

In what way do you plan on using those maximum and minimum dates to update the epic's field?

Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2017

OOOOH or are you saying that each of the stories has a custom date field with a single value, and you want to collect all of those values from every story and find the max and min out of those?

Sorry I'm questioning so much. I'm just trying to wrap my head around the scenario haha.

J C November 21, 2017

Yes that is correct and that value will update a scripted field in the Epic.

J C November 21, 2017

BasicAlly we want to roll up the max and Min date of custom fields from the stories associated to an Epic.

Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2017

Hey again James!

I think I more or less understand what you're looking for and have written up a quick example of how you may do this in a Script Field. However, keep in mind that this example is untested:

import com.atlassian.jira.component.ComponentAccessor

//Check that current issue is an Epic
if(issue.issueType.name == "Epic")
{
//Initialize appropriate managers
def linkManager = ComponentAccessor.issueLinkManager
def cfm = ComponentAccessor.customFieldManager

//Get the story's date custom field and initialize a list to hold the date values
def storyDateField = cfm.getCustomFieldObjectByName("Story Date Field")
def storyDates = [] as List<Date>
//Loop through all of Epic's stories and collect their date objects
linkManager.getOutwardLinks(issue.id).each{
if(it.issueLinkType.name == "Epic-Story Link")
{
def storyIssue = it.destinationObject
storyDates.add(storyIssue.getCustomFieldValue(storyDateField) as Date)
}
}

//Initialize min and max
def minDate = storyDates[0] as Date
def maxDate = storyDates[0] as Date

//Find actual min and max Dates
for(int i = 1; i < storyDates.size(); i++)
{
def currentDate = storyDates[i]
if(currentDate.before(minDate))
{
minDate = currentDate
}
if(currentDate.after(maxDate))
{
maxDate = currentDate
}
}

//You now have the overall maximum and minimum of all of the Story's custom Date fields
//From here on out you can do whatever you need with those minimums and maximums
}
else //Return null if issue is not an Epic
{
return null
}

See if you can get that working for you and let me know if I'm still misunderstanding the goal! :D

Best,

Aidan

J C November 27, 2017

Thank you Aidan, this worked exactly as wanted without any modifications at all!  Thank you so much, truly appreciate the quick response and solution!

J C November 27, 2017

Just one quick follow up question Aidan,  can you tell me how to format the min/maxDate to store it in a date/time picker, it is giving me an 'Invalid Date' but works fine if i'm using a text field.  Thanks in advance!

Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2017

No problem at all! :D

When exactly are you getting the 'Invalid Date' message?

J C December 6, 2017

I'm getting an invalid date when I try to store it as a date/time picker field rather than a text field, I'm not sure how to convert it to a date type for the date/time field to accept it.

Aidan Derossett _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 6, 2017

Could you show me how you're doing this in your code? :)

J C December 6, 2017

Sure, I have it exactly as your example:

 

import com.atlassian.jira.component.ComponentAccessor

//Check that current issue is an Epic
if(issue.issueType.name == "Epic")
{
//Initialize appropriate managers
def linkManager = ComponentAccessor.issueLinkManager
def cfm = ComponentAccessor.customFieldManager

//Get the story's date custom field and initialize a list to hold the date values
def storyDateField = cfm.getCustomFieldObjectByName("End date")
def storyDates = [] as List<Date>
//Loop through all of Epic's stories and collect their date objects
linkManager.getOutwardLinks(issue.id).each{
if(it.issueLinkType.name == "Epic-Story Link")
{
def storyIssue = it.destinationObject
storyDates.add(storyIssue.getCustomFieldValue(storyDateField) as Date)
}
}

//Initialize min and max
def minDate = storyDates[0] as Date
def maxDate = storyDates[0] as Date

//Find actual min and max Dates
for(int i = 1; i < storyDates.size(); i++)
{
def currentDate = storyDates[i]
if(currentDate.before(minDate))
{
minDate = currentDate
}
if(currentDate.after(maxDate))
{
maxDate = currentDate
}
}

//You now have the overall maximum and minimum of all of the Story's custom Date fields
//From here on out you can do whatever you need with those minimums and maximums

return(minDate)
}
else //Return null if issue is not an Epic
{
return null
}

0 votes
Aisha M August 13, 2018

@Aidan Derossett _Adaptavist_ Hi Aidan, I have a similar query , can you please help.

My scenario is, I want to have a scripted field in PROGRAM EPIC. So what this field should basically do is, it must automatically pick the most late date based off the "DUE DATE" field in the EPIC.

Example: If

ABC EPIC-1 DUE DATE is 08/10/18
ABC EPIC-2 DUE DATE is 10/10/18
ABC EPIC-3 DUE DATE is 12/10/18

ABC PROGRAM EPIC scripted DATE field should automatically pick 12/10/18

Suggest an answer

Log in or Sign up to answer