scriptrunner script to implement below business need

Dayanand February 25, 2024

scriptrunner script for below requirement 

The last due date of the stories should be the due date of the Epic. The last due date of the Epics should be the due date of the Container. 

Here “Container” is Jira issuetype which will have Epics as child issues (container is placeholder for epics like how epics placeholder stories)

Container due date (system due date) we referring as "Container Due Date"

Similarly for “Epic due date”

Here what user want that based on child last due date parent due date should set automatically say if container have 10 epics then container due date should set automatically based on 10 epics last due date set 

2 answers

2 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2024

Hi Dayanand

If I understand correctly, you want the due date to automatically roll up from issues->Epic->Container so that if an issue is updated to a later due date than anything else in the current chain, the epic and container will both reflect this new date.

The best approach I can think of for this is probably a script listener.

You can set a script listener to watch for changes in the due date field on that project.

Then, if the issue belongs to an epic, we look at all the other issues linked in that epic and find the latest one and set the Epic's due date to that due date. 

This will in turn re-trigger the same listener and similarly find all the epics in that container and identify the latest one.

Something like this might work as a starting point.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.greenhopper.manager.issuelink.EpicLinkManager
@WithPlugin("com.pyxis.greenhopper.jira") agilePlugin

def issueLinkManager = ComponentAccessor.issueLinkManager
//def event = IssueEvent

if (!event.changeLog) return false //no changeLog means nothing changed and no action to perform

def changeItems = ComponentAccessor.changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans

if(!changeItems?.any {changeItem-> changeItem.field == 'duedate' && changeItem.fromString != changeItem.toString }){
log.info "Due Date was not changed on this issue. Nothing more to do"
return false
}
def containerIssueType = 'Container' //ADJUST HERE WITH THE CORRECT ISSUE TYPE NAME for the container
if(event.issue.issueType.name == containerIssueType){
log.info "This is the top level issue type (container). We don't need to roll up the date further"
}
Issue sourceOfDateIssue, targetOfDateIssue

if(event.issue.issueType.name == 'Epic'){
//find the container
def containerLinkType = 'Container' //ADJUST HERE WITH THE CORRECT LINK TYPE NAME
def links = issueLinkManager.getLinkCollectionOverrideSecurity(event.issue)
def containerIssue = links.getOutwardIssues(containerLinkType)[0] //this assumes each Epic only links to 1 container
//BASED ON YOUR LINK SETUP, MAYBE YOU NEED THE FOLLOWING LINE INSTEAD OF THE PREVIOUS ONE
//def containerIssue = links.getInwardIssuesS(containerLinkType)[0]
def allEpics = issueLinkManager.getLinkCollectionOverrideSecurity(containerIssue).getInwardIssues(containerLinkType) //this should be reverse of the previous
//OR
//def allEpics = issueLinkManager.getLinkCollectionOverrideSecurity(containerIssue).getOutward(containerLinkType)
sourceOfDateIssue = allEpics.max{it.dueDate}
targetOfDateIssue = containerIssue

} else {
def epic = event.issue.getCustomFieldValue('Epic Link')
if(!epic){

log.info "There is no epic link on $event.issue.key. So there is nowhere to roll up the Due Date"
return false
}
@JiraAgileBean EpicLinkManager epicLinkManager
sourceOfDateIssue = epicLinkManager.getIssuesInEpic(epic).max{it.dueDate}
targetOfDateIssue = epic
}

if(sourceOfDateIssue.dueDate != targetOfDateIssue.dueDate){
log.info "Updating $targetOfDateIssue.issueType.name ($targetOfDateIssue.key) Due Date to $sourceOfDateIssue.dueDate"
targetOfDateIssue.update{
setDueDate(sourceOfDateIssue.dueDate.format('d/MMM/yyyy')) //adjust according your environment's expected date format
}
} else {
log.info "The $targetOfDateIssue.issueType.name ($targetOfDateIssue.key) is already at the max Due Date"
}

 

 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 20, 2024

In our Jira project relation between Container and Epics established by two issuelinkage 

Some Epics have Inward issuelink with Container as "is Child of"

and Some Epics have Inward issuelink with Container as "is relates to"

How to handle this situation in script to cover above both issuelink types to get lastest (highest due date for Containers) 

Ok, I see, the code I suggested only lets you look up linked issues using a single link type, but you have multiple.

Here is is a re-written section that achieves that:


//find the container
def containerLinkTypes = ['ContainerTyep1','ContainerType2'] //ADJUST HERE WITH THE CORRECT LINK TYPE NAME

def
links = issueLinkManager.getLinkCollectionOverrideSecurity(event.issue)
def containerIssue = containerLinkTypes.collect{links.getInwardIssues(it)}.flatten()[0] //this assumes each Epic only links to 1 container

def
allEpics = containerLinkTypes.collect{
issueLinkManager.getLinkCollectionOverrideSecurity(containerIssue).getOutward(it)
}.flatten()

 

 

0 votes
Dayanand March 19, 2024

@Peter-Dave Sheehan 

Thank you for your valuable support when i tried i encounter scenario 

Some Epics have Inward issuelink with Container as is Child of

and Some Epics have Inward issuelink with Container as is relates to

unable make code change that covers above scenario please help for this as well.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 19, 2024

I don't understand what you mean can you illustrate the problem?

Dayanand March 19, 2024

In our Jira project relation between Container and Epics established by two issuelinkage 

Some Epics have Inward issuelink with Container as "is Child of"

and Some Epics have Inward issuelink with Container as "is relates to"

How to handle this situation in script to cover above both issuelink types to get lastest (highest due date for Containers) 

Suggest an answer

Log in or Sign up to answer