It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Select Work Logs by Date Range of Linked Issues

Tobias Brunner Aug 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

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 Aug 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.

0 votes
Jonny Carter Aug 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 Aug 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.

Adaptavist Aug 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. 

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

6,266 views 22 30
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you