Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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.

2 answers

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.

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!

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. 

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?

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)

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 :(

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? 

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. 

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

Hi @David Fischer _Innovalog_ 

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.

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

Hi @David Fischer _Innovalog_ 

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.

 

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. 

Hello All,

 @Bastian Stehmann _neusta portal services_ 

 

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
Community showcase
Published in Marketplace Apps & Integrations

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

216 views 2 5
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you