Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Copy an Issue to a Separate Project

Created 1 year ago, Updated 2 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl

def projectKey = "Project Key" //Replace with the key of the project you want to copy to
def issueKey = "Issue Key" //Replace with the key of the issue you want to copy

def projectManager = ComponentAccessor.projectManager
def issueManager = ComponentAccessor.issueManager

def projectToCopyTo = projectManager.getProjectByCurrentKey(projectKey)
def issueToCopy = issueManager.getIssueObject(issueKey)

// Block creation of intra-project links
def blockIntraprojectLinks = '{l -> l.sourceObject.projectObject != l.destinationObject.projectObject}'

if (!issueToCopy) {
    log.warn("Issue ${issueKey} does not exist")
    return
}

//Set the creation parameters/inputs (use clone issue but with no link type)
def inputs = [
    (CloneIssue.FIELD_TARGET_PROJECT)       : projectToCopyTo.key,
    (CloneIssue.FIELD_LINK_TYPE)            : null,
    (ConditionUtils.FIELD_ADDITIONAL_SCRIPT): [
        "checkLink = $blockIntraprojectLinks;",
        ""
    ],
    (CloneIssue.FIELD_SELECTED_FIELDS)      : null, //clone all the fields
    (CloneIssue.SKIP_EPIC_LINKS)            : "true",
] as Map<String, Object>
def executionContext = [issue: issueToCopy] as Map<String, Object>

def newClonedIssue = ScriptRunnerImpl.scriptRunner.createBean(CloneIssue)
// Execute the clone action with the specified inputs
def updatedExecutionContext = newClonedIssue.execute(inputs, executionContext)
//The issue has been successfully cloned
assert updatedExecutionContext.newIssue
Having an issue with this script?
Report it here