Automatically Logging Work on Issue Transition

WorklogPRO has “Custom Log Work Field” which can be added to issue transition screens so that users can specify time spent and necessary worklog attributes when transitioning issue between different statuses.

Some times our users want more automation. For example, when a user transitions and issue from “In Progress” status to any other status, they want work to be automatically logged. You can use ScriptRunner’s “Scripted Workflow Post Function” and WorklogPRO Java API to archive this task. Following scripts shows an example of adding worklog to issue as a part of workflow transition and it also shows how to associate a custom attribute to newly created worklog.

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.worklog.Worklog import com.onresolve.scriptrunner.runner.customisers.PluginModule import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.deniz.jira.worklog.services.attr.AttrTypeService; import com.deniz.jira.worklog.data.attr.*; import com.deniz.jira.worklog.action.*; import com.atlassian.jira.bc.*; import com.atlassian.jira.util.*; import java.time.*; import com.atlassian.jira.util.*; @WithPlugin("com.deniz.jira.worklog") @PluginModule AttrTypeService attrTypeService; @PluginModule CreateWorklogOnIssue createWorklogOnIssueHelper; //get a reference to "Invoice Number" worklog attribute. We will also add invoice number to worklog def invoiceNumberAttrType = attrTypeService.getAttrTypeWithName("Invoice Number").get(); def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() def changeHistoryManager = ComponentAccessor.getChangeHistoryManager() def durationUtil = ComponentAccessor.getComponent(JiraDurationUtils.class) // calculate the time we entered this state def changeHistoryItems = changeHistoryManager.getAllChangeItems(issue).reverse() def timeLastStartProgress = changeHistoryItems.find { it.field == "status" && it.toValues.values().contains("In Progress") }.created def timeInProgress = new Date().time - timeLastStartProgress.time //this is in millis def timeToLog = Math.round( (double) timeInProgress.longValue() / 1000L) //round to seconds def formattedTimeToLog = durationUtil.getShortFormattedDuration(timeToLog); //conver to format "1h 30m" def serviceContext = new JiraServiceContextImpl(currentUser, new SimpleErrorCollection()); def newWorklog = createWorklogOnIssueHelper.logWork(serviceContext, serviceContext, issue, null, formattedTimeToLog, ZonedDateTime.now(), "Automatically logged by ScriptRunner due to transition from 'In Progress'", null, null, formattedTimeToLog); //create a new worklog attribute value AttrWorkLogImp attrWorkLogImp = new AttrWorkLogImp(); //set worklog id, issue id and project id attrWorkLogImp.setProjectId(issue.getProjectId()); attrWorkLogImp.setIssueId(issue.getId()); attrWorkLogImp.setWorkLogId(newWorklog.getId()); //set type of the attribute and it's value attrWorkLogImp.setAttrTypeId(invoiceNumberAttrType.getID()); //we are setting a predefined invoice number here. //You will most probably need a custom logic to determine it. attrWorkLogImp.setAttrValue(12345); //save new attribute value for worklog attrTypeService.addWorkLogAttribute(attrWorkLogImp);