How to add comment of issue during transition using cloud jira REST API

jay
Contributor
August 29, 2024

How to add comment of issue during the isssue transition from one state to another. Eg: in progress to resolved. For issue transition i am using below  rest url .But I am unable to add comment on it.

 

 


/rest/api/2/issue/{issueIdOrKey}/transitions

2 answers

1 accepted

3 votes
Answer accepted
Pears Whims
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.
August 29, 2024
Robert DaSilva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2024

Great option if comment details need to be provided as part of the REST request.

Like Pears Whims likes this
jay
Contributor
August 29, 2024

Thanks @Pears Whims it worked. I have a small question to you. Now I am able to save comment on the issue transition. In my case it is from 'in-progress' to 'resolved'. Is there any way to find the exact comment which was saved during this transition. Because the comment which saved during transition also saved as a normal comment. So is there any way to find whether it is a comment added during transition time.

Pears Whims
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.
August 29, 2024

Hey @jay 

Glad it did work!

User Interface Solution - Activity Tabs

In the Activity tab, you may find transitions and comments displayed together.

Mind there might be a pitfall with this approach: Jira may group actions that occur close together in the activity stream, making it difficult to isolate comments added during a transition. They might appear bundled with other actions.

 

API Solution - REST

If you’re looking for a more API-focused approach, you can use the Jira REST API to retrieve the current status of an issue. This might assist in tracking transitions:

/rest/api/2/issue/{issueIdOrKey}/?fields=status

Beware this might have similar issue as with UI if Jira decides to aggregate actions.

 

Special Cases

In case You have those apps as Jira Misc Workflow Extensions (JMWE) they introduce special Transitions tabs that display detailed transition history.

Previously Jira Cloud UI also had Transition tab, yet this UI may not be longer available. Sharing this just if some dark feature flags can activate this. https://community.atlassian.com/t5/Jira-questions/JIRA-Issue-Transition-view/qaq-p/1026872

Let me please know if this helps.

Pears.

jay
Contributor
August 29, 2024

@Pears Whims  I am looking for REST API approach. I will give you a detailed explanation. I have an issue and I added 5 comments already in it. After that I moved the issue from "in-progress " to "resolved" and along with that I added a resolved comment too using REST API. But now when I fetch the issue details I am getting these 6 comments(old 5 comments + Resolved comment) together. What I want is I am looking for a solution to find the comment which I added during the transition(Resolved comment) using REST API only. is there any way? 

Pears Whims
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.
August 29, 2024

Hi @jay ,

I spent some time working on a solution, and unfortunately, I can’t provide an out-of-the-box answer. It seems this may not be directly supported by Jira.

You might need to work with the changelog entries for the issue, which you can retrieve via the REST API:

GET rest/api/3/issue/ISSUE-KEY?expand=changelog

Key Paths in the JSON Response

  • Transitions: result.changelog.histories[].items[]
  • Comments: result.fields.comment.comments[]

In my setup, I couldn’t verify if the histories array entries contain comments, but it’s worth looking closely in your case.

Worse Case - Changelog Histories Don’t Contain Comments

If your setup doesn’t include comments in the changelog histories, here’s what you can do:

  • Fetch the changelog object.
  • Manually match the timestamps from the status transitions in the changelog with the created timestamps in the comment objects.

Let me please know if you find this helpful.

Cheers.

jay
Contributor
August 29, 2024

Thank you @Pears Whims . Thank you for your effort and help.

Like Pears Whims likes this
jay
Contributor
August 29, 2024

@Pears Whims  One more question to you.. Now I am able to save the comment .. But I am not able to save the comment's author name in that api. is there any way to save the author of the comment using jira rest api?

Pears Whims
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.
August 29, 2024

Hi @jay ,

Great You are making progress!

Let me try to capture how I understand the follow-up question.

 

You are able to use Transitions: result.changelog.histories[].items[]

  • You may capture transition
  • content of the comment

But the comment author is missing there.

 

If this is the case, I can see that result.fields.comment.comments[] does contain author in my instance:

"comment": {
"comments": [
{
"author": {
"displayName": "Pears Whims",
"emailAddress": "XXXXXXXXXX"
},
"body": "A test comment"
}
]
}


How does this work for Your case?

Pears.

jay
Contributor
August 29, 2024

@Pears Whims Thanks and But not like this. When I add a comment to issue using the REST API i am bot able to add the author tag. I am getting error.  So what happened is when I add comment without the 'Author' tag it is fetching the author name from my Jira API's credentials. 

I am using the below api to add comment to issue

url:/rest/api/3/issue/{issueIdOrKey}/comment

Pears Whims
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.
August 30, 2024

@jay,

If I understand You may seek ways to impersonate another user via API. I am afraid this is not supported. Please check these posts and feature requests linked:

Let me please know if that answers.

Pears.

jay
Contributor
August 30, 2024

@Pears Whims  This is what I was looking. Now I got the answer that it is not possible to save another username when I add the comment. Got it. Thank you so much for your effort.

Like Pears Whims likes this
Pears Whims
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.
August 30, 2024

I am happy to help 🤜🤛.

jay
Contributor
August 30, 2024

@Pears Whims I forgot to update you one thing regarding your previous answer. https://community.atlassian.com/t5/Jira-questions/Re-Re-How-to-add-comment-of-issue-during-transition-usi/qaq-p/2798728/comment-id/1047677#M1047677

 

GET rest/api/3/issue/ISSUE-KEY?expand=changelog

Key Paths in the JSON Response

  • Transitions: result.changelog.histories[].items[]
  • Comments: result.fields.comment.comments[]

In my setup, I couldn’t verify if the histories array entries contain comments, but it’s worth looking closely in your case.

