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

Validator for attachments sum size

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

1 answer

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

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.0
TAGS
Community showcase
Published in Jira

Announcing the waitlist for Jira Work Management

Hey there Cloud Community members! We’re excited to give you the first glimpse of the new home for business teams on Jira — Jira Work Management. Jira Work Management is the next generation of J...

750 views 12 19
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