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

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

This widget could not be displayed.

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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

511 views 3 18
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you