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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes
Answer accepted

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)

 

@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!

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

Like Peter-Dave Sheehan likes this

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
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Bitbucket Smart Commits vs. Genius Commits - What's the difference?

If you already heard about Smart Commits in Bitbucket, know that you just stumbled upon something even better (and smarter!): Genius Commits by Better DevOps Automation for Jira Data Center (+ Server...

106 views 0 2
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you