Scripted Field to return number of cloned issues not working

sharma-shweta September 24, 2013

My script to return the number of cloned issues not working as expected. it always returns 1.

Anyone know what the error might be? Also if I need to display the number of links that 'depend on' another link what do I replace the 'Cloners' with?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager

ComponentAccessor componentAccessor= ComponentAccessor.newInstance()

Collection<IssueLinkType> issueLinkTypes=componentAccessor.getComponentOfType(IssueLinkTypeManager.class).getIssueLinkTypes();
count = 0;
for (IssueLinkType linktype : issueLinkTypes) {
    String name=linktype.getName();
    if(name.equals("Cloners")){
        count++;
    }
}

return count;

5 answers

1 accepted

1 vote
Answer accepted
sharma-shweta September 24, 2013

Hi, Thanks a lot. Finally got it working. :)

I was running it as a scripted field. I was messing up the Outward and Inward Links. I got it to work for 'depends on' like this:

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.id)

def subElements = issueLinks.findAll {
    it.getIssueLinkType().getName() == "Dependency"
}

return subElements.size()

Marie Goodart May 24, 2017

I get the error

groovy.lang.MissingPropertyException: No such property: issue for class: Script5
at Script5.run(Script5.groovy:4)

1 vote
MichałS
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 24, 2013
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor	

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def issueLinks = issueLinkManager.getOutwardLinks(issue.id)

def subElements = issueLinks.findAll {
        it.getIssueLinkType().getName() == "Cloners"
}

subElements.size()

if you need to look for different link types just change the filter in the findAll closure to some other names (you can find them in Administration > System > Issue Features > Issue Linking).

For 'depends on' it would be probably

def subElements = issueLinks.findAll {
        it.getIssueLinkType().getName() == "depends on"
}

Best regards

MichałS
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 24, 2013

Do you run your code in a post function, custom JQL function, scripted field or what is the context? It may be that issue.id is null in your case.

You may look for InwardLinks, try this

def issueLinks = issueLinkManager.getInwardLinks(issue.id)

sharma-shweta September 24, 2013

Hi, Thanks for the help.

I tried to run the code for "Cloners", but Im getting 0 as the output for both number of issueLinks and and subElements.size().

Do you know what might mbe the issue?

1 vote
RambanamP
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 24, 2013

to get the issue link id's bi-directionally as follows

to get OUward links
List<IssueLink> allOutIssueLink = issueLinkManager.getOutwardLinks(sourceIssue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
    IssueLink issueLink = (IssueLink) outIterator.next();
    System.out.println(issueLink.getIssueLinkType().getId());   
}

same way to get inword issueLink's

List<IssueLink> allInIssueLink = issueLinkManager.getInwardLinks(sourceIssue.getId());
for (Iterator<IssueLink> inIterator = allInIssueLink.iterator(); inIterator.hasNext();) {
    IssueLink issueLink = (IssueLink) inIterator.next();
    System.out.println(issueLink.getIssueLinkType().getId());
	if (issueLink.getIssueLinkType().getId() == 10) {
	  Issue destinationIssue = issueLink.getDestinationObject();
	  }
}

check this

https://developer.atlassian.com/static/javadoc/jira/5.0.5/reference/com/atlassian/jira/issue/link/IssueLinkManager.html#getIssueLinks%2528java.lang.Long%2529

sharma-shweta September 24, 2013

Hi, Thanks for your ans.

I tried to run the code but Im getting this error:

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issueLinkManager for class: Script21

Do you know what might be the issue?

RambanamP
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 24, 2013

you have to get issuelinkmanager as follows

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

0 votes
sharma-shweta September 24, 2013

Hi,

Yes I need to get the number of cloned issues. (by counting the number of cloned issue links).

Do you how I could edit my code to achieve that ?

0 votes
MichałS
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 24, 2013

Hi,

Your script displays just the link types configured in the system. Thats why it is always 1.

But I suppose that you would like to display the number of clone issues connected to a given issue?

Suggest an answer

Log in or Sign up to answer