Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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

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

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") {  }

 

 

2 votes
Answer accepted

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") { }

1 vote

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

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

Do you mean Intellij IDEA by Script Editor?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

24 views 0 0
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