Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

ScriptRunner: Remove p7s attachments

Mark Cogan April 27, 2021

We have ScriptRunner, and we'd like to use it to remove all the .p7s attachments from our Service Desk tickets.  I think I have an idea how to do this, but my problem is that I'm getting general errors trying to add the commands.

First, is there a resource out there for the various "inc" options to include the appropriate API elements?

Second, if someone has already written something similar, would you be willing to share your code so I don't have to re-invent the wheel.

2 answers

1 vote
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 + "."}
1 vote
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.
April 27, 2021

Hi @Mark Cogan 

I don't have a complete code to do exactly that but you can fetch all the attachments on an issue using the attachmentManager.

def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)

 So I guess first fetch all the issues in the scope may be using a JQL then for each of those issues use the attachmentManager to get the attachments, retrieve filename and selective remove the one matching your criteria.

Take a look at http://library.adaptavist.com/ where you will find lot of examples to get started.

Ravi

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events