Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • The Lookup Manager gets the Component Table to search the Component column for the value Internal. If there is a matching row, return the value in the Custom_Select column.

  • Then set the value to custom field Custom_Select on the created issue. 

Code Block
languagegroovy
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.akelesconsulting.jira.plugins.rest.LookupService
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.project.Project
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonSlurper

@WithPlugin("com.akelesconsulting.jira.plugins.LookupManager")

@PluginModule
LookupService lookupService

def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

log.debug("***** Populate a custom field based on looking up Component in a table managed by Lookup Manager *****")

def components = issue.getComponentObjects();
log.debug ( issue.getComponentObjects()*.name ) //print out the name of each of the components

if (components) {
    
    def tableName = "Component Table"
    def sourceCol = "Component"
    def sourceVal = components[0].name  //E.g. "Internal"
    def destCol = "Field_1"

    int tableId = lookupService.getTableIdByName(tableName) //tableId = 1
    def matchingValuesArray = lookupService.lookup(tableId, sourceCol, sourceVal, destCol) // matchingValuesArray = [LOOKUP]
    def value = matchingValuesArray[0]
    log.debug("Matching Vallue: " + value)

    def customFieldName = "Custom_Select" //Custom Select Field
    def cfSelect = customFieldManager.getCustomFieldObjectByName(customFieldName)
    assert cfSelect: "Could not find custom field with name $customFieldName"

    def availableOptions = ComponentAccessor.optionsManager.getOptions(cfSelect.getRelevantConfig(issue))
    def optionToSet = availableOptions.find {it.value == value}
    assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}"

    issue.setCustomFieldValue(cfSelect, optionToSet)
    
    issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

...

  • The Lookup Manager gets the Component Table to search the Category column for the value INTERNAL. If there is a matching row, return the value in the Component column.

  • Then set the value to Component fieldon the created issue CS-2

Code Block
languagegroovy
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.akelesconsulting.jira.plugins.rest.LookupService
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.project.Project
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
import groovy.json.JsonSlurper

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.util.collect.MapBuilder;
import java.util.List;
import org.ofbiz.core.entity.GenericValue;
import org.ofbiz.core.entity.GenericEntityException;

@WithPlugin("com.akelesconsulting.jira.plugins.LookupManager")

@PluginModule
LookupService lookupService

def log = Logger.getLogger("LookupManager")
log.setLevel(Level.DEBUG)

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

log.debug("***** Set the Component value based on looking up another custom field in a table managed by Lookup Manager *****")

def customFieldName = "Category" //Custom Select Field
def cfSelect = customFieldManager.getCustomFieldObjectByName(customFieldName)
assert cfSelect: "Could not find custom field with name $customFieldName"

if (issue.getCustomFieldValue(cfSelect)) {
    
    def tableName = "Component Table"
    def sourceCol = "Category"
    def sourceVal = issue.getCustomFieldValue(cfSelect).getValue() // INTERNAL
    def destCol = "Component"

    def tableId = lookupService.getTableIdByName(tableName) //tableId = 1
    def matchingValuesArray = lookupService.lookup(tableId, sourceCol, sourceVal, destCol) // matchingValuesArray = [Internal]
    def value = matchingValuesArray[0]
    log.debug("Matching Vallue: " + value)

    def project = issue.getProjectObject()
    def projectComponentManager = ComponentAccessor.getProjectComponentManager()
    def component = projectComponentManager.findByComponentName(project.getId(), value)
    assert component: "Could not find component name $value"

    issue.setComponent([component])
    
    //Update Assignee field
    def componentType = component.getAssigneeType()

    def assignee = null
    switch (componentType) { //0 = Project Default, 3 = Component Lead,  2 = Project Lead, 3 = Unassigned,
        case 0:
            def projectDefault = issue.getProjectObject().getAssigneeType()  //2 = Project Lead, 3 = Unassigned
            if (projectDefault == 2) {
                assignee = issue.getProjectObject().getProjectLead()
            }
            break
        case 1:
            assignee = component.getComponentLead()
            break
        case 2:
            assignee = issue.getProjectObject().getProjectLead()
            break
        case 3:
            assignee = null
    }
    log.debug("Assignee: " + assignee)
    issue.setAssignee(assignee)
    
    issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

...

The custom script runner post-function should be placed after the Create the issue originally transition.

...