Scripted field - count links from specific issue type

Marco van der Heide February 4, 2018

Hi All,

I am trying to count the number of linked issues of a specific issue type. (e,g, issuetype Bug has x linked Patches)

All the examples I have found so far are counting the linktype which is not working for us (inconsistency in use by the users) 

Does anybody have a sample script for scriptrunner that will do the trick?

Thanks in advance

Regards,

Marco

1 answer

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
February 4, 2018

Hi Marco,

So basically you want to get the number of linked 'Bugs', yes?

Here's how you do it:

import com.atlassian.jira.component.ComponentAccessor

int x = 0
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkedIssues = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, currentUser).getAllIssues()
if (linkedIssues.size() > 0){
for (int i = 0; i < linkedIssues.size(); i++){
if (linkedIssues[i].getIssueType().getName() == "Bug"){
x = x + 1
}
}
}
if (x > 0){
return x
}else return null
Marco van der Heide February 5, 2018

Hi Ivan,

Thank you very much works like a charm.

Kind regards,

Marco

Barb O'Connell September 21, 2018

Hi,

How can this be modified to get the count of Linked Issues with a specific link type rather than a specific issue type?

Thanks,

-Barb

Ivan Tovbin
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 25, 2018

@Barb O'Connell

Try this:

import com.atlassian.jira.component.ComponentAccessor

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkedIssues = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, currentUser).getInwardIssues("linkName")

if(linkedIssues){
return linkedIssues.size()
}else{
return null
}

Depending on the link direction you need, you might wanna change 'getInwardIssues' to 'getOutwardIssues' 

Barb O'Connell October 1, 2018

Thank you @Ivan Tovbin I will give this a try as soon as my Staging server is available and post the results here.  Cheers!

Barb O'Connell October 9, 2018

Hi @Ivan Tovbin, sorry it did not work for me.  It always completes successfully with NULL value, even though I have linked issues.  I chose a simple issue link type like "blocks" which is inward (and I even tried with outward just in case).  Same results.

I don't know much about groovy, so I attempted to lookup what getInwardLink expects as an argument:  https://docs.atlassian.com/software/jira/docs/api/7.1.4/com/atlassian/jira/issue/link/IssueLinkManager.html

Looks like it wanted a sourceIssueId.  Am I reading that correctly?  Should I use something like getIssueLinks which takes the Issue Link Type Id as an argument, then I can use the specific linkt ype ID?

Maybe instead of

def linkedIssues = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, currentUser).getInwardIssues("linkName")

Is this a possibility?

def linkedIssues = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, currentUser).getIssueLinks("10020")

 

Note: I also see getInwardLink on https://docs.atlassian.com/software/jira/docs/api/7.1.4/com/atlassian/jira/issue/link/LinkCollection.html where it does use the name.   

 

Thanks for your help,

-Barb

Ivan Tovbin
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 10, 2018

Hi @Barb O'Connell,

I've just checked this in my test instance and it works just fine. I think the problem is that you are using link direction names (i.e. "clones" or "is cloned by") instead of link names (i.e. "Cloners" (case sensitive)). To get the link names go to Admin -> Issues -> Issue linking.

On a side note, getInwardIssues() method works perfectly fine here, as it returns you a collection of issues, if any, which are linked using the link, which name you pass as an argument, or an empty collection, if no such issues are found. Same goes for getOutwardIssues(). After that you simpy need to count the elements in this collection, hence the size() method is used.

Hope this helps.

Barb O'Connell October 18, 2018

Thank you @Ivan Tovbin that was the problem. I was not using the link name.  I was using the name of the Outward link.

siva October 6, 2019

@Ivan Tovbin 

The above script works fine,

 

could you add a script to validate all the stories in EPIC are closed

and then end date field to be updated to EPIC from STORY.


Maha vishnu v October 6, 2020

 @Ivan Tovbin

My project Bugs were linked in stories and requirement issue types with defined Epic , 

So i need consolidated linked bug count in EPIC level . 

For example ,

Requirement => has bug linked count 3

story => has bug linked  count 5 

Epic => has to be with bug linked count 8 

 

It could be helpful , if you share your code . 

 

Thank you in advance . 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events