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

Accepted Answer
4 votes

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

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
Community showcase
Published yesterday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

301 views 0 6
Read article

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