Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

NoClassDefFoundError encountered in custom JQL function

Edited

I need to write a custom JQL function.
In the workflow is the stage "Waiting for approval". Each approver can individually accept/decline the issue. Some approvers are belonging to the defined group. The purpose of this function is to hide those issues in which someone from this group has already accepted/declined.
I made sure that the function body is working properly. I tested it in the script console and everything was OK. However, when I insert the function into the Adaptavist template, I get the error java.lang.NoClassDefFoundError: com / atlassian / servicedesk / api / approval / ApprovalService. Error is generated by the last line.

Any help will be really appreciated.

 

package com.onresolve.jira.groovy.jql

import com.atlassian.servicedesk.api.approval.ApprovalService
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.jql.query.RangeQueryFactory
import com.atlassian.jira.util.MessageSetImpl
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.operand.FunctionOperand
import com.atlassian.query.operator.Operator
import org.apache.log4j.Category
import com.atlassian.jira.util.MessageSet
import com.atlassian.crowd.embedded.api.User
import com.atlassian.query.clause.TerminalClause
import com.atlassian.jira.jql.query.QueryCreationContext
import org.apache.lucene.document.NumberTools
import org.apache.lucene.index.Term
import org.apache.lucene.search.Query
import org.apache.lucene.search.TermQuery
import com.onresolve.jira.groovy.jql.AbstractScriptedJqlFunction
import org.apache.lucene.search.BooleanClause
import org.apache.lucene.search.BooleanQuery
import org.apache.lucene.search.Query
import org.apache.lucene.search.TermQuery
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.util.UserManager
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.bc.issue.search.SearchService
@WithPlugin("com.atlassian.servicedesk")


class mySGroupPendingApprovers extends AbstractScriptedJqlFunction implements JqlQueryFunction{
@Override
String getDescription() {
"Function to show only the inactive users"
}

@Override
List<Map> getArguments() {
[
[
"description": "Subquery",
"optional": false,
]
]
}

@Override
String getFunctionName() {
"mySGroupPendingApprovers"
}

//@Override
MessageSet validate(User user, FunctionOperand operand, TerminalClause terminalClause) {
def messageSet = new MessageSetImpl()
return messageSet
}

@@Override
Query getQuery(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
JiraAuthenticationContext context = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser actingUser = userManager.getUserByName("automat");
Class ApprClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.atlassian.servicedesk.api.approval.ApprovalService");
def approvalService = ComponentAccessor.getOSGiComponentInstanceOfType(ApprClass);
// collecting the userNames list (group)
// test code
Collection<String> userNames = ['pi24468','pi01697','pi10310','pi01498'];
// prod code
// .....

// collecting the output issue list
def booleanQuery = new BooleanQuery()
// test code
// def issues = [ComponentAccessor.getIssueManager().getIssueByCurrentKey("CHANGE-1315"),
// ComponentAccessor.getIssueManager().getIssueByCurrentKey("CHANGE-1814")];
// prod code
issues = getIssues(operand.args[0], applicationUser)
def approvalEither
def approvals
def pendingApproval
def approversEither
def approvers
def approversList
def approversDoneList

issues.each {Issue issue ->
try{
pendingApproval = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName('Approvals').first()).getApprovals()

if (pendingApproval) {
// found at least 1 pending approval
approversEither = approvalService.getApprovers(applicationUser, pendingApproval.first())
approvers = approversEither.isRight() ? approversEither.right : []
approversList = approvers.findAll{!it.getApproverDecision().getOrNull()}.collect{it.getApproverUser().name}
if ((approversList.size() > 0) && (approversList.intersect(userNames).size() > 0)) {
// found at least 1 pending approval for approver who belongs to group
approversDoneList = approvers.findAll{it.getApproverDecision().getOrNull()}.collect{it.getApproverUser().name}
if ((approversDoneList.size() == 0) || (approversDoneList.intersect(userNames).size() == 0)) {
// not found any approval done by approver who belongs to group
// this ticket to be shown in "ToApprove" queue
booleanQuery.add(new TermQuery(new Term("issue_id", issue.id as String)), BooleanClause.Occur.SHOULD)
}
}
}
}catch(NullPointerException NPE){
}
}

return booleanQuery }
}


