Incoming_sync_for_exisiting_issues_JIRA.groovy
issue.summary      = replica.summary
issue.description  = replica.description
issue.labels       = replica.labels
//issue.comments     = commentHelper.mergeComments(issue, replica)
issue.comments  = commentHelper.mergeComments(
        issue, replica, {
    comment ->
        comment.body = replica.customKeys."BG_USER_27" +replica.key+", "+comment.body
}
)
issue.attachments  = attachmentHelper.mergeAttachments(issue, replica)
issue.customFields."ALM - Defect Id".value = replica.key



def VWR_Project = nodeHelper.getOption(issue, "VWR Project",replica.customFields."VWR Project"?.value )
issue.customFields."VWR Project"?.value = VWR_Project

if (replica.customKeys."Business Justification Details" != null)
{

    issue.customFields."Business Justification Details".value= replica.customKeys."Business Justification Details".replaceAll("<[^>]*>", "")
}

if (replica.customKeys."Proof of User Test" != null)
{
    issue.customFields."Proof of User Test".value = replica.customKeys."Proof of User Test".replaceAll("<[^>]*>", "")
}

Status.receive(
        true,
        [
                "Open": "Requested",
                "Approved" : "Approved",
        ],
        [:]
)
Incoming_sync_rules_JIRA.groovy
issue.projectKey   = "DS"
issue.typeName     = "App Dev - Enhancement or Bug"
issue.customFields."Type of Request".value = nodeHelper.getOption(issue, "Type of Request","Bug")
issue.summary      = replica.summary
issue.description  = replica.description
issue.labels       = replica.labels
issue.comments     = commentHelper.mergeComments(issue, replica)
issue.attachments  = attachmentHelper.mergeAttachments(issue, replica)

issue.customFields."ALM - Defect Id".value = replica.key

issue.reporter     = nodeHelper.getUserByUsername(replica.reporter?.username)
issue.assignee = nodeHelper.getUserByUsername(replica.assignee?.username)
issue.customFields."Requestor".value = replica.customFields."Requestor".value.collect{nodeHelper.getUserByUsername(replica.customFields."Requestor"?.value)}

def defaultPriority = nodeHelper.getPriority("Low")
issue.priority = 	nodeHelper.getPriority(replica.priority?.name) ?: defaultPriority

issue.customFields?."Requirements"?.value = replica.customKeys?."Requirements"

def Who_Will_Benefit = nodeHelper.getOption(issue, "Who will benefit",replica.customFields."Who will benefit"?.value )
issue.customFields."Who will benefit"?.value = Who_Will_Benefit

def ES_Region_that_benefits = nodeHelper.getOption(issue, "ES-Region that benefits",replica.customFields."ES-Region that benefits"?.value )
issue.customFields."ES-Region that benefits"?.value = ES_Region_that_benefits

def Dept_That_Benefits = nodeHelper.getOption(issue, "Department that Benefits",replica.customFields."Department that Benefits"?.value )
issue.customFields."Department that Benefits"?.value = Dept_That_Benefits

issue.customFields."Benefit Value (USD)"?.value = replica.customKeys."BG_USER_04" as Double

def Business_Driver = nodeHelper.getOption(issue, "Business Driver",replica.customFields."Business Driver"?.value )
issue.customFields."Business Driver"?.value = Business_Driver

def business_Justification = nodeHelper.getOption(issue, "Business Justification",replica.customFields."Business Justification"?.value )
issue.customFields."Business Justification"?.value = business_Justification

def VWR_Project = nodeHelper.getOption(issue, "VWR Project",replica.customFields."VWR Project"?.value )
issue.customFields."VWR Project"?.value = VWR_Project

