Forums

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

How to enforce a workflow validator by a JQL query?

Dani Lynch January 7, 2025

Hello!

I’m trying to add a workflow validator to one of my transitions where the transition is blocked if a JQL query returns 0 results. Essentially, the requirement is to check that a Test Case linked to the issue, during that particular transition. We’re in the process of migrating off Zephyr Squad onto Zephyr Scale, so I can’t use the issue links functions. I also need to make the check for both Squad & Scale tests, since this workflow is common between all projects in our instance (some using Squad, some using Scale). 

I’m new to groovy but my script isn’t returning any errors. However, the transition is going through, even if I don’t have tests linked. Initiaally I had this working in a Condition, but the users were getting confused why the transition is missing. Stakeholders would prefer a validator so we can show the error on transition. Here is the script I have so far, any advice is greatly appreciated!

def ScaleTests = Issues.count(‘key={issue.key} and issue in hasTestCoverage()’)

def SquadTests = Issue.count(‘key = {issue.key} and issueFunction in linkedIssuesOf(“issue type = \”Test\””)’)

if (ScaleTests <0 || SquadTests < 0)

{return false;

InvalidInputException error = new InvalidInputException();

error.addError(“Test case must be linked to issue”);

throw error;

}

return true

 

 

 

1 answer

0 votes
Tuncay Senturk _Snapbytes_
Community Champion
January 8, 2025

Hi @Dani Lynch 

Are you sure you are not getting any errors? I don't know Zephyr but I can try to help you with the script. 

I don't think either Issue.count or Issues.count will wok as they are not Groovy valid syntax. You can use SearchService to search by JQL. I assume hasTestCoverage is valid in Zephyr, and below is the script which may help you start with. I hope it helps

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.workflow.WorkflowFunctionUtils
import com.opensymphony.workflow.InvalidInputException

def issue = transientVars["issue"] // Get the current issue
def jqlQueryParser = ComponentAccessor.getComponent(SearchService.JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def scaleTestQuery = "key = ${issue.key} AND issue in hasTestCoverage()"
def squadTestQuery = "key = ${issue.key} AND issueFunction in linkedIssuesOf('issueType = \"Test\"')"

def scaleTestResults = searchService.search(user, jqlQueryParser.parseQuery(scaleTestQuery), PagerFilter.getUnlimitedFilter())
def scaleTestsCount = scaleTestResults.total

def squadTestResults = searchService.search(user, jqlQueryParser.parseQuery(squadTestQuery), PagerFilter.getUnlimitedFilter())
def squadTestsCount = squadTestResults.total

if (scaleTestsCount == 0 && squadTestsCount == 0) {
InvalidInputException error = new InvalidInputException()
error.addError("Test case must be linked to the issue before transitioning.")
throw error
}

return true

 

Dani Lynch January 8, 2025

Hi @Tuncay Senturk _Snapbytes_ , 

Thank you for your insight! I’m unfortunately now getting an error on the import statement. Startup failed; unable to resolve class com.Atlassian.Jira.issue.search.SearchService

 

Would you know how to resolve? 

Tuncay Senturk _Snapbytes_
Community Champion
January 8, 2025

which Jira version are you using? and can you confirm that this is Jira Datacenter?

Dani Lynch January 8, 2025

Yes this is in Jira DataCenter, version is 9.17.4

Suggest an answer

Log in or Sign up to answer