Sending attachments via scriptrunner to another application

Catheline December 9, 2022

Hello All,

I'm trying to get attachments from the JIRA and pass them to the payload, which will send base 64 encoded data to another application.

Here's my code till now,

import groovy.transform.BaseScript
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.util.logging.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.core.util.DateUtils
import com.atlassian.jira.user.ApplicationUser
import java.text.SimpleDateFormat
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment

// Enable the logging
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")

/** code for getting Oauth token **//

response=get_bearer_token(****OauthRequest_url, *****OauthHeaders, *****OauthPayload) ("Response is " + response)

def ***URL="............"
def access_token=response['access_token']
def ****AppHeaders=[:]
***AppHeaders['Authorization']='Bearer '+access_token

def customFieldManager = ComponentAccessor.customFieldManager
def attachmentManager = ComponentAccessor.attachmentManager

//getting JIRA issue key for testing in script console only
def issueManager = ComponentAccessor.issueManager

def issueKey = '****'

def issue = issueManager.getIssueObject(issueKey)

def attachments = attachmentManager.getAttachments(issue)

final def jiraResourcesPath = '/..jira..path/data/attachments/'
final def project = issue.projectObject
final def projectKey = project.key

def latestAttachment = attachments.findAll {
}.sort { it.created }.last()

def fileName =
def filePath = "${jiraResourcesPath}/${projectKey}/20000/${issueKey}".toString()
def oldFile = new File("${filePath}/${fileName}")
def fileContent = new File("$oldFile").text

//Encode file to Base64 format "Encoding the file..."
def encodedFile = "$fileContent".getBytes("UTF-8").encodeBase64().toString()"Encoded File:" +encodedFile)

def PayloadJson = new JsonBuilder()
fileName oldFile
fileContent encodedFile

}"Converting Json Builder to Json")
finalBodyJson = JsonOutput.toJson(PayloadJson)"Final Json object: " + finalBodyJson)
jsonContent = new JsonSlurper().parseText(finalBodyJson)"JsonContent " + jsonContent.content)
finalJsonContent = groovy.json.JsonOutput.toJson(jsonContent.content)"JsonContent "+ finalJsonContent)

def create_req(url, headers, payload,log) {
try {
def jsonSlurper = new JsonSlurper()
conn = new URL(url).openConnection()
// Adding headers
headers.each {
// Doing a POST with the connection obtained.

def retCode = conn.getResponseCode();
respText = conn.getInputStream().getText()

if(retCode.equals(202)) {"Request received. A response will be sent asynchronously.")"The response text is " + respText)
Map parsedJson= jsonSlurper.parseText(respText)
return parsedJson
else {"Encountered an Issue" + retCode)
finally {

CreateResponse = create_req(**URL, ****AppHeaders, finalJsonContent,log)


Am I getting the attachments in the right way? is there an alternate way?

Currently i have used ".pdf" but ideally i should get all the attachments and send it across?

if I run the code i get error,

2022-12-09 12:18:59,482 ERROR [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingMethodException: No signature of method: is applicable for argument types: (File) values: [/...../data/attachments/**/20000/**-14426/1344000] Possible solutions: wait(), abs(), any(), abs(), wait(long), and(java.lang.Number) at Script163$_run_closure3.doCall(Script163.groovy:125) at Script163$_run_closure3.doCall(Script163.groovy) at

Would someone be able to assist me?


Thank You.

1 answer

0 votes
MrBadExample January 5, 2023

Hi @Catheline ,


I am pretty sure that you are getting the attachements correctly.

Please take a look here I believe this is an answear to your question:



Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events