With a Scriptrunner Post function I want to include the last added attachment in a comment.
Is this possible?
If so, what value should I use to determine the last added attachment and include it in the comment?
Greetings,
Marco
For your requirement, you can try the working code below:-
import com.atlassian.jira.component.ComponentAccessor
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def attachmentManager = ComponentAccessor.attachmentManager
def commentManager = ComponentAccessor.commentManager
def attachments = attachmentManager.getAttachments(issue)
def latestAttachment = attachments.findAll().sort { it.created }.last()
def commentBody = "file has been added to the comment"
commentManager.create(issue,loggedInUser, "[^${latestAttachment.filename}] ${commentBody}", false)
Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.
What this code does is that it will sort the attachments according to the time when it was created and will only take the last attachment when it transitions.
Below are the steps to configure the post function:-
1) Once you have selected which transition will trigger the post function, you will need to add a new post-function as shown below:-
2) You will need to select the Custom script post-function [ScriptRunner] option and click the Add button as shown below:-
3) Next, add the sample code to the Post-Function and click on the Update button as shown below:-
Next, publish the changes.
Below are a few test print screens:-
1) First, add your attachments to the ticket as shown below:-
here the test1.txt attachment was added after the sample1.txt attachment
2) Next, to trigger the Post-Function, the issue has to transition to In Progress as shown below:-
as expected, the last file is added to the comment.
I hope this helps to solve your question. :)
Thank you and Kind Regards,
Ram
@Ram Kumar Aravindakshan _Adaptavist_
Thanks, it works!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
There is a custom condition-script which checks if there is an attachment at the level of the entire ticket:
import com.atlassian.jira.component.ComponentAccessor
ComponentAccessor.attachmentManager.getAttachments(issue).size() >= 1
The question is, is it possible to create a simple condition-script that checks whether the attachment was added during the transition only.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ram,
Maybe you could help me as my needs are quite similar to what you have posted above. I simply need every attachment added also added as a comment. Could this be achieved through an easier method as its quite straight forward or with an adjustment to the script above?
Thanks
Caoimhin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For your requirement, you can pretty much use the same code provided in the example in my previous comment.
But instead of adding the filter only to take the last attachment, i.e.
def latestAttachment = attachments.findAll().sort { it.created }.last()
You will need to exclude the last() option as below:-
def latestAttachment = attachments.findAll().sort { it.created }
This is so that it will take all the attachments as a list instead of only the last attachment.
Next, you will need to iterate through the list and add the attachments like below:-
latestAttachment.each {
commentManager.create(issue,loggedInUser, "[^${it.filename}] ${commentBody}", false)
}
Below is the complete updated sample code for your reference:-
import com.atlassian.jira.component.ComponentAccessor
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def attachmentManager = ComponentAccessor.attachmentManager
def commentManager = ComponentAccessor.commentManager
def attachments = attachmentManager.getAttachments(issue)
def latestAttachment = attachments.findAll().sort { it.created }
def commentBody = "all file(s) have been added to the comment"
latestAttachment.each {
commentManager.create(issue,loggedInUser, "[^${it.filename}] ${commentBody}", false)
}
I hope this helps to answer your question. :)
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ram
Thanks a lot for that answer, really helpful going to try it today. Just one other question if you don’t mind, when you said above that the script isn’t 100% as differs for environment . What are the variables on this that I need to change custom to my Jira instance? Using server by the way
Thanks again
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You will need to check, for example, what are the actual field names you are using in your environment.
If, say, it is a REST Endpoint example that may require authentication, you must double-check the credentials used.
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ram,
This has worked , thanks very much for your help much appreciated
Regards
Caoimhin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ram,
It is actually failing to run for me, I have attached the failed log if you help I would appreciate it
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
From the error message that you have shared, if you have not made any modifications to the code, it appears to be complaining about line 7 of the code, i.e.
def attachments = attachmentManager.getAttachments(issue)
You need to double-check if that issue has any attachments. If not, then it is expected that that error message will be returned.
Please share the updated code that you are using.
Thank you and Kind Regards,
Ram
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.