/src/main/resources/com/onresolve/jira/groovy/canned/utils/ConditionUtils.groovy
Groovy | 144 lines | 116 code | 23 blank | 5 comment | 7 complexity | 87886adbe8dd969d789982d8db3828dc MD5 | raw file
- package com.onresolve.jira.groovy.canned.utils
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.event.issue.IssueEvent
- import com.atlassian.jira.issue.CustomFieldManager
- import com.atlassian.jira.issue.MutableIssue
- import com.atlassian.jira.issue.fields.CustomField
- import com.atlassian.jira.issue.link.IssueLinkManager
- import com.atlassian.jira.project.ProjectManager
- import com.atlassian.jira.security.roles.ProjectRoleActors
- import com.atlassian.jira.security.roles.ProjectRoleManager
- import com.onresolve.jira.groovy.CannedScriptRunner
- import javax.script.Bindings
- import javax.script.SimpleBindings
- import org.apache.log4j.Category
- import com.atlassian.crowd.embedded.api.User
-
- class ConditionUtils {
-
- public static Category log = Category.getInstance(ConditionUtils.class)
- public static String FIELD_CONDITION = "FIELD_CONDITION"
-
- public static LinkedHashMap<String, Serializable> getConditionParameter() {
- return [
- Name: FIELD_CONDITION,
- Label:"Condition",
- Type:"mediumtext",
- Description: """Enter the condition for which this function will fire. Blank will evaluate to \"true\".
- You can click one of the examples below, or see the wiki page for further examples.""",
- Examples: [
- "Priority is Major": "issue.priority?.name == \\'Major\\'",
- "Reporter is current user": "currentUser == issue.reporter",
- "Has greater than two subtasks": "issue.subTasks.size() > 2",
- "Has custom field value equal to ": "cfValues[\\'SomeCustomField\\'] == \\'Some Value\\'",
- "Has at least one outward <i>duplicate</i> link" :
- "issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains(\\'Duplicate\\')",
- "Has one or more attachments" : "attachmentManager.getAttachments(issue).size() >= 1",
- "Current user is member of Administrators role" : "isUserMemberOfRole(\\'Administrators\\')",
- "Has at least one PDF attachment" : "attachmentManager.getAttachments(issue).any{it.filename.endsWith(\\'.pdf\\')}",
- "Assignee changed (Workflow functions only)" : "issue.assignee != originalIssue.assignee",
- "Priority changed to Major (Listeners only)" : "issue.priority?.name == \\'Major\\' && changeItems.any {\\n\\tit.get(\\'field\\')==\\'priority\\'\\n}",
- "Priority changed to Major (Workflow functions only)" : "issue.priority?.name == \\'Major\\' && issue.priority != originalIssue.priority",
- ]
- ]
- }
-
- public static boolean processCondition(String condition, MutableIssue issue, boolean preview, Map scriptParams = [:]) {
- if (! condition) {
- return true
- }
-
- def gse = CannedScriptRunner.getGse()
- Bindings b = setupBinding(issue, scriptParams)
-
- try {
- def eval = gse.eval(condition, b)
- log.debug "Condition: $condition returned: " + eval
- if (! (eval instanceof Boolean)) {
- log.debug("Condition did not return a boolean, coercing to " + (eval ? "true" : "false"))
- }
- return eval as boolean
- }
- catch (Exception e) {
- log.error(e.getMessage())
- if (preview) {
- throw e
- }
- else {
- return false
- }
- }
- }
-
- public static void doAdditional(String script, MutableIssue issue, Map scriptParams = [:]) {
- if (script) {
- log.debug ("Running script: $script")
- def gse = CannedScriptRunner.getGse()
- Bindings b = setupBinding(issue, scriptParams)
- gse.eval(script, b)
- // issue.store() ?
- // todo: take cfValues and modify the issue
- }
- }
-
-
- public static Bindings setupBinding(MutableIssue issue, Map scriptParams = [:]) {
- Bindings b = new SimpleBindings()
-
- // if in a workflow put the user, otherwise put the person doing the preview
- User currentUser = WorkflowUtils.getUser(scriptParams)
- ComponentManager componentManager = ComponentManager.getInstance()
-
- b.putAll(scriptParams)
-
- def Closure isUserMemberOfRole = {String role ->
- log.debug("isUserMemberOfRole: $role" )
-
- ProjectManager projectManager = componentManager.getProjectManager()
-
-
- ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class)
- ProjectRoleActors projectRoleActors =
- projectRoleManager.getProjectRoleActors(projectRoleManager.getProjectRole(role), issue.getProjectObject())
- projectRoleActors.getUsers().contains(currentUser)
- }
-
- CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
- Map cfValues = [:]
- for (CustomField cf in customFieldManager.getCustomFieldObjects(issue)) {
- try {
- cfValues[cf.name] = issue.getCustomFieldValue(cf)
- }
- catch (Exception e) {
- // some fields seem to have a problem with this, eg LatestComment
- log.warn("Could not get value for field: ${cf.name}")
- }
- }
-
- IssueLinkManager issueLinkManager = componentManager.getIssueLinkManager()
- b.put("attachmentManager", componentManager.getAttachmentManager())
- b.put("issueLinkManager", issueLinkManager)
- b.put("customFieldManager", customFieldManager)
- b.put("issue", issue)
-
- // todo: this isn't right for listeners
- MutableIssue originalIssue = componentManager.getIssueManager().getIssueObject(issue.getId())
- b.put("originalIssue", originalIssue)
-
- b.put("isUserMemberOfRole", isUserMemberOfRole)
-
- IssueEvent event = scriptParams['event'] as IssueEvent
- if (event) {
- b.put("event", event)
- List changeItems = event?.getChangeLog()?.getRelated("ChildChangeItem")
- b.put("changeItems", changeItems)
- }
-
- b.put("currentUser", currentUser)
- b.put("cfValues", cfValues)
- b.put("log", log)
- return b
- }
-
- }