Snippets

Adaptavist Script Listener Get Issue Worklog And Transition Issue Based On Time Logged.

Created by Kristian Walker
/*
* This example script listner script must bve configured to fire on the 'Worklog Created' Event and shows how when work is logged that you can transition an issue to 'In Progress' and then to 'Done' when the remaining estimate reaches 0. 
* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/

// log out the worklog that was added to the issue incase it is needed for debugging purposes
logger.info("the worklog is: "+ worklog)

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

// get the remaining estimate value of the issue.
def remainingEstimateValue = issue.fields.timetracking.remainingEstimateSeconds
logger.info("Remaining Estimate Value Is:" + remainingEstimateValue)

//  If the remaining estimate value is not 0 then transition the issue to the In Progress status
if(remainingEstimateValue != 0){
    logger.info("Issu has no time left remaining so transition to Done")

// specify the ID for the In Progress transition of the workflow below
    def inProgressTransitonID = '<InProgressTransitionIDHere>'

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

// Check if the issue was transitioned correctly
// Log out the issues updated or which failed to update
    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 the remaining estimate value is 0 then transition the issue to the closed status
if (remainingEstimateValue == 0){
    logger.info("Issue still has time on the estimate so transition to In Progress")

// specify the ID for the Done transition of the workflow below
    def doneTransitonID = '<DoneTransitionIDHere>'

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

// Check if the issue was transitioned correctly
// Log out the issues updated or which failed to update
    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}")
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.