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

Clone issue from one project to another with groovy and ScriptRunner

John December 25, 2016

Hi, Team!

I write groovy script to take issue from one project and create it in backlog in another.This is my code:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import org.ofbiz.core.entity.GenericValue

def ProjectName = "ProjectName"
def ProjectName2 = "ProjectName2"
def ret = []
def time = []
def IssueStr
def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectObjByName(ProjectName)
IssueManager issueManager = ComponentAccessor.getIssueManager()
for (GenericValue genericValue : issueManager.getProjectIssues(project.getGenericValue())) {
    Issue issue = issueManager.getIssueObject(genericValue.id)
    ret.add(issue)
}
for(int i = 0; i < ret.size(); i++) {
    Issue issue1 = ret.get(i)
    time.add(issue1.getUpdated())
}
for(int i = 0; i < ret.size(); i++) {
    Issue issue2 = ret.get(i)
    if (issue2.getUpdated() == time.max()) {
        IssueStr = issue2//take issue with max update
    }
}

def mainCurIssue = ComponentAccessor.getIssueManager().getIssueObject(IssueStr.toString())
mainCurIssue.setProject(projectManager.getProjectObjByName(ProjectName2).genericValue)//change project

ComponentAccessor.getIssueManager().createIssueObject(ComponentAccessor.jiraAuthenticationContext.user, IssueStr)//creating issue

 

And after running this script in ScriptRunner, i've got Error:

com.atlassian.jira.workflow.WorkflowException: Issue workflow initialization error: unable to find Issue created with workflowId '560418'. Did the IssueCreateFunction run successfully on workflow.initialize() ?

 

What is wrong with my code?

3 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Thanos Batagiannis _Adaptavist_
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.
December 29, 2016

Hi John,

Try something like 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.web.bean.PagerFilter

def PROJECT_KEY_TO = "PT"

// edit the JQL to return the issues you want to clone
def jqlSearch = """
project = "Source Project"
"""

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()


def projectTo = ComponentAccessor.getProjectManager().getProjectByCurrentKey(PROJECT_KEY_TO)
List&lt;Issue&gt; issuesFrom = []

// get all the issues returned from the JQL
SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    issuesFrom = searchResult.issues.collect { issueManager.getIssueObject(it.id) } as List &lt;Issue&gt;
}

// clone all the issues returned from the JQL to project with key PT
issuesFrom?.each { it -&gt;
    def newIssue = issueFactory.cloneIssue(it)

    // set the project and any field you want to have a different value
    newIssue.setProjectObject(projectTo)

    Map&lt;String,Object&gt; newIssueParams = ["issue":newIssue] as Map&lt;String,Object&gt;
    issueManager.createIssueObject(user, newIssueParams)

    log.info "Issue ${newIssue?.key} cloned to project ${projectTo.key}"
}

You can also change the JQL in order to return only issue/s of the source project that its/their updated date conforms with your criteria.

Also this script is for the script console.

Let me know if you need further assistance

regards, Thanos 

Vineela Durbha May 21, 2019

Hi @Thanos Batagiannis _Adaptavist_ 

Is there a way to change the created date while using the above code? I am able to clone the issue , but the created date is same as  the parent ticket. I am expecting the created date to be to the current date and time

maharshi kondapaneni June 11, 2019

Hi @Thanos Batagiannis _Adaptavist_ 

I am trying to do the same thing with executing the same code but its shows me an error and is not running the code! can i know why? the error is in the line 

issuesFrom = searchResult.issues.collect { issueManager.getIssueObject(it.id) } as List <Issue>

can i know why  and how does this piece of code work. 

Thank you  in advance

Thanos Batagiannis _Adaptavist_
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.
June 11, 2019

What is the error @maharshi kondapaneni ?

maharshi kondapaneni June 11, 2019

Capture.PNGthese are the errors popping up ? I don"t know why can you please explain me whats wrong and what is happening over-there @Thanos Batagiannis _Adaptavist_ 

maharshi kondapaneni June 11, 2019

@Thanos Batagiannis _Adaptavist_  I did some modifications in the code By replacing that line with this 

SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

issuesFrom = searchResult.getIssues() as List<Issue>

but This was not working for all the Queries

0 votes
John December 28, 2016

Hi, Thanos.

Version of JIRA - 7.1.10, version of ScriptRunner- 4.3.9

0 votes
Thanos Batagiannis _Adaptavist_
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.
December 27, 2016

Hi John,

Which versions of ScriptRunner and JIRA you use ?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events