Groovy script for getting sprints for a given issue

Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2017

For a given set of issues, I am trying to get the active sprint for each issue and determine if they are all the same sprint. How can I achieve this using a groovy script?

I'm probably just missing something in the javadoc, but I can't find anything that will allow me to get an issue's active sprint(s). What is the return type of the Sprint custom field type? I found this answer to be helpful to me, but it only pulls the data for the last sprint, not the current active one (if it exists).

This regards JIRA Softare 7.3.1, by the way.

1 answer

1 accepted

2 votes
Answer accepted
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2017

I figured out a way to do this based on the previous answer I already found. This script also doesn't take into account parallel sprints because we don't have that enabled in our instance.

import com.atlassian.jira.component.ComponentAccessor

def sprintField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint")
def sprintName
def allSameSprint = false // initialize to "false" in case none of the issues belong to any sprint

for (issue in issues) {
 def sprint = issue.getCustomFieldValue(sprintField)
 def sprintState = sprint?.state?.last()?.toString()
 
 if (sprintState.equals("ACTIVE")) {
  // set to "true" sprintName isn't filled yet
  if (sprintName == null) {
   this.sprintName = sprint?.name?.last()?.toString()
   allSameSprint = true
  }
  // set back to "false" if another sprint name is found
  else if (!sprintName.equals(sprint?.name?.last()?.toString())) {
   allSameSprint = false
  }
 }
 // if an inactive sprint is found, set to "false"
 else {
  allSameSprint = false
 }
}

I'm not the most graceful coder, so if someone knows of a better, more elegant way to do the same thing, please let me know. :)

Sandor Krisztian Andre October 26, 2017

Hi,

 

I can't find the "state" property anywhere in the documentation. Tried:

 

I'm trying not to depend on implementation details, but rather on documented API features.

PhillipS July 13, 2018

same problem.  I would believe that the sprint field returns some sort of collection of sprints, and not the sprint itself, but still the line defining sprintState doesn't make sense to me, and won't compile in the groovy script runner as written

Deleted user June 11, 2020

For me, works.

Peter Macdonald June 18, 2020

Using Jira Misc Workflow Extension Add-on:

issue.get("Sprint")*.state?.toString().flatten()

Suggest an answer

Log in or Sign up to answer