Forums

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

How to write a validator for Jira on a script runner to calculate the number of issues by condition?

Viewer
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 7, 2021

You need a validator script to count the number of search tasks
(status = "IN PRODUCTION" AND assignee in (currentUser ()))
I want to check the condition for no more than 3 tasks

2 answers

0 votes
Payne
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.
April 7, 2021

This ought to work; it will return true, and thus pass validation, if the current user has 3 or fewer "In Production" issues assigned to him. Otherwise it will return false and fail validation.

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 jqlSearch = "assignee = currentUser() and status = \"IN PRODUCTION\""
final maxIssues = 3

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.isValid()) {
log.error('Invalid query')
return null
}

try {
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
return issues.size() <= maxIssues
} catch (SearchException e) {
e.printStackTrace()
null
}
Aleksey Golomidov April 16, 2021

Hi, thanks a lot for the validator script. He looks like a worker. I paste it into the Custom script validator form and it doesn't work.
What am I doing wrong?

Aleksey Golomidov April 16, 2021

scrin

Payne
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.
April 19, 2021

What do you mean by "it doesn't work"? Do you receive an error, or is it not stopping the issue from being assigned to someone with > assigned tasks?

Aleksey Golomidov April 20, 2021

It not stopping the issue from being assigned to someone with> assigned tasks

Payne
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.
April 20, 2021

You ARE changing the assignee during a transition, correct? This validator will not prevent someone from simply changing the assignee.

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
April 7, 2021

Hi @Viewer I will need better description of your use case. Could you describe it in steps? You are kind of mixing Validators and Conditions (which have different meaning in Jira). So can you describe what/when should be diplayed?

Suggest an answer

Log in or Sign up to answer