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,293,312
Community Members
 
Community Events
165
Community Groups

ScriptRunner: Remove p7s attachments

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

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

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
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

158 views 0 4
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you