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

I want to clone comments with Scriptrunner for Jira Cloud

Drew Nedderman February 6, 2018

How can I create a listener that will clone comments along with an issue? Does it matter that a comment author is an external customer?

Sometimes our JSD issues need extra development support, and I need to clone the issue into a Jira Software project. The comments in the JSD issue are valuable feedback and information that the developer needs to help resolve the issue.  

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Neta Elyakim
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.
July 18, 2018

You don't have an event for issue clone on Jira cloud, and even when you clone a ticket you can't know what was the original ticket because there is no link between them.

Brittany Wispell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2018

Hi @Neta Elyakim

The creation of the link when you clone is optional. It is done by design of our ScriptRunner team. 

If you have any questions about it let me know! 

Neta Elyakim
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.
July 18, 2018

Hey@Brittany Wispell,

Thank you for informing me.

@Drew Nedderman

If you have a link between them you can try to use this script-

def getComments = get('/rest/api/2/issue/${issue.key}/comment')
.header('Content-Type', 'application/json')
.asObject(Map)

if (getComments.status == 200){
def comments = getComments.body.comments["body"]

logger.info("comments: "+comments)

if(!comments.isEmpty()){

def issuelinks = issue.fields["issuelinks"]
def inwardIssue = issuelinks["outwardIssue"] // or def inwardIssue = issuelinks["inwardIssue"] you need to check this

def cloneIssue
def requestBody
for(int i=0;i<inwardIssue.size();i++){
// check here the like type if is clone type
cloneIssue = inwardIssue[i]
}
if(cloneIssue != null){
for(int i=0;i<comments.size();i++){
def commentResp = post("/rest/api/2/issue/${cloneIssue.key}/comment")
.header('Content-Type', 'application/json')
.body([body: comments[i].toString()])
.asObject(Map)

assert commentResp.status == 201
}
}
}
}
0 votes
Brittany Wispell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 15, 2018

Hey @Drew Nedderman

You could modify this code. It is for a parent subtask relation. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment

Issue subtask = event.issue
Comment comment = event.comment
def commentManager = ComponentAccessor.getCommentManager()

if (subtask.isSubTask() && comment) {
def parentIssue = subtask.getParentObject()
def commentToParent = commentManager.create(
parentIssue, //the parent issue to copy the comment
comment.getAuthorApplicationUser(), //the author of the subtask's comment
comment.getBody(), //the actual comment body
comment.getGroupLevel(), // is the group name to limit comment visibility to, this must be a valid group name.
comment.getRoleLevelId(), // is the id of the the ProjectRole to limit comment visibility to, this must reference a valid project role.
false) //if true then an event of type EventType.ISSUE_COMMENTED_ID will be dispatched and any notifications listening for that event will be triggered. If false no event will be dispatched.
log.info "Comment copied to parent issue ${parentIssue.getKey()}"
}

 

And here's a snippet of how you'd get the linked issues. 

def originIssue
issueLinkManager.getInwardLinks(issue.id)?.each {issueLink ->
issueLink.sourceId
Issue candidateIssue = issueManager.getIssueObject(issueLink.sourceId)
if(candidateIssue.projectObject.key.equals(cloneParentKey)){
originIssue = candidateIssue

 

I hope this helps get you started.  

Drew Nedderman February 15, 2018

Hi @Brittany Wispell, does the code that you provided work for cloud instances? When I past it into the console I get static type checking errors.

 

Unable to resolve class for these lines:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment

 Thank you for your assistance!

Brittany Wispell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 18, 2018

Hey @Drew Nedderman

Try adding ; to the end of those lines.

TAGS
AUG Leaders

Atlassian Community Events