Hi,
My requirement is that I want to show all the resolution values if the user is project admin but doesn't want to show "Fixed" if the user is into some other project role. I have written a behaviour which works until a point and then all of a sudden stops working. This code is added as an initializer.
This code works until I add the if-else clause and then it just stops working even if the clause are removed.
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
import com.atlassian.jira.security.roles.ProjectRoleManager
def userUtil = ComponentAccessor.getUserUtil()
def currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def remoteUsersRoles = projectRoleManager.getProjectRoles(currentUser, issueContext.projectObject)*.name
def constantsManager = ComponentAccessor.getConstantsManager()
log.info(remoteUsersRoles)
if (!("Administrators" in remoteUsersRoles) ) {
def removeResolutions = constantsManager.getResolutions().findAll { it.name in ["Fixed"] }
def allowedResolutions = constantsManager.getResolutions().findAll { it.name }
allowedResolutions=allowedResolutions-removeResolutions
getFieldById(RESOLUTION).setFieldOptions(allowedResolutions)
}
Error Log below -
at Script1.run(Script1.groovy:14)
java.lang.NullPointerException: Cannot get property 'name' on null object
2022-07-28 16:33:09,478 http-nio-8080-exec-36 ERROR asastry_admin 993x53804326x1 6u87vr 172.21.22.242,192.168.0.33 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: issue: SB-16037, user: asastry_admin, fieldId: resolution, file: <inline script>
2022-07-28 16:33:09,476 http-nio-8080-exec-36 ERROR asastry_admin 993x53804326x1 6u87vr 172.21.22.242,192.168.0.33 /rest/scriptrunner/behaviours/latest/runvalidator.json [c.o.jira.behaviours.BehaviourManagerImpl] *************************************************************************************
at Script1.run(Script1.groovy:14)
java.lang.NullPointerException: Cannot get property 'name' on null object
2022-07-28 16:33:09,435 http-nio-8080-exec-63 ERROR asastry_admin 993x53804325x1 6u87vr 172.21.22.242,192.168.0.33 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: issue: SB-16037, user: asastry_admin, fieldId: __init__, file: <inline script>
2022-07-28 16:33:09,434 http-nio-8080-exec-63 ERROR asastry_admin 993x53804325x1 6u87vr 172.21.22.242,192.168.0.33 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] *************************************************************************************
at java.lang.Thread.run(Thread.java:748)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
... 24 filtered
at com.atlassian.jira.servermetrics.MetricsCollectorFilter.doFilter(MetricsCollectorFilter.java:25)
... 26 filtered
at com.atlassian.web.servlet.plugin.LocationCleanerFilter.doFilter(LocationCleanerFilter.java:36)
... 4 filtered
at com.atlassian.web.servlet.plugin.request.RedirectInterceptingFilter.doFilter(RedirectInterceptingFilter.java:21)
... 3 filtered
at com.atlassian.jwt.internal.servlet.JwtAuthFilter.doFilter(JwtAuthFilter.java:32)
... 9 filtered
at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.doFilter(CustomerContextSettingFilter.java:121)
at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.doFilterImpl(CustomerContextSettingFilter.java:130)
at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.outOfCustomerContext(CustomerContextSettingFilter.java:174)
at com.atlassian.servicedesk.internal.utils.context.CustomerContextServiceImpl.runOutOfCustomerContext(CustomerContextServiceImpl.java:47)
at com.atlassian.servicedesk.internal.api.util.context.ReentrantThreadLocalBasedCodeContext.runOutOfContext(ReentrantThreadLocalBasedCodeContext.java:90)
at com.atlassian.servicedesk.internal.api.util.context.ReentrantThreadLocalBasedCodeContext.rteInvoke(ReentrantThreadLocalBasedCodeContext.java:137)
at com.atlassian.servicedesk.internal.web.CustomerContextSettingFilter.lambda$invokeFilterChain$0(CustomerContextSettingFilter.java:181)
... 3 filtered
at com.idalko.jira.plugins.igrid.servicedesk.ServletRequestContextFilter.doFilter(ServletRequestContextFilter.java:23)
... 3 filtered
at com.valiantys.jira.plugins.sql.service.servletcontext.ContextListenerServletFilter.doFilter(ContextListenerServletFilter.java:24)
... 5 filtered
at com.atlassian.jira.servermetrics.CorrelationIdPopulatorFilter.doFilter(CorrelationIdPopulatorFilter.java:30)
... 19 filtered
at com.atlassian.plugins.rest.module.servlet.RestSeraphFilter.doFilter(RestSeraphFilter.java:37)
... 16 filtered
at com.atlassian.jira.security.JiraSecurityFilter.doFilter(JiraSecurityFilter.java:64)
... 1 filtered
at com.atlassian.jira.security.JiraSecurityFilter.lambda$doFilter$0(JiraSecurityFilter.java:66)
... 53 filtered
at com.atlassian.web.servlet.plugin.request.RedirectInterceptingFilter.doFilter(RedirectInterceptingFilter.java:21)
... 13 filtered
at com.atlassian.servicedesk.internal.web.ExternalCustomerLockoutFilter.doFilter(ExternalCustomerLockoutFilter.java:56)
... 32 filtered
at com.atlassian.plugins.rest.module.RestDelegatingServletFilter.doFilter(RestDelegatingServletFilter.java:68)
... 1 filtered
at com.atlassian.plugins.rest.module.RestDelegatingServletFilter$JerseyOsgiServletContainer.doFilter(RestDelegatingServletFilter.java:154)
... 19 filtered
at java.lang.reflect.Method.invoke(Method.java:498)
... 2 filtered
at com.onresolve.jira.behaviours.restservice.BehavioursEndpoint.getValidatorsFromIssueId(BehavioursEndpoint.groovy:76)
at com.onresolve.jira.behaviours.BehaviourManagerImpl.runValidator(BehaviourManagerImpl.groovy:185)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.dgm$163.invoke(Unknown Source)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2151)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2110)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2125)
at groovy.lang.Closure.call(Closure.java:434)
at groovy.lang.Closure.call(Closure.java:418)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:264)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at java.lang.reflect.Method.invoke(Method.java:498)
... 2 filtered
at com.onresolve.jira.behaviours.BehaviourManagerImpl$_runValidator_closure9.doCall(BehaviourManagerImpl.groovy:239)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:58)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192)
at java.lang.reflect.Method.invoke(Method.java:498)
... 2 filtered
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at java.lang.reflect.Method.invoke(Method.java:498)
... 3 filtered
at Script1.run(Script1.groovy:8)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)
at org.codehaus.groovy.runtime.callsite.ClassMetaClassGetPropertySite.getProperty(ClassMetaClassGetPropertySite.java:50)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3773)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1828)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1852)
at groovy.lang.MetaClassImpl.invokeStaticMissingProperty(MetaClassImpl.java:1014)
Possible solutions: jiraAuthenticationContext
groovy.lang.MissingPropertyException: No such property: JiraAuthenticationContext for class: com.atlassian.jira.component.ComponentAccessor
2022-07-28 16:33:09,430 http-nio-8080-exec-63 ERROR asastry_admin 993x53804325x1 6u87vr 172.21.22.242,192.168.0.33 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] Found class but could not find method run
Thanks in advance,
Aditya Sastry
Does this script work better ?
import com.atlassian.jira.component.ComponentAccessor
import static com.atlassian.jira.issue.IssueFieldConstants.RESOLUTION
import com.atlassian.jira.security.roles.ProjectRoleManager
def userUtil = ComponentAccessor.getUserUtil()
def currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def remoteUsersRoles = projectRoleManager.getProjectRoles(currentUser, issueContext.projectObject).collect{it.getName()}
def constantsManager = ComponentAccessor.getConstantsManager()
log.info(remoteUsersRoles)
if (!("Administrators" in remoteUsersRoles) ) {
def allowedResolutions = constantsManager.getResolutions().findAll { it.name != "Fixed" }
getFieldById(RESOLUTION).setFieldOptions(allowedResolutions)
}
Thanks @Florian Bonniec for the neater version but I'd tried everything.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Also currentUser line seems weird.
I personnaly use ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
instead ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
After scratching my head for hours realized the mistake.
Incorrect line of code
def currentUser = ComponentAccessor.JiraAuthenticationContext.getLoggedInUser()
Correct line of code
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.