Stop large attachments from syncing (Jira cloud - Jira server)

Originally asked by Syed Majid Hassan on 30 June 2022 (original question)


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.

Answer by Syed Majid Hassan on 30 June 2022

The challenge here is not to stop the attachments from syncing over. We can easily do that by doing the following on the outgoing side:

replica.attachments = issue.attachments.findAll {it.filesize <= 50000}

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.

Thanks & Happy Syncing

Majid


Answer by Chana on 03 July 2022

Hi Majid,

Thanks for that, very useful!

Is it possible to add a link to the destination within the comment?

Thanks,

Chana


Comments:

Syed Majid Hassan commented on 14 July 2022

Hi Chana,

Yes that would be possible by refining the script to the following:

if (replica.customKeys."bigAttachments" != previous.customKeys."bigAttachments"){
       for (int i=previous.customKeys."bigAttachments".size(); i/secure/attachment/${replica.customKeys.'bigAttachments'[i].id}/${replica.customKeys.'bigAttachments'[i].filename}", issue.comments)
           
       }
    }

Please let me know if you have any further questions on this one.

Thanks

Majid

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.