/
Copying rich formatted description or Atlassian Document Format (ADF) example

Copying rich formatted description or Atlassian Document Format (ADF) example

Problem

You might assume it would be simple to copy the description from one issue to another. However, if the description contains attachments then it becomes more complex. For example, the issue below has 5 attachments and some formatted text. The text is formatted using the Atlassian Document Format, this is not the problem. The problem is that the attachments must already exist on the target issue and the ADF format must have the appropriate attachment and media ids in order to update the description.

image (15).png

Solution

To solve this problem we must get the order of operations correct. The correct order should be like this:

  1. Get the description and attachments from the source issue

  2. Copy the attachments from the source issue to the target issue

  3. Update the description from the source issue with new media id’s from the target issue

  4. Update the target issue with the new description

Example Script

string copyFromIssueKey = "TP-45"; string copyToIssueKey = "TP-1090"; //get the description from the source issue string fromDescription = copyFromIssueKey.description; //use the split() function to break up the text block into a string array //each element of the array will contain a block of description text which should contain an attchment string[] splitDescription = split(fromDescription, "<media"); //if the description contained attachments. If not, this can be skipped. if (size(splitDescription) > 0 ) { //use regex to search for the media ids string regex = "(.*?)(?=\\#)"; string[] existingMediaIds; //loop through the text blocks (array elements) for (string splitPart in splitDescription) { //find media ids and add them to the existingMediaIds array if (startsWith(splitPart, "group")) { string[] splitAgain = split(splitPart, "\n"); for (string splitPart2 in splitAgain) { string matched = matchText(splitPart2, regex); if (isNotNull(matched)) { existingMediaIds = arrayAddElement(existingMediaIds, matched); } } } else { string matched = matchText(splitPart, regex); if(isNotNull(matched)) { existingMediaIds = arrayAddElement(existingMediaIds, matched); } } } //if media ids were found in the description test if (size(existingMediaIds) > 0 ) { //get list of attachments from the source issue JAttachment[] attachments = getAllAttachments(copyFromIssueKey); int[] attachmentsIds; //loop through list of attachments for (JAttachment attachment in attachments) { //get the media id string mediaId = getAttachmentMediaId(copyFromIssueKey, attachment.id); //look up the corresponding attachment info from the source issue int index = arrayFind(existingMediaIds, mediaId); if (index != -1) { //if info was found //copy attachment from the source issue to the target issue copyAttachment(copyFromIssueKey, attachment.filename, copyToIssueKey); //get the id of the attachment that was just copied int[] attIds = getAttachmentIds(copyToIssueKey, attachment.filename); //if there are multiple attachments with the same name -> #badLuck, probably it can be done but... //update the description with the new id fromDescription = replace(fromDescription, mediaId, getAttachmentMediaId(copyToIssueKey, attIds[0])); } } } } //push the new description to the source issue %copyToIssueKey%.description = fromDescription;

Table of Contents





Related content

Need support? Create a request with our support team.

Copyright © 2005 - 2025 Appfire | All rights reserved.