Hello all.
I am trying to create a scripted validator to ensure that none of the attachments exceed 10mb in size. I've used code from this page and here is my script below:
import com.atlassian.jira.component.ComponentAccessor import webwork.action.ActionContext import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager import com.atlassian.jira.issue.IssueFieldConstants import com.opensymphony.workflow.InvalidInputException import com.atlassian.jira.issue.attachment.TemporaryWebAttachment def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager) def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN) def checker = true if (formToken) { def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken) if (tempWebAttachments.find{it.size >10485760}) { invalidInputException = new InvalidInputException("Attachment size shouldn't exceed 10 mb: size is ${tempWebAttachments.find{it.size >10485760}.size} bytes") checker=false } }
And it's working correctly at first, that is if I add an attachment bigger than 10 mb on the "create issue" screen, it's showing the error, but the problem is that the error doesn't disappear when I delete this big attachment and even all of the attachments and press "Create" again on the same screen. When I try to create from scratch without adding any big files, it works alright, no error showing.
So the question is why is this happening and is there a way around?
Community moderators have prevented the ability to post new answers.
HI Ksenia,
Good catch, try
import com.atlassian.jira.component.ComponentAccessor import webwork.action.ActionContext import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager import com.atlassian.jira.issue.IssueFieldConstants import com.opensymphony.workflow.InvalidInputException import com.atlassian.jira.issue.attachment.TemporaryWebAttachment def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager) def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN) def checker = true if (formToken) { def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken) if (tempWebAttachments.find{it.size >10485760}) { invalidInputException = new InvalidInputException("Attachment size shouldn't exceed 10 mb: size is ${tempWebAttachments.find{it.size >10485760}?.size} bytes") //Remove all remaining temporary attachments which were created with given form token temporaryAttachmentUtil.clearTemporaryAttachmentsByFormToken(formToken) checker=false } }
Please let me know if this does the trick and we'll update the documentation asap
regards, Thanos
I guess that's not what I need, because now after deleting the problem attachment I get this error when pressing "Create" again:
There were some errors when trying to create this issue
- Temporary attachment not found. Session may have timed out before submitting the form.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I do have the same problem, I don't think that using clearTemporaryAttachmentsByFormToken is the best solution here as it would delete all attachments, even the ones that are right. I've tried to use deleteTemporaryAttachment or even removeById but they just don't remove the attachment at all, no error or anything just that.
@Thanos Batagiannis _Adaptavist_
@Ksenia (If you solved it)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
TL;DR
Use
ActionContext.getRequest()?.getParameterValues(AttachmentSystemField.FILETOCONVERT)
To get attachments IDs which user sees at the screen and then
temporaryAttachmentUtil.getTemporaryWebAttachment(fileId)
To get attachment object, so you can validate it.
----------------
How adding attachments works in Jira?
When User adds an attachment at create issue screen (or any screen with attachments field) it's being created in the DB as "temp Attachment". If user removes attachment from the screen after it has been uploaded Jira do not remove it from temp attachments table. Instead when user submits the form (clicks submit at create issue screen) Jira creates proper attachments only from those visible at the screen and removes all other.
@Ksenia Said:
And it's working correctly at first, that is if I add an attachment bigger than 10 mb on the "create issue" screen, it's showing the error, but the problem is that the error doesn't disappear when I delete this big attachment
That's because:
temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
will give you all attachments assigned to the form, no matter if they have been removed by the User from the screen or not.
@KseniaSaid:
because now after deleting the problem attachment I get this error when pressing "Create" again:
Temporary attachment not found. Session may have timed out before submitting the form.
That's because:
temporaryAttachmentUtil.clearTemporaryAttachmentsByFormToken(formToken)
Will remove all attachments from the DB, but won't remove them from the Users screen. For Jira this is inconsistency, because there are no attachments in the DB, but they are on the Users screen.
@Javier SánchezSaid:
I've tried to use deleteTemporaryAttachment or even removeById but they just don't remove the attachment at all, no error or anything just that.
That's because you threw a Run Time Exception right after deleting the attachment. This caused DB rollback. When seeing inconsistency in Database refer to logs with SQL statements and make sure that your statement has "commit" after it. If it has "rollback" - your changes won't be applied.
Taking under consideration how Jira works what you want to validate are not all the attachments which are in the DB, but only those visible by the user at the screen.
To get those use code from TL;DR section at top :)
But I assume you know that already @Javier Sánchez ;) Anyway I hope it might help others.
Docs:
https://docs.atlassian.com/software/jira/docs/api/7.12.0/com/atlassian/jira/issue/attachment/TemporaryWebAttachmentManager.html
https://docs.atlassian.com/fugue/2.4.0/fugue/apidocs/com/atlassian/fugue/Option.html
Logging in H2:
http://www.h2database.com/html/features.html#trace_options
Cheers,
Natalia Wróblewska | Jira Server Developer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.