Read-Only custom Scripted field from Linked Issues

Heather Ronnebeck
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 14, 2021

I am trying to create a scripted field in my cloud instance that looks up the Linked Issues for a specific link type and then outputs the key & summary into the field. 

Example. 

Epic TEST-12 is linked to ABC-123 using a "Initiative/Project" issue link. 

On ticket TEST-12 there is a field called "Initiative" and that field should show "ABC-123: Initiative XYZ" 

I had this working on server but now that I'm on a cloud instance I can't get the script to work with the changes between the plugins. 

Any help would be appreciated. Thank you!

 

Edit: This is the script from the server version of JIRA. Please note that this code DOES work in server. I am trying to convert it to Cloud.

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.label.LabelManager

import com.atlassian.jira.issue.Issue

import org.apache.log4j.*

// Configure logger

def log = Category.getInstance("com.domain.jira.automation")

log.setLevel(Level.DEBUG)

// log.debug "debug statements"

// Get necessary managers

def cfm = ComponentAccessor.customFieldManager

def issueManager = ComponentAccessor.issueManager

// Link Type IDs can be found by inspecting HTML on the Issue Linking page: https://jira.domain.com/secure/admin/ViewLinkTypes!default.jspa

def linkTypeId_OutcomeDeliverable = 11101

List<Issue> getLinkedIssuesByLinkTypeId(Issue issue, Long linkTypeId) {

//Go through all linked issues and collect their keys if link is of specified type

def linkManager = ComponentAccessor.issueLinkManager

List<Issue> foundIssues = []

linkManager.getInwardLinks(issue.id).each{

def linkedIssue = it.sourceObject

if (it.linkTypeId == linkTypeId)

{

foundIssues.add(linkedIssue)

}

}

log.debug "found "+(foundIssues.size())+" linked issues of that type"

foundIssues

}

def initiatives = getLinkedIssuesByLinkTypeId(issue, linkTypeId_OutcomeDeliverable)

def initiativeKeys = initiatives.collect { it.key }.join(', ')

def initiativeSummary = initiatives.collect { it.summary }.join(', ')

log.debug "found initiatives: ${initiativeKeys}"

return "${initiativeKeys}: ${initiativeSummary}"

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 14, 2021

Ah, searching Google for: getLinkedIssuesByLinkTypeId cloud

Brought up, (of course) @Ravi Sagar _Sparxsys_ 's video on something related:

Now, that's a Post-function, but as you can see just through a little of the code, it's very different calls (having to talk to the API) to find linked issues. 

Because I was thinking - Scriptrunner on Cloud is very different from Server.

To wit:

Scriptrunner Server vs Cloud

Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2021

Oh man, now you've done it. I went and learned the tiniest bit of Groovy on Jira Cloud:

def links = get('/rest/api/3/issue/' + issue.key)
.header('Content-Type', 'application/json')
.asObject(Map)

// issueType IDs can be found by inspecting HTML on the Issue Linking page: https://YOURSITE.atlassian.net/jira/settings/issues/issue-types

def issueTypeId_OutcomeDeliverable = "11101"

//Go through all inbound links and collect their keys if link is of specified type

def initiatives= []

links.body.fields.issuelinks*.inwardIssue.each{

if(it.fields.issuetype.id == issueTypeId_OutcomeDeliverable)
{
initiatives.add(it)
}

}

def initiativeKeys = initiatives.collect { it.key }.join(', ')
def initiativeSummary = initiatives.collect { it.fields.summary }.join(', ')

//return initiatives
return "${initiativeKeys}: ${initiativeSummary}"

You'll want to make sure your Issue Type ID for Initiatives on your Cloud instance is 11101.

Thank goodness for @Ravi Sagar _Sparxsys_'s source code here, and this old script from @Aidan Derossett [Adaptavist] that showed me I could do this in a simpler way without Lists, which I don't fully understand because I AM NOT A PROGRAMMER.

Anyways so yeah, that was some definition of fun. As I look at the final outcome though, I'm not quite sure what the point of this field is, since it replicates information in the Linked issues section:

Screen Shot 2021-10-15 at 2.09.50 AM.png

Like # people like this
Ravi Sagar _Sparxsys_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2021

:) I am glad it was useful.

Like Dave Rosenlund likes this
Heather Ronnebeck
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2021

@Darryl Lee You need this field for sending the data to roadmapping tools and for listing as a column in Structure. That's the whole reason it was created in the first place. 

THANK YOU SO MUCH!!!!

Like Dave Rosenlund likes this
Darryl Lee
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2021

Hrm. Does it actually work in Structure and whatever roadmapping tool you're using? My understanding was that Script Fields are a little... weird.

To wit:

In Cloud, Scripted Fields are not stored as normal Jira custom fields. Instead, the field output is added to an issue’s properties and therefore inherits the issue’s permissions. Scripted Fields are not currently searchable with JQL, but we’re working hard to make this available as soon as possible.

https://docs.adaptavist.com/sr4jc/latest/scriptrunner-migration/feature-parity

TAGS
AUG Leaders

Atlassian Community Events