Select Work Logs by Date Range of Linked Issues

Tobias Brunner (Admin Account) August 15, 2016

I'd like to get work logs of all linked issues of the type "Blocks" since the beginning of the current month and sum them up for storing in a scripted field. I've already found some nice similar examples, f.e. here: https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html

Currently I'm struggling how to select the work logs of a date range and sum them up. The linked example accesses the getEstimate() function which has a sum of all worklogs of an issue.

2 answers

0 votes
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 17, 2016

The WorkLogManager can help you get the individual worklog items for a particular issue. Here's a rough example; you can fill in the blanks to match your use case.

import groovy.time.TimeCategory

//Get the relevant date range- a bit kludgy, I'll admit
Calendar c = Calendar.getInstance()
c.set(Calendar.DAY_OF_MONTH, 1)
def firstDayOfMonth = c.time.clearTime()
def lastDate = c.getActualMaximum(Calendar.DATE)
def lastSecond
use (TimeCategory) {
 lastSecond = (firstDayOfMonth + lastDate) - 1.seconds
}
def dateRange = firstDayOfMonth..lastSecond

//Now actually work with JIRA
def issues = //Get the linked issues of type Blocks, which it sounds like you already know how to do
def worklogManager = ComponentAccessor.getWorklogManager()
def allTimeLoggedWithinMonth issues.collect{ issue ->
 def worklogs = worklogManager.getByIssue(issue)
 worklogs.findAll{ 
 dateRange.contains(it.startDate) //Naive assumption: If your an individual worklog spans multiple days, this may not work as expected
 }.sum{ it.timeSpent }
}.sum{it}
Tobias Brunner (Admin Account) August 19, 2016

Thanks a lot for this example, that helped a lot! On the other side, as I'm just learning Groovy and my Java skills are really beginner style, I had to write it in a "beginner" like syntax:

package com.onresolve.jira.groovy.test.scriptfields.scripts

import groovy.time.TimeCategory
import com.atlassian.jira.component.ComponentAccessor

// Get the relevant date range
Calendar c = Calendar.getInstance()
c.set(Calendar.DAY_OF_MONTH, 1)
def firstDayOfMonth = c.time.clearTime()
def lastDate = c.getActualMaximum(Calendar.DATE)
def lastSecond
use (TimeCategory) {
 lastSecond = (firstDayOfMonth + lastDate) - 1.seconds
}

def dateRange = firstDayOfMonth..lastSecond
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def worklogManager = ComponentAccessor.getWorklogManager()

def totalLoggedCurrentMonth = 0
def linkedIssues = []

// Collect all linked issues with type blocks
issueLinkManager.getInwardLinks(issue.id).each { issueLink ->
    if (issueLink.issueLinkType.name == "Blocks") { 
        linkedIssues.add issueLink.sourceObject
    }
}

// Loop through all relevant issues
linkedIssues.each { issue ->
    def worklogs = worklogManager.getByIssue(issue)
    // Loop through all worklogs
    worklogs.each { worklog ->
        // Check if the start date is in the range
        if (dateRange.contains(worklog.getStartDate().clearTime())) {
            // Sum up relevant worklogs
            totalLoggedCurrentMonth += worklog.getTimeSpent()
        }
    }
}

return totalLoggedCurrentMonth

This script does exactly what I want. But the editor shows a few errors. Maybe you have some hints to make this Groovy script more Groovy like, so I can learn how to do it better.

A August 19, 2016

I'll take coding like a beginner over coding like a quitter any day. smile Nothing strikes me as immediately off about your code. If it makes sense to you and your team, that's the most important thing. Only thing I would change at first glance is using the .sum() method instead of .each to save making holder variables, but that is hardly important, particularly if you and yours find your version clearer.

As for the errors, sometimes, you don't need to sweat the errors displayed in the editor. We'd like to make them more robust, but running a mini-IDE in the browser is no mean feat. 

0 votes
Viacheslav Starovoytov August 15, 2016

Hi Tobias!

nFeed plugin will achieve this issue with ease, if your JIRA own database has SQL type. But it is solution without ScripRunner and reruires additional costs.

With regards

Vyacheslav

Tobias Brunner (Admin Account) August 15, 2016

Thanks, but I'm looking for a solution with ScriptRunner because I'm sure it's also easy to achieve with some Groovy script.

Suggest an answer

Log in or Sign up to answer