/src/main/resources/com/onresolve/jira/groovy/canned/workflow/conditions/JqlQueryCondition.groovy
Groovy | 142 lines | 112 code | 23 blank | 7 comment | 10 complexity | 4731bd32e1b3f1ef3eef15b76112004b MD5 | raw file
- package com.onresolve.jira.groovy.canned.workflow.conditions
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.issue.Issue
- import com.atlassian.jira.issue.MutableIssue
- import com.atlassian.jira.jql.parser.JqlQueryParser
- import com.atlassian.jira.util.ErrorCollection
- import com.onresolve.jira.groovy.canned.CannedScript
- import com.atlassian.jira.util.SimpleErrorCollection
- import org.apache.log4j.Category
- import com.atlassian.jira.issue.IssueManager
- import com.onresolve.jira.groovy.canned.admin.ConditionTester
- import com.onresolve.jira.groovy.canned.utils.ConditionUtils
-
- /**
- * User: jechlin2
- */
- class JqlQueryCondition implements CannedScript{
-
- public static String FIELD_JQL_QUERY = "FIELD_JQL_QUERY"
- public static String FIELD_PREVIEW_ISSUE = "FIELD_PREVIEW_ISSUE"
- Category log = Category.getInstance(JqlQueryCondition.class)
- def componentManager = ComponentManager.getInstance()
- def issueManager = componentManager.getIssueManager()
- def searchService = componentManager.getSearchService()
- def jqlQueryParser = ComponentManager.getComponentInstanceOfType(JqlQueryParser.class) as JqlQueryParser
-
- String getName() {
- "Allows the transition if this query matches a JQL query"
- }
-
- public String getHelpUrl() {
- "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-JQLQueryCondition"
- }
-
- String getDescription() {
- "Only allows the transition if this query would be returned when running a JQL query"
- }
-
- List getCategories() {
- ["ADMIN", "Condition"]
-
- }
-
- List getParameters(Map params) {
- [
- [
- Label: "JQL Query",
- Name: FIELD_JQL_QUERY,
- Description: "JQL query to test the issue with. For example \"issuetype = Task\""
- ],
- [
- Name:FIELD_PREVIEW_ISSUE,
- Label:"Preview Issue Key",
- Description:"Issue key for previewing the condition with. <b>Note: Preview only available when testing from the Admin panel</b>.",
- ],
- ]
- }
-
- ErrorCollection doValidate(Map params, boolean forPreview) {
- ErrorCollection errorCollection = new SimpleErrorCollection()
- String prvwIssueKey = params[FIELD_PREVIEW_ISSUE] as String
- String jqlQuery = params[FIELD_JQL_QUERY] as String
-
- if (forPreview) {
- if (! issueManager.getIssueObject(prvwIssueKey as String)) {
- errorCollection.addError(FIELD_PREVIEW_ISSUE, "This issue doesn't exist.")
- }
- }
-
- if (jqlQuery) {
- def queryErrors = validateQuery(jqlQuery)
- if (queryErrors.hasAnyErrors()) {
- errorCollection.addError(FIELD_JQL_QUERY, queryErrors.getErrorMessages().toList().first())
- }
- }
-
- errorCollection
- }
-
- Map doScript(Map params) {
- MutableIssue issue = params['issue'] as MutableIssue
-
- if (!issue) {
- params['output'] = "<b>Please press the Preview button<b>"
- }
- else {
- Boolean doIt = queryMatch(issue, params[FIELD_JQL_QUERY] as String)
- params['passesCondition'] = doIt
- }
- return params
- }
-
- String getDescription(Map params, boolean forPreview) {
- if (forPreview) {
- MutableIssue prvwIssue = issueManager.getIssueObject(params[FIELD_PREVIEW_ISSUE] as String)
- Boolean result = queryMatch(prvwIssue, params[FIELD_JQL_QUERY] as String)
- "The issue ${prvwIssue.key} " + (result ? "<b>did</b>" : "<b>did not</b>") + " match the query."
- }
- else {
- "The action will be allowed if the issue matches the JQL query: \"${params[FIELD_JQL_QUERY]}\""
- }
- }
-
- Boolean isFinalParamsPage(Map params) {
- true
- }
-
- public boolean queryMatch(Issue issue, String jqlQuery) {
- // no condition set equates to Always
- if (!(jqlQuery && issue)) {
- log.debug("No JQL or no issue")
- return true
- }
-
- def query = jqlQueryParser.parseQuery("issue = ${issue.id} and ($jqlQuery)")
-
- // todo: we're assuming the query is valid, but it might not be if say an issuetype has been removed from the system
- // def messageSet = searchService.validateQuery(componentManager.getJiraAuthenticationContext().getUser(), query)
-
- def rt = searchService.searchCount(componentManager.getJiraAuthenticationContext()?.getUser(), query) as Boolean
- log.debug("parsed query: $query returned: $rt")
- rt
- }
-
- private ErrorCollection validateQuery(String jqlCondition) {
- def errorCollection = new SimpleErrorCollection()
- try {
- def query = jqlQueryParser.parseQuery(jqlCondition)
- def messageSet = searchService.validateQuery(componentManager.getJiraAuthenticationContext().getUser(), query)
- messageSet.getErrorMessages().each {
- errorCollection.addErrorMessage(it)
- }
- }
- catch (Exception e) {
- // various exes can be thrown here
- errorCollection.addErrorMessage(e.cause?.message ?: e.message)
- }
-
- errorCollection
- }
- }