Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

JIRA all comments using Scriptrunner.

Ash
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.
February 4, 2019

Hi Team,

 

We are using JIRA server 7.12.3 with Scriptrunner  5.4.5

I'm trying to pull all the comments for an issue into a scripted field and below is the code i'm using. 

How can get comments in a sequence fashion with author name, date and their comment.

 

Here is the sample code i have tried.

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.getComments (issue)
return (comment.authorFullName + comment.body + comment.created )

 

TIA!!

 

1 answer

1 vote
Antoine Berry
Community Champion
February 4, 2019

Hi Ash,

I am not sure what you want to achieve, but you can use this code in the "Script Console" of scriptrunner to update existing issues using a JQL request : 

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.comments.Comment
import java.text.SimpleDateFormat

// Your jql
jqlSearch = "key in (XXX-1,XXX-2)"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = userUtil.getUserObject('automation')
List<Issue> issues = null
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
} else {
log.error("Invalid JQL: " + jqlSearch);
}
def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()

for (Issue issue:issues){
//Do your thing
String comments = ""
for (Comment comment:ComponentAccessor.getCommentManager().getComments(issue)){
comments = comments + comment.getAuthorFullName() + " " + comment.getBody() + " " + new SimpleDateFormat("yyyy-MM-dd hh:mm").format(comment.getCreated()) + "\n"
}
log.debug(comments.trim())

If you need the code to update a custom field, I can provide that as well but you can find that easily.

Ash
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.
February 4, 2019

@Antoine Berry Thanks for your reply.

Sorry for the confusion. I'm trying to pull all the issue comments to a custom field.

Antoine Berry
Community Champion
February 4, 2019

@Ash In which cases do you want to do that exactly ?

If you need to update existing tickets you can use this : 

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.index.IssueIndexingService
import java.text.SimpleDateFormat

// Your jql
jqlSearch = "key in (XXX-1, XXX-2)"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser user = userUtil.getUserObject('automation')
List<Issue> issues = null
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)}
} else {
log.error("Invalid JQL: " + jqlSearch);
}
def changeHistoryManager = com.atlassian.jira.component.ComponentAccessor.getChangeHistoryManager()

for (Issue issue:issues){
//Do your thing
String comments = ""
for (Comment comment:ComponentAccessor.getCommentManager().getComments(issue)){
comments = comments + comment.getAuthorFullName() + " " + comment.getBody() + " " + new SimpleDateFormat("yyyy-MM-dd hh:mm").format(comment.getCreated()) + "\n"
}
def cfId = 12403
def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfObject = customFieldManager.getCustomFieldObject(cfId)
def cfValue = issue.getCustomFieldValue(cfObject)
cfObject.updateValue(null, issue, new ModifiedValue(cfValue, comments.trim()), new DefaultIssueChangeHolder())

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)
}

 Replace your JQL and the ID of your custom field (here 12403).

If you want to perform that action in a post-function or a script listener, only use the code inside the for loop.

Ash
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.
February 4, 2019
Antoine Berry
Community Champion
February 5, 2019

@Ash What do you think we need to update ? I think Nic's answer is in line with mine.

Have you tried it ? It should work fine.

Ash
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.
February 6, 2019

@Antoine Berry Hi I'm trying to pull all the comments of an issue to a custom field.

 I'm confused why do we need to use JQL in the script.

Antoine Berry
Community Champion
February 6, 2019

@Ash JQL is only necessary if you need to update the value of the field for existing issues.

When exactly do you need to update this field ? Each time there is a new comment ?

Ash
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.
February 6, 2019

@Antoine Berry

No, if i have a all comments field(which display all the comments on a issue  it will be easy for me to add that filed to my JQL filter so i can export to excel.

 

If i use this code, it will pull all the comments but not in a order.

It will be great if i can get all the comments in an order.

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.getComments (issue)
return (comment.authorFullName + comment.body + comment.created )

TIA!!

Ash
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.
February 6, 2019
Antoine Berry
Community Champion
February 7, 2019

You need to use Script Listeners then to update the field. If you are only exporting closed issues, you could add a listener on the Close Issue event.

If you absolutely need to update the field whenever a comment is added, you need to add a listener on the Issue commented event. As I said just use the code in the for loop : 

 String comments = ""
for (Comment comment:ComponentAccessor.getCommentManager().getComments(issue)){
comments = comments + comment.getAuthorFullName() + " " + comment.getBody() + " " + new SimpleDateFormat("yyyy-MM-dd hh:mm").format(comment.getCreated()) + "\n"
}
def cfId = 12403
def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfObject = customFieldManager.getCustomFieldObject(cfId)
def cfValue = issue.getCustomFieldValue(cfObject)
cfObject.updateValue(null, issue, new ModifiedValue(cfValue, comments.trim()), new DefaultIssueChangeHolder())

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)

Problem is, this is not enough if users can add a comment in a transition. In that case you need to add a post function in that transition.

Use the same code but add this if at the top : 

if (transientVars.containsKey("commentValue")){
Copy of the code above here
}

In the end, I would advise to trigger this listener on a specific event (Close Issue, or Create one) so it does not get too complex. Or use the code with the JQL request and run it just before your extract with "Script Console".

 

Hope that was clear enough

Antoine Berry
Community Champion
February 12, 2019

@Ash did you make it working ?

Ash
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.
February 12, 2019

@Antoine Berry Thanks for the followup .

Sorry, i did not try this code. We are looking for a field which has all the comments for a particular issue whether it is closed or commented while transition. This field should contain all the comments. So users can add that field in the issue navigator to export those values to excel. 

 

Thank you so much for your response.

Antoine Berry
Community Champion
February 12, 2019

That is what this code does, should you only call it when needed (i.e. when a comment is added).

I guess you could use javascript as well, the code will still be very similar. I would not recommend it though, since the field would be re-calculated all the time.

Antoine Berry
Community Champion
February 18, 2019

@Ash Have you tried the "Script field" functionality from ScriptRunner ? I haven't used it yet but it seems to fit perfectly your need. This is an auto calculated field that you cannot edit.

See https://scriptrunner.adaptavist.com/latest/jira/scripted-fields.html?utm_source=product-help

You could use a scripted field with the code I provided.

Regards

Suggest an answer

Log in or Sign up to answer