Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Cast issue attachment in base64 string

Edited

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.
Mar 25, 2023 • edited Mar 27, 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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events