Change sprint value by groovy script

I need to change value of Sprint(for example, Name) of concrete issue. When i try simply update sprint, it appearances the next error: 'Java.lang.String cannot be cast to java.util.Collection'. How can i change necessary elements in this collection?

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.event.type.EventDispatchOption

 

def issueManager = ComponentAccessor.getIssueManager()

def curIssue = issueManager.getIssueObject("MyIssue")

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def curUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def customField = customFieldManager.getCustomFieldObjectByName("Sprint")

curIssue.setCustomFieldValue(customField,  "sprint4 (18/10/16-31/10/16)")

issueManager.updateIssue(curUser, curIssue, EventDispatchOption.ISSUE_UPDATED, false)

2 answers

1 accepted

1 vote
Answer accepted

Ok John, 

If you want to update the name of the sprint itself then for a JIRAv7 try 

import com.atlassian.greenhopper.service.sprint.Sprint
import com.atlassian.greenhopper.service.sprint.SprintManager
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.pyxis.greenhopper.jira")
def sprintManager = PluginModuleCompilationCustomiser.getGreenHopperBean(SprintManager)

def sprintServiceOutcome = sprintManager.getAllSprints()

if (sprintServiceOutcome.valid) {
    def sprint = sprintServiceOutcome.getValue().find {it.name == "Sprint Name"} as Sprint
    
    if (!sprint) {
        log.debug "Could not find sprint with name <Sprint Name>"
    } else {
        def newSprint = sprint.builder(sprint).name("New Name").build()
        def outcome = sprintManager.updateSprint(newSprint)
        
        if (outcome.isInvalid()) {
            log.debug "Could not update sprint with name : Sprint 2 because ${outcome.getErrors()}"
        } else {
            log.debug "Sprint Updated !!"
        }
        
    }
}

please let me know if this does the trick

Thank you for a lot, now it works!

Thank you! This worked well, when I needed to update a groovy script from Jira v6.1 to Jira v7.3

Try this code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue


MutableIssue curIssue = ComponentAccessor.getIssueManager().getIssueObject("MyIssue")

curIssue.setCustomFieldValue(
        ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint")
        , Arrays.asList("sprint4 (18/10/16-31/10/16)"))

issueManager.updateIssue(
        ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
        , curIssue
        , EventDispatchOption.ISSUE_UPDATED
        , false)

Not so easy. Now there is error "java.lang.String cannot be cast to com.atlassian.greenhopper.service.sprint.Sprint"

Hi John,

If you want to update the issue's sprint to one that exist and not update a property of the sprint itself (for example the name) you should get the Sprint Object and then set the custom field value. So in order to get the Sprint itself try something like 

import com.atlassian.greenhopper.service.sprint.SprintManager
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.pyxis.greenhopper.jira")
def sprintManager = PluginModuleCompilationCustomiser.getGreenHopperBean(SprintManager)

def sprintServiceOutcome = sprintManager.getAllSprints()

if (sprintServiceOutcome?.valid) {
    def sprint = sprintServiceOutcome.getValue().find {it.name == "Sprint 1"}
    if (!sprint) {
        log.debug "Could not find sprint with name <SPrint 1>"
    } else {
        // do something with this sprint
    }
}

Hi, Thanos!

I want to update property of the sprint(just name). I try to use this script and get the next error: "Cannot set readonly property". Is it real to change name of object Sprint?

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.greenhopper.service.sprint.Sprint

import com.atlassian.jira.event.type.EventDispatchOption

 

def issueManager1 = ComponentAccessor.getIssueManager();

def curIssue1 = issueManager1.getIssueObject("IssueName")

def customFieldManager1 = ComponentAccessor.getCustomFieldManager()

def Name = customFieldManager1.getCustomFieldObjectByName("Sprint")

ArrayList<Sprint> sprints = (ArrayList<Sprint>)curIssue1.getCustomFieldValue(Name)

sprints.get(0).putAt("name","dsa")

Hi @Thanos Batagiannis [Adaptavist] , I created a script to auto update the Sprint based on a change in the issue, but it seems like it is removing all the Completed Sprints when the automation ran, any possible solution for this?

Do you think instead of adding the sprint an update is required for this?

Check the excerpt below:

 // else

def issueService = ComponentAccessor.getIssueService() 
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters
.setSkipScreenCheck(true)
issueInputParameters
.addCustomFieldValue(sprintCustomFieldId, sprint?.id as String)            
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)

if
(validationResult.isValid()) {
issueService.update(user, validationResult)
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Thursday in Summit

Find a your Summit 2019 Buddy!

Can you believe there's less than one month until Summit 2019? It's time to start planning your agendas, not to mention packing ... In the meantime, introduce yourselves on this thread so that you ...

241 views 16 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you