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

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
Posted in Jira

We want to hear from you! How do you use Slack and Atlassian together?

Hi Everyone! My name is Mina and I am on Atlassian’s Ecosystems Marketing team. Our team is focused on our technology partnerships and marketplace apps. One of Atlassian’s partners is Slack, who ...

85 views 1 5
Join discussion

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