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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Custom JQL function is giving me inconsistent results

Hi.

I'm looking for someone who is expert in custom JQL functions. I've implemented one cool custom JQL function and it's playing games with me . Same search with this JQL function is giving me different results. And it's completely random. I get 21 results and then 21, 21, 4, 21, 4, 21, 21, 21, 4, .... I've tried it debug it in Script Console but it works perfectly ther. Can anyone here help me to move along? 

21results.png4results.png

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.JiraDataType
import com.atlassian.jira.JiraDataTypes
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.QueryCreationContext
import com.atlassian.jira.permission.ProjectPermissions
import com.atlassian.jira.security.PermissionManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand

import java.util.regex.Matcher

class ScopeOf extends AbstractScriptedJqlFunction implements JqlFunction {

PermissionManager permissionManager = ComponentAccessor.getPermissionManager()
List<Issue> finalListOfIssues

@Override
String getDescription() {
"Function finds all issues in scope of the issue"
}

@Override
List<Map> getArguments() {
[
[
"description": "Issue key or a subquery in \"\"",
optional: false
]
] as List<Map>
}

@Override
String getFunctionName() {
"scopeOf"
}

@Override
JiraDataType getDataType() {
JiraDataTypes.ISSUE
}

@Override
List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) {
finalListOfIssues = []
ApplicationUser currentUser = queryCreationContext.getApplicationUser()
getListOfIssuesInScope(initialListOfIssues(operand.args[0], currentUser))

finalListOfIssues.unique().findAll {
queryCreationContext.securityOverriden || permissionManager.hasPermission(ProjectPermissions.BROWSE_PROJECTS, it as Issue, currentUser)
}.collect {
new QueryLiteral(operand, it.id)
}
}

List<Issue> initialListOfIssues(String operand, ApplicationUser currentUser) {
String pattern = /(?i)^"?[A-Z]+_?[A-Z\d]+-\d+"?$/
Matcher matcher = operand =~ pattern
String finalJQL

// argument is only one issue key
if(matcher) {
String issueKey = (matcher[0] as String).replace('"', '').replace("'", "")
finalJQL = "issue = ${issueKey}"
} else {
finalJQL = operand
}

return executeJqlQuery(finalJQL, currentUser)
}


List<Issue> executeJqlQuery(String queryString, ApplicationUser currentUser) {
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)


def query = jqlQueryParser.parseQuery(queryString)

def results = searchProvider.search(query, currentUser, PagerFilter.getUnlimitedFilter())

return results.getIssues()
}


List<Issue> getSubTasks(Issue issue) {
return ComponentAccessor.getSubTaskManager()
.getSubTaskObjects(issue) as List<Issue>
}


List<Issue> getIssuesUnderEpic(Issue epicIssue) {
return ComponentAccessor.getIssueLinkManager()
.getOutwardLinks(epicIssue.getId())
.findAll {link -> link.issueLinkType.name == "Epic-Story Link"}
.collect { link -> return link.destinationObject}
}


List<Issue> getSolvingIssues(Issue solvedIssue) {
return ComponentAccessor.getIssueLinkManager()
.getInwardLinks(solvedIssue.getId())
.findAll {link -> link.issueLinkType.name == "Solves"}
.collect { link -> return link.sourceObject}
}


def getListOfIssuesInScope(List<Issue> listOfIssues) {
List<Issue> listToContinueWith

for (Issue issue : listOfIssues) {
switch (issue.getIssueType().getName()) {
case ["Story", "Task", "Improvement"]:
finalListOfIssues += getSubTasks(it)
break
case ["Epic"]:
listToContinueWith = getIssuesUnderEpic(issue) - finalListOfIssues
finalListOfIssues += listToContinueWith
getListOfIssuesInScope(listToContinueWith)
break
case ["Idea", "Theme"]:
listToContinueWith = getSolvingIssues(issue) - finalListOfIssues
finalListOfIssues += listToContinueWith
getListOfIssuesInScope(listToContinueWith)
break
}
}
}
}

 

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
Apr 02, 2020

Have you tried to use issueFunction IN linkedIssuesOfAllRecursive(<subQuery>) fuction? I believe is the same you are trying

I know the JQL function, but the query for our full hierarchy (Theme -> Grand Idea -> Idea -> Epic -> Story -> Subtask) will be insane. We need our custom JQL function to it easier for anyone to get same result.

0 votes
Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 02, 2020

Get some debug logging in there to show you what the JQL is being turned into for the Lucene query part. Use one of the Lucene index tools to analyze your index to check it is not changing.

I've added a ton of logging into the script and the result is very interesting. The function getValues returns always the correct result. Inconsistency arises beyond this point. I don't understand why :'(

TAGS
AUG Leaders

Atlassian Community Events