I want to add an attachment from JIRA server to every Task using post function

Prashant Karakanagoudar December 17, 2020

Hi Team,

 

Can someone please help me to add an attachment using scripted post function?

I used the suggestion in below link and it looks like the attachment is getting deleted after the script execution, how can I ensure the script does not delete the file from the location.

https://community.atlassian.com/t5/Answers-Developer-Questions/Is-there-a-way-to-add-an-attachment-with-automation-on-Subtask/qaq-p/467766

 

def attachmentManager = ComponentAccessor.getAttachmentManager()
def pathManager = ComponentAccessor.getAttachmentPathManager()
def user = ComponentAccessor.getJiraAuthenticationContext()?.getLoggedInUser()

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("/appl/jira_shared/test.txt"))
.filename("text.txt")
.contentType("plain/txt")
.author(user)
.issue(issue)
.build()
attachmentManager.createAttachment(bean)

 

Regards,

Prashant

1 answer

1 accepted

1 vote
Answer accepted
mogavenasan
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.
January 3, 2021

Hi Prashant,

Can you try the following code and see if it works.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean

def attachmentManager = ComponentAccessor.getAttachmentManager()
def pathManager = ComponentAccessor.getAttachmentPathManager()
def user = ComponentAccessor.getJiraAuthenticationContext()?.getLoggedInUser()

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("/appl/jira_shared/test.txt"))
.filename("text.txt")
.contentType("plain/txt")
.author(user)
.issue(issue)
.copySourceFile(true)
.build()

attachmentManager.createAttachment(bean)

You may refer to https://docs.atlassian.com/software/jira/docs/api/8.5.5/com/atlassian/jira/issue/attachment/CreateAttachmentParamsBean.html for more details on CreateAttachmentParamsBean.

I hope this helps.

Cheers,
Moga

Edited based on the reply. Thanks!

Prashant Karakanagoudar January 3, 2021

Hi Moga,

 

Thanks a lot for your inputs here.

It worked. :-) 

 

Changes the copySourceFile with Boolean value instead of a string.

.copySourceFile(true)

 

 

Prashant

Like mogavenasan likes this

Suggest an answer

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

Atlassian Community Events