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 avoiding copy of attachment if already existing

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.

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Ivan Tovbin
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.
Feb 24, 2018

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.

TAGS
AUG Leaders

Atlassian Community Events