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,364,763
Community Members
 
Community Events
168
Community Groups

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

Atlassian Community Events