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

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

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.

Hi Adam,

Unfortunately I get mulitple errors

Snap 2017-04-06 at 17.13.49.png

Hi Darren,

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

It doesn't populate the field unfortunately

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?

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.

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)

 

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.

Thanks

Our JIRA version is 6.4.7 and Scriptrunner is 4.1.3.24 

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.

Aplogies for the delay...been on annual leave

Thanks, this works!!

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
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

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