Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Validate JQL result on issue creation

Daniel Alonso
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.
October 21, 2019

This is my scenario:

I need to do a JQL and get the number of results (done), if this number is grater than X number do not allow the creation of an issue.

 

This is my current (example) code:

 

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter

final String jqlSearch = 'project = "Sample Project"' // the jql query you want to search with

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
def total_issues = 0
if (parseResult.isValid()) {
try {
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.issues

//log.warn(results)
issues.each {
//log.warn(it.key)
total_issues++
}
return total_issues
} catch (SearchException e) {
e.printStackTrace()
}
} else {
print("Invalid query")
return null
}

if (total_issues >= 23){
return false
}

Right now is not working, I put this code on the 'Create' transition. 

1 answer

1 accepted

1 vote
Answer accepted
Jack Nolddor _Sweet Bananas_
Atlassian Partner
October 21, 2019

Hi daniel,

Why dont use SearchService.searchCountOverrideSecurity(ApplicationUser searcher, Query query) method?

Which indeed returns the number of issues that matches the given query.


You code should looks like something like this (I've just type the code here so probably I missed something)


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.opensymphony.workflow.InvalidInputException


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = JRA and assignee = currentUser()")

def count = searchService.searchCountOverrideSecurity(user, query)


if(count >= 23 ) {
    throw new InvalidInputException("Too Many Issues")
}


Regards

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events