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

Accepted Answer
0 votes
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 Sign up to answer
Community showcase
Posted Tuesday in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

25 views 0 2
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