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.
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 + "."}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.