Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Bulk Changing issues Types via Groovy

Zachary Singh November 1, 2023

Hi! 

I am trying to bulk change issue types via groovy. I have been able to do so but only at a single issue, here is the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def issueManager = ComponentAccessor.issueManager
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueKey = 'xx-xxx' 

def issue = issueManager.getIssueObject(issueKey)
def summary = issue.summary
def description = issue.description
def sourceIssuetype = issue.getIssueType().getName()
def targetIssueType = constantsManager.allIssueTypeObjects.find {it.name == 'Task' }


if (sourceIssuetype = 'Bug') {
    issue.setIssueType(targetIssueType)
    issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

 I am hoping to pass a JQL into the script or a bulk amount of issues to change at once. The UI move is very tedious and often times out for us. Any suggestions?

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
November 1, 2023

I see you are using issueManager instead of the new HAPI methods.

Perhaps you don't have access to the recent version of Scriptrunner that includes HAPI.

So if you need to use the native JQL searching capabilities, here is how you could write your script:

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

SearchService searchService = ComponentAccessor.getComponent(SearchService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def jql = '' //specify your jql here

searchService.parseQuery(currentUser, jql)

def jqlParseResult = searchService.parseQuery(currentUser, jql)
assert jqlParseResult.isValid(), jqlParseResult.errors

searchService.search(currentUser, jqlParseResult.query, PagerFilter.unlimitedFilter).results.each { issue ->
//do your issue operations here
}

But there is a possibility with this code that you will run out of memory if you try to run it on a very large number of issues.

The better approach would be to use a limited-size PageFilter and run this in chunks.

Another couple of caveats:

1) changing the issuetype on an issue is usually not recommended. This will not take care of migrating the issue to the correct workflow for the new issue type and all other scheme-related adjustments like the "move" operation in the UI does.  The only place I would allow this is if the 2 issue types share ALL the same configuration: same workflow, same issue configs etc.

I am not aware of any single command that can replicate the move operation.

2) your code was missing a critical step of re-indexing the issue after updating it with the issueManager. Your search results would still be based on the old values. You need to also trigger a re-indexing. If you use IssueSearvice instead of IssueManager, you don't need to worry about the index.

Zachary Singh November 2, 2023

Thanks Peter! I was attempting to use HAPI, but it couldn't find any documentation around updating the issuetype via HAPI.

 

 

 

PD Sheehan
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.
November 2, 2023

You can still use HAPI to apply your JQL then run normal issue update code inside the search even if issue.update{ } doesn't offer a way to update the issue type (for good reason as mentioned above).

Issues.search(jql).each{ issue->
//do issue stuff here, and use issueManager.update
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events