Scripted field - last unrestricted comment date but only if user is in certain a group

Ken April 19, 2019

Hi all,

we are using the following scripted field to get the date of the last unrestricted comment:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.component.ComponentAccessor
import java.util.Comparator;

def commentManager = ComponentAccessor.getCommentManager()
def commentList = commentManager.getComments(issue).sort{it.getCreated()}
Collections.reverse(commentList)
for (Comment comment : commentList) {
if (comment.getRoleLevelId() == null && comment.getGroupLevel() == null) {
return comment.getCreated()
}
}
return null

 

Additionally, we would like for this script to only return a date if the user who added the comment is in a certain group. Is this possible?

Thank you for your assistance.

Best regards

2 answers

1 accepted

1 vote
Answer accepted
Wade Tracy _Boise_ ID_
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 19, 2019

I'm not in a position to actually test the code at the moment, but it should be possible.  Comment can return the ApplicationUser that authored the comment.  Then you just need to create a GroupManager the same way you created the CommentManager.  GroupManager has a function for determining if a user is in a group.  It would look something like this:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.component.ComponentAccessor
import java.util.Comparator;

def commentManager = ComponentAccessor.getCommentManager()
def groupManager = ComponentAccessor.getGroupManager()
def commentList = commentManager.getComments(issue).sort{it.getCreated()}
Collections.reverse(commentList)
for (Comment comment : commentList) {
if (comment.getRoleLevelId() == null &&
comment.getGroupLevel() == null &&
groupManager.isUserInGroup(comment.getAuthorApplicationUser(), 'Group Name')) {

return comment.getCreated()
}
}
return null

 

 Hope that helps!

Ken April 23, 2019

Thank you Wade. This worked without any issues.

Ken April 23, 2019

Hi Wade,

please ignore the other notifications, I was able to resolve the other issues I had using the correct searcher for the field. In this case Date Time Range Picker.

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 19, 2019

For that, you will want to use GroupManager

[...]
def groupManager = ComponentAccessor.groupManager
def group = groupManager.getGroup("name of your group")
[...]
if (comment.getRoleLevelId() == null && comment.getGroupLevel() == null) {
if(groupManager.isUserInGroup(comment.authorApplicationUser, group)) {
return comment.getCreated()
}
}


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 19, 2019

Found and corrected a typo  

comment.authorApplicationUser was comment.authorApplicatioUser

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 19, 2019

Also, the other signature of isUserInGroup(ApplicationUser, String groupName) as suggested by Wade will work. 

Ken April 23, 2019

thank you Peter

Suggest an answer

Log in or Sign up to answer