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

Scripted Field - Global Values

invinci
Contributor
May 2, 2017

Hi,

I am working on writing a groovy script for calculating Epic Rollups. I would like to know is there any way of getting the global values for the user, issue assuming there is an epic?  

I want to use something like this,

USER = 'invinci'
children = retrieveIssuesInEpic(issue)
total = sumOfOriginalEstimates(children)

Here,

1. USER is the one with browse projects permissions

2. children represents the subtasks

3.  sumOfOriginalEstimates(children) is the time taken.

 

How would I let the script know of the parent/child key values whenever an issue is created on the jira web application?

 

Thank you!

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Steven F Behnke
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 3, 2017

You don't invoke Script Fields. They are invoked when the issue is viewed. I use them heavily depending on the use-case.

Word to the wise: You should configure the custom field scope tightly for script fields. A script field can be a lot more expensive than say, a string text field. You should make sure the custom field context to only include projects and issue types that it's relevant for.

For example, this field would only be used by Software Development teams. You would only use this field on the Epic issue type. Your context should look something like this:
image.png

In addition to the custom field context, you should edit the custom field and change the searcher to Duration Searcher:
image.png

Your script should accomplish the following:

  • Set the view template to Duration
  • Disable the script field cache
  • Use IssueLinkManager to pass the Issue object and receive a list of IssueLinks
  • Using these IssueLinks, check each IssueLink object for the IssueLinkType, as you only want issues linked via the Epic-Story Link type
  • For each IssueLink object, use the getDestinationObject() method to obtain the Issue object for that linked issue
  • For each object, use the getOriginalEstimate() method to obtain the estimate time
  • Sum all of these original estimates
  • Return the value

An example of what this script could look like is below. I'm no groovy expert. I do not know what version of JIRA you're using and I do not know how you want your data presented, so this may not work for you. This doesn't consider sub-tasks, so you'll likely want to obtain the aggregate estimate instead. I didn't have the time to complete this though: I just mostly took an existing field I had and modified it.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.config.IssueTypeManager

enableCache = {-> false}

IssueType getIssueTypeByName(String name){
 for(IssueType issueType in ComponentAccessor.getComponent(IssueTypeManager.class).getIssueTypes()){
        if(issueType.getName() == name){
            return issueType
        }
    }
    return null
}

String issueTypeName = "Epic"
String issueLinkTypeName = "Epic-Story Link"

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
IssueLinkTypeManager issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager.class)

IssueLinkType[] issueLinkTypes = issueLinkTypeManager.getIssueLinkTypesByName(issueLinkTypeName)
IssueLink[] issueLinks = issueLinkManager.getOutwardLinks(issue.id)

IssueType issueType = getIssueTypeByName(issueTypeName)

if( issue && 
    issueType && 
    issueLinkTypes.size() > 0 && 
    issue.getIssueType().name == issueType.getName() && 
    issueLinks.size() > 0 )
{
    List<IssueLink> relevantIssueLinks = new ArrayList()
    for(issueLink in issueLinks){
        if(issueLink.getIssueLinkType().name == issueLinkTypes.first().getName()){
           relevantIssueLinks.add(issueLink)
        }
    }
    
    if(relevantIssueLinks.size() > 0){
        Long originalEstimate = 0
        for(issueLink in relevantIssueLinks){
            Issue linkedIssue = issueLink.getDestinationObject()
            
            if(linkedIssue.getOriginalEstimate()){
                originalEstimate = originalEstimate + linkedIssue.getOriginalEstimate()
            }
        }
if(originalEstimate > 0){ return originalEstimate
} } } 

 image.png

invinci
Contributor
May 5, 2017

hey Steven, 

Thanks for the detailed explanation. Now I get the idea of how to work on my script. 

Thanks a lot! 

0 votes
Steven F Behnke
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 2, 2017

It's unclear to me: Is this a listener? You want this to execute on Issue_Created/Issue_Updated?

invinci
Contributor
May 2, 2017

It is a Scripted Field which is invoked on issue events

TAGS
AUG Leaders

Atlassian Community Events