Skip to main content
Book your demo
Example scripts
To homepage
Jira
Cloud icon
Cloud

Update Time Spent in Work Item Worklogs

Created 2 year(s) ago, Updated 30 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
// Specify the work item to set the worklog on
final workItemKey = "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 a work item
def workItemWorklogs = get("/rest/api/2/issue/${workItemKey}/worklog")
    .header('Content-Type', 'application/json')
    .asObject(Map).body.worklogs as List<Map>
logger.info("The worklogs of issue ${workItemKey}: ${workItemWorklogs}")

// Iterate over each worklog
def statusByWorklogId = workItemWorklogs.collectEntries { worklog ->
    // Execute the rest call to update the worklog on the work item
    def result = put("/rest/api/2/issue/${workItemKey}/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 work items 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}"
Having an issue with this script?
Report it here