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

Help With Scriptrunner custom script

Trevor Nagy
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 15, 2020

Hello Everyone,

 

I need help getting my script to add an attachment to a ticket on transition. I've look at the other posts and have tried to use them without fail. Currently this is what I have, and am hoping someone will be able to help me get what is wrong.

//Custom Post-scrip function

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext

def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.loggedInUser

def bean = new CreateAttachmentParamsBean.Builder()


.file(new File("//server/folder/file.pdf"))

\\Path to folder
.filename("file.pdf")

\\File to attach
.contentType("Adobe Portable Document Format/PDF")
.author(user)
.issue(issue)
.copySourceFile(true) .build()
attachmentManager.createAttachment(bean)

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Ivica Vančina June 18, 2020

You can try this script:

Try to put your attachment file on server where Jira is running from.

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

def attachmentManager = ComponentAccessor.getAttachmentManager()

def user = ComponentAccessor.getJiraAuthenticationContext()?.loggedInUser

String pathToFile = "/var/atlassian/jira/"
String fileName   = "test.txt"
String pathAndFile = pathToFile+fileName

def bean = new CreateAttachmentParamsBean.Builder()
    .file(new File(pathAndFile))
    .filename(fileName)
    .contentType("text/txt")
    .author(user)
    .issue(issue)
    .copySourceFile(true)//you must do this otherwise it deletes the source file from the file system
    .build()
attachmentManager.createAttachment(bean)  

 

Trevor Nagy
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 22, 2020

That's the one I tried to update. I'm not going to be the one updating the file, so I was hoping to find a way to have it point to one of our windows file shares. 

 

I tried to use the one provided by @Mathis Hellensberg but I kept getting an unexpected token error.

Ivica Vančina June 23, 2020

Well the problem is then with your windows file share. You get token error because that win file share requires you to be logged in as well. So in this script you would need to somehow login in your win file share and then execute script that I posted to you. There is no other way...

0 votes
Mathis Hellensberg
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.
June 17, 2020

Hi @Trevor Nagy  

Out of pure curiosity, I tried this myself. I'm getting stuck at (Access denied) for the path I'm using, but maybe it can be of some help?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext


def attachmentManager = ComponentAccessor.getAttachmentManager()
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = issueManager.getIssueObject("HRSD-79")

def bean = new CreateAttachmentParamsBean.Builder()
.file(new File("C:/Users/mhellensb/Downloads"))
.filename("favicon-32x32.png")
.contentType("image/png")
.author(user)
.issue(issue)
.build()
//.copySourceFile(true)

attachmentManager.createAttachment(bean) 
TAGS
AUG Leaders

Atlassian Community Events