I checked in the changelog of an issue using the REST API. But I couldn't find the the comments in the histories items. I can see attachments and some other data but comments are not present there. Do you know why this is not coming? Do we have to do any extra settings

Pears Whims
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.
August 30, 2024

I took a guess that the ability to leave a comment during a transition might be tied to the presence of certain fields in the transition screen, such as whether or not it includes comments. In my environment, I couldn’t verify this quickly - yet that's why I did mention the environment.

As for the output of histories and changelog items, it shall be environment independent.

Cheers.

jay
Contributor
August 30, 2024

@Pears Whims  Now I understood What you said. Thank you so much.

Like Pears Whims likes this
jay
Contributor
September 2, 2024

@Pears Whims  if you don't mind can I ask one more question.

When I create a comment using the REST API, I must include attachment information. So I included the attachment id (10050), which I obtained from the upload attachment response. But when I enter this value, Jira reports a "attachment validation error".
Then I created a comment on Jira and used the REST API to retrieve all of the comment details, returning a lengthy result as attachment id. That's ""16ffe152-698f-5ed8-aa8a-f04b90f0c748." Next, I created a comment using this attachment id, which succeeded. However, I'm not sure what this attachment id is or how to retrieve it.

"content": [
{
"type": "media",
"attrs": {
"type": "file",
"id": "10050",
"collection": "",
"height": 183,
"width": 200
}
}

Pears Whims
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.
September 3, 2024

Hey @jay ,

Thanks for reaching out.

 

World of the IDs

Attachment ID (10050): This is the ID you get when you upload an attachment via the Jira REST API. It’s used within Jira for referencing attachments within the issue itself.

Media ID (16ffe152-698f-5ed8-aa8a-f04b90f0c748): This longer, GUID-style ID is used within the Atlassian Document Format (ADF), particularly when embedding media in comments or descriptions.

 

Attachment Validation Error

This may be due to the fact, that the REST API, Jira could expect the Media ID, not the Attachment ID.

This would make sense because the content in comments uses the ADF format, which requires the Media ID for embedded files.

 

Obtaining the Media ID

To retrieve the Media ID after uploading an attachment:

1. Use the Attachment Endpoint: After uploading an attachment, use the endpoint /rest/api/3/attachment/content/{id} to get the media ID. This endpoint will return a 3xx status, redirecting you to a URL that contains the media ID.

2. Fetch the Media ID from the Redirect URL: The redirected URL you receive from the attachment content endpoint will include the media ID as part of the URL. You can extract this ID to use in your comment.

Example:

curl --head --request GET "https://your-domain.atlassian.net/rest/api/3/attachment/content/217145"

The Location header in the response will contain the Media ID.

3. Use the Media ID in Your Comment: Once you have the Media ID, you can embed it in your comment’s content when using the REST API to create or update a comment.

 

To wrap this up

  • Step 1: Upload the attachment and get the attachment ID.
  • Step 2: Use the attachment ID with /rest/api/3/attachment/content/{id} to fetch the Media ID.
  • Step 3: Use the Media ID to embed the attachment in a comment using the ADF format.

 

References

 

P.S. New Questions

You may consider a separate threads for those follow-up questions, like this one.

Otherwise only few people in the discussion got the notification, thus You are limiting Yourself to getting answer from just 2 people instead of whole awesome community.

 

Please let me know if that's help!

 

Best of luck,
Pears.

jay
Contributor
September 3, 2024

@Pears Whims  Thank you it worked for me, I will create new thread for new questions . Thank you.

Like Pears Whims likes this
Pears Whims
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.
September 3, 2024

Thank You jay!
Great it did help!

0 votes
Robert DaSilva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2024

Hi @jay , if you want the same comment added to the Jira Issues after the transition is complete, I would recommend creating a Jira Automation routine to handle this.

You can do this in two ways, either on a per-project basis, or as a global rule on the entire Cloud site. I would recommend starting on a per-project basis and getting help from your organization's administrators to elevate this to a global rule if that's deemed necessary.

The rule should look something like below.

Trigger: Issue Transitioned

  • You are able to specify the "From Status" and "To Status" optionally, to have this rule apply to whichever transitions you need.

Action: Comment on Issue

  • Here you are able to add the details of the comment you want added. You have the ability to use "Smart Values" too, if you need some level of dynamic data.

Screenshot 2024-08-29 at 10.40.45 AM.png

 

This rule should apply to individual issues as they transition from one status to another in your chosen project. As always, I would recommend testing this in a test environment before creating it in your Live Production environment.

jay
Contributor
August 29, 2024

thank you @Robert DaSilva

I will check this and update you.  I have a small question to you. Now I am able to save comment on the issue transition. In my case it is from 'in-progress' to 'resolved'. Is there any way to find the exact comment which was saved during this transition. Because the comment which saved during transition also saved as a normal comment. So is there any way to find whether it is a comment added during transition time.

Robert DaSilva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2024

@jay If I recall, the comment will be added by the "Automation for Jira" user, but you are also welcome to add a specific string of text to the comment itself to indicate it was added via a transition.

Something like:

Issue transitioned automatically from In Progress to Resolved.

Comment added by automation.

jay
Contributor
August 29, 2024

Now I added the comment using the REST API only not using the automation. Now you suggest to add the indication string in the comment body right? But other than that is there any solution. Because we need to preserve the exact comment saved by the user. Is there any label or tag to add the string.

Robert DaSilva
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 29, 2024

@jay Unfortunately I do not believe that is possible. If you're using a "Service Account" of sorts, that might work, but the easiest way is appending something to the comment body to indicate it was an automatically created comment.

jay
Contributor
August 29, 2024

Thank you @Robert DaSilva .

Like khompitoonchaloem5 likes this

Suggest an answer

Log in or Sign up to answer