// main
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserManager userManager = ComponentAccessor.getUserManager();
ApplicationUser actingUser = userManager.getUserByName("automat");

def jqlSearch = "issueFunction in mySGroupPendingApprovers('";
jqlSearch += 'category="Service Desk"'
jqlSearch += "')"
SearchService.ParseResult parseResult = searchService.parseQuery(actingUser, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(actingUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
}

Errors:

java.lang.NoClassDefFoundError: com/atlassian/servicedesk/api/approval/ApprovalService at com.onresolve.jira.groovy.jql.mySGroupPendingApprovers.getQuery(mySGroupPendingApprovers.groovy:84) at com.onresolve.jira.groovy.jql.JqlQueryFunction$getQuery.call(Unknown Source) at com.onresolve.jira.groovy.jql.ScriptedJqlFunction.getQuery(ScriptedJqlFunction.groovy:128) at com.onresolve.jira.groovy.jql.ScriptedJqlFunction$getQuery.call(Unknown Source) at com.onresolve.jira.groovy.jql.ScriptedFunctionClauseFactory.getQuery(ScriptedFunctionClauseFactory.groovy:92) at com.atlassian.jira.jql.query.ContextAwareQueryVisitor.visit(ContextAwareQueryVisitor.java:97) at com.atlassian.jira.jql.query.ContextAwareQueryVisitor.visit(ContextAwareQueryVisitor.java:25) at com.atlassian.query.clause.TerminalClauseImpl.accept(TerminalClauseImpl.java:143) at com.atlassian.jira.jql.query.QueryVisitor.createQuery(QueryVisitor.java:51) at com.atlassian.jira.jql.query.DefaultLuceneQueryBuilder.createLuceneQuery(DefaultLuceneQueryBuilder.java:29) at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.createLuceneQuery(LuceneSearchProvider.java:359) at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.getHits(LuceneSearchProvider.java:238) at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search(LuceneSearchProvider.java:393) at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search(LuceneSearchProvider.java:128) at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.search(LuceneSearchProvider.java:124) at com.atlassian.jira.bc.issue.search.DefaultSearchService.search(DefaultSearchService.java:111) at com.atlassian.jira.bc.issue.search.SearchService$search$0.call(Unknown Source) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:144) at com.onresolve.jira.groovy.jql.Script396.run(Script396.groovy:137) at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) at javax.script.ScriptEngine$eval.call(Unknown Source) at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runScriptAndGetContext(AbstractScriptRunner.groovy:94) at com.onresolve.scriptrunner.runner.AbstractScriptRunner$runScriptAndGetContext$2.callCurrent(Unknown Source) at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runStringAsScript(AbstractScriptRunner.groovy:192) at com.onresolve.scriptrunner.runner.AbstractScriptRunner.runStringAsScript(AbstractScriptRunner.groovy) at com.onresolve.scriptrunner.runner.rest.common.UserScriptEndpoint.exec(UserScriptEndpoint.groovy:182) at com.onresolve.scriptrunner.runner.rest.common.UserScriptEndpoint.execFromJson(UserScriptEndpoint.groovy:122) at sun.reflect.GeneratedMethodAccessor1607.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$ResponseOutInvoker$1.invoke(DispatchProviderHelper.java:192) at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$1.intercept(DispatchProviderHelper.java:83) at com.atlassian.plugins.rest.common.interceptor.impl.DefaultMethodInvocation.invoke(DefaultMethodInvocation.java:53) at com.atlassian.plugins.rest.common.expand.interceptor.ExpandInterceptor.intercept(ExpandInterceptor.java:34) at com.atlassian.plugins.rest.common.interceptor.impl.DefaultMethodInvocation.invoke(DefaultMethodInvocation.java:53) at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper.invokeMethodWithInterceptors(DispatchProviderHelper.java:110)

....and many more

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

787 views 17 21
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you