I’m using ScriptRunner to add a scripted validator to a workflow transition. I’m having some issues getting the expression I need correct and hoping someone can help.
What I’m trying to do:
- Check current issue type; should be Bug or Story
- Check the linked issues to see if at least one is either a Test or a Test Set
- If both return true, allow the transition
The following expression code runs perfectly until I try running it against a Bug or Story that doesn’t have any linked issues. It then returns true, even though it should be false. What do I need to add/change to make this return false if there are no linked issues?
issue.issueType.name.match('^(Bug|Story)$') != null
&&
issue.links.map(l => l.linkedIssue)
.filter(s => s.issueType.name.match('^(Test|Test Set)$'))
.length == issue.links.map(l => l.linkedIssue).length
The second part of your condition:
issue.links.map(l => l.linkedIssue)
.filter(s => s.issueType.name.match('^(Test|Test Set)$'))
.length == issue.links.map(l => l.linkedIssue).length
Counts the number of linked issues of type (Test|Test Set) and compares this number with number of all links. If there are no links the number will be 0 in both cases and 0 == 0 is true so your condition returns true if there are no links.
So I suggest adding issue.links.length > 0
.
Your 2nd condition will return false if there are any additonal issue links that do not point to Test | Test Sets. E.g. if there is a “duplicates” link to another bug, the condtion will return false. If that’s what you intend, the expression would be:
issue.issueType.name.match('^(Bug|Story)$') != null
&&
issue.links.length > 0
&&
issue.links.map(l => l.linkedIssue)
.filter(s => s.issueType.name.match('^(Test|Test Set)$'))
.length == issue.links.map(l => l.linkedIssue).length
If you only want to check if at least one link to a Test | Test Set exists, it could be written as:
issue.issueType.name.match('^(Bug|Story)$') != null
&&
issue.links.length > 0
&&
issue.links.map(l => l.linkedIssue)
.filter(s => s.issueType.name.match('^(Test|Test Set)$'))
.length > 0
1 Like
Thank you! I was a PHP and python dev for 20 years, but the functional programming stuff is completely new to me and I’m stumbling my way through.