It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Plesae help me with the ScriptRunner code to auto transition the issue when the work is logged

We are trying to achieve the below. 

1. When any user logs the work to an issue, can the issue gets transitioned automatically and moved to "In Progress" status. and even when the remaining hours becomes zero, can it move automatically to "Closed" status?

If that is possible, Can we achieve this even if we use tempo timesheets plug-in for time tracking

I am looking for solutions from Cloud and Server versions perspective as well.

I could see the possibility through other plug-ins but we are more interested in ScriptRunner.

I heard from the support team that this can be achieved through listeners. Could someone please help me with the code and step by step instructions to implement this in both cloud and server vesrions. I am sorry, but I might need a details solution as I am a non-technical person.

 

1 answer

Hi Anna,

Thank you for your question.

I can confirm that to achieve your requirement

In order to achieve your requirement with ScriptRunner for Jira Cloud, you will need to create a Script Listener which is configured to execute on the Worklog Created event.

The script for the listener will need to get the worklog that was created on the issue and will need to use this to extract the remaining estimate field value off of the issue that the worklog was created on.

If the remaining estimate was 0 then the script would need to make a Rest API in order to transition the issue into the Done status and if the remaining estimate was not 0 then it would need to make a rest API call to transition the issue to the In Progress status.

I have created a sample script listener script located here which shows how you can get the worklog logged on an issue and can transition an issue based on the Remaining Estimate value.

You will be able to take this script and will be able to modify this to achieve your needs inside of ScriptRunner for Jira Cloud.

Please note that when the script transitions the issue, that it will only be able to transition the issue if a valid transition, exists in the workflow to transition to that status.

I am unable to advise on how to achieve this requirement in Jira Server and would ask you to raise a request with the ScriptRunner for Jira Server team located here who can advise how to achieve this requirement in Jira Server.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

I have copied the below code to the listerner and saved: And created an issue and legged the work using Tempo. But nothing happend to the status. The issue is still in Open status only. It is not moving to In Progress or Closed status. 

Am I missing something? Once we add below code, how to execute the listner? I am getting static type error as well at "def remainingEstimateValue = issue.fields.timetracking.remainingEstimateSeconds"Script Listner Error.PNG

 

*******************************************

logger.info("the worklog is: "+ worklog)

def issue = get('/rest/api/2/issue/' + worklog.issueId)
.header('Content-Type', 'application/json')
.asObject(Map)
.body
logger.info("Issue object is : " + issue.fields)

def remainingEstimateValue = issue.fields.timetracking.remainingEstimateSeconds
logger.info("Remaining Estimate Value Is:" + remainingEstimateValue)


if(remainingEstimateValue != 0){
logger.info("Issu has no time left remaining so transition to Done")


def inProgressTransitonID = 11


def inProgressTransition = post("/rest/api/2/issue/${issue.key}/transitions")
.header("Content-Type", "application/json")
.body([transition: [id: inProgressTransitonID]])
.asObject(Map)

if (inProgressTransition.status == 204) {
logger.info("The ${issue.key} issue was transitioned by the listiner.")
} else {
logger.warn("The listiner failed to transition the issue. ${inProgressTransition.status}: ${inProgressTransition.body}")
}

}


if (remainingEstimateValue == 0){
logger.info("Issue still has time on the estimate so transition to In Progress")


def doneTransitonID = 21

def doneTransition = post("/rest/api/2/issue/${issue.key}/transitions")
.header("Content-Type", "application/json")
.body([transition: [id: doneTransitonID]])
.asObject(Map)

if (doneTransition.status == 204) {
logger.info("The ${issue.key} issue was transitioned by the listiner.")
} else {
logger.warn("The listiner failed to transition the issue. ${doneTransition.status}: ${doneTransition.body}")
}
}

*******************************************

Hi Anna,

Thank you for your response.

I can confirm that the static type checking error is just a warning, where the compiler cannot fully understand the code but can be ignored and you can see more details of how static type checking works in the documentation page located here.

Also, I notice that you have configured your listener to execute on the worklog updated event when it needs to be configured to fire on the worklog created event as the worklog created event is what is fired when you add a new worklog. 

Can you please update the configuration for your listener to execute on the worklog created event and to advise if this resolves your issue.

Finally, Can I please ask you to review the execution history from when the script was run as the logging statements output will be shown here and will help to debug what happened when the script was run.

Regards,

Kristian

Hi Kristian,

 

It is working as expected. Thanks a lot. But it is sending below notification to me everytime. Is there a way to control this. That too it is sending during done status only.

 

Script Listner notification.PNG

Hi Anna,

Thank you for confirming that the script is working as expected.

I can confirm that the notificaitons made by an issue being updated are triggered by Jira itself and not by ScriptRunner for Jira Cloud

This means that to avoid  emails being sent that you will need to update the notification sheme for the project to change who gets notified when an issue is updated as described in the Atlassian documentation page here.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

Hi Anna,

Could I please ask you to accept the answer above in order to show other users searching for a similar question that the script and answer above works as expected.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

308 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you