Change status without a transition

Bouman August 6, 2019

UPDATED - "searchResults" needed to be updated.

I wanted to create a script that would change the status of an issue without having to transition it. After a lot of searching and head banging, I was able to create a groovy script that I can run in the Script Console in ScriptRunner. This script accepts two inputs.

1 - A JQL to provide the script with a list of issues that it needs to modify.  

2 - The destination status you want the issues in.

The script will loop through the issues and "migrate" the issues into the status you Identified. 

I have found this extremely helpful for issues that get "stuck" in statuses that have no way out. I hope it will help others.

 


/*****************************
Purpose: This script is used to change the status of issues regardless of were they are in the workflow. No transition is needed to move an issue into another status. However, The destination status has to reside in the workflow.
Author: Kevin Bouman
Date: 08/06/2019
Updated: 12/26/2020 Kevin Bouman (Added instructions for updating the resolutions)
*****************************/

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.workflow.WorkflowManager;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.config.ConstantsManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.util.ImportUtils;

def searchService = ComponentAccessor.getComponent(SearchService.class);
def issueManager = ComponentAccessor.getIssueManager();
def constantsManager = ComponentAccessor.getConstantsManager();


//********************** VARIABLES ******************************

//Place the name of the Destination status
def destinationStatusName = "Approved";

/****************
If you need to update the Resolution of an issue please refer to the "ScriptRunner" Built-In-Scipt called "Bulk Fix Resolutions".
- Update the "Fix - Resolution" JQL to grab the issues you need to update the resolution for.
- Navigate to the "Bulk Fix Resolutions" built-in-script
- Select the "Fix - Resolution" JQL that you just updated.
- Select the resolution you want to update the issues to.
- Run the script
****************/

//Define the JQL to get the issues you want to affect
def jqlQuery = 'key = PRISK-613';

//********************** ********* ******************************

//Results array
def results = [];

//Get the current user
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
def user = authContext.getLoggedInUser();

//Run the JQL
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlQuery);
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter());

//create the list that contains all the issue keys
def issues = searchResult.results.collect {issueManager.getIssueObject(it.id)};

//Loop through the list of results from the JQL.
for (MutableIssue i in issues){

//Get the workflow
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager();

def workflow = workflowManager.getWorkflow(i);
def workflowName = workflow.getName();

//Create map to store return values
def issueMap = [:];

def currentStatusName = i.getStatus().getName();

//Check if the current status is the same as the destinationStatus
if (currentStatusName != destinationStatusName){

//Get Destination Status Object
def destinationStatus = constantsManager.getStatusByName(destinationStatusName);

//Migrate issue with WorkflowManager
workflowManager.migrateIssueToWorkflow(i, workflow, destinationStatus);

//Update the issue in the DB
issueManager.updateIssue(user, i, EventDispatchOption.DO_NOT_DISPATCH, false)

//log.warn("The status of ${i.key} was changed from $currentStatusName to $destinationStatusName")

//Add to map
issueMap.put("issue", i.getKey());
issueMap.put("result", "The status was changed from $currentStatusName to $destinationStatusName");
}
else {
//Add to map
issueMap.put("issue", i.getKey());
issueMap.put("result", "The status of " + i.getKey() + " was not changed");
}

//Reindex the issue
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${i.key} ${i.id}")
issueIndexingService.reIndex(i);
ImportUtils.setIndexIssues(wasIndexing);

//Add map to results array
results.add(issueMap);

}

return results

 

1 answer

1 vote
Nicolás Figueroa May 12, 2021

Hello,

This could be very helpfull in some cases, I try to use it but gives some errors 

example:

//create the list that contains all the issue keys
def issues = searchResult.issues.collect {issueManager.getIssueObject(it.id)};

Any solution? 

Bouman May 12, 2021

@Nicolás Figueroa 

I updated the code to use "searchResults.results.collect" instead of "searchResults.issues.collect". It should work for you now.

Like Nicolás Figueroa likes this
Nicolás Figueroa May 12, 2021

Oh this is great!

Thanks for your quick response @Bouman , Im always wandering how to solve this errors in Script, you just take a look in the libraries and update it?

Bouman May 12, 2021

Ya you can look at the documentation. A huge resource for me is the Atlassian community. You can find a lot of other scripts that use pieces that you might need. I treat them like Legos... plug and play. However that can get you in trouble so it is best to use both the community and documentation. Its also good to keep in mind that Cloud can be very different than Datacenter.

Like Nicolás Figueroa likes this
Nicolás Figueroa May 12, 2021

Thanks a lot for your advice,

I really explore the community but not always have the time to find the correct answers,

Have a great week!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events