Hello All,
When a subtask created then it should copy all the existing comments from the Parent issue. Also, I want to exclude comments made by Automation user.
I got an script but its showing error. Can some one help me with the correct script.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.Comment
def subtask = issue // Newly created subtask
def parentIssue = subtask.getParentObject() // Parent issue
// Get the automation user (change 'Automation User' to your actual automation user's name)
def automationUser = ComponentAccessor.getUserManager().getUserByName('Automation User')
// Get comments from the parent issue
def comments = parentIssue.getComments()
// Loop through comments and copy to the subtask, excluding comments made by the automation user
comments.each { comment ->
if (comment.getAuthor() != automationUser) {
ComponentAccessor.getCommentManager().create(subtask, comment.getAuthor(), comment.getBody(), comment.getGroupLevel(), comment.getRoleLevelId(), comment.getCreated(), null)
}
}
Could you please share the error message you are currently getting along with a screenshot of your Automation configuration?
Instead of using Automation, why not do it directly on the workflow's Post-Function using ScriptRunner?
I am looking forward to your feedback and clarification.
Thank you and Kind regards,
Ram
@Ram Kumar Aravindakshan _Adaptavist_ I am getting errors as in the attached screenshot. I can add it to the post function just looking for correct one.
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, I suggest creating a Post-Function in the Create transition.
Below are the steps for your reference:-
1. Go to the Create transition and add a Post-Function as shown in the screenshot below:-
2. Add a Post-Function as shown below:-
3. Select ScriptRunner's Custom Script Post-Function, as shown in the screenshot below:-
Once you are on the Custom Script Post-Function page, add the sample code below:-
if (issue.isSubTask()) {
def parent = issue.parentObject
issue.set {
parent.comments.each {
issue.addComment(it.body)
}
}
}
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Custom Script Post-Function configuration:-
Save and Publish the Post-Function.
Note: Please ensure the Post-Function is added below the Issue Created Post-Function as shown in the Screenshot Below:-
Below are a couple of test screenshots for your reference:-
1. Below is an example Story issue with some comments added to it.
2. A Sub-task is added to the issue as shown in the screenshot below:-
3. Add some details for the Sub-task on the Create screen and click the Create button as shown in the screenshot below.
4. Once the Sub-task is created, view it, and you will see that the comments, from the parent issue, have been copied as shown in the screenshot below:-
I hope this helps to solve 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.
@Ram Kumar Aravindakshan _Adaptavist_ , But Also, I want to exclude copy comments made by Automation user from parent issue to subtasks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your last comment, you mentioned:-
But Also, I want to exclude copy comments made by Automation user from parent issue to subtasks.
Have you included a filter in your Automation to ensure that the Comments will not be included in the Sub-task, or are you expecting Automation to include the comment in the Sub-task as well and ScriptRunner to exclude the same comment, i.e. from the comments copied from the Parent issue?
Please clarify.
Either way, if you want to exclude the Automation Comment from the Parent issue, you will need to update the code to:-
if (issue.isSubTask()) {
def parent = issue.parentObject
def filter = parent.comments.findAll { it.body == 'This is an Automated Comment' } //The comment message set in Automation
def output = parent.comments - filter
issue.set {
output.each {
issue.addComment(it.body)
}
}
}
Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
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.
@Ram Kumar Aravindakshan _Adaptavist_ ,
I am expecting ScriptRunner to exclude the same comment, i.e. from the comments copied from the Parent issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the clarification.
If you only intend to exclude the Automation comment copied from the Parent, the solution I provided in my last comment will do this.
Here is the code once again:-
if (issue.isSubTask()) {
def parent = issue.parentObject
def filter = parent.comments.findAll { it.body == 'This is an Automated Comment' } //The comment message set in Automation
def output = parent.comments - filter
issue.set {
output.each {
issue.addComment(it.body)
}
}
}
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.
Are you set on using a script rather than Automation?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm working with @Manoj Gangwar on this. We would be prefer to use Automation rather than a post-function as this could be applied to the whole project rather than to each workflow. However, I think we would still need to use some scripting to exclude comments made by the automation user.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you branch over the comments, you should be able to access the author with a smart value {{issue.comments.author.displayName}} (.id should work as well) and use that in a condition.
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.