How to transit issues via Groovy script (ScriptRunner)?

Ivan Shkabara December 4, 2019

Hello!
Please tell me how to change the status for tasks correctly?

Given:
issueKey = "TEST-1"
currentStatus = "In Progress"
nextStatus = "Done"

So, the transition "InProgress -> Done" has an id = 900. Community documentation recommends using {{IssueService}}, which is what I am trying to do...


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue

final String ISSUE_KEY = "TEST-1"

int ACTION_ID = 900

def issueService = ComponentAccessor.getIssueService()

// context of the currentUser
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// getting issue via IssueService
MutableIssue issue = null

def getIssueResult = issueService.getIssue( user, ISSUE_KEY )
if( getIssueResult.isValid() == true && getIssueResult.getIssue() != null )
{
issue = getIssueResult.getIssue()
}
else
{
return "Issue not found!"
}

def params = issueService.newIssueInputParameters()

def transitionValidationResult = issueService.validateTransition( user, issue?.getId(), ACTION_ID, params )


if( transitionValidationResult.isValid() == true )
{
def issueResult = issueService.transition( user, transitionValidationResult )

if( issueResult.isValid() == true )
{
log.debug "valid. Issue is = ${issueResult.getIssue()}"
return issueResult.getIssue()
}
else
{
log?.warn "error"
return issueResult.getErrorCollection()?.getErrorMessages()
}
}
else
{
return transitionValidationResult.getErrorCollection()?.getErrorMessages()
}
"${transitionValidationResult.isValid()}"

 

and, my result is:
[It seems that you have tried to perform a workflow operation (Resolve) that is not valid for the current state of this issue (TEST-1). The likely cause is that somebody has changed the issue recently, please look at the issue history for details.]

But, an issue TEST-1 haves the status "In Progress" and, this issue have the direct step/link to the "Resolved" status! Btw, i can change the status "In Progress" to "Resolved" manually, via IssueService - i got error.

So, how to transit an issues as correctly?

3 answers

1 accepted

0 votes
Answer accepted
Ivan Shkabara February 8, 2021

I could not move the task to the next status because it was an ISSUE_CREATED event. That is, at the time of creation of the task - it is not possible to move the task to the next status (but you can do it during the event ISSUE_UPDATED).

def srv = ComponentAccessor.issueService

class ThreadService extends Thread
{
private def srv = ComponentAccessor.issueService
private def threadUser
private Issue issue


public ThreadService(final ApplicationUser user, final Issue issue)
{
super()

threadUser = user
this.issue = issue
}


public void run( )
{
//Thread.sleep(1000); // delay for indexing
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(threadUser)
// there we can do transition
}
};

if (event.eventTypeId == ISSUE_CREATED)
{
Thread t = new ThreadService(event.user, event.issue)
t.start() // !!! no join for CREATED event !!!
}
else if(event.eventTypeId == ISSUE_UPDATED)
{
//srv.transition( ... )
}
0 votes
Nitinraj May 15, 2020

IssueService issueService = ComponentAccessor.getIssueService();
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class);
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
TransitionValidationResult validationResult = issueService.validateTransition(loggedInUserName, issue.getId(),
actionId, issueInputParameters);
if (validationResult.isValid()) {
issueService.transition(loggedInUserName, validationResult);
issue.setStatusId(statusID);
ComponentAccessor.getIssueManager().updateIssue(loggedInUserName, issue, EventDispatchOption.ISSUE_UPDATED,
false);
try {
issueIndexingService.reIndex(validationResult.getIssue());
} catch (IndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {
Collection<String> errors = validationResult.getErrorCollection().getErrorMessages();
for (String error : errors) {
// Logger.getLogger(error);

}
}

 

 

 

Try This, it will work!

Nitinraj May 15, 2020

And to get Issue object try below:

def issue = issueManager.getIssueObject("DW-903")

 

Not, 

final String ISSUE_KEY = "TEST-1"
0 votes
Will C
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.
December 5, 2019

Hi there,

 

I think your script looks fine, can I check that the ID 900 is the ID of the next status and not the ID of the transition itself?

I think from memory you need to provide the ID of the target status and not the transition itself.

Ivan Shkabara December 6, 2019

Hi,

it's actionId

Suggest an answer

Log in or Sign up to answer