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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That makes sense. Thank you very much for the help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How are you adding the input element in the transition screen?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello All,
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.