We recently received the following requirement from a client who was syncing between Jira Server and Jira Cloud:
Any large attachments should not be synced over to the destination
But for every large attachment added to the source, a comment should be added to the destination side informing them that a large attachment was recently added on source.
This line would not add any attachments to the replica which are greater than 50,000 bytes.
The challenge, however, comes when you want to add a comment to the destination side informing the users that a large attachment has been added. Here is an approach to solve this:
If a large attachment is added on source side, set a customKey to 1. Now read the value of that customKey on the destination side and if it is one, we can add a custom comment. But how do we stop duplication of this i.e. every time a sync is triggered, the customKey would be set and duplicate custom comments would be added on destination side.
So, it is clear that we somehow need to maintain the attachments data on source side and pass it on the destination side to avoid this duplication:
On the source side, add a customKey to track the attachments which are being filtered out:
def bigAndSmallEnough = issue.attachments.inject(["big":[], "smallEnough":[]]) { r, a ->
if (a.filesize <= 10) { r."smallEnough" += a }
else { r."big" += a }
r
}
replica.attachments = bigAndSmallEnough."smallEnough"
replica.customKeys."bigAttachments" = bigAndSmallEnough."big"
On the destination side, check if the “bigAttachments” has changed since the previous sync. This is the key to saving you from duplicate comments. You can easily do this by using:
if (replica.customKeys."bigAttachments" != previous.customKeys."bigAttachments")
Now within this if block, you are free to add any comments and format them as per requirement. What we have done here is to run over the “new” values in bigAttachments and printed out the filenames of those new files:
if (replica.customKeys."bigAttachments" != previous.customKeys."bigAttachments"){
for (int i = previous.customKeys."bigAttachments".size(); i < replica.customKeys.'bigAttachments'.size();i++){
issue.comments = commentHelper.addComment("WARNING: New Large File Added #${i+1}. ${replica.customKeys.'bigAttachments'[i].filename}", issue.comments)
}
}
Here is a video demonstration(old community) of the aforementioned solution here.