When there are two or more attachments with the same name count them as one


I created a "Version" custom script field (used the code from the given link) which is supposed to count how many attachments we have, but it so happens that not every new uploaded attachment is an actual Version change. When attachment with the same name as previously uploaded documents are uploaded it means that only some minor cosmetical or grammatical corrections were needed in the document and I would need "Version" counter not to go up in that case. Is it possible somehow?



2 answers

1 accepted

This widget could not be displayed.

Hi Rok,

I think a solution in your problem would be instead of getting as a result the size of the list with all the attachments, to get the size of the list with unique attachments (an attachment with *exactly* the same name will not count twice)

def attachmentManager = ComponentAccessor.getAttachmentManager()
def uniqueAttachmentsList = attachmentManager.getAttachments(issue).unique {
def numberAttachments = uniqueAttachmentsList.size()
return numberAttachments ?: null

Hope that helps.

About the performance that @Mike Friedrich mentioned, the complexity of the groovy's unique() method is O(n^2), which means that may have performance issues on collections with lot of elements. If that is the case you can copy the list of all attachments to a Set (a collection that contains no duplicate elements) and then get the size of this Set as a result.

Thanx man :), works like a charm. Cheers, Rok

This widget could not be displayed.

I am assuming you have to modify the script yourself and compare the attachments. But I expect that feature to have unacceptable performance impacts.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

196 views 3 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you