Validating Attachments Added this Transition using scriptrunner

ZubatyNos October 17, 2022

Hi everyone,

I'm using this groovy code to validate whether any of attachments added during workflow transition go beyond my allowed size. It works the first time, meaning that it correctly validates that there's an attachment with size that goes over the MAX_ATTACHMENT_SIZE limit, which can be seen on this screen

 

Screenshot 2022-10-14 150748.png

But when I then removed the file which goes over the limit (merged.pdf) and i try to transition again the validator still fails like this:

Screenshot 2022-10-14 150721.png

What I found out in the log is that the tempWebAttachments variable still holds the merged.pdf even though I removed it from the form. Is there anyway for tempWebAttachments to be up to date, even after I removed/added some attachments?

 

My validator code:


import com.atlassian.jira.component.ComponentAccessor
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 temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)

def MAX_ATTACHMENT_SIZE = 20971520

if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
tempWebAttachments.each { it ->
log.debug "Uploaded attachment name: ${it.filename}"
log.debug "Uploaded attachment filesize: ${it.size}"
}
return tempWebAttachments.every{it -> it.size <= MAX_ATTACHMENT_SIZE}
}


Regards.

 

 

 

 

 

 

1 answer

1 accepted

2 votes
Answer accepted
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 17, 2022

Hi @ZubatyNos 

There is no API call to 'refresh' the TemporaryWebAttachment.

Try putting in a log message to examine the content type with strip params

it.getContentType(true)

ContentType header might include extra parameters (the part starts with a semicolon),
On the basis that Jira does know which attachments to save there will be data to indicate that. Content type  may include some 'active' flag for attachments to be saved.
Alternatively try getting each attachment specifically for each TemporaryWebAttachment in the loop
temporaryAttachmentUtil.getTemporaryWebAttachment(it.getTemporaryAttachmentId()
This will return an Option on the attachment or null if not found. It may be that the removed attachments still in the list will return null in this case.
Tom
ZubatyNos October 21, 2022

Hi @Tom Lister , thank you for taking your time.

So logging it.getContentType(true) didn' t show any additional "active" flag sadly, just the MIME type.

Looping through attachments and getting them spefically didn't help

temporaryAttachmentUtil.getTemporaryWebAttachment(
it.getTemporaryAttachmentId().toStringId()
).getOrNull()

The "inactive" tempAttachments were still present the same way as getting them by the form token.

temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)

 


The missing puzzle piece was this community post.

Where variable newAttachmentsNames gets all the "active" attachments from the form but in the form of temporaryAttachmentIds

def newAttachmentNames = issue2.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue as List

the final script where i looped through the newAttachmentNames and paired them via the temporaryAttachmentUtil.getTemporaryWebAttachment function:

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



def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)

def MAX_ATTACHMENT_SIZE = 20971520

if (formToken) {
def issue2 = issue as MutableIssue
def newAttachmentNames = issue2.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue as List
return newAttachmentNames.every{it ->
def utilAttach = temporaryAttachmentUtil.getTemporaryWebAttachment(it).getOrNull()
if (utilAttach != null) {
return utilAttach.size <= MAX_ATTACHMENT_SIZE
}
return true
}
}
Tom Lister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 21, 2022

Glad to see you got to the solution. Could you mark it as solved for other users?

Suggest an answer

Log in or Sign up to answer