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?
Community moderators have prevented the ability to post new answers.
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<Issue> 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 <Issue> } // clone all the issues returned from the JQL to project with key PT issuesFrom?.each { it -> def newIssue = issueFactory.cloneIssue(it) // set the project and any field you want to have a different value newIssue.setProjectObject(projectTo) Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object> 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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is the error @maharshi kondapaneni ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
these 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_
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi John,
Which versions of ScriptRunner and JIRA you use ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.