It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Change sprint value by groovy script

John Nov 01, 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

1 vote
Answer accepted
Thanos Batagiannis [Adaptavist] Community Leader Nov 02, 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 Nov 02, 2016

Thank you for a lot, now it works!

Airpas_ICT Jul 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 Nov 02, 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 Nov 02, 2016

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

Thanos Batagiannis [Adaptavist] Community Leader Nov 02, 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 Nov 02, 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")

Bryan Bojorque Mar 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)
}

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

964 views 12 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you