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

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!

1 answer

1 accepted

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

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!

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?

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.

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

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

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

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

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!

No problem at all! :D

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

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.

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

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
}

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,317 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot