Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scripted field - count links from specific issue type

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

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

Hi Ivan,

Thank you very much works like a charm.

Kind regards,

Marco

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

@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' 

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

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

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.

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

@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.


 @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
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

681 views 7 23
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you