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

Hi All,

I have seen a few posts around this, but wanted your help, essentially I have sync between Zendesk and Jira, Zendesk cant received attachement that is more than 50mb. This causes the push a sync error.

I have a code: issue.attachments.findAll {it.filesize <= 50000}

This works fine, as it ignores the sync, but the text shows a script

I have tried the alternative

replica.key = issue.key
replica.type = issue.type
replica.assignee = issue.assignee
replica.reporter = issue.reporter
replica.summary = issue.summary
replica.description = issue.description
replica.labels = issue.labels
replica.comments = issue.comments
replica.resolution = issue.resolution
replica.status = issue.status
replica.parentId = issue.parentId
replica.priority = issue.priority

// Exclude attachments larger than 50,000 bytes
List filteredAttachments = issue.attachments.findAll { it.filesize <= 50000 }
replica.attachments = filteredAttachments

replica.project = issue.project

// Add a comment if some attachments were excluded due to size
if (issue.attachments.any { it.filesize > 50000 }) {
def warningMsg = “Some attachments were not synced because they exceeded the 50,000 bytes size limit. Please see the Jira ticket for the original files.”
replica.comments = commentHelper.addComment(warningMsg, replica.comments)

}

/*
Uncomment these lines if you want to send the full list of versions and components of the source project.
replica.project.versions =
replica.project.components =
/
/

Custom Fields (CF)
How to send any field value from the source side to the destination side.
1/ Add the value to the replica object using the Display Name of the specific field.
2/ Uncomment this next statement out and change accordingly:
replica.customFields.“CF Name” = issue.customFields.“CF Name”
*/
// Exalate API Reference Documentation: Exalate API Reference Documentation


So on first sync this when the attachment is vast it works fine, however when testing and I add a second attachment in Jira, the sync errors out.

All I want is to prevent to sync from erroring, to send a comment to zendesk that the attachments are too large and sends a comment.

The error showing its unexpected and unsure how solve this issue. Any guidance on what I could be missing? Please advise?

Hey @Ellesh_Miyangar

Can you please share your zendesk and Jira instance URL?

Thanks,

Sonal

@Sonal_Otwani Jira side: https://familyzone.atlassian.net/

Zendesk side sandbox : https://smoothwall1714624339.zendesk.com/agent/apps/exalate

After further testing I believe there is a customkey that needs to be utilised:

// Handle attachment warning as an internal comment, only add if new and non-empty/non-blank, remove if warning is now blank

if (replica.customKeys?.“attachment_warning” instanceof String) {

def warningText = replica.customKeys."attachment_warning"

def prevWarningText = previous?.customKeys?."attachment_warning"

if (warningText?.trim()) {

    // Only add if not already present as a comment

    def alreadyPresent = (issue.comments ?: \[\]).any { it.body == warningText }

    if (!alreadyPresent) {

        issue.comments = commentHelper.addComment(warningText, false, issue.comments)

    }

    // Remove any previous warning comment if the text has changed

    if (prevWarningText && prevWarningText != warningText) {

        issue.comments = (issue.comments ?: \[\]).findAll { it.body != prevWarningText }

    }

} else if (prevWarningText) {

    // Remove any internal comment whose body matches the previous warning text

    issue.comments = (issue.comments ?: \[\]).findAll { it.body != prevWarningText }

What I am trying to achieve is only comment when a oversized attachment is detected then should comment with the warning. This partially works but seems when you just comment its still displays the warning, ideally want it so it will only show when over 50mb attachment is apparent. I think could due to cached and tried to push a value after sync to disappear.

example as you can see when testing on every comment the warning appears:

Hi @Ellesh_Miyangar

Following script you can change for Jira outgoing sync script:

Existing script:

// 1. Filter attachments (50MB)
def limit = 52428800 
replica.attachments = issue.attachments.findAll { it.filesize <= limit }
// 2. Detect oversized files
def oversizedFiles = issue.attachments.findAll { it.filesize > limit }
// 3. Create or clear the custom property for attachment warning
if (!oversizedFiles.isEmpty()) {
    def baseUrl = "https://familyzone.atlassian.net" // CHANGE THIS
    replica.customKeys."attachment_warning" = """
    *System Note:* An attachment exceeded the limit of 50MB and was not synced. 
    Please see the original Jira ticket for details: ${baseUrl}/browse/${issue.key}
    """.stripIndent()
} else {
    // Remove the warning if no oversized attachments
    replica.customKeys.remove("attachment_warning")
}

Replace with:

// 1. Filter attachments (50MB)
def limit = 52428800 
replica.attachments = issue.attachments.findAll { it.filesize <= limit }
// 2. Detect oversized files
def oversizedFiles = issue.attachments.findAll { it.filesize > limit }
// 3. Create or clear the custom property for attachment warning
                                                                     
  if (oversizedFiles) {
 def baseUrl = "https://familyzone.atlassian.net" // CHANGE THIS
      def names = oversizedFiles*.filename.join(", ")                     
      replica.customKeys."attachment_warning" =          
          "System Note: The following attachment(s) exceeded the 50  
  MB limit and were not synced: ${names}. " +
          "Please see the original Jira ticket for details: ${baseUrl}/browse/${issue.key}"              
  } else {
      replica.customKeys."attachment_warning" = ""   // empty, not   
  .remove()                                                          
  }

Then change for Zendesk Incoming Sync Script:

Existing script:

    
  if (replica.customKeys?."attachment_warning" instanceof String) 
  {  
      def warningText = replica.customKeys."attachment_warning"      
      def prevWarningText = previous?.customKeys?."attachment_warning"                                                               
      if (warningText?.trim()) {                                     
          // Only add if not already present as a comment            
          def alreadyPresent = (issue.comments ?: []).any { it.body  == warningText }                                                   
          if (!alreadyPresent) {                                     
              issue.comments = commentHelper.addComment(warningText,false, issue.comments)                                            
          }                                                          
          // Remove any previous warning comment if the text has changed                                                            
          if (prevWarningText && prevWarningText != warningText) {   
              issue.comments = (issue.comments ?: []).findAll {      
  it.body != prevWarningText }                                       
          }                                                          
      } else if (prevWarningText) {                                  
          // Remove any ient whose body matches the previous warning text                                                              
          issue.comments = (issue.comments ?: []).findAll { it.body != prevWarningText }                                               
      }                                                              
  } 

Replace with:

def warningText     = replica.customKeys?."attachment_warning"
  def prevWarningText = previous?.customKeys?."attachment_warning"   
  
  if (warningText instanceof String && warningText.trim() && warningText != prevWarningText)      {           
      issue.comments = commentHelper.addComment(warningText, false, issue.comments)
  }    

The warning re-appears on every sync because the oversized attachment is still on the Jira ticket on every subsequent sync, so the outgoing script keeps re-setting attachment_warning, and the incoming alreadyPresent dedupe check is fragile (any whitespace/encoding difference between the stored comment body and warningText makes it look “new”)

Let me know if this is working or not.

Thanks,

Sonal

@Sonal_Otwani This is exactly what I needed, and I have tested this and works, thankyou so much!