Trying to write a Scriptrunner custom field called Previous Sprints, using ChangeHistoryItem

Crystal Baker October 25, 2023

So posing this question here is a stretch I know, but I'm hoping to use scriptrunner to create a custom field that calls the count of previous sprints that an issue has been in, in order to calculate how many times it has rolled over. I used this groovy script, and it retrieved the current sprint ONLY:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprintCf = customFieldManager.getCustomFieldObjectByName("Sprint")
Issue issue = issue

But I want all of the old sprints from change history, not just the current one. 

I know I can invoke ChangeHistoryItem function but don't have the groovy skills to get it to work. 

I found the api documentation but it doesn't tell me field names so I'm stuck. 

Any groovy wizards around?

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 25, 2023

You can extract all the changes for a specific field like this:

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

@JiraAgileBean SprintManager sprintManager

def chm = ComponentAccessor.changeHistoryManager
def sprintChangeItems = chm.getChangeItemsForField(issue, 'Sprint')

if(!sprintChangeItems) return null
(sprintChangeItems.from +{sprintManager.getSprint(it as Long) }

By combining all the 'from' and 'to' elements from the change Items you should get all the sprints including the original sprint if included during creation and the current sprint (unless it was included during creation and never changed).
The unique() is self evident.
findAll() will remove all the null
Then, I use "collect" to convert the list of string values stored in the change items for the sprint id into a list of sprint objects.

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events