Jira Groovy/Script Runner get list of attachments in a screen (transitions attachments)

Burak Kaya February 20, 2021

Hi Folks,

I need a groovy script or function to get a list of attachments in a transition. For example, I just uploaded 2 attachments in transition screen but the ticket has already attachments that uploaded before, so in my code I have to list of 2 new attachments just uploaded in screen, not already uploaded to the ticket.

I've just tried some methods and changed the scripts for myself, but it currently gets all of the attachments in the ticket. Can someone please send me an example or tell me what is missing in the code below.

import webwork.action.ActionContext
import org.apache.log4j.Level
import org.apache.log4j.Logger

String className = "tr.com.as.postfunction.ProdDefectAttachmentLinks";
Logger logger = Logger.getLogger(className);
logger.setLevel(Level.INFO);
logger.info ("Script " + className + " started");

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment

def commentManager = ComponentAccessor.getCommentManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().loggedInUser

List<Attachment> lastAttachment = attachmentManager.getAttachments(issue)
lastAttachment.each{ attachment ->
logger.info("Attachment: " + attachment.getFilename() + ", Created: " + attachment.getCreated())
}

Best Regards,
Burak

2 answers

1 accepted

0 votes
Answer accepted
WW
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.
February 25, 2021

I was just about to give up looking for sample code to look for the opposite of what you're looking for, and you provided it: existing attachments. Literally, I was about to close my browser.  :)

I'll return the favor.  Here's how you find attachments that have just been added:

Source: https://community.atlassian.com/t5/Jira-questions/Validating-Attachments-Added-this-Transition/qaq-p/691981

This  is meant to run in a custom Validator, but hopefully it'll get you to where you need to be.

Basically, you need to get the attachments already in the issue, like you have, and also look for any new attachments that have been added using something like the code below.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor

def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)
def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()

def attachmentNames = issue.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue

if(attachmentNames) {
return true

 I hope it works for you!

Burak Kaya February 26, 2021

Hi there,

This is exactly what I am looking for. Thanks a lot for the provided solution. I’m very glad we helped each other! This is why say “community” :)

Thanks again, I’ve just marked this as accepted answer.

p.s I need the name of the attachments in a transition, so I made a little changes. Can be find in script runner doc or the link you’ve provided.

Regards,

Like WW likes this
0 votes
Евгений Баев April 11, 2022

Get List of current filenames on issue

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("issuekey")
log.info(issue.getAttachments().filename)

Suggest an answer

Log in or Sign up to answer