Workflow validator - check linked issues

Etai Leers
Contributor
June 27, 2022

Hi community!

I'm trying to write a validator script that checks if an Epic got linked issues.

if yes -  the workflow transition can move, otherwise it sends an error message.
so far I got this but it's not working for me. (Using Jira server).

Code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.opensymphony.workflow.InvalidInputException

// Allow logging for debug and tracking purposes
import org.apache.log4j.Level
import org.apache.log4j.Logger


// Setup the log and leave a message to show what we're doing
Logger logger = log
logger.setLevel( Level.ERROR )
logger.debug( "$scriptCode Triggered by $issue.key" )

def passesCondition = true
if (issue.linkedIssues.size() > 0)
{
IssueLinkManager issueLinkManager = ComponentAccessor.issueLinkManager
def found = issueLinkManager.getOutwardLinks(issue.id).any{ }

logger.debug( "$scriptCode Found = $found " )
if (found) {
logger.debug( "$scriptCode return false" )
passesCondition = false
invalidInputException = new InvalidInputException("Please create issues")
} else {
logger.debug( "$scriptCode return true" )
passesCondition = true
}
}
else
{
passesCondition = true
}

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
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.
June 27, 2022

Are you looking for your Epic to have regular issue links?

Or are you looking for "epic link" or "issues in epic" link?

Because those are different.

The IssueLinkManager will only get you issue links like "Cloned By", "Related To", "Blocks" etc.

If you want to look to see if the epic has issues in the epic link, then you need to EpicLInkManager

import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.greenhopper.manager.issuelink.EpicLinkManager
@WithPlugin("com.pyxis.greenhopper.jira") agilePlugin
@JiraAgileBean EpicLinkManager epicLinkManager

def issuesInEpic = epicLinkManager.getIssuesInEpic(issue)
if(issuesInEpic) {
log.debug "${issuesInEpic.size()} issues were linked to this epic."
return true
} else {
log.debug "No issues are linked to this epic"
return false
}

Etai Leers
Contributor
June 28, 2022

Hi @Peter-Dave Sheehan thanks for your response.

I used:

import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.greenhopper.manager.issuelink.EpicLinkManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.opensymphony.workflow.InvalidInputException
@WithPlugin("com.pyxis.greenhopper.jira") agilePlugin
@JiraAgileBean EpicLinkManager epicLinkManager

import org.apache.log4j.Logger
import org.apache.log4j.Level
 
def log = Logger.getLogger("com.acme.ListLinks")
log.setLevel(Level.DEBUG)


def passesCondition = true
def issuesInEpic = epicLinkManager.getIssuesInEpic(issue)

if(issuesInEpic) {
    log.debug "${issuesInEpic.size()} issues were linked to this epic."
    passesCondition = true
} else {
    log.debug "No issues are linked to this epic"
    invalidInputException = new InvalidInputException("Please create linked issues to this Epic in order to transition the Epic to 'DEV TO DO' status")
    passesCondition = false
}



Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events