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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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
issue.getCustomFieldValue(sprintCf)

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. 

https://docs.atlassian.com/software/jira/docs/api/7.1.7/com/atlassian/jira/issue/changehistory/ChangeHistoryItem.html 

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.
Oct 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 + sprintChangeItems.to).unique().findAll().collect{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
TAGS
AUG Leaders

Atlassian Community Events