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

1 vote

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

110 views 0 3
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