ScriptRunner: Postfunction to check what's going to be attached to an issue

Hello,

I am going to write a postfunction which will remove a file with specific name from the attached files to an issue. 

Would you suggest any API function names, examples? Where should I start from?

 

Thanks. 

1 answer

Hi,

I think you could have a code similar to following one in your post-function:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.exception.RemoveException
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.Issue
 
def Issue issue = issue

def AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager();

issue.attachments.each {
    if (it.filename.contains("specificName")) {
        try {
            attachmentManager.deleteAttachment(it)
        } catch (RemoveException e) {
            log.error("Attachment ${it.filename} could not be removed", e)
        }
    }
}

Oh, thank you! This code works just perfectly! Would you be so kind as to specify the criteria I could use? For example - name (contains();), what else? Size? MD5 sum? Thanks!

As "it" is an instance of the Attachment class, you will be able to retrieve the file size but not the MD5 sum. For more information please check the javadoc of that class: https://developer.atlassian.com/static/javadoc/jira/reference/com/atlassian/jira/issue/attachment/Attachment.html

Hey, Alejo The script works just fine when I create my issues from the JIRA itself. It does not work though when I create issue via email handlers (by sending an email). It seems to me that the issue is not created at the moment. The attachments collection is empty. Any suggestions?

I tested the issue in a local instance and my guess is that the mail handler are simply firing the post-function too early, meaning that the issue is not ready when the post function is called.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,971 views 12 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot