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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,461,809
Community Members
 
Community Events
176
Community Groups

Move an issue to another project using ScriptRunner

We have a listener for the following functionality:

Whenever a ticket is created, if the issue summary is a specific value, it does the following: 

  1. Assigns the ticket to someone
  2. Changes the ticket type
  3. Moves the ticket to a new project

I'm able to do #1 and #2 without any problems. For #3, I tried updating the Project Id but it seems to keep the issue in the same project, not the new project.

How can I programmatically move an issue from one project to another. Do I need to recreate this ticket in the new project, and then delete the ticket from the old project?

I understand the configurations in both projects need to be the same, that's not an issue (nor will it be an issue in the future)

I don't think it helps much, but here are the relevant parts of the code I currently have

MutableIssue issue = event.issue as MutableIssue

// Omitted code:
// 1) Logic to check the summary and continue if not relevant
// 2) Update assignee
// 3) Change ticket type

// Move the issue to the Data Recon project
issue.setProjectId(12100)
ComponentAccessor.getIssueManager().updateIssue(user,issue,EventDispatchOption.ISSUE_UPDATED, false)

Everything but the project is getting updated, so the updateIssue call is going through, just not for the project. 

3 answers

0 votes

Hello,

I know it's a bit late, but I also had this problem and I just solved it.
In summary, what you have to do is change the project of the original issue and recreate it, in this way you make an update indirectly.

For example:

 

IssueManager issueManager = ComponentAccessor.getIssueManager()
def projectManager = ComponentAccessor.getProjectManager()
Issue issue = event.getIssue() as MutableIssue
// Get project destination
def projectDestination = projectManager.getProjectObjByName(PROJECTNAME)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Set in our origin issue the project to destination
issue.setProject(projectDestination.genericValue)
 // Create map with updated issue
Map<String,Object> updatedIssue = ["issue": issue] as Map<String,Object>
// Create the updated issue in destination project
issueManager.createIssue(user, updatedIssue)
With this example, the issue will move completely, with all the fields you have and even the history.
Regards, Anton

The issue's attachments break when I try this. The rest of the fields move successfully to the mentioned project.

Any idea if this can be fixed?

import java.lang.String  
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*
import com.onresolve.scriptrunner.parameters.annotation.*
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import com.atlassian.jira.issue.AttachmentManager
import com.atlassian.jira.issue.attachment.*


def issueManager = ComponentAccessor.issueManager
def projectManager = ComponentAccessor.getProjectManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def attachmentManager = ComponentAccessor.getAttachmentManager();


//for specific issues
@ShortTextInput(description = 'Issue you want to move', label = 'Provide Issue Key')
String issueKeyInput

@ShortTextInput(description = 'Project you want to move it to', label = 'Provide the Project Key')
String projectKeyInput

def issueKeys = issueManager.getIssueByCurrentKey(issueKeyInput)
def projectKey = projectManager.getProjectObjByKey(projectKeyInput)

//return issueKeys.getSummary()

//return issueKeys
 
def oldIssueKey = issueKeys
   
//for use in automation rules
//def subTaskissueKeys = issue as MutableIssue
//def parentTaskIssueKey = subTaskissueKeys.getParentObject()
   
def issueService = ComponentAccessor.issueService
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def oldAttachments = attachmentManager.getAttachments(issueKeys)



try
{  
issueKeys.setProjectObject(projectKey)
// Create map with updated issue
Map<String,Object> updatedIssue = ["${issueKeys.getSummary()}": issueKeys] as Map<String,Object>
return updatedIssue
// Create the updated issue in destination project
issueManager.createIssue(user, updatedIssue)
issueKeys.store()
}

catch (Exception ex)
{
    log.info("Error in moving to be checked")
}


return issueKeys.getSummary() + "<br>" + issueKeys.toString()

 

0 votes

@Dymas Coelho  I think your code would work with any other field, the problem is that you are changing the project id.

In my opinion, you should clone the issue with IssueFactory.

def newIssue= issueFactory.cloneIssue(issue)

newIssue.setProjectId(XXXX)

Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>

issueManager.createIssueObject(user,newIssueParams)

@Dymas Coelho I'm sorry but it is not as simple. You can try to Move issue manually and you will see what must be solved.

Check this thread: https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Move-Jira-issue-from-one-project-to-another-project/qaq-p/960195

Suggest an answer

Log in or Sign up to answer