Scriptrunner - Last comment date for a public comment

Hi, 

How do I get the last comment date for a public comment only? 

Right now, I am using this script I found here, but it doesn't take into account whether the comment is public or private. I want to get the last comment date for a public comment only. 

import com.atlassian.jira.ComponentManager

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

def commentManager = ComponentAccessor.getCommentManager()
def comment = commentManager.getLastComment (issue)
def date = comment.getCreated()

return (date)

Thank you!

1 answer

1 accepted

0 vote
Alexey Matveev Community Champion Dec 19, 2017

Hello,

What do you mean by public and private comment? Do you mean share with customer comment and internal comment in Jira Service Desk?

Hi Alexey, 

Yes, something like that. We have comments shared with the customer/client (public) and some are restricted to an internal group only (private), for example, a developer group. 

Thank you. 

I guess you are not using Jira Service Desk and private means that the comment was set either role or group visibility. In this case your code would look 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.component.ComponentAccessor
import java.util.Comparator;

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

Hi Alexey, 

Sorry, yes, we are not using Jira service desk, just Jira software. 

I tried your code but I am getting an error message on this line - 

commentList.each{

 [Static type checking] - Cannot return value of type java.util.List <com.atlassian.jira.issue.comments.Comment> on method returning type java.util.Date

I am using the Date Time Picker template. 

Sorry, I am not familiar with Scriptrunner at all. 

Thank you for all your help. 

Alexey Matveev Community Champion Dec 19, 2017

Could you try 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.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

It worked! Thank you so much, Alexey. :)

Hi Alexey, 

What are the values that this command  return?

comment.getGroupLevel()

 Is there a documentation where I can check these values? 

Thank you so much.

Alexey Matveev Community Champion Dec 21, 2017

I am afraid there is no documentation. You just try to restrict your comment to a group and check what is returned.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,337 views 14 20
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot