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

Select project based on custom field during Clone an issue, and links post function (Scriptrunner)

Phillip Campbell May 21, 2018

I am using the ScriptRunner "Clone an issue, and links" post function.

 

I am trying to figure out what I need to add in the "Additional issue actions" section to create the issue in a project based on a project picker custom field. 

 

E.g. Users can select the project they want to clone to during the transition and the issue will be cloned to that project.

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Alexey Matveev
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.
May 21, 2018

How can users choose the project? Is it a custom field? This post function already creates an issue. Do you want to create another one?

Phillip Campbell May 21, 2018

It is a custom field that users can select on the workflow transition screen. 

 

I only want to clone the issue once but I want the project key for the cloned issue to be based on the user selection (custom field)

Alexey Matveev
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.
May 21, 2018

I see. It is not that simple to change the project key. You should not use the built-in function. You should create your own custom script, which would create an issue out of the source issue.

Phillip Campbell May 22, 2018

Ok. I have most of that working but I cannot seem to copy the issue comments.

 

Here is my code so far: 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
def customFieldManager = ComponentAccessor.getCustomFieldManager()


def issueManager = ComponentAccessor.getIssueManager()
def cField = customFieldManager.getCustomFieldObject("Move to Project")
def originalIssueKey = issueManager.getIssueByCurrentKey(issue.key)
def destinationProjectKey = ComponentAccessor.projectManager.getProjectObjByKey(cField.toString())
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(originalIssueKey)

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_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>

new CloneIssue().doScript(params)
}

doAfterCreate{
for (Comment comment : comments)
{
commentManager.create(originalIssueKey, comment.getAuthorApplicationUser(), comment.getBody(), false);
}
}

 

doAfterCreate seems to be giving me some problems.

 

Also,  since I am using the same code as the clone issue listener, what should I return so I can reference the new issue in the "doAfterCreate" block?

 

Lastly, I am running this code as a Scriptrunner action in the Automation Plug-in

Alexey Matveev
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.
May 22, 2018

I never used ScriptRunner in automation plugin

Phillip Campbell May 22, 2018

It is the same. I just lets me run any scriptrunner script I create as part of the automation tool. Think of it as a listener that fires whenever something happens in JIRA.

Daniel Ornelas September 27, 2018

Phillip, Not sure if you figured this out but if your intent is to copy the comments over you can pass them in the params

def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
**(CloneIssue.FIELD_COPY_COMMENTS) : true, **
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>

The doAfterCreate you would use stuff like Linking the two issue together or assigning watchers 

TAGS
AUG Leaders

Atlassian Community Events