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

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

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.

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

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

It prints null. There is no stack trace

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

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"

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

scjira.png

 

Here is the screenshot

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.

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

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Fadoua M. Boualem
Published yesterday in Trello

Using Trello to manage events

As a Jira power user, I was at first doubtful that Trello could benefit my workflow. Jira already uses boards (ones you can customize!), so why would I even need to use Trello?! In this post you will...

385 views 5 5
Read article

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