Moving an Issue between projects

I'm attempting to move an issue between jira core projects using code I've seen from others who claim success. So far I get to invoking MoveIssueUpdateFields doExecute() method, but get a null pointer exception with the following stack trace: 

java.lang.reflect.InvocationTargetException 
at ProjectIssueMove.moveIssueToAnotherProject(Script514.groovy:110)
at ProjectIssueMove$moveIssueToAnotherProject.call(Unknown Source) at Script514.run(Script514.groovy:41)

Caused by: java.lang.NullPointerException at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction.readPrepopulatedIssueFromRequest(AbstractIssueSelectAction.java:124)
at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction.getIssueResultFromRequestOrDatabase(AbstractIssueSelectAction.java:102)
at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction.access$100(AbstractIssueSelectAction.java:30)
at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction$IssueGetter.get(AbstractIssueSelectAction.java:519)
at com.atlassian.jira.web.action.issue.SelectedIssue.fetchIssueResult(SelectedIssue.java:85)
at com.atlassian.jira.web.action.issue.SelectedIssue.exists(SelectedIssue.java:73)
at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction.isIssueExists(AbstractIssueSelectAction.java:60)
at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction.assertIssueIsValid(AbstractIssueSelectAction.java:488)
at com.atlassian.jira.web.action.issue.AbstractIssueSelectAction.getIssueObject(AbstractIssueSelectAction.java:157)
at com.atlassian.jira.web.action.issue.MoveIssueUpdateFields.doExecute(MoveIssueUpdateFields.java:241)
... 3 more

 I can't seem to find what the null object is, any help would be greatly appreciated.

The following is the code used: 

import com.atlassian.core.ofbiz.util.CoreTransactionUtil
import com.atlassian.jira.bc.issue.comment.CommentService
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.event.issue.IssueEventBundleFactory
import com.atlassian.jira.event.issue.txnaware.TxnAwareEventFactory
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.config.StatusManager
import com.atlassian.jira.config.properties.JiraSystemProperties
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.security.IssueSecurityHelper
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.web.SessionKeys
import com.atlassian.jira.web.action.issue.MoveIssueConfirm
import com.atlassian.jira.web.action.issue.MoveIssueUpdateFields
import com.atlassian.jira.web.bean.MoveIssueBean
import org.apache.log4j.Category
import org.apache.log4j.Logger
import org.apache.log4j.Level
import java.lang.reflect.Method
import webwork.action.ActionContext

//For testing one issue at a time//----------------------------------------
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueService = ComponentAccessor.getComponentOfType(IssueService.class)
MutableIssue issue = issueService.getIssue(user,212961).getIssue() //enter issue id here
//-------------------------------------------------------------------------


ProjectIssueMove pim = new ProjectIssueMove()
String newProjectKey = "LN"
String issueType = issue.getIssueType().getName()
String statusName = issue.getStatus().getName()

pim.moveIssueToAnotherProject(newProjectKey,issueType,statusName,issue)


class ProjectIssueMove {
Category log = Category.getInstance(ProjectIssueMove.class)

public void moveIssueToAnotherProject(String newProjectKey, String newIssueType, String newStatusName, MutableIssue targetIssue) {

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.INFO)

MoveIssueBean moveIssueBean = new MoveIssueBean(ComponentAccessor.getConstantsManager(), ComponentAccessor.getProjectManager())
moveIssueBean.getFieldValuesHolder().put(IssueFieldConstants.PROJECT, getProjectFromKey(newProjectKey).id)
moveIssueBean.getFieldValuesHolder().put(IssueFieldConstants.ISSUE_TYPE, getTargetIssueType(newIssueType).id)
moveIssueBean.setIssueId(targetIssue.getId())
moveIssueBean.setTargetStatusId(getStatusToSet(newStatusName).id)
moveIssueBean.setSourceIssueKey(targetIssue.getKey())
moveIssueBean.setUpdatedIssue(targetIssue)

ActionContext.getSession().put(SessionKeys.MOVEISSUEBEAN, moveIssueBean)

MoveIssueUpdateFields moveIssueUpdateFields = new MoveIssueUpdateFields(
ComponentAccessor.getSubTaskManager(),
ComponentAccessor.getConstantsManager(),
ComponentAccessor.getWorkflowManager(),
ComponentAccessor.getFieldManager(),
ComponentAccessor.getFieldLayoutManager(),
ComponentAccessor.getIssueFactory(),
ComponentAccessor.getFieldScreenRendererFactory(),
ComponentAccessor.getComponentOfType(CommentService.class),
ComponentAccessor.getComponentOfType(IssueSecurityHelper.class),
ComponentAccessor.getUserUtil()
)

MoveIssueConfirm moveIssueConfirm = new MoveIssueConfirm(
ComponentAccessor.getSubTaskManager(),
ComponentAccessor.getComponentOfType(AttachmentManager.class),
ComponentAccessor.getConstantsManager(),
ComponentAccessor.getWorkflowManager(),
ComponentAccessor.getFieldManager(),
ComponentAccessor.getFieldLayoutManager(),
ComponentAccessor.getIssueFactory(),
ComponentAccessor.getFieldScreenRendererFactory(),
ComponentAccessor.getComponentOfType(CommentService.class),
ComponentAccessor.getComponentOfType(IssueSecurityHelper.class),
ComponentAccessor.getIssueManager(),
ComponentAccessor.getUserUtil(),
ComponentAccessor.getIssueEventManager(),
ComponentAccessor.getComponentOfType(IssueEventBundleFactory.class),
ComponentAccessor.getComponentOfType(TxnAwareEventFactory.class)
)

JiraSystemProperties.getInstance()
moveIssueUpdateFields.setId(targetIssue.getId())
Method privateUpdateFieldsDoExecute = MoveIssueUpdateFields.class.getDeclaredMethod("doExecute")
privateUpdateFieldsDoExecute.setAccessible(true)
privateUpdateFieldsDoExecute.invoke(moveIssueUpdateFields) //fails here
moveIssueConfirm.setId(targetIssue.getId())
Method privateIssueConfirmDoExecute = MoveIssueConfirm.class.getDeclaredMethod("doExecute")

privateIssueConfirmDoExecute.setAccessible(true)
privateIssueConfirmDoExecute.invoke(moveIssueConfirm)
CoreTransactionUtil.commit(true)

}
private Project getProjectFromKey(String projectKey) { //get the projectObject from a String key
ProjectManager projectManager = ComponentAccessor.getProjectManager()
return projectManager.getProjectObjByKeyIgnoreCase(projectKey)
}
def getStatusToSet(String statusName) { //find the status object from a status name
StatusManager statusManager = ComponentAccessor.getComponentOfType(StatusManager.class)
def statuses = statusManager.getStatuses()
def iterator = statuses.iterator()
while (iterator.hasNext()) {
def status = iterator.next()
if (status.getName() == statusName) {
return status
}
}
return null
}
def IssueType getTargetIssueType(String issueTypeName) { //Find the issue type object from a issue type name
ConstantsManager constantsManager = ComponentAccessor.getConstantsManager()
def issueTypes = constantsManager.getAllIssueTypeObjects()
def issuetypeiterator = issueTypes.iterator()
while (issuetypeiterator.hasNext()) {
def issueType = issuetypeiterator.next()
if (issueType.name.toLowerCase() == issueTypeName.toLowerCase()) {
return issueType
}
}
return null
}
}

Main Reference question:

 How-to-move-an-issue-programatically-to-a-different-project

0 answers

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

87 views 1 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you