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

Groovy Script Cast Exception

Yogesh Mude
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.
April 30, 2019

As m getting the list of incomplete issues from the sprint, i want to reset the sprint field of those incomplete issues to null.

i tried with the below code but getting error.

 

import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.greenhopper.service.rapid.view.RapidViewService;
import com.atlassian.greenhopper.service.sprint.Sprint
import com.atlassian.greenhopper.web.rapid.chart.HistoricSprintDataFactory
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.atlassian.greenhopper.service.sprint.SprintManager
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import java.util.ArrayList;
import java.util.Collection;

@WithPlugin("com.pyxis.greenhopper.jira")

@BaseScript CustomEndpointDelegate customEndpointDelegate

String sprintId = event.sprint.getId();
//def sprint = event.sprint;

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
System.out.println("Sprint Name : " + event.sprint.getName() );

def sprintManager = PluginModuleCompilationCustomiser.getGreenHopperBean(SprintManager)
def historicSprintDataFactory = PluginModuleCompilationCustomiser.getGreenHopperBean(HistoricSprintDataFactory)
def rapidViewService = PluginModuleCompilationCustomiser.getGreenHopperBean(RapidViewService)

def userManager = ComponentAccessor.getUserManager()
def issueService = ComponentAccessor.getIssueService()

def closedSprint = sprintManager.getSprint(sprintId.toLong()).value
def view = rapidViewService.getRapidView(user, closedSprint.rapidViewId).value
def sprintContents = historicSprintDataFactory.getSprintOriginalContents(user, view, closedSprint)
def sprintData = sprintContents.value
def sprintField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint");

if(sprintData != null)
{

def incompleteIssues =sprintData.contents.issuesNotCompletedInCurrentSprint*.issueId;
System.out.println("Incomplete Issues : " + incompleteIssues);
if(incompleteIssues != null)
{
for(int i=0;i < incompleteIssues.size();i++ )
{
//String value = null;
System.out.println("Issue Key : " + incompleteIssues.get(i).toString());
MutableIssue mutableObject = (MutableIssue)incompleteIssues;
mutableObject.setCustomFieldValue(sprintField, null);
ComponentAccessor.getIssueManager().updateIssue(user, mutableObject, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_UPDATED, false);
}
}

}
 

Error:

2019-04-30 20:48:33,859 http-nio-8712-exec-13 ERROR admin 1248x1199x1 18hxn2v 0:0:0:0:0:0:0:1 /rest/greenhopper/1.0/sprint/107/complete [c.o.scriptrunner.runner.AbstractScriptListener] Script function failed on event: com.atlassian.greenhopper.api.events.sprint.SprintClosedEvent, file: <inline script>
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[10601, 11001]' with class 'java.util.ArrayList' to class 'com.atlassian.jira.issue.MutableIssue' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.atlassian.jira.issue.MutableIssue(java.lang.Long, java.lang.Long)
at Script13.run(Script13.groovy:62)

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Payne
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.
May 1, 2019

You're attempting to cast your list of issues, rather than the current issue in the loop, to one MutableIssue object.

Rather than

MutableIssue mutableObject = (MutableIssue)incompleteIssues;

You need something like

MutableIssue mutableObject = (MutableIssue)incompleteIssues.get(i);

Marc Minten _EVS_
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.
May 6, 2019

...I think it is even worse... you try to cast a list of issueIDs to one issue ??:-(

You should for all the issueIDs in the list, instantiate first the issue (and cast it to MutabelIssue) , and then ....

TAGS
AUG Leaders

Atlassian Community Events