Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Listener with Current Issuekey question

Cory Burns
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 30, 2021

I have a bit of a issue trying to do something I am not sure is 100% possible but wanted to see if someone may have already attempted to solve or I am wasting my time.

 

The scenario

I am using JIRA Roadmap formally known as Portfolio. Thus I have the issue type of Initiative and everything is working fine, in that regards. What I am attempting to do is as soon as I updated a cf in the Initiative I have a listener that will copy the value of that custom field to the associated Epic. This also works with no issues. The problem I am running into, is I need to expand beyond the current epic.

I need to update all issue in the initiative with the same field value. 

 

This is the script I am using to get the value from the Initiative and update the epic.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

 


def log = Logger.getLogger("scriptrunner logs")
log.setLevel(Level.DEBUG);


// The issue is not an Initiative, nothing to do here
if (issue.issueType.name != "Initiative") {
return
}

log.info "initiative change"
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def change = event.changeLog.getRelated("ChildChangeItem")?.find { it.field == "Folio Name" }

// a field other than the Folio Name field updated, nothing to do here
if (!change) {
return
}

log.info "Folio Name changed"

def customFieldManager = ComponentAccessor.customFieldManager
def FNField = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Folio Name" }
def FolioName = issue.getCustomFieldValue(FNField)

 

// Update the Folio Name of all issues linked via child issues Link
ComponentAccessor.issueLinkManager.getLinkCollection(issue, loggedInUser, false).getOutwardIssues("Parent-Child Link").each {
def selectedFolioOnLinkedIssue = it.getCustomFieldValue(FNField)

FNField.updateValue(null, it, new ModifiedValue(selectedFolioOnLinkedIssue, FolioName), new DefaultIssueChangeHolder())
}

 

 

----

 

Again the above works what I need to do is also effect everything in the epic and all of the subtasks associated with the children of the EPIC.

 

I know I can perform a JQL query of 

issue in childIssuesOf(InitiativeProjectKey)

example

issue in childIssuesOf(INITIATIVE-909)

and this will get me every issue in that initiative. I was trying to write the listener to update everything as soon as cf "Folio Name" is updated on the Initiative basically run the query in the listener of issue in childIssuesOF(currentissue)

 

I am doing this with the following script

 

// Initiaitve account is updated -- update All issues under Initiative

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

 


def log = Logger.getLogger("scriptrunner logs")
log.setLevel(Level.DEBUG);
def currentissue = issue.getKey()

// The issue is not an Initiative, nothing to do here
if (issue.issueType.name != "Initiative") {
return
}

log.info "initiative change"
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def change = event.changeLog.getRelated("ChildChangeItem")?.find { it.field == "Folio Name" }

// a field other than the Folio Name field updated, nothing to do here
if (!change) {
return

}

log.info "Folio Name changed"

 

 

def customFieldManager = ComponentAccessor.customFieldManager
def FNField = customFieldManager.getCustomFieldObjects(issue).find { it.name == "Folio Name" }
def FolioName = issue.getCustomFieldValue(FNField)

//my issue is here, I cant for the life of me figure how to use a qlQueryParser to run the query I need to return the list of items and them update their custom field.


def affectedissues = qlQueryParser.parseQuery("issue in childIssuesOf()")

 

String value = issue.getCustomFieldValue("affectedissues")


// Update the Folio Name of all issues linked via child issues Link
ComponentAccessor.issueLinkManager.getLinkCollection(issue, loggedInUser, false).getOutwardIssues("value").each {
def selectedFolioOnLinkedIssue = it.getCustomFieldValue(FNField)

FNField.updateValue(null, it, new ModifiedValue(selectedFolioOnLinkedIssue, FolioName), new DefaultIssueChangeHolder())
}

 

As seen above there may be a more elegant and simplistic approach. What I am trying to do is whenever custom field "Folio Name" is updated on the Initiative all issues under that Initiative are updated with the same value.

I already have scripts in place that if they are added after that fact will look at the parent and grab the value and update it so I am not worried about that.

 

Thanks to all and any that have any insight been looking at it for days and keep coming up short.

 

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 31, 2021

Hi @Cory Burns

For your requirement, you could try something like this with the Listener:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issue = event.issue as MutableIssue

def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager

def linkedCollection = issueLinkManager.getLinkCollection(issue, loggedInUser,false)
def linkedIssues = linkedCollection.getOutwardIssues('Parent-Child Link')

def sampleValue = customFieldManager.getCustomFieldObjectsByName('Sample Value')[0]
def sampleValueResult = issue.getCustomFieldValue(sampleValue) as Double

if (issue.issueType.name == 'Initiative' && sampleValue != null) {

linkedIssues.findAll {
def epicIssue = it as MutableIssue
epicIssue.setCustomFieldValue(sampleValue, sampleValueResult)
issueManager.updateIssue(loggedInUser, epicIssue, EventDispatchOption.DO_NOT_DISPATCH,false)

if (epicIssue.issueType.name == 'Epic') {
def links = issueLinkManager.getOutwardLinks(epicIssue.id)

links.findAll {
def name = it.issueLinkType.name
def destinationIssue = it.destinationObject as MutableIssue
if (name == 'Epic-Story Link') {
destinationIssue.setCustomFieldValue(sampleValue, sampleValueResult)
issueManager.updateIssue(loggedInUser, destinationIssue, EventDispatchOption.DO_NOT_DISPATCH,false)

destinationIssue.subTaskObjects.findAll {
def subTasks = it as MutableIssue
subTasks.setCustomFieldValue(sampleValue, sampleValueResult)
issueManager.updateIssue(loggedInUser, subTasks, EventDispatchOption.DO_NOT_DISPATCH,false)
}
}
}
}
}
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the Listener configuration:-

listener_config.png

 

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer