ScriptRunner how to change status of an Issue

Hello Community,

I am trying to figure out how I can change the Status of an Issue.

I would like to add a functionality such that when a certain event occurs, the status of an issue is automatically changed from "Open" to "Done".

I tried accomplishing this with the following code but had no success:

//Checks if the issue is an Epic
if(issue.getIssueType().name=="Epic"){

MutableIssue epic = issue
epic.setStatusId("2"); //Read that 2 meant status = "Done"

}

The above code runs, as I had previously put a log() and it displayed relevant information regarding the issue such as the issue Key. But for somereason when I go check my Epic the status continues to be the same it was before I ran my code. 

Could someone provide me with guidance regarding this issue.

I appreciate all the help!

-Roberto

3 answers

2 accepted

1 vote

You can't just "set" a status - that only works when you're importing a new issue.  You need to transition the status through the workflow.  Your code will need to trigger the transition between "open" and "done".

Is this transition to be done on the issue you are looking at?  e.g. If an issue is in "new" and moves to "open", then some times you want it to go to "done" immediately.  If so, ScriptRunner has a "fast track transition" function whcih can do that.  If it's more complex (like it's an event on a different issue), that might be a bit more involved, but can still be done.

Hi @Nic Brough [Adaptavist],

Thank you for your your quick response. I looked into Fast Track listener as an option for this but i do not think it can fully achieve the functionality I desire.

Currently in my custom listener script I am listening for issues updated, from there I check if the issue is a story. If it is then I get its parent (the Epic) and all the issue links associated with it. From there I loop through them to figure out if they are in the "Done" status, if they all are then I want to change The status of my Epic to "Done". 

I didn't believe that the Fast Track listener was able to accommodate this. Therefore I wrote the current script. I am missing the last portion which is updating the Epic to "Done". 

I understand now that It cant be simply "set". Could you elaborate more on this "transition". 

-Roberto

Hi @Jonny Carter and @Nic Brough [Adaptavist],

I just developed this transition code, I was hoping you could take a look at it and confirm if it works or not.

I ran it and recieved no error messages in Jira, but it still did not change the status of my Epic from "In progress" to "Done" even though all the above conditions where met.

 


Issue epic = issueManager.getIssueObject("EP-12");

// Get the details of the user executing the workflow transition for the issue change history
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);

// Update the issue by validating the transition can be completed first before executing it.
workflowTransitionUtil.setIssue(epic);
workflowTransitionUtil.setUserkey("genericuser2103");
workflowTransitionUtil.setAction(1); // not 100% sure how to get the ID of my transitions but when I put 1 it runs, any other number so far gives me errors.
workflowTransitionUtil.validate();
workflowTransitionUtil.progress();

 The above code runs but it doesnt change any status.

I was wondering if you could help me figure this out and if the code is correct but my transition number isnt, could you provide me with insight as to how I can figure out what the number should be to move an issue into the "Done" stage. I am an admin in my system, I think that might help when it comes to looking.

Thank you for your time and expertise!!

-Roberto

Hey Roberto,

Here is an example of properly transitioning an issue in a script listener:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()
def actionId = 21 // change this to the step that you want the issues to be transitioned to
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()
log.debug("The issue type is: " + issue.getIssueType().name)

if (issue.getIssueType().name == "Task") {

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
if (transitionResult.isValid())
{ log.debug("Transitioned issue $issue through action $actionId") }
else
{ log.debug("Transition result is not valid") }
}
else {
log.debug("The transitionValidation is not valid")
}
}

Take a look at that and let me know if you need help getting it to work for your specific case. I've tested this myself and it does work as intended.

Hope that helps!

-Aidan 

Hey @Aidan Derossett [Adaptavist],

I appreciate you taking the time to write such an example for me.

It works perfectly! Thank you so much for that.

@Jonny Carter@Nic Brough [Adaptavist] Thank you both for the guidance on this issue. I really appreciate it.

-Roberto

Hey Roberto!

Instead of trying to create a custom listener, ScriptRunner provides a built-in listener called Fast-track transition an issue. It should be available as a listener option (when creating a new listener) to choose from in your instance. Try using that built-in function instead and see if that works any better!

Let us know how it goes!

HI @Jonny Carter

thank you for your your quick response. I looked into Fast Track listener as an option for this but i do not think it can fully achieve the functionality I desire.

currently in my custom listener script I am listening for issues updated, from there I check if the issue is a story. If it is then I get its parent (the Epic) and all the issue links associated with it. From there I loop through them to figure out if they are in the "Done" status, if they all are then I want to change The status of my Epic to "Done". 

I didn't believe that the Fast Track listener was able to accommodate this. Therefore I wrote the current script. I am missing the last portion which is updating the Epic to "Done". 

I understand now that It cant be simply "set". I have heard that you can use transitions to move an issue from a certain status to another one. Would you be able to provide me with some guidance regarding this?

-Roberto

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

77 views 0 5
Read article

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