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

Test User September 18, 2019

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

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 19, 2019

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

Test User September 23, 2019

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}")
}
}

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

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2019

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

Test User September 23, 2019

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

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2019

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

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 25, 2019

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