Pull in all comments for an issue

I have found sample code for pulling in comment timestamps as well as authors.

Does anyone have any sample code for pulling in all comments related to an issue so they can be displayed on the screen?

I am using the latest comment field which is added as a custom field to the overall Settings for Last Comment Date and Last Comment Text.

Thanks in advance,
Dennis

For anyone else looking to do this here is one option:

[jira.customfield_all_comments]
name = "All Comments"
data_type = "text"
javascript_code = '''
var commenta = new Array();
var allcomments = issue.fields.comment;
if (allcomments && allcomments.comments ) {
  var comments = issue.fields.comment.comments;
  for (var i = 0; i < comments.length; i++) {
    var comment = comments[i];
	  var commentdate = comment.created.toString().substr(0,19);
      commenta.push( commentdate + ', ' + comment.author.accountId + ', ' + comment.author.displayName + ', ' + comment.body );
      }
  issue.fields.customfield_all_comments = commenta.join("\n");
}
'''

This will provide the date / time stamp - author account id - author display name - comment body all on individual lines within the report.

The will show as oldest first and newest on the bottom.

2 Likes

Hi @DennisT

It is great that you already found the answer to your own question and shared it with others.
Sharing is the real deal in communities!

Btw, that is the right way to import all comments in a property for each issue dimension member (by using a calculated field with Javascript in advanced settings)

Well done!
Martins / eazyBI support

Hello @martins.vanags

This is very close to what I’m trying to accomplish, however I would need each comment to be in the same row according to the date they were posted instead all in the same row, please see the images bellow. Essentially I’d like to separate the comments by date.


@nolivaiko

eazyBI does not support importing a separate “Comment” dimension as you might expect.
If you need to count comments by days, you could try a different Javascript for field definition.
That would let you see the count of comments per day.

Martins / eazyBI

Hi @martins.vanags

I was able to pull the comments of assignees and reporters but instead if I want of pull the last comment of a particular issue irrespective of reporter or assignees.
How to pull that.

Hi,

I built this in the Advanced settings for eazyBI so they data is there as a measure when we want to display just the last comment information.

Split into three measures so we have the comment text, comment date, and comment author

Code I used is as follows:

# Pulls the last comment made on an issue
[jira.customfield_lastcommentt]
name = "Last comment text"
data_type = "text"
javascript_code = '''
if (issue.fields.comment && issue.fields.comment.comments && issue.fields.comment.comments.length > 0)  {
  var lastcomment = issue.fields.comment.comments[issue.fields.comment.comments.length-1];
  issue.fields.customfield_lastcommentt = lastcomment.body;
}
'''

# Pulls the last comment date from an issue
[jira.customfield_lastcommentdate]
name = "Latest comment date"
data_type = "datetime"
json_fields = ["comment"]
javascript_code = '''
var comments = issue.fields.comment.comments;
if (comments.length > 0) {
  var comment = comments[comments.length - 1];
  issue.fields.customfield_lastcommentdate = comment.created;
}
'''

# Pulls the last comment author from an issue
[jira.customfield_lastcommentauthor]
name = "Latest comment author"
data_type = "text"
json_fields = ["comment"]
javascript_code = '''
var comments = issue.fields.comment.comments;
if (comments.length > 0) {
  var comment = comments[comments.length - 1];
  issue.fields.customfield_lastcommentauthor = comment.author;
}
'''

Hi @DennisT

But please find the below screenshot its showing:

Hi,

My guess would be the quotes getting modified when posting here.

I would copy the code from here then paste into notepad or something to get the double quotes and single quotes formatted properly.

@DennisT
Your guess makes sense.
These are not correct quotes for definition in advanced settings.
See syntax examples here: JavaScript calculated custom fields

Martins / eazyBI

Hi @DennisT

Thank you that worked perfectly


here issue comment displaying in advance setting test but in the report the commant are not displaying

Hi @Yashwanthkumar_Shiva

When defining a new calculated field in the import options you should use the following Javascript code to return the last comment:

if (issue.fields.comment && issue.fields.comment.comments && issue.fields.comment.comments.length > 0)  {
  var lastcomment = issue.fields.comment.comments[issue.fields.comment.comments.length-1];
  return lastcomment.body;
}

Nauris

I nee the code where comment of the issue list or disply in the reports

Hi @Yashwanthkumar_Shiva

When you have defined the custom measure, you can select to import it as a property and run the import. After the import finishes, you will be able to select this property in the report and see the last comment for the issue:

Let me know if you need additional help with setting this up!
Nauris

Thank you @nauris.malitis This was helpfull

Hey guys,

Here is the code for advanced settings to import last 10 comments in descending order:

[jira.customfield_l10comm]
name="Last 10 comments"
data_type = "string"
json_fields = ["comment"]
javascript_code = '''
var commenta = new Array();
var allcomments = issue.fields.comment;
if (allcomments && allcomments.comments) {
var comments = issue.fields.comment.comments;
var commentl = comments.length
 
for (var i = commentl-1; i > commentl-11 && i>=0; i--) {
var comment = comments[i];
commenta.push(comment.body.substr(0,255) + " (" + comment.author.displayName + ", "
+ comment.created.substr(0,10) + ")");
}
issue.fields.customfield_l10comm = commenta.join("\n__\n");
}
'''

Martins / eazyBI

Here is a similar code to import all comments with a specific string somewhere in the comment body

[jira.customfield_upd_com]
name="Comments with Update string"
data_type = "string"
json_fields = ["comment"]
javascript_code = '''
var commenta = new Array();
var allcomments = issue.fields.comment;
if (allcomments && allcomments.comments) {
var comments = issue.fields.comment.comments;
var commentl = comments.length;
var substring = "Update:";
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if(comment.body.toLowerCase().includes(substring.toLowerCase())){
commenta.push(comment.body.substr(0,255) + " (" + comment.author.displayName + ", "
+ comment.created.substr(0,10) + ")");
}}
issue.fields.customfield_upd_com = commenta.join("\n");
}
'''