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

How to access attached file from workflow transition screen in jira (with groovy)

Tobias Dittrich Oct 21, 2012

In a workflow transition I have a screen with an "attach file" input. In the post-function I want to access the attached file (if any) and create another issue with this file as attachment.

I tried to achieve this via the ServletActionContext.getRequest() but I can't seem to get the uploaded file this way. The HttpServletRequest does not have the getPart() function (does jira use servlet 3 spec?).

Is there an official way to access attached files from post-functions?

Many thanks in advance

3 answers

1 accepted

1 vote
Answer accepted
Artur A. Papikyan Oct 21, 2012

Hi Tobias.

You can reach this by following

1. Script Runner Installed, e.g. Groovy Runner

2. Use Script Post function or Script Listener

in Script use event to to get changeItems:

def gvChangeLog = event.getChangeLog()

        if (gvChangeLog == null) {
            return
        }
        
        def changeItems = gvChangeLog.getRelated("ChildChangeItem")

        def coll = changeItems.findAll{ chItem -> chItem.getString("field") == "Attachment" && chItem.getString("fieldtype") == "jira"}

Remember that in Listener you should filter events by type, also you can use issuetype in addition

This will help you get attachment id, then using AttachmnetManager you will be able to get rest attachment info.

4. As you may know attachments path is {JIRA_HOME}/data/attachments/{PROJECT-KEY}/{ISSUE-KEY}/{fileattachment.id}, so you can physically access attached file.

Hope this will help you.

Tobias Dittrich Oct 21, 2012

Hi,

thanks a lot for you answer! One question remains, though: where does the "event" come from? Standard groovy script runner does not seem to bind this in post-function.

It seems to me that this is exclusiv to listeners. After all there is no "event" in a post-function?

So how can I get hold of the change log in a post function?

Thanks

Tobias Dittrich Oct 23, 2012

Hi Naykipap,

after playing around a bit with your solution I adjusted a few things and finally got it to work. Maybe you can edit your answer for future people searching for help with this, so they can see the whole thing. After all credit for the right answer belongs to you.

This is what I came up with:

def changeItems = transientVars["changeItems"]
def uploadChanges = changeItems.findAll { item -> item.getField() == "Attachment" && item.getFieldType() == "jira" }

uploadChanges.each { uploadChange ->
	def Attachment attachment = attachmentManager.getAttachment(uploadChange.getTo()?.toLong())
	if (attachment) {
		def filePath = PathUtils.joinPaths(pathManager.attachmentPath, currentIssue.projectObject.key, currentIssue.key, attachment.id.toString())
		def atFile = new File(filePath)
		if (atFile.canRead()) {
			log.debug("Cloning attachment ${attachment.filename}")
			attachmentManager.createAttachmentCopySourceFile(atFile, attachment.filename, attachment.mimetype, attachment.author, newIssue, [:], attachment.created)
		}
	}
}

where newIssue is the newly created issue in another project.

Important: you have to move down the script execution in the order of post-steps. I moved it right to the end as last step but I don't know how far exactly you have to move it. If it's not late enough the attachment is not yet created and there is no changeItems in the transientVars.

Hope this helps

Artur A. Papikyan Oct 24, 2012

Hi.

Good job, Tobias.

I'am glad that you've resolved your task.

Good luck.

Kleber Fonseca Jul 09, 2018

@Tobias Dittrich Hi... Please...

Script Runner gives me an error when I try the code above.

Line 2: "def uploadChanges = changeItems.findAll { item -> ..."

The message shows "expecting '}', found '-' "

 

Do you know why?

Thanks in advance...

Tobias Dittrich Jul 10, 2018

Seems like the quotation got messed up in the last years ... you have to replace all occurences of 

->

with > and 

&&

with &&

1 vote
JamieA Oct 24, 2012

I know you already have a solution, but...

*event* will be in the bind variables for listeners only, not post-functions.

There is code for retrieving attachments that works for both post-functions and listeners here: https://bitbucket.org/jamieechlin/scriptrunner-public/src/7c7cb23511a2/src/main/resources/com/onresolve/jira/groovy/canned/workflow/postfunctions/SendCustomEmail.groovy#cl-247

Tobias Dittrich Oct 24, 2012

Thanks, that would've been exactly what I was looking for. But seeing it after I found out myself gives me a good feeling I didn't do it wrong ;)

I was copying from CopyIssueWithAttachment, but did not expect anything in SendCustomEmail, though.

Shaikh Moiz Nov 04, 2013

Hello Jamie,

I want to configure the same thing, instead of post function i need to validate the attachment field and prompt user via a custom error message,because at different level of points we must prompt different documents to be attached,

making a field required via validator will not saffice the requirement,Instead we must have different error messages.

I hope i am clear with my question.

Thanks and regards,

Moiz.

0 votes
Artur A. Papikyan Oct 21, 2012

Yes, you're right. There is "transientVars" object in post function. Try to access "changeItems" element in it.

Here is link for example

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

1,943 views 3 15
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