Latest comment update using JIRA API

Hello,

I am trying to create a script that will send an email when there is a new comment added to JIRA story but i was unable to come up with good logic to know if the comment is new/latest comment or not. I am open to suggestions. Any help would be appreciated.

One logic I thought off:
<> to compare the current day timestamp with last comment timestamp to know if the comment is latest or not.

Thanks
Peter

Hello @peter1 :slight_smile: I suggest looking at the webhooks feature - You’ll be notified whenever the comment has been added to the issue.

https://developer.atlassian.com/cloud/jira/platform/webhooks/#comment-webhooks.

Thank you for the reply. i am exploring the other ways of doing it instead of using webhooks.

Welcome to the Atlassian developer community @peter1,

I think your stateful approach makes sense. If your client “remembers” the last timestamp it saw, then you can check against the latest comment. In case you hadn’t figured it out, when you get comments using GET /rest/api/3/issue/{issueIdOrKey}/comment, you can pass a couple parameters to get the latest. Use orderBy=-created to sort descending by create date and maxResults=1 if you only care about the last one. Hopefully, that helps keep your client logic simple.

3 Likes

hi @ibuchanan thank you for the reply. I have question - what if there are 2 comments in the same day. I think i need to check the timestamp of previous comment and latest comment and check the latest comment has the current date or not.

for ex,

Ticket ABC have the below comments.

comment 1 - March 28 2:30 PM
comment2 - March 28 2:45 PM
comment3 - March 28 3:20 PM

@peter1, I think I misunderstood your approach. After your question, I’ve reread and I have a slight adjustment to suggest.

Your client should keep a map of issues to “last comment seen date”. What I mean is use the date from the last comment, not any local system clock time. That keeps all timestamp comparisons based on Jira’s timestamp formats and system clock. This will avoid complexities of timezones and clock synchronization.

To play through an example, let’s suppose ABC-1 has the 3 comments you suggested:

  1. The first time the client runs is about 2:40PM. The client has never seen any comments. It finds comment 1 as the latest and the 2:30PM timestamp (in code, this should be a full datetime type, but I’m using time short-hand to explain the logic). It sends the email. The client remembers the comment’s timestamp: commentMap["ABC-1"]="2:30PM"
  2. The next time the client runs is about 2:50PM. It finds comment 2 as the new latest with a new timestamp at 2:45PM. The client checks “2:45PM > 2:30PM” and recognizes it as a new comment, so it sends an email. The client updates the map: commentMap["ABC-1"]="2:45PM"
  3. The client runs again around 3:00PM. The latest comment is still 2:45PM. “2:45PM > 2:45PM” is false, so no new email and no update to the map.

… repeat every 10 mins.

What do you think?

@ibuchanan I thought the same way but when i started using the comments in jira python api, for example, if a story has 4 comments with different comment bodies, it was printing output 4 times with the last comment body, all those 4 times.

Code:

   for com in gen3_issues.fields.comment.comments:
       comment = com.body
       time = com.created
       comment_dict['Jira No'] = gen3_issues.key
       comment_dict['Issue ID'] = gen3_issues.id
       comment_dict['comment'] = comment
       comment_dict['Time'] = time
       gen3_comments.append(comment_dict)
for i in gen3_comments:
    if parser.parse(i['Time']).strftime("%Y-%m-%d") == current_time:
        updated_Stories.append((i.keys(), i.values()))
        print(i.keys(), i.values())

Output:

dict_keys(['Jira No', 'Issue ID', 'comment', 'Time']) dict_values(['TOOL-2517', '11597655', 'testint comment 12', '2022-03-29T16:40:48.434-0400'])
dict_keys(['Jira No', 'Issue ID', 'comment', 'Time']) dict_values(['TOOL-2517', '11597655', 'testint comment 12', '2022-03-29T16:40:48.434-0400'])
dict_keys(['Jira No', 'Issue ID', 'comment', 'Time']) dict_values(['TOOL-2517', '11597655', 'testint comment 12', '2022-03-29T16:40:48.434-0400'])
dict_keys(['Jira No', 'Issue ID', 'comment', 'Time']) dict_values(['TOOL-2517', '11597655', 'testint comment 12', '2022-03-29T16:40:48.434-0400'])

@peter1,

I’m not sure about the data you want in the email. Or if your client needs to remember all the comments, or just the last one. But I think you should design the dictionary keys to be unique for what your email needs. It seems like you have more keys than you need and that could be confusing to loop over. For example, key and id correspond to the same issue.

2 posts were split to a new topic: How do I update comments in a workflow post-function?