Get the Content of the Last Comment

Fabio Rodrigues July 21, 2017

Is it possible to create a script with "ScriptRunner" that runs a "JQL" and displays the last comment of the filtered issues?

2 answers

0 votes
Szymon F December 4, 2020

Hi, 

I am trying to achieve the same.

  1. SR runs the JQL 
  2. Based on JQL return the last/array of issue key and last comment

Based on research I have code which gives me the issue keys but does not know how to add comments to this. Some errors I got referring to the arrays that could not be created.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.component.ComponentAccessor

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def query = jqlQueryParser.parseQuery("project = XXX and labels = YYY")
def issue_results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

def issueList = issue_results.issues.collect{issueManager.getIssueByCurrentKey(it.key,)}

issueList.each{
def issue = it as MutableIssue
}

If you could help I am thrilled.

Best,

Simon

0 votes
Steven Behnke
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.
July 21, 2017

How would you expect JIRA to show this information?

Fabio Rodrigues July 21, 2017

Actually I would like to analyze the content of the last comment and check if I was mentioned there (@username). I would like to get a list of issues where I was mentioned in the last comment.

Steven Behnke
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.
July 21, 2017

It sounds like you're looking to create a JQL function then? You'd like to write a query like issueFunction in mentionedInLatestComment(currentUser()) and be returned a list of issues that match this function?

Fabio Rodrigues July 21, 2017

Yes, exactly!

Steven F Behnke
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.
July 21, 2017

You're looking for this ScriptRunner module: https://scriptrunner.adaptavist.com/latest/jira/custom-jql-functions.html

I have created JQL functions to search on Projects, Users, and Versions before, but I haven't tried to extract @mentions from comments.

You're likely going to need to use CommentManager.getLastComment(Issue issue) to obtain the last comment for each issue, then you can use Comment.getBody() to obtain the comment text. Finally, you can parse the content to search for the mention format, which is stored like this: [~username]. Once you extract this, you can compare the mentions to the argument passed to the JQL function.

Suggest an answer

Log in or Sign up to answer