How to purge the "attachment" field?

Germain Vincent February 8, 2018

Hi everybody,

 

I'm working on a project where I have 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 allowing the users to add a document to the subtask and with the help of a script the document is also added to the main ticket.

It's working fine.

The problem occurs if I re-open a subtask to which a document has been attached on the "close" transition. When I will close this subtask again, the attachment I added the first time will be added again in the main ticket. So I have the same document two times in the main JIRA. That's a big problem for my project.

So I tried to add a "clear field value" script on "attachment" field on the "close" transition (after the script that send the attachment to main JIRA of course) but it doesn't works.

I use the "clear field value" script on many transitions to reset te values of many fields and it works perfectly. The problem only concerns the "attachment" field. For example it works perfectly with the "assignee" field and "delivery date" field.

When I try to purge the ttachment field an error occurs and JIRA send a message advertising me that the transition is impossible to execute.
You can see the screenshot below:

Capture.PNG

I tried on different transition. Even on transition of the main ticket workflows. I still got the same error.

Do you have any idea on how I can "purge" the attachment field?

 

I'm using script runner 5.1.6 and JIRA 7.2.0

 

Thanks in advance!

 

Regards,

 

Germain.

1 answer

1 accepted

1 vote
Answer accepted
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.
February 8, 2018

Hi Germain,

I think a slightly different approach will help you solve this. What if you just modify the script that is used to copy an attachment from a sub-task to its parent so that if there's already an attachment in the parent issue with the EXACT SAME name then the attachment from the sub-task will not be copied? Would that work for you?

Germain Vincent February 8, 2018

Hi Ivan,

 

It's a great  idea but in my context it will not work as users will sometimes re open a subtask to modify what's inside the attachment but not the name. I need to allow them to send the same name attachment.

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.
February 8, 2018

No problem, let's add a file size check, to be extra safe. Sure, it's not 100% bulletproof, but I have a feeling that a scenario where a file will have an EXACT size of the original after the edits is VERY unlikely.

Germain Vincent February 8, 2018

That's another great idea. I will ask my management but I'm pretty sure they will tell me it's not a "safe" enough solution.

I keep you up to date.

Thanks!

Germain Vincent February 8, 2018

In fact that's a solution that my management would accept!

 

So, Ivan, how would you write the script to compare name and size of the subtask's attachment and attachments already in the main ticket?

I want to allow the attachment to be added to main ticket if attachment's name is different of attachments in main ticket or if attachment's size is different if name is the same.

For information, here is the script I use to get the attachment (Yes, it's your code ^^):

def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def parentAttachments = attMgr.getAttachments(parentIssue)
def lastAtt = attachments[0]
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
if (attachments.size() < 1){
  for (int i = 1; i < attachments.size(); i++){
    if (lastAtt.getCreated().getTime() < attachments[i].getCreated().getTime()){
      lastAtt = attachments[i]
    }
  }
}

attMgr.copyAttachment(lastAtt, currentUser, issue.getParentObject().getKey())

 

Thanks!

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.
February 8, 2018

Right, let's get this show on the road, shall we? Try this:

import com.atlassian.jira.component.ComponentAccessor

def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def parentAttachments = attMgr.getAttachments(issue.getParentObject())
def lastAtt = attachments[0]
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

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, currentUser, issue.getParentObject().getKey())
}
Germain Vincent February 9, 2018

Ivan,

 

That's perfect, it works on first try!

It's a really good point for my project.

Thank you very much :)

 

Regards,

Suggest an answer

Log in or Sign up to answer