ScriptRunner script to delete attachments smaller than 10kb

Stephan Maass September 9, 2020

I try to delete attachments like small jpgs/pngs that are coming with issues created by mail. I tried to do it by automation for Jira, but this app only allows for regular expression for file names I think.

So I'd like to delete them "trash-attachments" with a scriptrunner script, but it's my first script and I have real trouble getting on the right path. 

 

Does someone know how to do it or has at least some pathway to set me on.

3 answers

1 accepted

2 votes
Answer accepted
Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 14, 2022

I just accomplished this by using a ScriptListener, and the script below.  You could use IssueUpdated, WorkLogged, etc as the Listener trigger.

You can tweak the script below for whatever your minimum desired file size is, or other attachments that you want to remove regularly.

https://yourJIRAurl/plugins/servlet/scriptrunner/admin/listeners

/*
Remove unhelpful attachments that clutter an issue, especially when they have a LOT of these attachments.
Primary target is smime.p7s files, but all files targeted here are ultimately small files that get attached by an Outlook reply. They're metadata for the email, or logo images from signature blocks.
*/

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

// Use the next 2 lines if you want to test this in the script console with a specified issue. Otherwise, "issue" will be the current issue.
// String issuekey = "CUSTSVC-12569" // Change this issuekey to the ticket you will be testing with.
// MutableIssue issue = issueManager.getIssueObject(issuekey)

def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
int deletedAttachmentsCount = 0;

for (attachment in attachments) {

def attachmentname = attachment.getFilename()
def attachmentsize = attachment.getFilesize()
if (attachmentsize <= 1024 || attachmentname.endsWith(".p7s")) {
deletedAttachmentsCount = deletedAttachmentsCount + 1
log.info attachmentname + " = " + attachmentsize + " bytes is being removed from " + issuekey + ". See <task where you logged your work for deploying the script> for details."
ComponentAccessor.attachmentManager.deleteAttachment(attachment)
}

}

if (deletedAttachmentsCount > 0){log.info "Purged " + deletedAttachmentsCount.toString() + " small attachments that were likely added by Outlook from " + issuekey + "."}

  

Stephan Maass June 15, 2022

Thanks very much! what a great sript.

Like Patrick S likes this
Patrick S
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2022

Quite welcome!

0 votes
Andre Serrano
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.
September 10, 2020

Maybe using ScriptRunner Mail Handler could be a good way out as well?

0 votes
Ravi Sagar _Sparxsys_
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.
September 9, 2020

Hi @Stephan Maass 

Can you share your script?

Some code snippets for you to get started.

def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
//Get attachment size
attachments*.filesize

//Delete the first attachment
ComponentAccessor.attachmentManager.deleteAttachment(attachments.first())

You can write condition to only delete the attachment based on the file size.

I hope it helps.

Ravi

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events