Timetracker API for Scriptrunner 

Banner of Scriptrunner integration in Timetracker article

Scriptrunner is one of the most successful automation tools for Jira. It lets you write groovy scripts for many automation jobs, transitions, scripted fields, etc.

Now Scriptrunner is able to script other plugins with the help of two annotations if they have the necessary services and API-s.

We implemented these services to give you the possibility to use Timetracker with your scripted/automated processes. Below we gathered some examples to show you how Scriptrunner and Timetracker can work together to accomplish what you need, for example:

  • Add worklog with validation: how to log time using Timetracker API so it’s validations will be applied to the new worklogs

  • Worklog category summary on Issue screen: How to implement a scripted field that summarizes worklogs on Issue screen based on the selected attribute

  • Worklog cost summary on Issue screen: How to implement a scripted field that summarizes cost on Issue screen for that Issue

Examples 

Add worklog with validation

Timetracker has numerous validation rules, permissions and additional attributes that apply when we create a worklog with it. Below is an example of how they can be used when we log time in a transition with a post-function.

This is a script workflow post-function, the code is:

import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import org.everit.jira.timetracker.service.WorklogService;
import java.util.ArrayList;
import java.time.Instant;


@WithPlugin("org.everit.jira.timetracker.plugin")

@PluginModule
WorklogService worklogService;

List worklogAttributes = new ArrayList();
worklogAttributes.add("billable");

// Issue key, Worklog duration, Worklog start date, Worklog Comment, Worklog attributes list (names)
// The worklog will be created by the logged in user
def worklogCreateFailed = worklogService.createWorklog(issue.getKey(),"1h 30m", Instant.now(), "PostFunction Worklog", worklogAttributes);
if(!worklogCreateFailed.empty){
  log.error("Worklog failed: " + worklogCreateFailed);    
}
 

Worklog attribute summary on Issue screen

Timetracker allows you to add additional attributes to your worklogs. You can use this categorization for example if you have an external and internal project as well, and you want to separate the “Billable” and “Not Billable” worklogs. Sometimes it can be useful to see the total time for each category on an Issue so below is an example of how to do it.

Create a custom scripted field and paste the following script:

import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import org.everit.jira.timetracker.service.WorklogService;
import java.util.ArrayList;
import java.time.Instant;
import java.time.Period;


@WithPlugin("org.everit.jira.timetracker.plugin")

@PluginModule
WorklogService worklogService;

List<String> worklogAttributes = new ArrayList<String>();
worklogAttributes.add("billable");
// To add the worklogs without atrributes to the summary use the "#NO_CATEGORY" constant

// Check the last 7 days billable worklogs on the issue
Instant startDate = Instant.now().minus(Period.ofDays(7));
Instant endDate = Instant.now();

//Empty users and groups list measn the summary will include all users
List<String> users = new ArrayList<String>();
List<String> groups = new ArrayList<String>();

return worklogService.categorySummary(issue.getKey(), worklogAttributes, startDate ,endDate, users, groups); 

In this scripted field, the categorySummary() function returns long type. Based on the parameters, the logged time is summarized and it will return in seconds.
Selecting the Duration (time-tracking) custom script field template for this field will ensure a better time unit display.

Screenshot of how the category and cost summary fields appear in a Jira Issue

Worklog cost summary on Issue screen

The costs involved are at least as important in project management as the logged times. To help you plan your future projects even better and analyze the cost of previous projects or features, Timetracker provides cost management functionality. You can add cost rates to each employee so besides the total time needed to finish a project or feature, you can also check how much it cost.

To calculate and display the cost of an issue for everyone who worked on it, create a custom scripted field and paste the following script:

import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import org.everit.jira.timetracker.service.WorklogService;
import java.util.ArrayList;
import java.time.Instant;
import java.time.Period;


@WithPlugin("org.everit.jira.timetracker.plugin")

@PluginModule
WorklogService worklogService;

// Check the last 7 days worklogs on the issue
Instant startDate = Instant.now().minus(Period.ofDays(7));
Instant endDate = Instant.now();

//Empty users and groups list measn the summary will include all users
List<String> users = new ArrayList<String>();
List<String> groups = new ArrayList<String>();

return worklogService.costSummary(issue.getKey(), startDate ,endDate, users, groups);

 

You can find and try our Timetracker app for free on the Atlassian Marketplace.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Icon of an email
Icon of a telephone

info@everit.biz

+36 20 248 7670

Logos of the customers of Everit