Hi and thank you for taking the time to read my question!
I want to add a comment to an issue through the API.
I can successfully add a comment to an issue and I can also successfully add an attachment to the issue. The problem is that I want to add an image IN a comment. On the Jira Website you can drop an image or add one through an explorer, so it is possible.
When I query an issue with an image as comment I see it as:
"content": [
{
"type": "mediaSingle",
"attrs": {
"layout": "center"
},
"content": [
{
"type": "media",
"attrs": {
"id": "4f190z82-945d-4be6-cb21-9a815fb7f1v",
"type": "file",
"collection": "",
"width": 200,
"height": 183
}
}
]
}
]
I thought of adding a comment with this structure, but I'd need the ID to link the attachment. The ID of the comment attachment is super long ("4f190z82-945d-4be6-cb21-9a815fb7f1v") while the ID of the uploaded attachment is much shorter (23575).
Is there something I'm missing, or is adding an image as comment not possible through the API?
Insert it like external url
version: 1, type: 'doc', content: [ { "type": "mediaSingle", "content": [ { "type": "media", "attrs": { "type": "external", "url": "https://yordomain.atlassian.net/rest/api/2/attachment/content/10013", "width": 710, "height": 163, } } ] } ]
works perfect
You are a real king. At least I don't need use any media api crazyness!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This one took me a while to figure out with a lot of trial-and-error, so I feel I should share to save the world some time.
I am using Jira Cloud, so all the documentation pushes you to V3 API, which uses Atlassian Document Format. There is a section for embedding attachments under Node - media, however the documentation says you need to pass in an ID which you can get by "...querying the media services API to retrieve metadata, such as, filename. Consumers of the document should always fetch fresh metadata using the Media API." I have found no documentation on a "Media API", so I gave up on V3.
However, you can use the V2 Add Comment API in Jira Cloud.
First you call the Add Attachment API.
POST /rest/api/2/issue/{issueIdOrKey}/attachments
Response (trimmed for brevity)
{
"self":"https://your-domain.atlassian.net/rest/api/2/attachments/12345",
"id":"12345",
"filename":"image-20210221-170959.png",
"content":"https://your-company.atlassian.net/secure/attachments/12345/picture.jpg",
"thumbnail":"https://your-company.atlassian.net/secure/thumbnail/12345/picture.jpg"
}
Using the "content" or "thumbnail" value, call the V2 Add Comment API and surround the value with ! on either end:
POST /rest/api/2/issue/{issueIdOrKey}/comment
{
"body": "Here is my image !https://your-company.atlassian.net/secure/attachment/12345/image-20210221-170959.png!"
}
This also works if you are adding comments via the "External System Import":
JiraKey,Summary,Comment
TP-3,"Test API Issue","2021-02-21 03:00:00;5e5e5e5e5e5e5e5e5e5e5e5e;Here is my image !https://your-company.atlassian.net/secure/attachment/12345/image-20210221-170959.png!"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Workaround using only V3 API:
PHP sample code:
$r1 = \Unirest\Request::get('/attachment/'.$attachment->id, [
'Accept' => 'application/json'
]);
\Unirest\Request::curlOpt(CURLOPT_FOLLOWLOCATION, false);
$r2 = \Unirest\Request::get($r1->body->content);
\Unirest\Request::curlOpt(CURLOPT_FOLLOWLOCATION, true);
if ($r2->code === 302 && isset($r2->headers['location']) &&
preg_match('|/file/([^/]+)/|', $r2->headers['location'], $matches)) {
$mediaId = $matches[1];
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can attach a file or image via the REST API in Jira. The thing to understand about doing this though is that it is not the exact same REST endpoint used to post a comment to an issue in Jira. I understand that since you can drag/drop an image in the comment box of Jira's web UI the appearance might be that you can do this in a single go. But when you do that, the image is actually being uploaded to the issue before you actually submit your comment. (You can test this by dragging an image file into a comment in Jira, and then not actually submitting your comment by then pressing 'Cancel', the image will still remain attached to the Jira issue).
So there are actually two different steps happening here, even in the web UI. In your case, I believe you're going to need to call two separate rest endpoints to make this work as well. Also you would need to make sure that the file attachment endpoint is being called first.
The KB on How to add an attachment to a JIRA issue using REST API explains how to upload images to Jira via the REST API.
Once the file is attached to the issue in question, then the steps need to reference that image in the comment is just using the wiki markup syntax to reference the filename of that image. So in Jira, if your attachment name was testimage.png, you can make your comment look something like this
test message here
!testimage.png|thumbnail!
see my image above
I hope this helps.
Andy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
As Andy said, you have to attach the image to the issue first and know what its ID is before you can refer to it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.