PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/resources/com/onresolve/jira/groovy/canned/utils/ConditionUtils.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 144 lines | 116 code | 23 blank | 5 comment | 7 complexity | 87886adbe8dd969d789982d8db3828dc MD5 | raw file
  1. package com.onresolve.jira.groovy.canned.utils
  2. import com.atlassian.jira.ComponentManager
  3. import com.atlassian.jira.event.issue.IssueEvent
  4. import com.atlassian.jira.issue.CustomFieldManager
  5. import com.atlassian.jira.issue.MutableIssue
  6. import com.atlassian.jira.issue.fields.CustomField
  7. import com.atlassian.jira.issue.link.IssueLinkManager
  8. import com.atlassian.jira.project.ProjectManager
  9. import com.atlassian.jira.security.roles.ProjectRoleActors
  10. import com.atlassian.jira.security.roles.ProjectRoleManager
  11. import com.onresolve.jira.groovy.CannedScriptRunner
  12. import javax.script.Bindings
  13. import javax.script.SimpleBindings
  14. import org.apache.log4j.Category
  15. import com.atlassian.crowd.embedded.api.User
  16. class ConditionUtils {
  17. public static Category log = Category.getInstance(ConditionUtils.class)
  18. public static String FIELD_CONDITION = "FIELD_CONDITION"
  19. public static LinkedHashMap<String, Serializable> getConditionParameter() {
  20. return [
  21. Name: FIELD_CONDITION,
  22. Label:"Condition",
  23. Type:"mediumtext",
  24. Description: """Enter the condition for which this function will fire. Blank will evaluate to \"true\".
  25. You can click one of the examples below, or see the wiki page for further examples.""",
  26. Examples: [
  27. "Priority is Major": "issue.priority?.name == \\'Major\\'",
  28. "Reporter is current user": "currentUser == issue.reporter",
  29. "Has greater than two subtasks": "issue.subTasks.size() > 2",
  30. "Has custom field value equal to ": "cfValues[\\'SomeCustomField\\'] == \\'Some Value\\'",
  31. "Has at least one outward <i>duplicate</i> link" :
  32. "issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains(\\'Duplicate\\')",
  33. "Has one or more attachments" : "attachmentManager.getAttachments(issue).size() >= 1",
  34. "Current user is member of Administrators role" : "isUserMemberOfRole(\\'Administrators\\')",
  35. "Has at least one PDF attachment" : "attachmentManager.getAttachments(issue).any{it.filename.endsWith(\\'.pdf\\')}",
  36. "Assignee changed (Workflow functions only)" : "issue.assignee != originalIssue.assignee",
  37. "Priority changed to Major (Listeners only)" : "issue.priority?.name == \\'Major\\' && changeItems.any {\\n\\tit.get(\\'field\\')==\\'priority\\'\\n}",
  38. "Priority changed to Major (Workflow functions only)" : "issue.priority?.name == \\'Major\\' && issue.priority != originalIssue.priority",
  39. ]
  40. ]
  41. }
  42. public static boolean processCondition(String condition, MutableIssue issue, boolean preview, Map scriptParams = [:]) {
  43. if (! condition) {
  44. return true
  45. }
  46. def gse = CannedScriptRunner.getGse()
  47. Bindings b = setupBinding(issue, scriptParams)
  48. try {
  49. def eval = gse.eval(condition, b)
  50. log.debug "Condition: $condition returned: " + eval
  51. if (! (eval instanceof Boolean)) {
  52. log.debug("Condition did not return a boolean, coercing to " + (eval ? "true" : "false"))
  53. }
  54. return eval as boolean
  55. }
  56. catch (Exception e) {
  57. log.error(e.getMessage())
  58. if (preview) {
  59. throw e
  60. }
  61. else {
  62. return false
  63. }
  64. }
  65. }
  66. public static void doAdditional(String script, MutableIssue issue, Map scriptParams = [:]) {
  67. if (script) {
  68. log.debug ("Running script: $script")
  69. def gse = CannedScriptRunner.getGse()
  70. Bindings b = setupBinding(issue, scriptParams)
  71. gse.eval(script, b)
  72. // issue.store() ?
  73. // todo: take cfValues and modify the issue
  74. }
  75. }
  76. public static Bindings setupBinding(MutableIssue issue, Map scriptParams = [:]) {
  77. Bindings b = new SimpleBindings()
  78. // if in a workflow put the user, otherwise put the person doing the preview
  79. User currentUser = WorkflowUtils.getUser(scriptParams)
  80. ComponentManager componentManager = ComponentManager.getInstance()
  81. b.putAll(scriptParams)
  82. def Closure isUserMemberOfRole = {String role ->
  83. log.debug("isUserMemberOfRole: $role" )
  84. ProjectManager projectManager = componentManager.getProjectManager()
  85. ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class)
  86. ProjectRoleActors projectRoleActors =
  87. projectRoleManager.getProjectRoleActors(projectRoleManager.getProjectRole(role), issue.getProjectObject())
  88. projectRoleActors.getUsers().contains(currentUser)
  89. }
  90. CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
  91. Map cfValues = [:]
  92. for (CustomField cf in customFieldManager.getCustomFieldObjects(issue)) {
  93. try {
  94. cfValues[cf.name] = issue.getCustomFieldValue(cf)
  95. }
  96. catch (Exception e) {
  97. // some fields seem to have a problem with this, eg LatestComment
  98. log.warn("Could not get value for field: ${cf.name}")
  99. }
  100. }
  101. IssueLinkManager issueLinkManager = componentManager.getIssueLinkManager()
  102. b.put("attachmentManager", componentManager.getAttachmentManager())
  103. b.put("issueLinkManager", issueLinkManager)
  104. b.put("customFieldManager", customFieldManager)
  105. b.put("issue", issue)
  106. // todo: this isn't right for listeners
  107. MutableIssue originalIssue = componentManager.getIssueManager().getIssueObject(issue.getId())
  108. b.put("originalIssue", originalIssue)
  109. b.put("isUserMemberOfRole", isUserMemberOfRole)
  110. IssueEvent event = scriptParams['event'] as IssueEvent
  111. if (event) {
  112. b.put("event", event)
  113. List changeItems = event?.getChangeLog()?.getRelated("ChildChangeItem")
  114. b.put("changeItems", changeItems)
  115. }
  116. b.put("currentUser", currentUser)
  117. b.put("cfValues", cfValues)
  118. b.put("log", log)
  119. return b
  120. }
  121. }