Issue with script: "Cannot cast object 'user(user)' "

Sean Bennett
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 25, 2015

Hi,


We have recently upgraded from JIRA v4.3 to v6.3 and during the process had to change a Post-Function setting on a Workflow for "the security level based on user's specified group" to use a Groovy script as we were experiencing an error with JIRA.

It appeared to resolve the issue but looking at the logs we're now receiving the error message for certain users (Please note I've masked some references):

 

 

[scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: issueID, actionId: 1, file: D:\JIRA 6.3.6\groovy\security-level.groovy
javax.script.ScriptException: javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'user(user)' 
with class 'com.atlassian.jira.user.DelegatingApplicationUser' to class 'com.atlassian.crowd.embedded.api.User'
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runFileAsScript(ScriptRunnerImpl.groovy:195)
at com.onresolve.scriptrunner.runner.ScriptRunner$runFileAsScript$3.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:47)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.doScript(CustomScriptFunction.groovy:20)
Caused by: javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'user(user)' with class 'com.atlassian.jira.user.DelegatingApplicationUser' to class 'com.atlassian.crowd.embedded.api.User'
... 5 more
Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'user(user)' with class 'com.atlassian.jira.user.DelegatingApplicationUser' to class 'com.atlassian.crowd.embedded.api.User'
at Script1.run(Script1.groovy:19)
... 5 more

Our script is as follows:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.issue.security.IssueSecurityLevelManager
import com.atlassian.jira.issue.security.IssueSecuritySchemeManager
import com.atlassian.crowd.embedded.api.User
import org.ofbiz.core.entity.GenericValue
def secLevelString = "#Customers"
     
    ComponentManager componentManager = ComponentManager.getInstance()
    IssueSecurityLevelManager securityLevelManager = componentManager.getComponentInstanceOfType(com.atlassian.jira.issue.security.IssueSecurityLevelManager)
    IssueSecuritySchemeManager issueSecuritySchemeManager = ManagerFactory.getIssueSecuritySchemeManager()
    IssueSecurityLevelManager issueSecurityLevelManager = ManagerFactory.getIssueSecurityLevelManager()
    
    GenericValue srcProjectGV = issue.getProject()
 
    def issueSecurityScheme = issueSecuritySchemeManager.getSchemes(srcProjectGV).size() == 0 ? null : issueSecuritySchemeManager.getSchemes(srcProjectGV)[0]
     def secLevelGv=securityLevelManager.getSchemeIssueSecurityLevels(issueSecurityScheme.id).find ({it.get("name")==secLevelString})
    User currentUser = componentManager.getJiraAuthenticationContext().getUser()
    if(componentManager.getCrowdService().isUserMemberOfGroup(currentUser.getName(),"Customers")) {          
    if (secLevelGv) {
        issue.setSecurityLevel(secLevelGv)
        
    }
  }
  issue.store();

 

I think the issue sits with importing com.atlassian.crowd.embedded.api.user but having not had much exposure to this I'm not sure what to do to resolve so any help would be greatly appreciated.

Thanks a million!

 

1 answer

1 vote
JamieA
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.
September 25, 2015

Change this:

User currentUser = componentManager.getJiraAuthenticationContext().getUser()

to:

def currentUser = componentManager.getJiraAuthenticationContext().getUser()

You don't need to declare the types in groovy, and if you don't then you won't get caught out when JiraAuthContext.getUser() changes from returning a User to an ApplicationUser.

You can also remove the import for com.atlassian.crowd.embedded.api.User, which will save you a task to do when you upgrade to JIRA 7.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events