Not able to retrieve the current user in groovy script

Naren
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.
February 27, 2013

I am trying to write a groovy service for fetching the issues from the query, but stuck at retrieving the e current loggedin user part. Below is my code to get the current logged in user in the groovy service -

import org.apache.log4j.Category 
import com.atlassian.jira.ComponentManager 
import com.atlassian.crowd.embedded.api.User 

log = Category.getInstance("com.onresolve.jira.groovy.MyTestService") 
// Get logged in user 

ComponentManager componentManager = ComponentManager.getInstance() 
User user = componentManager.getJiraAuthenticationContext().getLoggedInUser() 
log.debug("Currently logged in user: "+ user.getName())

I keep hitting the below errors -

2013-02-28 15:16:50,225 QuartzWorker-0 ERROR ServiceRunner    test [onresolve.jira.groovy.GroovyService] javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getName() on null object
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getName() on null object
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:195)
	at com.onresolve.jira.groovy.GroovyRunner.runFile(GroovyRunner.java:97)
	at com.onresolve.jira.groovy.GroovyRunner.run(GroovyRunner.java:57)
	at com.onresolve.jira.groovy.GroovyService.run(GroovyService.java:53)
	at com.atlassian.jira.service.JiraServiceContainerImpl.run(JiraServiceContainerImpl.java:61)
	at com.atlassian.jira.service.ServiceRunner.execute(ServiceRunner.java:47)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:195)
	at com.atlassian.multitenant.quartz.MultiTenantThreadPool$MultiTenantRunnable.run(MultiTenantThreadPool.java:72)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
Caused by: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getName() on null object
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
	... 10 more
Caused by: java.lang.NullPointerException: Cannot invoke method getName() on null object
	at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
	at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:32)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
	at Script4.run(Script4.groovy:12)
	at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)
	... 11 more

Clearly the user is retruning null value, but don't know where I am going wrong. Any help wuould be appreciated

1 answer

1 accepted

1 vote
Answer accepted
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.
February 27, 2013

There is no logged in user for a service... it's that simple.

If you need a user then you have to get one using eg UserManager.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events