Scriptrunner - Last comment date for a public comment

Jasmin Cua December 19, 2017

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

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 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?

Jasmin Cua December 19, 2017

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. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 19, 2017

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()
    }
}
Jasmin Cua December 19, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 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
Jasmin Cua December 19, 2017

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

Jasmin Cua December 21, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 21, 2017

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

Ryan_McAlevey September 16, 2019

Hi Alexey,

Is there a way to display the last internal comment? I'm using the following script but it does not filter out customer comments.

 

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)

if(comment != null)
{
" Author: " + comment.authorFullName + " - " + comment.getCreated() + "<br>" + comment.body
}

 

jira guy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 14, 2019
comment.getGroupLevel() == "Service Desk Team"

Check to see if the above condition would work. Service Desk Team would be your service desk agent access group.

Suggest an answer

Log in or Sign up to answer