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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'll take coding like a beginner over coding like a quitter any day. 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, but I'm looking for a solution with ScriptRunner because I'm sure it's also easy to achieve with some Groovy script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.