Validate that all linked issues are DONE - ScriptRunner

Daniel Alonso April 9, 2021

Hi,

I need to add a Transition validation that checks if all the Linked Issues are in a done status category, if found this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Logger
import org.apache.log4j.Level



def linkMgr = ComponentAccessor.getIssueLinkManager()
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
   		log.info(link.sourceObject.resolutionId)
        if (!link.sourceObject.resolutionId) {
            return false
            throw new InvalidInputException("Issue Links must be close before continue")
        }

}

But it only works on Condition, I need to set it on Validation  to show the error message.

Any suggestion?

I'm very basic on Groovy

2 answers

1 accepted

2 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 9, 2021

You can use the "simple scripted validator" for this.

I would write the condition like this:

import com.atlassian.jira.component.ComponentAccessor
def linkMgr = ComponentAccessor.issueLinkManager
linkMgr.getLinkCollection(issue, currentUser).allIssues.findAll{it != issue}.every{it.resolution}
Daniel Alonso April 10, 2021

It works perfectly. Thanks so much Peter-Dave.

Just a side note: I just noticed when you have a Condition for pending approval, the validation is skipped and don't performed.

Abhilash Gunturu November 3, 2022

Hello @Peter-Dave Sheehan , what if I have to restrict this validator only to a specific request type. Is it possible? If so can I please know how can we do that. 

Thanks Much

Abhi

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 3, 2022

Service Desk Request type? Or did you mean issue type?
And do you mean only validate linked issues if the current issue is of a specific type? Or only prevent closing the current issue if linked issues of a specific type are still open (ignore the status of other linked issues)?

Like landis likes this
1 vote
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 9, 2021

Hi @Daniel Alonso when using it with Validators, you should throw or set InvalidInputException to invalidInputException property. But you have "return false" code on the line before throwing exception, so it will never throw an exception.

Fix for validator is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Logger
import org.apache.log4j.Level




def linkMgr = ComponentAccessor.getIssueLinkManager()
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
log.info(link.sourceObject.resolutionId)
if (!link.sourceObject.resolutionId) {
throw new InvalidInputException("Issue Links must be close before continue")
}

}
Daniel Alonso April 10, 2021

Thanks for the tip Martin.

jun lee October 26, 2022

How can I modify, when I want to use for only "is contained in" linked issue

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 26, 2022

Something like this should work:

import com.atlassian.jira.component.ComponentAccessor
def linkMgr = ComponentAccessor.issueLinkManager
linkMgr.getLinkCollectionOverrideSecurity(issue).getOutwardIssues('is contained in').every{it.resolution}
jun lee October 26, 2022

Thank you.
But it doesn't work the way I wanted.


When issue B is created in issue A, the linkedtype of issue A is "contain" and the linkedtype of issue B is "is contained in"
I want a validation that issue A can be compledted when status of issue B is "completion"

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 31, 2022

I was wrong about the way "getOutwardIssue" works... you need to provide the name of the link type, not the outward description.

For example, I have a link type called "Requires". The outward description reads "requires" and the inward reads "is required by". So in my case, I can return the issues with 

linkMgr.getLinkCollectionOverrideSecurity(issue).getOutwardIssues('Requires')

I don't know what your link type name is, you'll have to figure that out. But assuming it's something like "Contains" and the status you are looking for is exactly "completion" you can try this:

import com.atlassian.jira.component.ComponentAccessor
def linkMgr = ComponentAccessor.issueLinkManager
linkMgr.getLinkCollectionOverrideSecurity(issue).getOutwardIssues('Contains').every{it.status.name == "completion"}

This will return false if any of the "Contains" linked issue is not in status "completion" and cause the validator to fail.

jun lee November 1, 2022

Thanks, but issue A is completed even though issue B is not in the "Completion" status.

 

Below image is issue A, and link type is "contains" with issue B.(ENMCOMM-269). When I click "complete" button to completion, it should not be changed to "completion" status because status of issue B.(ENMCOMM-269) is not "completion"

screen1.jpg

 

Workflow is like below.

screen2.jpg

 

However issue A is completed even though issue B is not in the "Completion" status.

screen3.jpg

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2022

Can you include your complete script as it currently stands?

Also, a screenshot of the "Issue Linking"  page showing your "contains" link type. I need to see all 3 elements: "Name", "Outward Description" "Inward Description"

jun lee November 6, 2022

I attached current script and link type screen shot file.

 

2022-11-07 13 40 48.jpg

 

 

2022-11-07 13 38 01.jpg

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 7, 2022

Just replace "Contains" with "Hierarchy link (WBSGantt)" in the script and if should do what you want.

jun lee November 7, 2022

Successed!   

Thank so~~~~~~~~~~o much.

Suggest an answer

Log in or Sign up to answer