ScriptRunner: The sum of estimated time of all linked issues to this task.

zaharovvv_suek_ru
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.
May 31, 2018

Hello, Guys!
I want to create two custom fields:

  • the sum of estimated time of all linked issues to this task.
  • the sum of logged time of all linked issues to this task.

For the above two purposes I use the following link:

https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html

However, the time is not calculated.
My link name is "Composition"(I've attached an image "Link type - composition") like in the example:

https://scriptrunner.adaptavist.com/latest/jira/recipes/scriptfields/workRemainingInLinkedIssues.html

However, there is no calculated time(attached image of field "").

Could you clarify what I am doing wrong? 
We want to buy ScriptRunner, but we would like to learn how to use it before we buy it.
Thanks in advance!

In addition, I've attached my code.

Link type is:

Link type - composition.png

No calculated time in field "Sum logged time of linked issues":No time for calculated fields.png

My code is:

package com.onresolve.jira.groovy.test.scriptfields.scripts

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def totalRemaining = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Composition") {
def linkedIssue = issueLink.destinationObject
totalRemaining += linkedIssue.getEstimate() ?: 0
}
}

// add the remaining work for this issue if there are children, otherwise it's just the same as the remaining estimate,
// so we won't display it,
if (totalRemaining) {
totalRemaining += issue.getEstimate() ?: 0
}

return totalRemaining as Long ?: 0L

3 answers

2 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
zaharovvv_suek_ru
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.
June 4, 2018

In case of somebody need to calculate the sum of estimated time of all linked issues to the task.

package com.onresolve.jira.groovy.test.scriptfields.scripts
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogManager
import com.atlassian.jira.bc.issue.worklog.TimeTrackingConfiguration

def worklogManager = ComponentAccessor.getComponent(WorklogManager)
def workLogs = worklogManager.getByIssue(issue)
def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def totalLoggedWork = 0
//totalLoggedWork = totalLoggedWork + issue.getTimeSpent() ?:0
issueLinkManager.getOutwardLinks(issue.id).each { issueLink ->
    if (issueLink.issueLinkType.name == "Solve")  {
        def linkedIssue = issueLink.destinationObject
        log.debug(linkedIssue)
        log.debug(issueLink.issueLinkType.name)
        totalLoggedWork += linkedIssue.originalEstimate ?: 0
    }
}

def timeLoggedWork = ""
if(totalLoggedWork) {
    def hours = totalLoggedWork / 3600;
    def minutes = (totalLoggedWork % 3600) / 60;
    def seconds = totalLoggedWork % 60;
    timeLoggedWork = "часов: ${hours}, минут: ${minutes}, сек: ${seconds}"
    log.debug(timeLoggedWork)
}
return timeLoggedWork

 

The sum of logged time:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogManager
import com.atlassian.jira.bc.issue.worklog.TimeTrackingConfiguration
import org.apache.log4j.Logger
import org.apache.log4j.Level


def worklogManager = ComponentAccessor.getComponent(WorklogManager)
def workLogs = worklogManager.getByIssue(issue)
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def log = Logger.getLogger("com.acme.ListLinks")
log.setLevel(Level.DEBUG)

def totalEstimatedWork = 0
issueLinkManager.getOutwardLinks(issue.id).each { issueLink ->
    if (issueLink.issueLinkType.name == "Solve") {
        def linkedIssue = issueLink.destinationObject
        def linkedIssueWorkLogs = worklogManager.getByIssue(linkedIssue);

        linkedIssueWorkLogs.each {  linkedIssueWorkLog ->
             totalEstimatedWork += linkedIssueWorkLog.getTimeSpent() ?: 0;
        }
    }
}

def timeEstimatedWork = ""
if(totalEstimatedWork) {
    def hours = totalEstimatedWork / 3600;
    def minutes = (totalEstimatedWork % 3600) / 60;
    def seconds = totalEstimatedWork % 60;
    timeEstimatedWork = "часов: ${hours}, минут: ${minutes}, сек: ${seconds}"
    log.debug(timeEstimatedWork)
}
return timeEstimatedWork

 

