Question about correctness of groovy

Katarzyna Kacperska April 18, 2024

I wanted to limit the list of issues to only those with link type "Epic-Story Link"  using the "collect" command. I used this code:

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = issueLinkManager.getOutwardLinks(issue.id).collect() { issueLink ->

        if (issueLink.issueLinkType.name == "Epic-Story Link") {
          issueLink
        } 
}
The syntax is correct but it doesn't work. Where am I making a mistake?

The code below works. Can't use if in collect command?

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def links = issueLinkManager.getOutwardLinks(issue.id).collect() { issueLink ->
          issueLink
        
}

links.each { issueLink ->
       
        if (issueLink.issueLinkType.name == "Epic-Story Link") {  
           ...
       }
}









2 answers

1 vote
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 26, 2024

findAll() will filter the collection. The closure must return a true or false to decide if the item in the collection should be returned

collect() will transform the collection (it's called "map" in many other languages). A collect() will always return the same number of items as the original collection it was called on. The closure can return anything and that's what the new collection will contain.

There is a method that combines the two. findResults{}.

This functions like a collect{} but only non-null values are kept in the resulting collection

0 votes
Aron Gombas _Midori_
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 18, 2024

I haven't tried, but IMO you need to use findAll() instead of collect().

The latter is not meant to be used for filtering.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events