Hi community,
i'm trying to write a simple validator script to tell that the sum of alll attachments while creating an issue must be < to 1 MB ( for ex ).
I've tested it with an issue key using the script console and it works but not on the create transition validator.
Can you please help me ?
That's my code using an issuekey
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.core.util.FileSize
import com.atlassian.jira.issue.Issue
import java.util.*
import com.atlassian.jira.component.ComponentAccessor;
import org.apache.log4j.Logger
import org.apache.log4j.Level
def issuekey = "jira-xxxx"
def issueManager = ComponentAccessor.getIssueManager() ;
def issue = issueManager.getIssueObject(issuekey)
def sum = issue.attachments*.filesize.sum()
def sum1 = sum as Float
sum1 < 1049000.0
When you are creating the issue, attachments are not "yet" stored on the system or the issue per se - as far as I understand it at least - it happens after the transition, so in a sense, only after you create the Issue.
I would recommend taking a peek here - https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/validate-attachments-links-in-transition.html there is an example you may want to try if this solves your validator.
I modified it to your example and seems to work:
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.opensymphony.workflow.InvalidInputException
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)
final Long MAXIMUM_SIZE = 1000000L
Long attachedFileSize_sum = 0L
if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
tempWebAttachments.each { TemporaryWebAttachment it ->
long size = Long.valueOf(it.size)
log.debug "File name and size: " + it.filename + " (" + size + ")"
attachedFileSize_sum += size
}
log.debug("total attached size: " + attachedFileSize_sum)
if (attachedFileSize_sum > MAXIMUM_SIZE) throw new InvalidInputException("Over the size of 1MB.")
}
else {
//No form token, should this actually ever happen? Maybe if you crafted a custom http form but otherwise no I don't think this should ever get here.
log.debug("no formtoken?")
return true
}
However, I sometimes get to the "no formtoken" part and don't know why. It seems to happen when I'm uploading attachments and then surfing someplace else, maybe it changes the token, not sure. But it's hopefully a place to start.
Hi @Radek Dostál ,
it works like a charm and really thanks a lot for your help :) I've detect one thing the call of the create transiton will be stored and we need to refresh and try again in case of we uploaded over 1MB of size in attachment otherwise will have always the exception thrown .
Again thanks a lot.
Wajih
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.