During custom script post-function, use custom field to define what project to place cloned issue in

Lara Arthur February 26, 2021

Hi there Atlassian community/ ScriptRunner peeps,

Hoping to get some assistance in figuring out how to properly code the following...

  • I created a Custom web section and a Constrained create issue dialog that creates a Support Request issue type (working perfectly with the behaviour I've got along with it)
  • Once the Support Request is 'ACCEPTED', I want to clone the Support Request as a task in the 'acceptors' project (I can't use the pre-scripted post function to clone issue since I need it to go to different projects based on info in the Support Request).
  • I have a custom project picker field that I'm using on the Support Request issue type screen (this is what I want to use to tell jira what project I want the cloned issue created in)
  • I've found code from SR that works if I define the issue I want cloned and the specific project to create it in
  • I've found code that doesn't quite work to take that a step further and use the project picker field to designate which project to create the cloned issue in

Here's the code that I've mushed together...and it's not working. Hoping someone can provide some add'l insight.

Thanks in advance

//Pulled from SR script found online recipe
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.onresolve.scriptrunner.canned.jira.utils.AbstractCloneIssue
import com.onresolve.scriptrunner.canned.jira.utils.CannedScriptUtils
import com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import groovy.xml.MarkupBuilder

//pulled from atlassian user

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def cField = customFieldManager.getCustomFieldObject("Supporting Project")
def originalIssueKey = issueManager.getIssueByCurrentKey(issue.key)
def issueToClone = issueManager.getIssueByCurrentKey(issue.key)
def destinationProjectKey = ComponentAccessor.projectManager.getProjectObjByKey(cField.toString())
def commentManager = ComponentAccessor.getCommentManager()

def issueToClone = ComponentAccessor.issueManager.getIssueByCurrentKey(issue.key)
def linkBetweenIssues = CannedScriptUtils.getAllLinkTypesWithInwards(true).find { it.value == "support work" }?.key?.toString()


cloneIssue(originalIssueKey, destinationProjectKey)
issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
void cloneIssue (MutableIssue issue, Project project) {
def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.Key,
(CloneIssue.FIELD_TARGET_ISSUE_TYPE) : '3',
(CloneIssue.FIELD_COPY_FIELDS) : AbstractCloneIssue.COPY_ALL_FIELDS,
(CloneIssue.FIELD_SELECTED_FIELDS) : null,
(CloneIssue.FIELD_COPY_COMMENTS) : true,
(CloneIssue.FIELD_USER_KEY) : null,
(ConditionUtils.FIELD_ADDITIONAL_SCRIPT): ["", ""],
(CloneIssue.FIELD_LINK_TYPE) : linkBetweenIssues
] as Map<String, Object>
def executionContext = [issue: issueToClone] as Map<String, Object>

def cloneIssueAction = ScriptRunnerImpl.scriptRunner.createBean(CloneIssue)
// Execute the clone action with the specified params
def updatedExecutionContext = cloneIssueAction.execute(params, executionContext)

// Return the link to the cloned issue
cloneIssueAction ? "Cloned issue: ${createHrefLinkToIssue(updatedExecutionContext.newIssue as String)}" :
"Could not clone issue, check the logs for errors"

static String createHrefLinkToIssue(String issueKey) {
def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
def writer = new StringWriter()
def html = new MarkupBuilder(writer)

html.html {
a href: "$baseUrl/browse/$issueKey", issueKey
}

writer
}

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
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 27, 2021

Why don't you use the built-in function and use the additional issue actions to change the project from the default selected in the form?

import com.atlassian.jira.project.ProjectImpl
def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Supporting Project'}
def project = sourceIssue.getCustomFieldValue(cf) as ProjectImpl
issue.setProjectObject(project)

 

Lara Arthur March 1, 2021

@Peter-Dave Sheehan - I didn't think it was possible since there is a drop down to choose the project...I'll give it a try. Thanks!

Lara Arthur March 1, 2021

@Peter-Dave Sheehan  Thank you soooo much for providing that fix! Working perfectly - appreciate the assistance!

Like Peter-Dave Sheehan likes this
Peter-Dave Sheehan
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 1, 2021

To be honest, I did not test this and wasn't 100% certain it would work. But based on the documentation saying "if you want to override some of the field values rather than have them inherited from the source issue you can specify that in the Additional Code" I was pretty confident it would work.

Suggest an answer

Log in or Sign up to answer