PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/resources/com/onresolve/jira/groovy/canned/workflow/conditions/JqlQueryCondition.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 142 lines | 112 code | 23 blank | 7 comment | 10 complexity | 4731bd32e1b3f1ef3eef15b76112004b MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.workflow.conditions
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.issue.Issue
  4. import com.atlassian.jira.issue.MutableIssue
  5. import com.atlassian.jira.jql.parser.JqlQueryParser
  6. import com.atlassian.jira.util.ErrorCollection
  7. import com.onresolve.jira.groovy.canned.CannedScript
  8. import com.atlassian.jira.util.SimpleErrorCollection
  9. import org.apache.log4j.Category
  10. import com.atlassian.jira.issue.IssueManager
  11. import com.onresolve.jira.groovy.canned.admin.ConditionTester
  12. import com.onresolve.jira.groovy.canned.utils.ConditionUtils
  13. /**
  14. * User: jechlin2
  15. */
  16. class JqlQueryCondition implements CannedScript{
  17. public static String FIELD_JQL_QUERY = "FIELD_JQL_QUERY"
  18. public static String FIELD_PREVIEW_ISSUE = "FIELD_PREVIEW_ISSUE"
  19. Category log = Category.getInstance(JqlQueryCondition.class)
  20. def componentManager = ComponentManager.getInstance()
  21. def issueManager = componentManager.getIssueManager()
  22. def searchService = componentManager.getSearchService()
  23. def jqlQueryParser = ComponentManager.getComponentInstanceOfType(JqlQueryParser.class) as JqlQueryParser
  24. String getName() {
  25. "Allows the transition if this query matches a JQL query"
  26. }
  27. public String getHelpUrl() {
  28. "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-JQLQueryCondition"
  29. }
  30. String getDescription() {
  31. "Only allows the transition if this query would be returned when running a JQL query"
  32. }
  33. List getCategories() {
  34. ["ADMIN", "Condition"]
  35. }
  36. List getParameters(Map params) {
  37. [
  38. [
  39. Label: "JQL Query",
  40. Name: FIELD_JQL_QUERY,
  41. Description: "JQL query to test the issue with. For example \"issuetype = Task\""
  42. ],
  43. [
  44. Name:FIELD_PREVIEW_ISSUE,
  45. Label:"Preview Issue Key",
  46. Description:"Issue key for previewing the condition with. <b>Note: Preview only available when testing from the Admin panel</b>.",
  47. ],
  48. ]
  49. }
  50. ErrorCollection doValidate(Map params, boolean forPreview) {
  51. ErrorCollection errorCollection = new SimpleErrorCollection()
  52. String prvwIssueKey = params[FIELD_PREVIEW_ISSUE] as String
  53. String jqlQuery = params[FIELD_JQL_QUERY] as String
  54. if (forPreview) {
  55. if (! issueManager.getIssueObject(prvwIssueKey as String)) {
  56. errorCollection.addError(FIELD_PREVIEW_ISSUE, "This issue doesn't exist.")
  57. }
  58. }
  59. if (jqlQuery) {
  60. def queryErrors = validateQuery(jqlQuery)
  61. if (queryErrors.hasAnyErrors()) {
  62. errorCollection.addError(FIELD_JQL_QUERY, queryErrors.getErrorMessages().toList().first())
  63. }
  64. }
  65. errorCollection
  66. }
  67. Map doScript(Map params) {
  68. MutableIssue issue = params['issue'] as MutableIssue
  69. if (!issue) {
  70. params['output'] = "<b>Please press the Preview button<b>"
  71. }
  72. else {
  73. Boolean doIt = queryMatch(issue, params[FIELD_JQL_QUERY] as String)
  74. params['passesCondition'] = doIt
  75. }
  76. return params
  77. }
  78. String getDescription(Map params, boolean forPreview) {
  79. if (forPreview) {
  80. MutableIssue prvwIssue = issueManager.getIssueObject(params[FIELD_PREVIEW_ISSUE] as String)
  81. Boolean result = queryMatch(prvwIssue, params[FIELD_JQL_QUERY] as String)
  82. "The issue ${prvwIssue.key} " + (result ? "<b>did</b>" : "<b>did not</b>") + " match the query."
  83. }
  84. else {
  85. "The action will be allowed if the issue matches the JQL query: \"${params[FIELD_JQL_QUERY]}\""
  86. }
  87. }
  88. Boolean isFinalParamsPage(Map params) {
  89. true
  90. }
  91. public boolean queryMatch(Issue issue, String jqlQuery) {
  92. // no condition set equates to Always
  93. if (!(jqlQuery && issue)) {
  94. log.debug("No JQL or no issue")
  95. return true
  96. }
  97. def query = jqlQueryParser.parseQuery("issue = ${issue.id} and ($jqlQuery)")
  98. // todo: we're assuming the query is valid, but it might not be if say an issuetype has been removed from the system
  99. // def messageSet = searchService.validateQuery(componentManager.getJiraAuthenticationContext().getUser(), query)
  100. def rt = searchService.searchCount(componentManager.getJiraAuthenticationContext()?.getUser(), query) as Boolean
  101. log.debug("parsed query: $query returned: $rt")
  102. rt
  103. }
  104. private ErrorCollection validateQuery(String jqlCondition) {
  105. def errorCollection = new SimpleErrorCollection()
  106. try {
  107. def query = jqlQueryParser.parseQuery(jqlCondition)
  108. def messageSet = searchService.validateQuery(componentManager.getJiraAuthenticationContext().getUser(), query)
  109. messageSet.getErrorMessages().each {
  110. errorCollection.addErrorMessage(it)
  111. }
  112. }
  113. catch (Exception e) {
  114. // various exes can be thrown here
  115. errorCollection.addErrorMessage(e.cause?.message ?: e.message)
  116. }
  117. errorCollection
  118. }
  119. }