Groovy Script to sum all work logged (time spent) for issues linked to an issue with specific link type and issue type

Anere Goodness November 10, 2015

Hi All,

My JIRA instance have issues linked to other issues with a specific link type, e.g (relates to) and specific issue type, e.g (bug) I'm trying to sum all the work logged in all bug issues with relates to link . Example. Ticket ABC-1 is linked to ABC-2(release), ABC-3(bug), ABC-4 (bug), ABC-5(bug) and BCD-1 (new) (in another project) i'm trying to sum total work log in all these tickets and store it in a scripted field eventually for this project. I found out that using groovy script is the best way to do it. I have done some research and I have written this code from script console to try out but i'm stuck.  ABC-2 has work logged as null, but the rest of the issues has work logged. I cannot seem to continue checking the rest of the issues apart from the first one from the script and I'm stuck Here is the script. Please help

 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueObject("ABC-1")
def totalTimeSpent = 0

issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
		def linkedIssue = issueLink.destinationObject
if(issueLink.issueLinkType.name == "Relates" || linkedIssue.getIssueType() == "Bug"){
  def linkedIssueTimeSpent = linkedIssue.timeSpent        
	if(linkedIssueTimeSpent) {
	totalTimeSpent += linkedIssueTimeSpent
  }
}
    }
if(totalTimeSpent > 0) {
    totalTimeSpent = totalTimeSpent  / 60.0d / 60.0d / 8.0d 
    return (double)totalTimeSpent
}
else{
    null
}

3 answers

1 accepted

1 vote
Answer accepted
JamieA
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.
November 11, 2015

This is very similar to this example: https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html - did you check that out?

Also it would help readability if the code was formatted properly.

0 votes
Anere Goodness November 15, 2015

scjira.png

 

Here is the screenshot

JamieA
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.
November 16, 2015

Relates is tricky because the destinationObject might be the current issue. Try adding the relates link the other way as well to see if that's the problem.

Anere Goodness November 23, 2015

Hi Jamie, You are right. Relates is a bit tricky with destinationObject. It worked for me. Sorry for saying this a bit late. Thanks for your help...

0 votes
Anere Goodness November 11, 2015

Hi Jamie,

Here is my original code

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueManager = ComponentAccessor.getIssueManager()
MutableIssue issue = issueManager.getIssueObject("ABC-1")
def totalTimeSpent = 0

issueLinkManager.getOutwardLinks(issue.id).each { issueLink ->
    def linkedIssue = issueLink.destinationObject
    //if(linkedIssue.getIssueType() == "Bug"){
    if (issueLink.issueLinkType.name == "Relates" || linkedIssue.getIssueType() == "Bug") {
        def linkedIssueTimeSpent = linkedIssue.timeSpent
        if (linkedIssueTimeSpent) {
            totalTimeSpent += linkedIssueTimeSpent
        }
    }
}
if (totalTimeSpent > 0) {
    totalTimeSpent += issue.timeSpent
    //totalTimeSpent = totalTimeSpent  / 60.0d / 60.0d / 8.0d //Seconds to Hours
    return (double) totalTimeSpent
} else {
    null
}

I've seen that example and it didn't work for me... Please help to point where I missed out something.

it prints "null"... and there is no stack trace

JamieA
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.
November 11, 2015

I fixed the formatting. Can you post a stack trace or something, or what the actual result is compared to the expected.

Anere Goodness November 11, 2015

It prints null. There is no stack trace

JamieA
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.
November 12, 2015

linkedIssue.getIssueType() returns an issue type not a string. So you would want: linkedIssue.issueType.name == "Bug" there may be other problems too.

Anere Goodness November 12, 2015

Hi Jamie, This is how it looks like. issueLinkManager.getOutwardLinks(issue.id).each {issueLink -> def linkedIssue = issueLink.destinationObject if(issueLink.issueLinkType.name == "Relates" || linkedIssue.issueType.name == "Bug"){ def linkedIssueTimeSpent = linkedIssue.timeSpent if(linkedIssueTimeSpent) { totalTimeSpent += linkedIssueTimeSpent } } } if(totalTimeSpent > 0) { totalTimeSpent += issue.timeSpent //totalTimeSpent = totalTimeSpent / 60.0d / 60.0d / 8.0d //Seconds to Hours return (double)totalTimeSpent } else{ null } It returns "null"

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 12, 2015

can you attach a screenshot of the script field config, and one where you see the "null"?

Suggest an answer

Log in or Sign up to answer