Cast issue attachment in base64 string

Antoine _Klee Group_
Contributor
March 15, 2023

Hi.

I wish to send Jira issue's attachments to another software using json.

The final goal is to produce a json of that kind.

{
"attachments": [{
"b64content": "whatever b64 string ==",
"name": "image.png"
}
]
}

I am using scriptrunner do perform this action. It seems like the AttachmentManager.streamAttachmentContent can do the job but I do not know how to use the InputStreamConsumer (Atlassian Jira - Server 9.3.3 API) class.

Here is the code I use so far.

import com.atlassian.jira.component.ComponentAccessor
import groovy.json.*
import org.apache.log4j.Level

import org.apache.commons.codec.binary.Base64;
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment
log.setLevel(Level.INFO);

def issueManager = ComponentAccessor.getIssueManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()

def issue = issueManager.getIssueObject('PTIJ-68')

def attachment_list = issue.getAttachments()

// Once I have the list, I am working on the first attachment right now and try to stream it as a list of bytes but do not know how to open the stream.
log.info(attachmentManager.streamAttachmentContent(attachment_list[0], ??? ))

Can anyone help? 

1 answer

3 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 25, 2023

Hi @Antoine _Klee Group_

If you intend to encode the attachments to Base64, why not try something like this:-

import com.adaptavist.hapi.jira.issues.Issues

def issue = Issues.getByKey('MOCK-3')
def encoding = [] as List<String>

issue.attachments.each {
encoding << it.toString().bytes.encodeBase64().toString()
}

encoding

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the output returned:-

encoding.png

You will basically have to pass the List of encoding values returned into your JSON.

If you intend only to take the first Attachment, you could modify the code slightly to:-

import com.adaptavist.hapi.jira.issues.Issues

def issue = Issues.getByKey('MOCK-3')

def attachment = issue.attachments.sort {it.created}.first()

attachment.toString().bytes.encodeBase64().toString()

 Below is the screenshot from the ScriptRunner console:-

encoding2.png

Also, I have run the test above using ScriptRunner's HAPI feature. I suggest upgrading your ScriptRunner plugin to the latest release, i.e. 7.13.0, to use the HAPI feature.

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Antoine _Klee Group_
Contributor
November 9, 2023

Hi Ram sorry i completely missed your comment.

I will try your solution asap.

My environnement is: Scriptrunner v5.5.2 / Jira Server v7.13.2

Antoine _Klee Group_
Contributor
November 13, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thanks for your answer but that is not really what I want.

the `attachment` variable, then encoded to b64 in your script just encodes the string "com.atlassian.jira.issue.attachment.Attachment@ff95584b" into its b64 counterpart. I wish to have the whole binary stream of the attachment to be turned into a b64 string, not just the descriptor.

 

Thanks.

 

My environnement is: Scriptrunner v5.5.2 / Jira Server v7.13.2

Parvaneh Zand
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.
December 18, 2023

Hi @Antoine _Klee Group_ 

Did you find a solution?

Antoine _Klee Group_
Contributor
December 20, 2023

Hi @Parvaneh Zand infact I found a solution, basically I perform a GET requests on the "contentURL" of the attachment. Did not find a way to do this internally.

Here is an extract of my code:

def issue_all_attachments = issue.attachments

log.debug(issue_all_attachments)

List issue_all_attachments_list = []
issue_all_attachments.each{
Map<String, String> issue_all_attachments_map = new HashMap<String, String>()
issue_all_attachments_map.put("filename", it.getFilename().toString())
issue_all_attachments_map.put("localContentURL", "http://localhost:8080/secure/attachment/" + it.getId().toString() + "/" + it.getFilename().toString())
issue_all_attachments_list.add(issue_all_attachments_map)
}

log.debug(issue_all_attachments_list)


log.info("Conversion to b64")

for (my_attachment in issue_all_attachments_list) {
try {

def request_url_attachment = new URL(my_attachment.localContentURL);
HttpURLConnection get_attachment_request = (HttpURLConnection) request_url_attachment.openConnection();
get_attachment_request.setRequestMethod("GET")
get_attachment_request.setDoOutput(true)
get_attachment_request.setRequestProperty("Authorization", JIRA_AUTH_STRING)

my_attachment.put("base64content", get_attachment_request.getInputStream().bytes.encodeBase64().toString())
}
catch (e) {
log.error("error message = ${e}")
my_attachment.put("base64content", "")
}
}

log.debug(issue_all_attachments_list)

 This will create an array of the following format:

[{content:<b64string>,filename:<string>},{content:<b64string>,filename:<string>}]

Suggest an answer

Log in or Sign up to answer