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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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) 

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

Suggest an answer

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

📹 What is a Trello board?

Hi, Atlassian Community! My name is Brittany Joiner and I am a Trello enthusiast and Atlassian Community Leader. I'll be sharing with you a series of videos with my top tips on how to best use Tre...

14 views 0 3
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