Update Priority based on two Custom FieldS (Impact/Urgency matrix)

Like https://library.adaptavist.com/entity/update-priority-based-on-a-custom-field this resolves the problem for two fields and also uses a Groovy Matrix to calculate the resulting priority.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.PriorityManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

final impactFieldName = "Impact"
final urgencyFieldName = "Urgency"

final priorityMatrix = [
    // Priority = Urgency[Impact]
    Critical: [
        Critical: 'Highest', 
        High: 'High', 
        Medium: 'High',
        Low: 'Medium'
    ],
    High: [
        Critical: 'High', 
        High: 'High', 
        Medium: 'Medium',
        Low: 'Medium'
    ],
    Medium: [
        Critical: 'High', 
        High: 'Medium', 
        Medium: 'Medium',
        Low: 'Low'
    ],
    Low: [
        Critical: 'Medium', 
        High: 'Medium', 
        Medium: 'Low',
        Low: 'Low'
    ]
]

// Get Impact and Urgency fields' values
def customFieldManager = ComponentAccessor.customFieldManager

def impactCustomField = customFieldManager.getCustomFieldObjects(issue).findByName(impactFieldName)
assert impactCustomField: "Could not find custom field with name $impactFieldName"
def impactCustomFieldVal = issue.getCustomFieldValue(impactCustomField) as LazyLoadedOption

def urgencyCustomField = customFieldManager.getCustomFieldObjects(issue).findByName(urgencyFieldName)
def urgencyCustomFieldVal = issue.getCustomFieldValue(urgencyCustomField) as LazyLoadedOption
assert urgencyCustomField: "Could not find custom field with name $urgencyFieldName"

// Calculate priority 
def priority = priorityMatrix[urgencyCustomFieldVal?.value][impactCustomFieldVal?.value]

if(priority == null) {
    log.debug "No need to change the priority"
} else {
    // Check priority existance
    def priorities = ComponentAccessor.getComponent(PriorityManager).priorities
    def priorityItem = priorities.findByName(priority)
    assert priorityItem: "The priority '$priority' doesn't exist"

    // Change issue priority
    issue.setPriorityId(priorityItem.id)
}
1 Like