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
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:
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!
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.