JIRA 7 has broken groovy post-function to assign sprint?

Nick Shaw January 12, 2016

So I built a script post-function based on one I found from Jamie about setting an issue to a sprint (mine sets based on a custom field). This script works fine on JIRA 6, but upgrading to JIRA 7, it's throwing me an error:

"[Static type checking] - Cannot find matching method com.atlassian.greenhopper.service.sprint.SprintIssueService#addIssuesToSprint(com.atlassian.jira.user.ApplicationUser, java.lang.Object, java.util.List). Please check if the declared type is right and if the method exists."

I know the API has changed up a bit and is no longer working with GenericValues, but even after I started casting all my variables (which, on all of my other scripts, I haven't yet had to do), it still throws only this error.

Is there something that's changed about the addIssuesToSprint function? Or do I just need to cast my variables in better ways? Thanks!

import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.sprint.SprintIssueService
import com.atlassian.greenhopper.service.sprint.SprintManager
import com.atlassian.greenhopper.service.sprint.SprintService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

//Set Variables
log.setLevel(org.apache.log4j.Level.INFO)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfSprint = customFieldManager.getCustomFieldObjectByName("Sprint")
def cfAgileBoard = customFieldManager.getCustomFieldObjectByName("Agile Board")
def sprints = issue.getCustomFieldValue(cfSprint)
def agileBoard = issue.getCustomFieldValue(cfAgileBoard)
def rapidBoardId = 0

//See if the issue is already in an active sprint
if (sprints != null) {
	for (i in sprints.size) {
        ii = i-1
        isprint = sprints.get(ii)
        istate = isprint.state.toString()
        if (istate == "ACTIVE") {
        	log.info("Issue is already in active sprint")
            return
        }
	}
}

//Sets to a specific board for all issues in a certain project
if (issue.getProjectObject().getKey() == 'KEY') {
	setBoard(18)
	return
}

//Sets to an agile board based on a custom field
switch (agileBoard) {
	case "Agile Board 1":
		setBoard(30)
	break
	case "Agile Board 2":
		setBoard(39)
	break
	case "Agile Board 3":
		setBoard(31)
	break
	case "Agile Board 4":
		setBoard(32)
	break
	case "Agile Board 5":
		setBoard(33)
    default:
        log.info("Agile Board field is empty")
		return
    break
}

//Performs the actual sprint setting
public setBoard(rapidBoardId) {
	
	@WithPlugin("com.pyxis.greenhopper.jira")
	@JiraAgileBean
	RapidViewService rapidViewService
	@JiraAgileBean
	SprintService sprintService
	@JiraAgileBean
	SprintIssueService sprintIssueService
	@JiraAgileBean
	SprintManager sprintManager
	def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
	def view = rapidViewService.getRapidView(loggedInUser, rapidBoardId).getValue()
	def boardSprints = sprintManager.getSprintsForView(view).getValue()
	def activeSprint = boardSprints.find { it.active }
    def boardName = view.getName()
	
	if (activeSprint) {
		sprintIssueService.addIssuesToSprint(loggedInUser, activeSprint, [issue])
        log.info("Auto-setting Sprint to active sprint of board: $boardName")
	}
    else {
        log.info("$boardName has no active sprint")
    }
	return
}

1 answer

1 accepted

1 vote
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 12, 2016

There is now a built-in script to handle this: https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_add_remove_from_to_active_sprint

But I'm not sure you will be able to use it if you need to dynamically set the board based on your rules. JA is not a public API, and it changes often, so if you can use the built-in script that would be easier.

You will need to replace addIssuesToSprint with moveIssuesToSprint ... that might be all. I'm not convinced that this method for getting the first active sprint works properly, but please make that change and have a try.

 

Nick Shaw January 13, 2016

I did want to use your new built-in script, but the dynamic board setting is a requirement of the script I'm afraid. That said, the simple change worked perfectly! Luckily we don't have parallel sprints active so hopefully we don't need to worry about having to find multiple active sprints per board, but I think in worst case I could probably work up getting a list of active ones and find the 'first' one if need be. Kinda unfortunate they're not maintaining the JA API documentation the same way they are the regular JIRA API docs, but I suppose that's a risk I'll take. Thank you very much!

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 13, 2016

No problem. As it stands, the code will pick the *first active sprint*, so you should not need to make changes if you have multiple active sprints. They will come in order of creation IIRC.

Suggest an answer

Log in or Sign up to answer