Not able to retrieve the current user in groovy script

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

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

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

355 views 0 1
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