Select Work Logs by Date Range of Linked Issues

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

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

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

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}

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.

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. 

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,890 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot