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

Help With Scriptrunner custom script

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

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)  

 

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.

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...

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) 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

805 views 17 21
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