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

Validate JQL result on issue creation

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

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
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

269 views 9 6
Join discussion

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