Displaying Overtime On The Issue

ScriptRunner has scripted custom fields support and that allows you to create custom fields without creating your own add-on. WorklogPRO has concept of worklog attributes which allows you to associate extra information on worklogs.

For this example we will create a “Boolean” worklog attribute with name “Overtime” and employees will check this to indicate they work outside of usual work hours.

After adding “Overtime” attribute, worklog dialog of WorklogPRO will contain an additional checkbox. Assuming that employees are checking this checkbox correctly we can continue to configure ScriptRunner side of our task.

Navigate to “Manage apps/add-ons → ScriptRunner → Fields” setting and press the “Create Script Field” button and than select “Custom script Field”. As “Template” please chose “Duration (time-tracking)” so that values are properly formatted like “3 hours 30 minutes” instead of just a number. Enter following script to script field.

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; @WithPlugin("com.deniz.jira.worklog") @PluginModule AttrTypeService attrTypeService; def worklogManager = ComponentAccessor.getWorklogManager() //get all worklogs of the issue using worklog manager. Note that "issue" refers to the issue user is viewing def worklogs = worklogManager.getByIssue(issue) //get "Overtime" attribute definition def overtimeAttrType = attrTypeService.getAttrTypeWithName("Overtime"); //try to some-up overtime only if "Overtime" attribute is defined as a guard if (overtimeAttrType.isPresent()) { //find all worklogs which has overtime attribute set and sum them up worklogs.findAll { worklog -> attrTypeService.getWorklogAttribute(overtimeAttrType.get().getID(), worklog.id).isPresent() } .sum { Worklog worklog -> worklog.timeSpent } as Long }

Let me to explain a little bit what this script is doing just in case you want to modify it. It starts with usual ScriptRunner boilerplate like @WithPlugin and @PluginModule to access WorklogPRO services. “issue” is build-in value provided by ScriptRunner and refers to the issue user is viewing. We first try to access “Overtime” attribute definition using AttrTypeService of WorklogPRO. If attribute is not defined we skip all calculations. This is just guard so that we don’t get any error if someone deletes this attribute. If attribute definition is present we check all worklogs of the issue and filter the ones which has “Overtime” attribute set. After that we sum up “time spent” values of worklog. Note that this value is actually number of seconds so we cast it to Long. Since we have chosen “Duration(time-tracking” template at the beginning of field definition this number of seconds value will be properly converted to user readable format, such as “3 hours 30 minutes”.

After adding the custom field you need to specify on which Screens and for which Projects this custom field will be visible. You can do this by navigating to “Jira Administration → Issues → Custom Fields” and searching for “Overtime” using the search field. To configure which projects and screens are applicable for this field click on “Configure”. To configure a custom searcher, click on “Edit” inside the context menu as shown in the screenshot. You need to configure at least one project and screen for field to be visible. If you also want to write queries like “Overtime > 2h” for searching issues, you also need to configure searcher. Since our custom field shows “Duration” you need to chose “Duration Searcher”.

This is our result. I don’t know why Overtime field is displayed under Dates section by ScriptRunner but I’m sure it is something configurable but we will not deal with it for now.

This is our final “ScriptRunner Custom Field Configuration”.