How can we check if the 'parent' issue of a duplicate linkedIssue is not a duplicate of another?

Hung Nguyen March 16, 2015

Thanks to what Henning and Jamie have helped here: https://answers.atlassian.com/questions/139592, I know how to validate if the current issue has a duplicate link to another one (called parent)

Now, on top of that, I need add an additional checking that the parent that user has specified for this link should NOT be a duplicate of another parent. 

This sounds easy since I thought I could just get the Id of the parent issue of the link and check its own OutwardLinks to see if any of them is also Duplicate, something like this:

linkCnt = 0
 
request = webwork.action.ActionContext.getRequest()
if (request) {
    // check for new duplicates link
    linktype = request.getParameter('issuelinks-linktype')
    linkedIssue = request.getParameter('issuelinks-issues')
    
    if (linktype == 'duplicates' && linkedIssue)
    {   //----- This is the additional checking
        if(! issueLinkManager.getOutwardLinks(linkedIssue.getId())*.issueLinkType.name.contains('Duplicate') )
	    	linkCnt = 1
    }
}
 
// check for existing duplicates link
if ( issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains('Duplicate') ) linkCnt += 1
 
// Transition requires at least one duplicates link
(linkCnt>=1)

 

But this doesn't work correctly. I guess that it's because of  

linkedIssue.getId()

doesn't give me the correct Id of the parent (destination) issue of the link.

What is the correct function to use here to make this works?

Thanks

Hung

3 answers

1 accepted

1 vote
Answer accepted
Alejo Villarrubia [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.
March 17, 2015

linkedIssue is a String, so it doesn't have an id property that you can retrieve. Therefore you will need to replace linkedIssue.getId() with this:

issueManager.getIssueObject(linkedIssue).id

On the other hand, request.getParameter('issuelinks-issues') will only return the first of the issues specified in the parameter, so it might be good to use the following and then iterate over the list of linked issues:

request.getParameterValues('issuelinks-issues')

Hope that helped.

0 votes
Hung Nguyen March 17, 2015

Ok, I figured out finally that to use issueManager, we have to explicitly import several packages: Here is the final code that I got which works correctly as I expected, in case someone want to do the same thing

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
linkCnt = 0
 
request = webwork.action.ActionContext.getRequest()
if (request) {
    // check for new duplicates link
    linktype = request.getParameter('issuelinks-linktype')
    linkedIssue = request.getParameter('issuelinks-issues')
    log.debug("linked issue: " + linkedIssue)
    
    if (linktype == 'duplicates' && linkedIssue)
{
       // validate that the parent bug of this link does not have any duplicate link
       if (!issueLinkManager.getOutwardLinks(issueManager.getIssueObject(linkedIssue).id)*.issueLinkType.name.contains('Duplicate'))
	    	linkCnt = 1
    }
}
 
// check for existing duplicates link
if ( issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains('Duplicate') ) linkCnt += 1
 
// Transition requires at least one duplicates link
(linkCnt>=1)

 

 

 

Alejo Villarrubia [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.
March 17, 2015

thanks for sharing the complete code

0 votes
Hung Nguyen March 17, 2015

Hi Alejo,

Thank you for the tips.

With this update, I got a different error. Now it says:

built-in script:com.onresolve.scriptrunner.canned.jira.workflow.validators.SimpleScriptedValidator groovy.lang.MissingPropertyException: No such property: issueManager for class: Script9

 

It seemed that I have to import certain module to use this.

I had tried to put an import

import com.atlassian.jira.issue.IssueManager

 

at the top, but it seemed to be the same.

Any advice on this?

Suggest an answer

Log in or Sign up to answer