Search for the linked issuetype and populate the linked issue ticket numbers into one customfield

A November 20, 2015

Search for the linked issuetype ( no matter what type of link it has)

Pull the result content : Issue id and summary

 

Current code an give only the number specific of linked issues, but cannot write the issue id

 

import com.atlassian.jira.component.ComponentAccessor
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getInwardLinks(issue.getId()) 
def issueLinks1 = issueLinkManager.getOutwardLinks(issue.getId())  
def subElements = issueLinks.findAll { it.issueLinkType.name =='UseCase_Link'}
def subElements1 = issueLinks1.findAll { it.issueLinkType.name =='UseCase_Link'}
log.error(subElements)
return subElements.size()+subElements1.size() as Double

 

 

4 answers

1 vote
Thanos Batagiannis _Adaptavist_
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.
November 23, 2015

Hi Amar,

The code below gives you 3 collections of issues (1 Collection<Issue> and 2 List<Issue>). Then I suppose you can create a scripted field or update an existing custom field and you can store whatever information you want for the returned issues. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issueManager = ComponentAccessor.getIssueManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().user

Issue issue = issueManager.getIssueObject("IssueKey")
def linkCollection = issueLinkManager?.getLinkCollection(issue, applicationUser.directoryUser)

// get the scripted field you want to save the result
def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "ScriptedFieldName"}

//Returns a collection of issues that contains both inward and outward linking issues.
def newValueForCF = ''
def allLinkedIssues = linkCollection?.getAllIssues()
def bugIssues = allLinkedIssues?.findAll {linkedIssue -> linkedIssue.getIssueTypeObject().name == 'Bug' }
for (bug in bugIssues) {
    newValueForCF += "${bug.key} id: ${bug.id} - Summary: ${bug.summary} <br>"
}

//Return the value for the scripted custom field
//return newValueForCF

//Looks up and returns a "sort.order" sorted list of all inward linked issues by given link name.
def inwardBlockIssues = linkCollection?.getInwardIssues('Blocks')
inwardBlockIssues?.each {linkedIssue ->
    log.debug("Issue ${linkedIssue.key} - id: ${linkedIssue.id} - Summary: ${linkedIssue.summary}")
}

// Looks up and returns a "sort.order" sorted list of all outward linked issues by given link name.
def outwardBlockIssues = linkCollection?.getOutwardIssues('Blocks')
outwardBlockIssues?.each {linkedIssue ->
    log.debug("Issue ${linkedIssue.key} - id: ${linkedIssue.id} - Summary: ${linkedIssue.summary}")
}
A November 23, 2015

Thanks thanus. I want to condition and Group the issues by the type of issue. Such as linked issue is Task, then it the issue key and summary should be written in an another custom field

Thanos Batagiannis _Adaptavist_
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.
November 24, 2015

Amar, I updated the above script. If you remove the return from comments, this script will add in a text scripted field the id and the summary of each linked issue that has 'bug' type

Marco van der Heide September 25, 2017

Hi Thanos,

 

I am using part of your script but having getting an error when compiling into a scripted field.

 

it returns expecting '}' found - on the def line

Could you let me know what I am doing wrong?

JamieA
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.
September 26, 2017

This ridiculous web site converted > to &gt; etc... which is kind of a fatal flaw for any site where you are expected to put code.

I've tried to edit the answer to make it correct, please try now.

JamieA
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.
September 26, 2017
Marco van der Heide September 28, 2017

Thank you very much Jamie,

I would appreciate it if you can have a look at the following code as it is now giving me an warning on def applicationUser that it is depreciated and cannot find matching method on

def linkCollection

def allLinkedIssues

def PatchIssues

// Code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issueManager = ComponentAccessor.getIssueManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().user

Issue issue = issueManager.getIssueObject("IssueKey")
def linkCollection = issueLinkManager?.getLinkCollection(issue, applicationUser.directoryUser)

//Returns a collection of issues that contains both inward and outward linking issues for the issuetype Patch.

def allLinkedIssues = linkCollection?.getAllIssues()
def PatchIssues = allLinkedIssues?.findAll {linkedIssue -> linkedIssue.getIssueTypeObject().name == 'Patch' }
PatchIssues.toString()

//

I am trying to count the number of linked Patches to an issue

 

Kind regards,

Marco

0 votes
A November 22, 2015

Hi Thanos Any comments

0 votes
A November 20, 2015

Both :-)

0 votes
Thanos Batagiannis _Adaptavist_
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.
November 20, 2015

Hi Amar, You want all the linked issues or the issues that are linked via a specific issue link type (in your case the UseCase_Link) ?

Suggest an answer

Log in or Sign up to answer