Setting Issue Security Level Based on Parent Component

Using ScriptRunner and Subcomponents feature you can set issue security level based on parent components. Even if you have hundreds of subcomponents you can define a mapping between top level components and security level which is a lot easier to maintain. Assuming you have component hierarchy on the right. Following script updates issue's security level based on the mapping defined on the lines 21-24 of the script.



import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import org.apache.log4j.Logger
import org.apache.log4j.Level

import com.deniz.jira.versioning.subcomponents.SubcomponentsService;
import com.deniz.jira.versioning.subcomponents.ParentComponentHierarchy;
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;

@WithPlugin("com.deniz.jira.versioning")
@PluginModule
SubcomponentsService subcomponentsService;

def log = Logger.getLogger("com.acme.SetSecurity")
log.setLevel(Level.INFO)
def jac = ComponentAccessor.getJiraAuthenticationContext();
def im = ComponentAccessor.getIssueManager();
//map of component/virtual component name to security level
def parentComponentToSecurityLevelMap = [
        "Functional Modules": 10000L,
        "Mobile Apps": 10001L,
        "Database Support": 10002L,
        "Other": 10003L
];
//def issue = im.getIssueObject("ERP-2"); //JUST FOR TESTING
//Retrieve list of components set
def allComponents = issue.getComponentObjects();
def int project_match = 0;

ParentComponentHierarchy.metaClass.isParentComponent = {String componentName ->
    return delegate.componentName.equals(componentName) || (delegate.child != null && delegate.child.isParentComponent(componentName));
}

// Check if a component is chosen.
for (int i = 0; i < allComponents.size(); i++) {
    String checkedComponentName = allComponents[i].getName();
    Long checkedComponentId = allComponents[i].getId();
    log.info("Checking:" + checkedComponentName);
    ParentComponentHierarchy parentComponentHierarchy = subcomponentsService.getParentComponentHierarchy(checkedComponentId);
    log.info("parent:" + parentComponentHierarchy)
    def securityMapping = parentComponentToSecurityLevelMap.find { mapping ->
        return parentComponentHierarchy.isParentComponent(mapping.key);
    }
    log.info("security mapping:" + securityMapping);
    if (securityMapping != null) {
        log.info("Applying security level:" + securityMapping.key)
        issue.setSecurityLevelId(securityMapping.value);
        im.updateIssue(jac.getUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);
        project_match = 1;
        break;
    }
}

if (!project_match) {
    //log.info("No project match, assigning to Other with number "+parentComponentToSecurityLevelMap.get('Other'))
    issue.setSecurityLevelId(parentComponentToSecurityLevelMap.get('Other'));
    im.updateIssue(jac.getUser(), issue, EventDispatchOption.ISSUE_UPDATED, false);
    log.info("NO Project Match")
}