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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,540
Community Members
 
Community Events
176
Community Groups

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

Cole Rising Star Feb 08, 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

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 Feb 08, 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!

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

Cole Rising Star Feb 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? 

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 Feb 12, 2019

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

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

Hi @David Fischer _Appfire_ 

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 _Appfire_ 

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 

 

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

Atlassian Community Events