Groovy script help: how to set EPIC's Due Date field, by getting "Issues in Epic" issues Max Due Date updated?

Hi,

I need to set the EPIC's Due Date field, by traversing all the "Issues in Epic" of specific Issue Type and get the Maximum(greater one) Due Date set for a Issue to be copied to EPIC's Due Date?

Can some one help me on how to achieve this ! Thanks. 

  • Manju

 

4 answers

1 vote
Tarun Sapra Community Champion Jul 29, 2016

Hi 

It's doable with scrpit runner, for a epic there can be direct subtasks or can be outward links. What you need to do is traverse over all the linked issues or the sub-tasks and just keep storing the largest date as in groovy the comparison operators ( >,<) are supported for date types

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def greatestDate;
issueLinkManager.getOutwardLinks(issue.id).each {issueLink -&gt;
    if (issueLink.issueLinkType.name == "&lt;Name of Link&gt;") { 
        def linkedIssue = issueLink.destinationObject
       
    }
}

 

or in case of subtasks

issue.getSubTaskObjects(){ &lt;code bloc as above&gt;}

 

and in each code block you can have a field "largestDate" which you compare with each date and set the greater date's value in this field. Just like here - http://www.java-examples.com/find-largest-and-smallest-number-array-example

Hi Tarun,

I has written the groovy script to modify the EPIC Due Date value into another Due Date value from the linked Issues Max Due Date set as below:

But I am getting error, while doing this set operation:: epicLink.setDueDate(issue.dueDate);

Cann't find matching method; please suggest why it's so??

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
log.setLevel(org.apache.log4j.Level.DEBUG)
def issue = issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def epicLinkCf = customFieldManager.getCustomFieldObjectByName("Epic Link")
def epicLink = issue.getCustomFieldValue(epicLinkCf) as Issue
def dueDate = epicLink.dueDate
log.debug (dueDate)
log.debug (issue.dueDate)
if (! issue.dueDate){
throw new InvalidInputException("Release requested 'ITG_IN' Due Date is to be updated, to Commit the code changes into planned Release branch !")

if(issue.dueDate > dueDate){
epicLink.setDueDate(issue.dueDate)
log.debug (issue.dueDate)
log.debug (dueDate)
return true }

Tarun Sapra Community Champion Aug 08, 2016

why are you doing this

def epicLink = issue.getCustomFieldValue(epicLinkCf) as Issue

you are retrieving a customFieldValue as an Issue, why?

I have pasted the code and that should do the trick and help you get the list of all the issues -

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
 
def greatestDate;
issueLinkManager.getOutwardLinks(issue.id).each {issueLink -&gt;
    if (issueLink.issueLinkType.name == "&lt;Name of Link&gt;") {
        def linkedIssue = issueLink.destinationObject
        
    }
}

Sorry Tarun, I missed to explain you the change in my requirement for the above posted groovy script:

Now instead of traversing from EPIC to "Issues in Epic". From the specific "Issues in Epic" issue type, I will try to get the EPIC's Due Date and comparing with "Issues in Epic" issue type's Due Date. 

In case "Issues in Epic" issue type's Due Date is greater than the EPIC Due Date, I need to modify/set this bigger date as EPIC's Due Date for my EPIC.

This is my requirement to add the above groovy script just now posted.

But in this case, I need help on how to set the epicLink.setDueDate(issue.dueDate) in case if(issue.dueDate > dueDate) is True.

Also I need to change/set the EPIC JIRA Issue status along with modifying the EPIC's Due Date, based on the "Issues in Epic" Issue Type's Status after validating the Due Dates as explained above. 

I tried to use issue.setDueDate(dueDate) function to set it but this method doesn't exit to set the EPIC due date !

Please advice on how to set the EPIC's Due Date based on the script posted above.

Tarun Sapra Community Champion Aug 09, 2016

Epic is also a instance object of type MutableIssue which has the setDueDate method, please see here - https://developer.atlassian.com/static/javadoc/jira/latest/reference/com/atlassian/jira/issue/MutableIssue.html

Are you new to groovy or Java?

Hi Tarun, Can I use the syntax you had mentioned above for my scenario ?
I want a scripted field on Program Epic, which picks the farthest due date value of the epics. 

0 votes
Tarun Sapra Community Champion Aug 07, 2018

Hello @Aisha M

What do you mean by "farthest due date"

@Tarun Sapra Hello:)

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

Tarun Sapra Community Champion Aug 10, 2018

How is the Program Epic connected to the 3 epics? Is it connected via normal issue linking feature of Jira or via child issues of portfolio?

@Tarun Sapra

Its connected via child issues Tarun 

Tarun Sapra Community Champion Aug 10, 2018

Hello @Aisha M

Since it's connected via child issues, then it means the 3 epics are connected to "Program Epic" via parent link? Am I correct. As Parent link is the default portfolio field at epic level which makes it visible to which Program epic/initiative an epic is connected to.

Now, in order to show the farthest date you on Program Epic you first have to fetch the 3 epics and then compare their date.

This is how to fetch the 3 epics (as all 3 should have parent link field on the view screen pointing to the program epic) So the scripted field is present on the program epic has the code

def issueKey = issue.key;

//issue is the implicit object available

def
jqlSearch = "'Parent Link' =$issueKey"

getListOfEpics(jqlSearch).each {
def date = it.getDueDate()

//write code to compare dates.
}

//issueKey is key of program epic

List
<Issue> getListOfEpics(String jqlSearch) {

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser;
def issues = []
def searchService = ComponentAccessor.getComponent(SearchService)
SearchResults searchResults;
def parseQuery = searchService.parseQuery(user, jqlSearch);
if (parseQuery.isValid()) {
searchResults = searchService.search(user, parseQuery.query, PagerFilter.getUnlimitedFilter())
if (searchResults && searchResults.issues) {
return searchResults.issues;
}

}
return issues;


}

 Now, you just need to write the code to compare dates and that should be it.

In the method getListOfEpics - We execute JQL query to fetch all epics which have the same parent link i.e. same program epic upon which this scripted field is present.

@Tarun Sapra Thank you Tarun ! 
Well, I just mentioned 3 epics as an example. The program epic can have any number of epics under it. 

Also, for comparing the dates, should I use a similar syntax as you have mentioned above for this original post or something like below,

//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
}
}

@Tarun Sapra Hi Tarun,

In your code I have an error at 

SearchResults searchResults;

 Error - Unable to resolve class Search Results 

Any help on how to fix it , please. Thank you

Tarun Sapra Community Champion Sep 21, 2018

You need to have the import statement

import com.atlassian.jira.issue.search.SearchResults

@Tarun Sapra Getting a lot moree errors sfter adding the import statement . . It says "SearchService" is undeclared

@Tarun Sapra Also, can you please post your script under the below link, so that I could mark yer reply as answer after it works :) Thanks

 

https://community.atlassian.com/t5/Marketplace-Apps-questions/How-to-create-field-that-takes-the-farthest-date-value-from/qaq-p/851256

Tarun Sapra Community Champion Sep 25, 2018

Hello @Aisha M

You have to declare and import the classes and variables which you are using in your code, I suggest you to do go through a basic java and groovy tutorial else it will be very hard for you to use the SR plugin as it does require some level of programming proficiency. 

Hi @Tarun Sapra thank you much for your help so far with this, and also with other questions. Really appreciate it :)

And yes, I understand this requires some level of scripting knowledge, but unfortunately we don't have anybody as such to help me out here. Also, our work with SR is limited to a minimum. So, just looking for ways to create like a sample script, so that I can work on it bit by bit and somehow make it work. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,126 views 0 8
Read article

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