Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Script to copy existing comments from parent issue to newly created sub-task

Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 22, 2023

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)
}
}

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 24, 2023

Hi @Manoj Gangwar

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

Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 26, 2023

@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.sceensh.PNG

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 27, 2023

Hi @Rebekka Heilmann (viadee)

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:-

configuration1.png

2. Add a Post-Function as shown below:-

configuration2.png

 

3. Select ScriptRunner's Custom Script Post-Function, as shown in the screenshot below:-configuration3.png

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:-

configuration4.png

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:-

configuration5.png

Below are a couple of test screenshots for your reference:-

1. Below is an example Story issue with some comments added to it.

test1.png

2. A Sub-task is added to the issue as shown in the screenshot below:-

test2.png

3. Add some details for the Sub-task on the Create screen and click the Create button as shown in the screenshot below.

test3.png

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:-

test4.png

 

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 27, 2023

@Ram Kumar Aravindakshan _Adaptavist_ , But Also, I want to exclude copy comments made by Automation user from parent issue to subtasks.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 28, 2023

Hi @Manoj Gangwar

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

Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 29, 2023

@Ram Kumar Aravindakshan _Adaptavist_ ,

I am expecting ScriptRunner to exclude the same comment, i.e. from the comments copied from the Parent issue.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 29, 2023

Hi @Manoj Gangwar

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 

0 votes
Rebekka Heilmann (viadee)
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 22, 2023

Are you set on using a script rather than Automation?

Mathew Lederman
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.
January 5, 2024

@Rebekka Heilmann (viadee)

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. 

Rebekka Heilmann (viadee)
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 5, 2024

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.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events