Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Find Active Sprint using GroovyScript but when there are multiple projects on a board

Lee Cash
Contributor
November 26, 2021

Hi, 

This lovely piece of script can be found here: https://innovalog.atlassian.net/wiki/spaces/JMWE/pages/163348640/Fetch+the+current+active+sprint+of+a+Scrum+board 

import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.sprint.SprintService
import com.atlassian.greenhopper.service.PageRequests
import com.atlassian.greenhopper.service.sprint.SprintQuery
import com.atlassian.greenhopper.service.sprint.Sprint
import com.atlassian.greenhopper.service.sprint.Sprint.State

def boardId = <Board Id>
def board = getComponent(RapidViewService).getRapidView(currentUser, boardId).get()
def SprintService = getComponent(SprintService)
def State = Sprint.getClasses()[1]

def sprintQuery = SprintQuery.builder().states(Collections.singleton(State.ACTIVE)).build()
def sprintsOutcome = SprintService.getSprints(currentUser, board, PageRequests.all(), sprintQuery)

if (sprintsOutcome.isInvalid()){
  throw new UnsupportedOperationException(sprintsOutcome.toString())
}
def sprints = sprintsOutcome.get().getValues()

if (sprints.size() == 0){
  throw new UnsupportedOperationException("No active sprints")
}
if (sprints.size() > 1){
  throw new UnsupportedOperationException("More than one sprint is active")
}
return sprints[0].getId()

 

It essentially takes a Board ID and returns the associated sprints so you can do clever things like have a self-referencing transition that lets you "Add To Sprint" which just pops the Jira into the active sprint without having to edit and select the sctive sprint from the dropdown. Lazy? Perhaps. But when you're triaging 200 bugs and you just want to click a button and move on, it's a godsend. 

The issue I have is that we have added another project to the board. Hence, I no longer have ONE active Sprint. I now have two. This means this script will always throw the "More than one sprint is active" exception. 

My question is: does anyone know how to modify the code so that it can filter out the active sprint of the project I don't care about? 

Example: 

Let's say there are two projects on a board called Animals. The projects are called Cat and Dog. Each have their own sprints. 

  • Cat Sprint 3 (Active) 
  • Cat Sprint 2 (Closed) 
  • Cat Sprint 1 (Closed) 
  • Dog Sprint 2 (Active) 
  • Dog Sprint 1 (Closed) 

--

I want the query above to IGNORE the active Dog sprint and only return the details of Cat Sprint 3. 

Any ideas? 

Is the best option to simply stop throwing the exception for multiple sprints, accept that there could be more than one and do some sort of check (i.e. looking for the "Cat" string) on the sprints[] array? 

Thanks in advance, 

Lee 

 

 

1 answer

1 accepted

2 votes
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 26, 2021

Hi @Lee Cash ,

your problem is not related to having more than one project on your Board, because Sprints are not associated with projects but boards. You just have multiple active sprints, and you just happen to only put issues of a single project in each. The only differentiation between your sprints is... their name. Therefore, you could simply filter active sprints based on the name. Just replace:

def sprints = sprintsOutcome.get().getValues()

with:

def sprints = sprintsOutcome.get().getValues().findAll{
it.name.startsWith("Cat ");
}

 David

Lee Cash
Contributor
November 29, 2021

Thanks, David. This worked perfectly. I appreciate all your help. 

Suggest an answer

Log in or Sign up to answer