Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert an Attachment Object to a File Object using Scriptrunner in Jira v10

Chris Solgat
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.
May 21, 2025

We have upgraded our Jira instance to version 10.3.5.

Previously, we used the com.atlassian.jira.util.AttachmentUtils.getAttachmentFile(Attachment attachment) method to retrieve a File object for a given attachment, which we would then use to copy the file to a local directory for secondary processing.

In Jira 10, AttachmentUtils.getAttachmentFile() has been removed, so we replaced it with ComponentAccessor.getAttachmentManager().getAttachment(attachment.id). Based on the migration recommendations, we are now using com.atlassian.jira.issue.AttachmentManager to retrieve Attachment objects. However, we are unable to determine how to convert attachment object to file object and are receiving the below error:

ERROR org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.atlassian.jira.issue.attachment.Attachment@********' with class 'com.atlassian.jira.issue.attachment.Attachment' to class 'java.io.File'

Or even more simple, does anyone know how to find the filepath of the Attachment object in the Jira 10 api's?

1 answer

0 votes
Tobias Jentzsch
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 28, 2025

Hello Chris,

I am using the following code to create a list of Files from the attachments of an issue:

package console

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.util.JiraHome
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager

IssueManager issueManager = ComponentAccessor.issueManager
Issue thisIssue = issueManager.getIssueObject('ABC-1')

return getAttachments(thisIssue)



List<File> getAttachments(Issue issue) {
    List<File> files = []

    List<Attachment> attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
    String bucket = (10000L * Math.ceil(issue.number / 10000D).toInteger()).toString()
    JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome)
    String path = "${jiraHome.homePath}/data/attachments/${issue.projectObject.key}/${bucket}/${issue.key}"

    attachments.each { attachment ->
        if (attachment.mimetype.toString().startsWith("image")) {
            String thisFile = "${path}/${attachment.id}"

            File file = new File(thisFile)
            files.add(file)
        }
    }

    return files
}

I hope this helps. :-)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events