How to check linked issue summary before creating issue?

April January 6, 2017

Hi there,

I'm using the ScriptRunner post-function "Clones an issue and links." I need to create a set of linked issues, but the team wishes to avoid duplicates.

New issues will be created with a summary like:

issue.summary = 'Training for ' + sourceIssue.summary

I wrote the condition below to check linked issue summaries for a match:

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.getId())
Issue issue  = issue
String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
// Check if instance of this issue name already exists
!(issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->  
    def linkedIssue = issueLink.destinationObject
    String issueName = linkedIssue.getSummary()
    log.info "New issue name is " + issueName
    issueName == "Training for " + sourceName})

 

I see the values I expect in the log, but a duplicate issue is created anyway.

I also tried creating a second variable in case I was not comparing apples to apples somehow:

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.getId())
Issue issue  = issue
String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
String newName = "Training for " + sourceName
log.info "New name is " + newName
// newName returns the string I expect
// Check if instance of this issue name already exists
!(issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->  
    def linkedIssue = issueLink.destinationObject
    String issueName = linkedIssue.getSummary()
    log.info "New issue name is " + issueName
    issueName == newName})

 

I can see from the log that I'm definitely finding linked issues with the summary I don't want to duplicate:

 /secure/WorkflowUIDispatcher.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] New issue name is Training for test

But it seems finding does not mean actually preventing duplicate issues.

Anyone know what I'm doing wrong?

Thanks!

 

 

1 answer

1 accepted

0 votes
Answer accepted
Vasiliy Zverev
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.
January 9, 2017

Try this code to check dublicates:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink

String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
String newName = "Training for " + sourceName
log.info "New name is " + newName

// newName returns the string I expect
// Check if instance of this issue name already exists
boolean isAlreadyExist = false;
for(IssueLink issueLink: ComponentAccessor.getIssueLinkManager().getOutwardLinks (issue.id)){
    if(newName == issueLink.destinationObject.getSummary()){
        isAlreadyExist = true;
        break;
    }
}

if(isAlreadyExist)
    log.info "isAlreadyExist"
April January 9, 2017

Hey thanks!

Unfortunately, that returns "Condition did not return a boolean, coercing to false."

I've tried declaring a Boolean before as a condition in a workflow, which always returns that same error. I'm no programmer, but still, I'm a little confused about why the result of a Boolean is not a Boolean... (laughing).

I've only ever gotten this to work by doing something like "!(statement to evaluate)".

Trying to figure out how to restate what you have there....

Vasiliy Zverev
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.
January 9, 2017
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
 
String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
String newName = "Training for " + sourceName
log.info "New name is " + newName
 
// newName returns the string I expect
// Check if instance of this issue name already exists
for(IssueLink issueLink: ComponentAccessor.getIssueLinkManager().getOutwardLinks (issue.id)){
    if(newName == issueLink.destinationObject.getSummary()){
        return true;    
    }
}
 
return false

 

  
April January 9, 2017

Aha, that was it!

Except, whatever code is behind this condition seems to create an issue if the result is true, and not if the answer is false, so I just switched the logic, and it is working like this:

for(IssueLink issueLink: ComponentAccessor.getIssueLinkManager().getOutwardLinks (issue.id)){
    if(newName == issueLink.destinationObject.getSummary()){
        return false;    
    }
}
return true

Thanks so much for all your help!

THAT wasn't counter-intuitive at all smile

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events