It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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!

Thank you Wade. This worked without any issues.

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.

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()
}
}


Found and corrected a typo  

comment.authorApplicationUser was comment.authorApplicatioUser

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

thank you Peter

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

Managing Incidents - The roof is on FIRE!

The roof is on FIRE… network outages, broken processes, upset clients and employees. Each day seemed to bring more and more issues. Incidents were communicated via email, messengers (skype or teams) ...

121 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you