ScriptRunner (Scripted Field) - Calculation using a numeric values and current Sprint

Darren Campbell April 6, 2017

Hi,

 

We are trying to write a script for a scripted field.

Our scenario is that we have a custom number field (Sprint Variance) and a scripted one (Max Sprint).  Our Epic will be put into an current sprint variance will indicate that it may take up to x additional sprints before it’s resolved.  Based on the current Sprint Name (e.g. UI Sprint 34) and Sprint Variance (2), we want the scripted field to display (UI Sprint 36).

Obviously the assumption here is that the numbers are at the end of the sprint name and after the word "Sprint".  Does anyone have any ideas on how to write this script as I’m struggling to devise the correct syntax and methods :(

 

In advance I appreciate your help!

1 answer

0 votes
adammarkham
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.
April 6, 2017

Please try the following code in a scripted field. This should do what you want, assuming Sprint Variance is a number custom field.

import com.atlassian.greenhopper.service.sprint.SprintIssueService
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean

@JiraAgileBean
SprintIssueService sprintIssueService

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def sprintVarianceCf = customFieldManager.getCustomFieldObjectByName("Sprint Variance")

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def sprintResult = sprintIssueService.getActiveSprintForIssue(loggedInUser, issue)

def variance = issue.getCustomFieldValue(sprintVarianceCf) as Integer

def activeSprint = sprintResult.get().getOrElse(null)

def sprintName = activeSprint.name

if (! variance || ! activeSprint) {
    // variance or active sprint not set so no max sprint
    return null
}

def tokenizedSprintName = sprintName.tokenize(" ")

// remove old variance
def sprintNumber = tokenizedSprintName.pop().toInteger()

// calculate new variance
tokenizedSprintName.push(sprintNumber + variance)

// join tokens together
tokenizedSprintName.join(" ")

Let  us know how you get on with that.

Please also see our documentation around working with JIRA Agile here.

Darren Campbell April 6, 2017

Hi Adam,

Unfortunately I get mulitple errors

Snap 2017-04-06 at 17.13.49.png

adammarkham
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.
April 6, 2017

Hi Darren,

They are just static type checking errors. The script will still work as expected when added and run.

Darren Campbell April 6, 2017

It doesn't populate the field unfortunately

adammarkham
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.
April 6, 2017

Are you on the view epic screen with a "Sprint Variance custom field set as a number custom field?

Also is your sprint called something like "UI Sprint 2"?

Then you should see this appear.

Do you see any errors in the log file when you view the epic?

Which version of JIRA and ScriptRunner do you have?

adammarkham
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.
April 6, 2017

I edited the script a while ago, I think you have the old version. Please update it and try again.

Also the epic should be in an active sprint.

Darren Campbell April 6, 2017

Copied and pasted provided run again but the field still doesnt display.

 

I'm the EPIC via the normal issue view screen

Current sprint is called SEPI Sprint 32

 

Logs show:

[onresolve.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: SEPI-793, field: Max Sprint
groovy.lang.MissingMethodException: No signature of method: com.atlassian.greenhopper.service.sprint.SprintIssueServiceImpl.getActiveSprintForIssue() is applicable for argument types: (com.atlassian.jira.user.BridgedDirectoryUser, com.atlassian.jira.issue.IssueImpl) values: [MYEMAIL:10000, SEPI-793]
Possible solutions: getActiveSprintForIssue(com.atlassian.jira.user.ApplicationUser, com.atlassian.jira.issue.Issue)
at Script533.run(Script533.groovy:14)

 

adammarkham
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.
April 7, 2017

Hi Darren,

What version of JIRA and JIRA Software/Agile do you have?

There's a few JIRA Agile versions where some of these methods were changed so that may be causing the issue. Let me know and I'll provide an alternative.

Darren Campbell April 7, 2017

Thanks

Our JIRA version is 6.4.7 and Scriptrunner is 4.1.3.24 

adammarkham
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.
April 7, 2017

You should use the following script for JIRA 6:

import com.atlassian.greenhopper.service.sprint.SprintIssueService
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean

@JiraAgileBean
SprintIssueService sprintIssueService

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def sprintVarianceCf = customFieldManager.getCustomFieldObjectByName("Sprint Variance")

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getUser()

def sprintResult = sprintIssueService.getActiveSprintForIssue(loggedInUser, issue)

def variance = issue.getCustomFieldValue(sprintVarianceCf) as Integer

def activeSprint = sprintResult.get().getOrElse(null)

def sprintName = activeSprint.name

if (! variance || ! activeSprint) {
// variance or active sprint not set so no max sprint
return null
}

def tokenizedSprintName = sprintName.tokenize(" ")

// remove old variance
def sprintNumber = tokenizedSprintName.pop().toInteger()

// calculate new variance
tokenizedSprintName.push(sprintNumber + variance)

// join tokens together
tokenizedSprintName.join(" ")

Only change was this line:

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getUser()

JIRA 6 and 7 have different users either an ApplicationUser or directory User.

Darren Campbell April 19, 2017

Aplogies for the delay...been on annual leave

Thanks, this works!!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events