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

Hello,

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?

Cheers,

Rok

2 answers

1 accepted

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 {
    it.filename
}
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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Asked 5 hours ago in Confluence

What are the resources that you use to learn more about Atlassian Products?

I am gathering information about resources available for Atlassian product knowledge transferring for a presentation in our local Atlassian User Group. I want to group them in four categories From ...

46 views 3 3
View question

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