Hello!
I am trying to write a ScriptRunner groovy script that clones an issue. Eventually this will be a post-function that clones to multiple projects, but baby steps.
As a test, here's my script that clones a hard-coded issue to another project.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils
import org.apache.log4j.Logger
import com.atlassian.jira.issue.MutableIssue
def issue = ComponentAccessor.getComponent(IssueManager).getIssueByKeyIgnoreCase("SRC-1")
def clonesId = ComponentAccessor.getComponent(IssueLinkTypeManager).getIssueLinkTypes().findByName("Cloners")?.id
def cloneIssue = new CloneIssue()
def inputs = [
issue : issue,
(ConditionUtils.FIELD_ADDITIONAL_SCRIPT): """
issue.setSummary("CLONED: " + issue.getSummary())
checkLink = {link -> link.issueLinkType.name != "Cloners"}
""",
(CloneIssue.FIELD_COPY_COMMENTS) : true,
(CloneIssue.FIELD_TARGET_PROJECT) : "DEST",
(CloneIssue.FIELD_LINK_TYPE) : clonesId + " inward",
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
]
def errorCollection = cloneIssue.doValidate(inputs, false)
if (errorCollection.hasAnyErrors()) {
log.warn("Couldn't clone issue: ${errorCollection}")
}
else {
cloneIssue.doScript(inputs)
}
The issue does get cloned, but the ADDITIONAL_SCRIPT does not seem to get called. That is, the summary of the clone remains identical to the original, and all links are cloned.
Is there any trick to getting the ADDITIONAL_SCRIPT code to run?
Hi Damon,
What version of SR are you on? You might try this instead:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
def issue = ComponentAccessor.getComponent(IssueManager).getIssueByKeyIgnoreCase("SRC-1")
def clonesId = ComponentAccessor.getComponent(IssueLinkTypeManager).getIssueLinkTypes().findByName("Cloners")?.id
def cloneIssue = new CloneIssue()
def additionalCode = """
issue.setSummary("CLONED: " + issue.getSummary())
checkLink = {link -> link.issueLinkType.name != "Cloners"}
"""
Map<String, Object> inputs = [
issue : issue,
(ConditionUtils.FIELD_ADDITIONAL_SCRIPT): [additionalCode, ""],
(CloneIssue.FIELD_COPY_COMMENTS) : true,
(CloneIssue.FIELD_TARGET_PROJECT) : "DEST",
(CloneIssue.FIELD_LINK_TYPE) : clonesId + " inward",
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>
def errorCollection = cloneIssue.doValidate(inputs, false)
if (errorCollection.hasAnyErrors()) {
log.warn("Couldn't clone issue: ${errorCollection}")
}
else {
cloneIssue.doScript(inputs)
}
Adaptavist ScriptRunner for JIRA 5.4.12
Yes, that did the trick! Thank you!
How did you know that, btw? Is there documentation that would have told me that the interface seems to have changed at some point?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Damon,
That's a good question. I took a quick look at our documentation and I don't see an example of CloneIssue being used manually, so I don't think you would have known about any changes. I can see how that would be frustrating!
The problem here, in my opinion, is that running CloneIssue manually is something we don't particularly want everyone to do, so it's not likely to end up in the documentation. It can be difficult to use and often creates more problems for users than it solves, since the built-in clone script is often "good enough." Only in niche cases do we recommend doing it, such as in your situation.
In any case, if you run into a problem like this again, I'd suggest submitting a support request through our Service Desk portal here: https://marketplace.atlassian.com/apps/6820/scriptrunner-for-jira?hosting=server&tab=support
Regards,
Josh
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.
Hi Joshua Yamdogo @ Adaptavist ,
is there a full documentation out there in the internetz about the possible map values of the CloneIssue params?
CloneIssue.FIELD_COPY_COMMENTS
CloneIssue.*WHAT CAN I DO HERE ¯\_(ツ)_/¯ *
Thanks for your time,
cheers
slothy
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.