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

Rok Antolic November 25, 2015

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

4 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
November 25, 2015

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.

Thanos Batagiannis _Adaptavist_
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.
November 25, 2015

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.

Rok Antolic November 26, 2015

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

0 votes
Mike Friedrich
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.
November 25, 2015

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

JamieA
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.
November 25, 2015

why?

Suggest an answer

Log in or Sign up to answer