How can I add an attachment to an issue in a post-function?

Cole
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.
February 8, 2019

Ideally, I want to add 1 of 4 attachments based on what a certain field value. I was trying to mess around with the "Set field value (JMWE add-on)" post-function, but I haven't made too much progress thus far.

3 answers

1 vote
Bastian Stehmann
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 8, 2019

Hi @Cole,

 

So you have the 4 attachments stored on your server and want to attach ne of them?

Or where do you get the attachments from?

 

I'm not used to JMWE, but I once did something similar with Script Runner.

There I generated a text file within the script and attached that file to the issue.

If you are interested in this, I could look up how I did that.

Cole
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.
February 8, 2019

I appreciate the reply!

I was unsure where was the best place to have the files. At the moment they are on my PC. Do they have to be on the Jira server?

I am really open to suggestions here!

Bastian Stehmann
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 8, 2019

I don't think that Jira can access the files on your computer because the post function is happening on the server and the server then don't know, where to find the files. So I think the files must be stored on the server. 

Cole
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.
February 8, 2019

That makes sense. I was wondering if Jira could somehow access them on a network drive as I can access them buy putting the path in a browser url.

But if it has to be on the server, then I can work with that. Can you share the script you used so I might get some ideas of where to start?

Bastian Stehmann
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 8, 2019
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager


def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.getUser()
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject('IS-1')

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10302")
def cFieldValue = issue.getCustomFieldValue(cField)
log.warn(cField)
log.warn(cFieldValue)
if (cFieldValue != null){
def String filename = ""+cFieldValue + ".txt"

File f = new File("/tmp/"+filename);

f.getParentFile().mkdirs();
f.createNewFile();


FileWriter fw = new FileWriter("/tmp/"+filename, true)
BufferedWriter bw = new BufferedWriter(fw)
PrintWriter out = new PrintWriter(bw)

out.println("Some Text");

out.close()

def bean = new CreateAttachmentParamsBean.Builder()
.file(f)
.filename(filename)
.contentType("text/plain")
.author(user)
.issue(issue)
.build()
attachmentManager.createAttachment(bean)
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 8, 2019

Same idea, but for a JMWE Scripted (Groovy) post-function:

import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean

if (!issue.get("your custom field"))
return;

def filename
switch (issue.getAsString("your custom field")) {
case "value 1":
filename = "file 1.txt"
break
case "value 2":
filename = "file 2.txt"
break
}

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("<path to files>/"+filename))
.filename(filename)
.contentType("text/plain")
.author(currentUser)
.issue(issue)
.build()
ComponentAccessor.attachmentManager.createAttachment(bean)

 Please note that I didn't test this code, so there might be bugs :(

Cole
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.
February 11, 2019

Hi David,

Thank you very much! Do the files have to be on the JIRA server or could they be on a network drive or elsewhere potentially? 

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 11, 2019

They need to be accessible by the Jira process. So technically they can be on a network drive if that drive is mapped on your server and accessible by the user that is used to run Jira. 

Cole
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.
February 12, 2019

That makes sense. Thank you very much for the help!

0 votes
rbalusu3 December 24, 2020

I have successfully attached the file using script runner and i created file in efs and attach the file to issue. This is the code.

packages 

import   com.atlassian.jira.issue.Issue;import import  com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import  com.atlassian.jira.issue.AttachmentManager

code

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("Test-123")
issue.setSummary("Test user detils")
ApplicationUser user = userManager.getUserByName("ravi")

def newFile = new File("/efs/jira-data-shared/data/attachments/764401")
newFile.write("attaching a file to an issue")

def attachmentManager = ComponentAccessor.getComponent(AttachmentManager)

def bean = new CreateAttachmentParamsBean.Builder()

bean.file(newFile)
bean.filename("project_details.txt")
bean.contentType("text/plain")
bean.author(user)
bean.issue(issue)

attachmentManager.createAttachment(bean.build())

0 votes
Shruti Chauhan November 11, 2019

Hi @David Fischer 

Using the above-provided code, I am successfully able to attach a file to the issue(Giving absolute file loc like ".file(new File("C:/temp"+filename))").

However, I need to browse the file from <input type="file">. This attribute only returns file name, not the complete location of a file because of security risk and without the full location of the file, code generates an error.

Could you please lemme know how I can proceed with this like how to pass file object in file method.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2019

How are you adding the input element in the transition screen?

Shruti Chauhan November 11, 2019

Hi @David Fischer 

I have created a custom button on Jira issue screen using script runner and after clicking on the button, a pop-up screen has appeared. On this screen, I have added a file attribute.

And on form submission, I need to create an issue and attach a file with it. 

Rest task is completed. All I am stuck is getting the file location.

 

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 11, 2019

This is likely handled by Tomcat, but I am really shooting in the dark here. I'm not even sure you will be able to access the uploaded file. 

gupta_i January 10, 2020

Hello All,

 @Bastian Stehmann 

 

I need to copy attachments from one of the issue in project to new issue using post function. I am using on-demand jira and have script runner.  Please help me !!!.

Thanks in advance.

 

Regards,

Ishant

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events