Update Time Spent in Issue Worklogs

Overview

Update the time spent in all the worklogs of an issue.

Example

As an admin, I want to fix the worklogs of an issue to display the correct time spent. Using this script, I can bulk update all of the worklogs of an issue at once, saving time and reducing the risk of error.

Good to Know

  • You must execute the script as "ScriptRunner Add-On User" to override screen security.
  • You can fine-tune the worklogs to update applying extra query parameters, or extra conditions based on attributes, to the worklog search request.

Requirements

Jira Jira

// Specify the issue to set the worklog on
final issueKey = "TEST-1"

// Specify the time to be set on each worklog (format: 24m for 24 minutes or 2h for 2 hours)
final timeSpentValue = "1h"

// Execute the rest call to get all worklogs of an issue
def issueWorklogs = get("/rest/api/2/issue/${issueKey}/worklog")
    .header('Content-Type', 'application/json')
    .asObject(Map).body.worklogs
logger.info("The worklogs of issue ${issueKey}: ${issueWorklogs}")

// Iterate over each worklog
def statusByWorklogId = issueWorklogs.collectEntries { worklog ->
    // Execute the rest call to update the worklog on the issue
    def result = put("/rest/api/2/issue/${issueKey}/worklog/${worklog.id}")
        .header('Content-Type', 'application/json')
        // Override screen security if the field is not on screen (this means the script must be run as the "ScriptRunner Add-On User")
        .queryString("overrideScreenSecurity", true)
        .body([
            // Specify the time to be set updated on the worklog
            "timeSpent": timeSpentValue
        ])
        .asObject(Map)

    // Log out the issues transitioned or which failed to be transitioned
    if (result.status == 200) {
        logger.info("Update of worklog ${worklog.id} performed successfully")
    } else {
        logger.warn("Failed to update the worklog ${worklog.id}")
    }

    // Collect the success status by worklog key to show them as part of the script return value
    [(worklog.id): (result.status == 200)]
}

"Status by worklog id (updated?): ${statusByWorklogId}"

Discovered an issue? Report it here

Suggested for you