Add comment with attachment JSM

Hi,
I want to add a comment visible on the Customer portal, and include one of the attachments on the issue.
How can I do it? I planning to implement this on a Post-Function.

Hi @DanAlonso14 , i hope this can guide you on how to add the comment. https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post

Hi @imadero thank you for your answer. Yes I know how to add the comment, but how can I add an Attachment to that comment, that’s the real challenge. Any ideas?

I didn’t do it before, but maybe the solution is to add the attachment directly to the issue, and then in the comment body, reference this attachment. How i suppose it’s possible to do that:
On the API that send you before, the body can follow this Object, Atlassian document format. So probably in the comment body you can something similar to that:

"body": {
    "type": "doc",
    "version": 1,
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
            "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.",
            "type": "text"
          }
        ]
      }, 
{
  "type": "mediaGroup",
  "content": [
    {
      "type": "media",
      "attrs": {
        "type": "file",
        "id": "6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5",
        "collection": "ae730abd-a389-46a7-90eb-c03e75a45bf6",
      }
    },
    {
      "type": "media",
      "attrs": {
        "type": "file",
        "id": "6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5",
        "collection": "ae730abd-a389-46a7-90eb-c03e75a45bf6",
      }
    }
  ]
}
    ]
  }
}

I’m not sure if you can combine different Atlassian document formats on the same body, but could be an option to fit your needs.
The information about the media type is on Node - mediaGroup

Let me know if it works for you.

1 Like

Thank you @imadero
I was able to reproduce your example, but I cannot implement it until this bug is solved:
https://jira.atlassian.com/browse/JRACLOUD-72208

This is how I implement it with ScriptRunner, it will work as long as the attachment is already added in some previous comment:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;


JsonNodeFactory jnf = JsonNodeFactory.instance;
ObjectNode payload = jnf.objectNode();
ObjectNode body = payload.putObject("body");
A:{
    body.put("type", "doc");
    body.put("version", 1);
    ArrayNode content = body.putArray("content");
    ObjectNode content0 = content.addObject();
    ObjectNode content1 = content.addObject();
    
    B:{
        content0.put("type", "paragraph");
        content = content0.putArray("content");
        content0 = content.addObject();
        C:{
            content0.put("text", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.");
            content0.put("type", "text"); 
        }
        
        content1.put("type", "mediaGroup");
        content = content1.putArray("content");
        content1 = content.addObject();
        D:{
            content1.put("type", "media");
            ObjectNode attrs = content1.putObject("attrs");
            attrs.put("type", "file");
            attrs.put("id", "4d6a6bgg8f-b7f9-4b6b-b367-09e93369b234");
        }
       
    }
}

logger.info(body.toString())

def resultComment = post("/rest/api/3/issue/ABC-10775/comment").header("Content-Type", "application/json").body(payload).asObject(Map)

Thanks for your help.
Daniel