Groovy script for JIRA work logging not adding up correctly

I’m having trouble with a Groovy script for logging work in JIRA. The script is supposed to create 5 worklog entries of 1 hour each, but the total logged time shows only 1 hour instead of 5. Here’s a simplified version of what I’m trying:

def jiraService = getJiraService()
def issue = jiraService.getIssue('PROJ-123')

for (int i = 0; i < 5; i++) {
    def worklogParams = [
        startDate: new Date(),
        timeSpent: '1h',
        comment: 'Work done',
        author: issue.reporter
    ]
    
    jiraService.addWorklog(issue, worklogParams)
}

This creates 5 separate worklog entries, but the total time logged doesn’t add up correctly. I’ve tried this on both JIRA 5.1.7 and 6.1 with the latest Script Runner addon, but I get the same result.

Has anyone encountered this issue or know how to fix it? Any help would be appreciated!

hey there, i’ve seen this issue before. try changing ‘timeSpent’ to ‘timeSpentSeconds’ and use seconds instead of hours. like this:

timeSpentSeconds: 3600 // 1 hour in seconds

also, make sure ur script has the right permissions to add worklogs. sometimes that can cause weird behavior. hope this helps!

I’ve encountered this issue before when working with JIRA scripts. The problem might be related to how JIRA interprets the ‘timeSpent’ parameter. Try using ‘timeSpentSeconds’ instead and specify the time in seconds:

timeSpentSeconds: 3600 // 1 hour in seconds

Also, ensure your script has the necessary permissions to add worklogs. Sometimes permission issues can cause unexpected behavior.

If that doesn’t work, you might want to add a small delay between each worklog entry:

Thread.sleep(1000) // 1 second delay

This can help ensure each entry is properly processed. If you’re still having trouble, I’d recommend checking the JIRA logs for any error messages during script execution. They often provide valuable clues about what’s going wrong behind the scenes.

I’ve dealt with similar JIRA scripting issues before. One thing to check is how JIRA interprets the ‘timeSpent’ parameter. Try using ‘timeSpentSeconds’ instead and specify the time in seconds:

timeSpentSeconds: 3600 // 1 hour in seconds

Also, ensure your script has the necessary permissions to add worklogs. Sometimes permission issues can cause unexpected behavior.

Another thing to consider is the script execution context. JIRA might only be committing the last entry in your loop. You could try adding a small delay between each worklog entry:

Thread.sleep(1000) // 1 second delay

If these don’t work, I’d recommend checking the JIRA logs for any error messages during script execution. They often provide valuable clues about what’s going wrong behind the scenes.

Lastly, if you’re still stuck, consider using JIRA’s REST API directly. It gives you more control over the worklog entries and can help pinpoint where things are going awry.