Create a Tempo Worklog

Overview

Create an auto-generated Tempo worklog using the Tempo Java API.

Example

As a Project Manager, I want to add a worklog automatically when a user transitions an issue out of any state. For example, in our workflow, all issues raised by customers go through a Triage state. When the triage is complete, I want to automatically log the amount of time it took us to triage the issue.

Good to Know

  • This script requires Tempo Timesheets
  • Use this script as a post function associated with the step you defined.

Requirements

Jira Jira (8.0 - 8.6)

    
import com.atlassian.jira.component.ComponentAccessor 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.worklog.v4.rest.InputWorklogsFactory import com.tempoplugin.worklog.v4.rest.TimesheetWorklogBean import com.tempoplugin.worklog.v4.services.WorklogService import org.apache.log4j.Level import org.apache.log4j.Logger import java.sql.Timestamp import java.time.Instant import java.time.temporal.ChronoUnit @WithPlugin("is.origo.jira.tempo-plugin") @PluginModule WorklogService worklogService @PluginModule InputWorklogsFactory inputWorklogsFactory // Set log level def log = Logger.getLogger(getClass()) log.setLevel(Level.ERROR) // Status to be counted final statusName = '' def changeHistoryManager = ComponentAccessor.changeHistoryManager // Calculate the time we entered this state def changeHistoryItems = changeHistoryManager.getAllChangeItems(issue).reverse() def timeLastStatus = changeHistoryItems.find { it.field == "status" && it.toValues.values().contains(statusName) }?.created as Timestamp final chronoUnit = ChronoUnit.SECONDS def timeToLog = chronoUnit.between(timeLastStatus.toInstant(), Instant.now()) // Remaining estimate is calculated if and only if there is original estimate and it is greater than time to log def remaining = issue.estimate && issue.estimate > timeToLog ? (issue.estimate - timeToLog) : 0 def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser def startDate = TempoDateTimeFormatter.formatTempoDate(TempoDate.now()) // Add all fields needed to create a new worklog def timesheetWorklogBean = new TimesheetWorklogBean.Builder() .issueIdOrKey(issue.key) .comment('Auto-created worklog') .startDate(startDate) .workerKey(currentUser.key) .timeSpentSeconds(timeToLog) .remainingEstimate(remaining) .build() def inputWorklogs = inputWorklogsFactory.buildForCreate(timesheetWorklogBean) worklogService.createTempoWorklogs(inputWorklogs)
Discovered an issue? Report it here

Suggested for you