Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,714
Community Members
 
Community Events
168
Community Groups

ScriptRunner script to delete attachments smaller than 10kb

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

1 vote
Answer accepted

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 + "."}

  

Thanks very much! what a great sript.

Like Patrick S likes this

Quite welcome!

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

0 votes

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
Community showcase
Published in Apps & Integrations

Apps for Confluence you won't want to miss: RSVP for September's Appy Hours

Calling all collaborators and Confluence users! Our Appy Hours event on September 29th features 4 presenters demoing functionality to superpower Confluence. Don't miss learning about these apps i...

123 views 0 9
Read article

Atlassian Community Events