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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

1 accepted

0 votes
Answer accepted
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
Jan 07, 2021

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

how is it possible to refresh it @Wajd ?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.0
TAGS
AUG Leaders

Atlassian Community Events