3

我正在为 Jira 编写一个 groovy 脚本——我正在从一个问题中收集评论列表,并存储最后一条评论的用户名。

例子:

import com.atlassian.jira.component.ComponentAccessor
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)
if (comments) {
comments.last().authorUser
}

有时,我不想存储用户名(如果它属于预定义角色)。基本上,我想先检查评论,然后从列表中删除任何符合我标准的评论,然后调用 last().authorUser。就像是:

comments.each {
if (it.authorUser.toString().contains(user.toString())) {    
// Here is where I'd want to remove the element from the list**
}
}
comments.last().authorUser  // then store the last element as my most recent comment. 

有道理?我是 Groovy 的新手——所以我完全怀疑这会让人头疼。我遇到的大多数示例都涉及数字检查……有点难过。

4

2 回答 2

4

您可以使用Collection.removeAll():它通过删除与传递的闭包条件匹配的元素来修改集合。

comments.removeAll {
    it.authorUser.toString().contains(user.toString())
}
comments.last().authorUser
于 2013-10-09T18:52:28.800 回答
1

现在可以使用 Java 的removeIf { it -> it.isNotDog() }. 请参阅SO上的一些示例。

于 2019-02-18T07:37:01.913 回答