//START SYSTEM/AREA/SUBAREA
def getMultiLevelCascadeValue = { String jiraCfName, String remoteFirstLevelCfName, String remoteSecondLevelCfName, String remoteThirdLevelCfName ->

    def _remoteFirstLevel = (replica.customFields[remoteFirstLevelCfName]?.value as String)?.trim();

    def _remoteSecondLevel = (replica.customFields[remoteSecondLevelCfName]?.value as String)?.trim();

    def _remoteThirdLevel = (replica.customFields[remoteThirdLevelCfName]?.value as String)?.trim();

    def _cfm = com.atlassian.jira.component.ComponentAccessor.customFieldManager;

    def _om = com.atlassian.jira.component.ComponentAccessor.optionsManager;

    def _cf = _cfm.getCustomFieldObject(issue.customFields[jiraCfName]?.id as Long);

    def _options = _om.getOptions(_cf.getRelevantConfig(new com.atlassian.jira.issue.context.IssueContextImpl(
            (issue.project?.id ?: nodeHelper.getProject(issue.projectKey)?.id) as Long,
            (issue.type?.id ?: nodeHelper.getIssueType(issue.typeName)?.id) as String
    )));

    def _firstLevelOpt = _options.find { option -> option.value?.trim()?.equalsIgnoreCase(_remoteFirstLevel) };

    def _secondLevelOpt = _firstLevelOpt?.childOptions?.find { option -> option.value?.trim()?.equalsIgnoreCase(_remoteSecondLevel) };

    def _thirdLevelOpt = _secondLevelOpt?.childOptions?.find { option -> option.value?.trim()?.equalsIgnoreCase(_remoteThirdLevel) };

    return [_firstLevelOpt, _secondLevelOpt, _thirdLevelOpt].findAll();
}

issue.customFields."System/Area/Sub Area".value = getMultiLevelCascadeValue("System/Area/Sub Area", "System", "Area", "Sub Area")

//END SYSTEM/AREA/SUBAREA

//START REGION/COUNTRY
def getExCustomFieldOption = { Long jCfId, String v, Closure findOptFn ->
    def cfm = com.atlassian.jira.component.ComponentAccessor.customFieldManager
    def om = com.atlassian.jira.component.ComponentAccessor.optionsManager
    final def nhocs = nodeHelper.nodeHubObjectConverter
    def issueContext = new com.atlassian.jira.issue.context.IssueContextImpl(
            (issue.project?.id ?: nodeHelper.getProject(issue.projectKey)?.id) as Long,
            (issue.type?.id ?: nodeHelper.getIssueType(issue.typeName, issue.projectKey ?: issue.project?.key)?.id) as String
    )
    def jCf = cfm.getCustomFieldObject(jCfId)
    def fieldConfig = jCf.getRelevantConfig(issueContext)
    def allOptions = om.getOptions(fieldConfig)

    def jOpt = findOptFn(allOptions, v)
    if (jOpt != null){
        nhocs.getHubOption(jOpt)
    } else {
        null
    }
}
def parentOpt = getExCustomFieldOption(issue.customFields."Region/Country"?.id as Long, replica.customFields."BG_USER_13"?.value?.value as String, { opts, v ->
    def jOptMaps = opts.rootOptions.collect { o ->
        [
                "found"           : o.value?.trim()?.equalsIgnoreCase(v?.trim()),
                "`o.value`"       : o.value,
                "`v`"             : v,
                "`o.value?.class`": o.value?.class,
                "`v?.class`"      : v?.class,
                "opt"             : o
        ]
    }
    jOptMaps.find { x -> x.found }?.opt
})
def childOpt = getExCustomFieldOption(issue.customFields."Region/Country"?.id as Long, replica.customFields."BG_USER_15"?.value?.value as String, { opts, v ->
    def jOptMaps = parentOpt ? opts.getOptionById(parentOpt.id)?.childOptions?.collect { o -> [
            "found":o.value?.trim()?.equalsIgnoreCase(v?.trim()),
            "`o.value`":o.value,
            "`v`": v,
            "`o.value?.class`":o.value?.class,
            "`v?.class`":v?.class,
            "opt":o
    ] } : null;
    jOptMaps?.find {x -> x.found}?.opt
})
if (replica.customFields."BG_USER_15"?.value?.value == null) {
    issue.customFields."Region/Country"?.value = null
} else {
    if (parentOpt == null) {
        throw new com.exalate.api.exception.IssueTrackerException("""Failed to find the proper Region for remote region `${replica.customFields."BG_USER_15"?.value?.value}`. Please adapt your script.""".toString())
    }

    issue.customFields."Region/Country"?.value = nodeHelper.getCascadingSelect(
            parentOpt,
            childOpt
    )
}
//END REGION/COUNTRY

Status.receive(
        true,
        [
                "Open": "Requested",
        ],
        [:]
)