Clone issue with comments in script runner for jira cloud

Pooja March 8, 2019

Hi All,

I am I am using jira cloud and script runner for jira.

When I tried to clone from post function with script runner Comments were not getting cloned. 

I tried https://community.atlassian.com/t5/Adaptavist-questions/I-want-to-clone-comments-with-Scriptrunner-for-Jira-Cloud/qaq-p/720819

https://community.atlassian.com/t5/Jira-questions/Scriptrunner-When-I-clone-issue-I-wanna-include-comments-to/qaq-p/610237

but no luck.

Thanks in advance

 

 

4 answers

1 accepted

3 votes
Answer accepted
Kristian Walker _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.
March 11, 2019

Hi Pooja,

I can confirm that you have linked to above will not work inside of ScriptRunner for Jira Cloud is due to the fact the code you have provided is fro ScriptRunner for Jira Server and this will not work as Atlassian only provide a rest API in Jira Cloud and do not provide a Java API in the cloud like they do in Jira Server.

You can see more detailed information on the differences between the cloud and server versions inside of our documentation page located here.

I can also confirm that it is not possible to copy comments using the Clone Issue post function inside of Jira Cloud due to the restricted nature of Jira Cloud which means that it is not possible to copy the issue and its comments at the same time.

This means that you will need to use a seperate approach such as writing a custom script to copy the issue which can use the Get Comments and Add Comment API's to copy the comments between issues.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

0 votes
Lukasz Grobelny
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.
February 17, 2022

If you want a custom script I can share my code :)

Paul Norris April 12, 2022

I really need this, so I would love to get your custom script!

Joel Batac June 22, 2022

i need this script as well :)

Lukasz Grobelny
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 22, 2022
package com.adaptavist.sr.cloud.samples.events
import org.apache.log4j.Logger
import groovy.json.JsonSlurper
def log = Logger.getLogger("com.acme.workflows")

//Wybieramy pole z project picker
def customFieldName = 'customfield_10036'

//wyciagamy pola
def fields = get("/rest/api/2/issue/${issue.key}?fields")
.header('Content-Type', 'application/json')
.asObject(Map).body.fields

def projectKeyFromField = fields.customfield_10036.key
def selectedFields = fields.customfield_10037.value as List
def summary = fields.summary
def components = fields.components.id
def reporter = fields.reporter
def labels = fields.labels
def assignee = fields.assignee
def comment = fields.comment
def attachment = fields.attachment
def subtasks = fields.subtasks
def originalKey = issue.key

log.warn(originalKey)

//def projectKey = projectKeyFromField
def projectId = get("/rest/api/2/project/${projectKeyFromField}").asObject(Map).body.id
log.warn(projectId)
// Wyszukujemy ID typu taska
def taskType = get("/rest/api/2/issuetype/project?projectId=${projectId}").asObject(List).body.find { it['name'] == 'Zadanie' }['id']
log.warn(taskType)
//Tworzymy zgłoszenie
def postedIssue = post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body(
[
fields: [
summary : summary,
description: "Test",
labels : labels,
reporter : reporter,
assignee : assignee,
components : [{
id : components}
],
project : [
key: projectKeyFromField
],
issuetype : [
id: taskType
]
]
])
.asObject(Map).body.key

log.warn(postedIssue)


def createdIssueKey = postedIssue as String
log.warn(fields.comment.comments.created)
if(selectedFields.contains("Komentarze")){
log.warn(fields.comment.comments.body.size())
for(int i = 0; i < fields.comment.comments.body.size(); i++){
post('/rest/api/2/issue/' + createdIssueKey + '/comment')
.header('Content-Type', 'application/json')
.body([
body : fields.comment.comments.body[i],
created:fields.comment.comments.created[i],
author: [
displayName: comment.comments.author.displayName[i]
]
])
.asObject(Map)
}
}

//wrzucamy zalaczniki
if(selectedFields.contains("Załączniki")){
for(int i = 0; i < attachment.size(); i++ ){
def attId = attachment.id[i]
def att = Unirest.get('/rest/api/2/attachment/' + attId).asObject(Map).body
def downloadUrl = attachment.content[i]
def readFileResp = Unirest.get(downloadUrl).asBinary()
log.warn(downloadUrl)
def response = Unirest.post('/rest/api/2/issue/' + createdIssueKey + '/attachments')
.header("Accept", "application/json")
.header("X-Atlassian-Token", "nocheck")
.field("file", readFileResp.body, att.title.toString())
.asObject(Map)

}
}

//id subtaska
def subTaskType = get("/rest/api/2/issuetype/project?projectId=${projectId}").asObject(List).body.find { it['name'] == 'Podzadanie' }['id']
log.warn(subTaskType)

//sprawdzmay czy są subtaski, jeśli tak - są przenoszone
if(subtasks != null){
for(int i = 0; i< subtasks.size(); i++){
post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body(
[
fields: [
summary : subtasks.fields.summary[i],
description: subtasks.fields.description[i],
reporter : reporter,
assignee : assignee,
parent : [
key : postedIssue
],
project : [
key: projectKeyFromField
],
issuetype : [
id: subTaskType
]
]
])
.asObject(Map).body.key
}
}

//Linkowanie zgloszen
// Specify the source issue
def soucrceIssueKey = "TS-2"
// Specify the target issue
def targetIssueKey = "TS-3"

// Spcecify the link type to use
def linkType = "Duplicate"

// Create the issue link between both issues
def link = post('/rest/api/3/issueLink')
.header('Content-Type', 'application/json')
.body([
type: [ name: linkType ],
outwardIssue: [ key: originalKey ], // This is the issue that the link 'starts' at.
inwardIssue: [ key: createdIssueKey ] // This is the issue that the link 'finises' at.
])
.asString()
// validate that the issue link created correctly

// Validate the issue updated correctly
if (link.status == 201) {
return "Success - The issues with the key of ${soucrceIssueKey} and ${targetIssueKey} have been linked with the ${linkType} link type."
} else {
return "${link.status}: ${link.body}"
}
Like Tomasz Osik likes this
Tomasz Osik July 22, 2022

Hi Lukasz

I owe you a big bottle of "Zoladkowa Gorzka" (or any other vodka :) )

Lukasz Grobelny
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 22, 2022

Here to help, just check if it's work, I've created it some time ago :) 

Carney Calcutt October 10, 2023

I have also run into this same issue. We are currently cloning an issue as Workflow Post Function. I am very new to all of this. Where does this script go?

Lukasz Grobelny
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.
October 25, 2023

My code is for cloud, works as a postfunction

0 votes
Jennifer Mun June 2, 2020

Is there a way to clone issue comments in Jira Next Gen (cloud)?

0 votes
Marlene Kegel - codefortynine
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 13, 2019

If you don't want to write a custom script you can try our cloud app Deep Clone for Jira. With that app you can clone comments and other advanced content.

It's also possible to bulk clone thousands of issues and move them in one go.

Tomasz Osik March 18, 2022

Hi,

 

But is it possible to deep clone it with Jira Automation?

Marlene Kegel - codefortynine
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 21, 2022

If you want to trigger a clone from post-function, you can work with the Deep Clone workflow post-function.

If you want to trigger a clone with Jira Automation, but don't want to change the status, you can work with a looping transition and a Deep Clone post-function.

If you want to trigger an automation from a Deep Clone you can create an incoming webhook.

Suggest an answer

Log in or Sign up to answer