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

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

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}")
}

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

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

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?

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.

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

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) ?

Both :-)

Hi Thanos Any comments

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

103 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you