Scriptrunner help - limit script field results to specific project(s)

Jamie Rogers November 21, 2019

Hi Community

I need a little bit of help with a scripted field and looking for ways to limit the returned results to specific projects. My scriptrunner knowledge is very basic and have not been able to work this out.

Below is the code the script field - it simply returns the issue keys for specific link type (we have several script fields like this) and they're used for reporting. A team wants to limit the return values of specific projects only - so if one of their issues is linked to multiple projects that are linked via that type ; they only want to show the results for a specific project.

E.g. Issue ABC-123 is linked via 'delivers' type to DEF-456 ; XYZ-789 ; XYZ-890 but team only wants to know what XYZ issues are linked. The current script returns all delivers issues for ABC-123.

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def retVal = ''

// Get only outward links
def links = issueLinkManager.getOutwardLinks(issue.id)

def itr = links.iterator()
while (itr.hasNext()) {
def link = itr.next()

// Only care about "Delivered by" link types
if (link.issueLinkType.name != 'Delivers') {
continue
}

// Get other issue
def linkedIssue = link.destinationObject

// Line seperators if more than one ticket
if (retVal != '') {
retVal += '<br>'
}
retVal += "${linkedIssue.getKey()} "
}

return retVal

This script field was setup by a previous admin so unable to get assistance from them. I looked at scriptrunner documentation and seen you can add JQL queries but I don't understand the code enough to put that into my existing script fields.

I appreciate any assistance the community can offer.

Regards, Jamie 

2 answers

1 accepted

0 votes
Answer accepted
Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 21, 2019

Hi, try the following:

Note tht you must replace 'XYZ' with the desired project key to display

 

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def retVal = ''

// Get only outward links
def links = issueLinkManager.getOutwardLinks(issue.id)

def itr = links.iterator()
while (itr.hasNext()) {
def link = itr.next()

// Only care about "Delivered by" link types
if (link.issueLinkType.name != 'Delivers') {
continue
}

// Only care about linked issue of 'XYZ' project
if (link.destinationObject.projectObject.key != 'XYZ') {
continue
}


// Get other issue
def linkedIssue = link.destinationObject

// Line seperators if more than one ticket
if (retVal != '') {
retVal += '<br>'
}
retVal += "${linkedIssue.getKey()} "
}

return retVal

 

Jamie Rogers November 21, 2019

Thanks @Jack Nolddor _Sweet Bananas_ for the fast reply. 

This works perfectly and meets the requirements for the teams that wanted this.

0 votes
Jamie Rogers November 21, 2019

HI @Jack Nolddor _Sweet Bananas_ 

I have a followup question - if i wanted to include two or three project's issues in the returned results but exclude the rest is there any easy way to do this? 

E.g. I want to see linked issues from ABC, XYZ, JKL projects but not DEF or anyone else.

Would you be simply modifying this line in the script or adding additional code? 

// Only care about linked issue of 'XYZ' project
if (link.destinationObject.projectObject.key != 'XYZ') {
continue
}

I really appreciate you help with this.

Regards, Jamie 

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 22, 2019

Just use this version instead:

 

 

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def retVal = ''

// Get only outward links
def links = issueLinkManager.getOutwardLinks(issue.id)

def itr = links.iterator()
while (itr.hasNext()) {
def link = itr.next()

// Only care about "Delivered by" link types
if (link.issueLinkType.name != 'Delivers') {
continue
}

// Only care about linked issue of given projects
def allowedProjects = ['ABC', 'XYZ', 'JKL']
if (!allowedProjects.contains(link.destinationObject.projectObject.key)) {
continue
}


// Get other issue
def linkedIssue = link.destinationObject

// Line seperators if more than one ticket
if (retVal != '') {
retVal += '<br>'
}
retVal += "${linkedIssue.getKey()} "
}

return retVal: :
Like Jamie Rogers likes this
Jamie Rogers November 24, 2019

Thanks again @Jack Nolddor _Sweet Bananas_ this does exactly what I need it to do.

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 24, 2019

You're welcome

Suggest an answer

Log in or Sign up to answer