Last active
August 29, 2015 14:11
-
-
Save cben0ist/6e31bd2c0fb1825837c5 to your computer and use it in GitHub Desktop.
Jira Script Runner JQL function I wrote to filter my request by member of the project role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @Author:cben0ist (https://github.com/cben0ist) | |
* @Date: Dec 2015 | |
* @Description: Jira Script Runner JQL function I wrote to filter my request by member of the project role | |
*/ | |
package org.cben0ist.jira.jql | |
import com.atlassian.crowd.embedded.api.User | |
import com.atlassian.jira.issue.Issue | |
import com.atlassian.jira.jql.operand.QueryLiteral | |
import com.atlassian.jira.jql.query.QueryCreationContext | |
import com.atlassian.jira.util.MessageSet | |
import com.atlassian.query.clause.TerminalClause | |
import com.atlassian.query.operand.FunctionOperand | |
import com.onresolve.jira.groovy.jql.AbstractScriptedJqlFunction | |
import org.apache.lucene.index.Term | |
import org.apache.lucene.search.BooleanClause | |
import org.apache.lucene.search.BooleanQuery | |
import org.apache.lucene.search.Query | |
import org.apache.lucene.search.TermQuery | |
import com.atlassian.jira.util.MessageSet | |
import com.atlassian.jira.util.MessageSetImpl | |
import com.atlassian.jira.jql.validator.NumberOfArgumentsValidator | |
import org.apache.lucene.search.ConstantScoreQuery | |
import com.atlassian.jira.issue.search.filters.IssueIdFilter | |
import com.atlassian.jira.security.roles.ProjectRoleManager | |
import com.atlassian.jira.project.Project | |
import com.atlassian.jira.security.roles.ProjectRole | |
import com.atlassian.jira.security.roles.ProjectRoleActors | |
import com.atlassian.jira.ComponentManager | |
import com.atlassian.jira.component.ComponentAccessor | |
import org.apache.log4j.Category | |
class assignedToMembersOfProjectRole extends AbstractScriptedJqlFunction { | |
def log = Category.getInstance(assignedToMembersOfProjectRole.class) | |
private ComponentManager componentManager = ComponentManager.getInstance() | |
private ProjectRoleManager projectRoleManager = (ProjectRoleManager) ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) | |
@Override | |
MessageSet validate (User searcher, FunctionOperand operand, TerminalClause terminalClause) { | |
setParameters(operand, searcher).messageSet | |
} | |
private class ConfigParams { | |
def String pKey | |
def String pRole | |
def String subquery | |
def MessageSet messageSet | |
} | |
private ConfigParams setParameters(FunctionOperand operand, User searcher) { | |
def messageSet = new MessageSetImpl() | |
def config = new ConfigParams() | |
config.messageSet = messageSet | |
messageSet = new NumberOfArgumentsValidator(3, 3, getI18n()).validate(operand) | |
if (messageSet.hasAnyErrors()) { | |
return messageSet | |
} | |
config.subquery = operand.args[0] | |
config.pKey = operand.args[1] | |
config.pRole = operand.args[2] | |
config | |
} | |
/** | |
* @param queryCreationContext | |
* @param operand | |
* @param terminalClause | |
* @return lucene query | |
*/ | |
Query getQuery(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause) { | |
def config = setParameters(operand, queryCreationContext.user) | |
Set issueIds = new HashSet() | |
def List<Issue> issues = getIssues(config.subquery) | |
def fieldManager = ComponentAccessor.getFieldManager() | |
// Parse users and build a Set of user names | |
ProjectRole projectRole = projectRoleManager.getProjectRole(config.pRole) | |
Project project = componentManager.getProjectManager().getProjectObjByKeyIgnoreCase(config.pKey) | |
ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project) | |
final Set<QueryLiteral> usernames = new LinkedHashSet<QueryLiteral>() | |
Set<User> users = projectRoleActors.getUsers() | |
log.debug(users.size() + " in " + config.pRole) | |
Set<String> userNames = new TreeSet<String>() | |
for(User user : users) { | |
userNames.add(user.getName()) | |
} | |
// Parse issues and return the matching ones | |
issues.eachWithIndex {Issue issue, i -> | |
def field = getField("assignee", issue) | |
if (!field) { | |
return | |
} | |
def addIssueTerm = {Long issueId -> | |
issueIds << (issue.id as String) | |
} | |
def issueField = (issue[field.id] as String) ?: "" | |
if (issueField.length() >= 2){ | |
issueField = issueField.substring(0, issueField.length()-2); | |
} | |
if (userNames.contains(issueField)) { | |
addIssueTerm(issue.id) | |
} | |
} | |
new ConstantScoreQuery(new IssueIdFilter(issueIds)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
issueFunction in assignedToMembersOfProjectRole(subquery, Project Key, Project Role)
issueFunction in assignedToMembersOfProjectRole("project = PRJ AND sprint = "PRJ-Sprint 29"", "PRJ", "Developers") ORDER BY status ASC