The above code snippets calculates time for linked issues that has link type "Solve". If you want to calculate for all types, then just delete the following condition:

    if (issueLink.issueLinkType.name == "Solve") { }

2 votes
Answer accepted
zaharovvv_suek_ru
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.
June 1, 2018

I paste the code, If somebody need  it. 

  • The sum of estimated time of linked issues  which has type of link "Solve" to this task.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogManager
import com.atlassian.jira.bc.issue.worklog.TimeTrackingConfiguration
import org.apache.log4j.Logger
import org.apache.log4j.Level

//def issue = ComponentAccessor.getIssueManager().getIssueObject("ELIS-104")
def worklogManager = ComponentAccessor.getComponent(WorklogManager)
def workLogs = worklogManager.getByIssue(issue)
def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def log = Logger.getLogger("com.acme.ListLinks")
log.setLevel(Level.DEBUG)


def totalEstimatedWork = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Solve") { 
def linkedIssue = issueLink.destinationObject 
def linkedIssueWorkLogs = worklogManager.getByIssue(linkedIssue); 
linkedIssueWorkLogs.each {linkedIssueWorkLog -> 
totalEstimatedWork += linkedIssueWorkLog.getTimeSpent() ?: 0;
} 
}

}

def timeEstimatedWork = "" 
if(totalEstimatedWork){
def hours = totalEstimatedWork / 3600;
def minutes = (totalEstimatedWork % 3600) / 60;
def seconds = totalEstimatedWork % 60;
timeEstimatedWork = "hours: ${hours}, minutes: ${minutes}, seconds: ${seconds}" 
log.debug(timeEstimatedWork)
}
return timeEstimatedWork

 

 

  • The sum of logged time of all linked issues   which has type of link "Solve" to this task.

package com.onresolve.jira.groovy.test.scriptfields.scripts
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogManager
import com.atlassian.jira.bc.issue.worklog.TimeTrackingConfiguration

def worklogManager = ComponentAccessor.getComponent(WorklogManager)
def workLogs = worklogManager.getByIssue(issue)
def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def totalLoggedWork = 0
//totalLoggedWork = totalLoggedWork + issue.getTimeSpent() ?:0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Solve") { 
def linkedIssue = issueLink.destinationObject
log.debug(linkedIssue)
log.debug(issueLink.issueLinkType.name)
totalLoggedWork += linkedIssue.originalEstimate ?: 0
} 
}

def timeLoggedWork = "" 
if(totalLoggedWork){
def hours = totalLoggedWork / 3600;
def minutes = (totalLoggedWork % 3600) / 60;
def seconds = totalLoggedWork % 60;
timeLoggedWork = "hours: ${hours}, minutes: ${minutes}, seconds: ${seconds}" 
log.debug(timeLoggedWork)
}
return timeLoggedWork

 

If you want to find a sum to all issues, then just remove the following condition and its brackets:

if (issueLink.issueLinkType.name == "Solve") {  }

 

 

1 vote
Nic Brough -Adaptavist-
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.
May 31, 2018

There's some things in your script that are a bit suspect, but they should all be picked up by the basic script editor built into ScriptRunner.

Could you go back to your script for the scripted field and look at the editor.  Are there any red crosses next to any lines?

Then look below it, for a box labelled "preview issue" - put your master issue in there and click the preview button.  You should see the calculated value appear, but there is also a "logs" tab there, and that's worth a look. 

You can also embed log.debug ("something") lines into the script here to find out what it is doing and getting back from the issues.  Inside the "issueLink ->" loop, I would try "log.debug(issueLink.issueLinkType.name + "//" + issueLink.getDestinationIssue.getKey() )" for example, to see what links it is reading and what the types are.  Then another one inside the "if" to see if it gets there.  And so-on

zaharovvv_suek_ru
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.
June 1, 2018

Nic, thanks for your reply. However, I've not found where Script Editor is located. 

Do you mean Intellij IDEA by Script Editor?

TAGS
AUG Leaders

Atlassian Community Events