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

Change sprint value by groovy script

John November 1, 2016

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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
November 2, 2016

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

John November 2, 2016

Thank you for a lot, now it works!

Skymetrix IT July 20, 2017

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

0 votes
Vasiliy Zverev
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.
November 2, 2016

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)
John November 2, 2016

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

Thanos Batagiannis _Adaptavist_
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.
November 2, 2016

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
    }
}
John November 2, 2016

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")

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 18, 2019

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)
}
TAGS
AUG Leaders

Atlassian Community Events