Hi everybody,
I'm working on a project with two workflows. one for the main ticket and one for the many subtasks that can be created during the main ticket resolution process.
When I close a subtask I have a screen with an attachment field.
Using the script below I can copy the attachment that is entered in the attachment field to the attachment field in the main ticket.
import com.atlassian.jira.component.ComponentAccessor
def cwdUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def parentAttachments = attMgr.getAttachments(issue.getParentObject())
def lastAtt = attachments[0]
if (attachments.size() > 1){
for (int i = 1; i < attachments.size(); i++){
if (lastAtt.getCreated().getTime() < attachments[i].getCreated().getTime()){
lastAtt = attachments[i]
}
}
}
boolean sameFileExists = false
if (parentAttachments.size() > 0){
for (int i = 0; i < parentAttachments.size(); i++){
if (lastAtt.getFilename() == parentAttachments[i].getFilename() && lastAtt.getFilesize() == parentAttachments[i].getFilesize()){
sameFileExists = true
}
}
}
if (sameFileExists == false){
attMgr.copyAttachment(lastAtt, cwdUser, issue.getParentObject().getKey())
}
The script is perfectly working.
Now, I would like a validator that will compare the attachment that is entered in the attachment field of the close subtask transition with the existing subtasks in the main ticket. If the new attachment has the same name than one of the attachment in the main ticket I would like the validator to shoot a warning message and block the transition.
Do you have any idea on how to manage that?
Thanks in advance.
Regards.
Germain.
Hi Germain,
This is totally doable actually. Please keep in mind a few things though:
1) The validator checks temporary attachments (files which are added on your screen but not yet uploaded to the issue) against attachments in other subtasks, and if a match is found (I've added not only a filename but also a filesize check to be extra safe) then an error message is thrown.
2) To pass validation, simply removing the file you have attached on your screen is not enough, because the file will still remain in the action context and to clear the context you have to CLOSE and REOPEN your transition screen. Otherwise you'll still be getting validation errors even if you attach completely different files.
The code:
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import com.atlassian.jira.issue.IssueFieldConstants
import webwork.action.ActionContext
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)
if (formToken){
def tempAttMgr = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def tempWebAtts = tempAttMgr.getTemporaryWebAttachmentsByFormToken(formToken)
if (tempWebAtts){
def attMgr = ComponentAccessor.getAttachmentManager()
def subtasks = issue.getParentObject().getSubTaskObjects()
if (subtasks.size() > 1){
subtasks.remove(subtasks.find{it.getId() == issue.getId()})
for (int i = 0; i < tempWebAtts.size(); i++){
for (int v = 0; v < subtasks.size(); v++){
if (attMgr.getAttachments(subtasks[v]).find{it.getFilename() == tempWebAtts[i].getFilename() && it.getFilesize() == tempWebAtts[i].getSize()} != null){
throw new InvalidInputException ("File ${tempWebAtts[i].getFilename()} already exists in subtask ${subtasks[v].getKey()}".toString())
}
}
}
}
}
}
Hi Ivan,
Thanks for your answer! I was on a long WE. I will test your code this afternoon.
Many thanks.
Germain.
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.