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

1 vote

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
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 Jul 10, 2018 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...

179 views 0 4
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