Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Create a Tempo Worklog

Created 1 year ago, Updated 30 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.19)
compatibility bullet
ScriptRunner For Jira (7.10.0)
Language |
groovy
import com.adaptavist.hapi.jira.issues.Issues
import com.adaptavist.hapi.jira.users.Users
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.common.TempoDateTimeFormatter
import com.tempoplugin.core.datetime.api.TempoDate
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.worklog.v4.rest.InputWorklogsFactory
import com.tempoplugin.worklog.v4.rest.TimesheetWorklogBean
import com.tempoplugin.worklog.v4.rest.WorkAttributeValueInputBean
import com.tempoplugin.worklog.v4.services.WorklogService

import java.util.concurrent.TimeUnit

@WithPlugin('is.origo.jira.tempo-plugin')

@PluginModule
WorkAttributeService workAttributeService

@PluginModule
WorklogService worklogService

@PluginModule
InputWorklogsFactory inputWorklogsFactory

def issue = Issues.getByKey('SR-1')

def currentUser = Users.loggedInUser
def startDate = TempoDateTimeFormatter.formatTempoDate(TempoDate.now())

def geAttributeId = { String name ->
    def workAttribute = workAttributeService.workAttributes.get().find { it.name == name }
    if (!workAttribute) {
        throw new IllegalArgumentException("Could not find attribute with name '${name}'")
    }
    workAttribute.key
}

// Add all fields needed to create a new worklog
def timesheetWorklogBean = new TimesheetWorklogBean.Builder()
    .issueIdOrKey(issue.key)
    .comment('my worklog')
    .startDate(startDate)
    .workerKey(currentUser.key)
    .timeSpentSeconds(TimeUnit.HOURS.toSeconds(5)) // log 5 hours
    .remainingEstimate(TimeUnit.HOURS.toSeconds(3)) // 3 hours remaining
    .attributes([
        // add as many work attributes as you need
        (geAttributeId('A text attribute')): new WorkAttributeValueInputBean('Set by automation'),
        (geAttributeId('Static Select'))   : new WorkAttributeValueInputBean('Bananas'),
    ])
    .build()

def inputWorklogs = inputWorklogsFactory.buildForCreate(timesheetWorklogBean)
worklogService.createTempoWorklogs(inputWorklogs)
Having an issue with this script?
Report it here