How to construct link to an attachment

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 19, 2019

Hello,

I'm trying to create a comment in issue, which contains link to some specific attachment from another issue (let's assume it has id = 10001).

Below is the code, which I use to get the attachment and construct the link.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.Attachment

ApplicationProperties applicationProperties = ComponentAccessor.getApplicationProperties()
AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager()

String baseUrl = applicationProperties.getString(APKeys.JIRA_BASEURL)
Long attachmentId = 10001

Attachment attachment = attachmentManager.getAttachment(attachmentId)
String attachmentUrl = "$baseUrl/secure/attachment/${attachment.id}/${URLEncoder.encode(attachment.filename, 'UTF-8')}"

Everything works fine for me, but I've been wondering, if there is a better way to create the attachment's url (variable attachmentUrl). This is the best solution, I've been able to find, but it seems to me, that there should be some method, to which I provide attachment id and which returns constructed link. Am I missing something?

Thank you very much!

0 answers

Suggest an answer

Log in or Sign up to answer