Subtask trigger

outgoing_sync.groovy
/*
** In case you don't have script runner, the same code can also be used in the outgoing sync script
** The big difference is that the connection is already available and doesn't need to be looked up
**
** Paste this at the end of the outgoing sync script
**
*/



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue


def imInternal = ComponentAccessor.issueManager
// load necessary classes
def exaPlugin = ComponentAccessor.pluginAccessor.getEnabledPlugin("com.exalate.jiranode")
def exaCl     = exaPlugin.getClassLoader()

def basicIssueKey      = exaCl.loadClass("com.exalate.basic.domain.BasicIssueKey")
def essClass           = exaCl.loadClass("com.exalate.api.replication.out.IEventSchedulerService")
def intNodeHelperClass = exaCl.loadClass("com.exalate.api.node.hubobject.v1_4.INodeHubIssueHelper")


def ess           = ComponentAccessor.getOSGiComponentInstanceOfType(essClass)
def intNodeHelper = ComponentAccessor.getOSGiComponentInstanceOfType(intNodeHelperClass) // this is not the nodeHelper of the Exalate API



def exalateNow = {
    Issue subTask ->


    def exaIssueKey = basicIssueKey.newInstance(subTask.id, subTask.key)
    log.info("Issue key available ${exaIssueKey.properties}")

    def hubIssue = intNodeHelper.getHubIssueWithInitializedCustomFieldsValues(subTask)
    log.info("hubIssue built")
    
    ess.schedulePairEvent(exaIssueKey, connection, hubIssue, null )
    log.info("Pair event scheduled")
    
}

def jIssue = imInternal.getIssueObject(issue.id as Long)
def subTasks = jIssue.subTaskObjects

subTasks.each {
    log.info("Subtask trigger - ${it.key}")
    exalateNow(it)
}

script_listener.groovy
/*
** Use this as a script listener as explained on 
**     https://community.atlassian.com/t5/Jira-articles/How-to-use-script-runner-to-automatically-exalate-subtasks/ba-p/1950068
*/


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue


// load necessary classes
def exaPlugin = ComponentAccessor.pluginAccessor.getEnabledPlugin("com.exalate.jiranode")
def exaCl     = exaPlugin.getClassLoader()

def basicIssueKey      = exaCl.loadClass("com.exalate.basic.domain.BasicIssueKey")
def essClass           = exaCl.loadClass("com.exalate.api.replication.out.IEventSchedulerService")
def ttRepoClass        = exaCl.loadClass("com.exalate.api.persistence.twintrace.ITwinTraceRepository")
def relationRepoClass  = exaCl.loadClass("com.exalate.api.persistence.relation.IRelationRepository")
def intNodeHelperClass = exaCl.loadClass("com.exalate.api.node.hubobject.v1_4.INodeHubIssueHelper")

def ttrepo        = ComponentAccessor.getOSGiComponentInstanceOfType(ttRepoClass)
def ess           = ComponentAccessor.getOSGiComponentInstanceOfType(essClass)
def relrepo       = ComponentAccessor.getOSGiComponentInstanceOfType(relationRepoClass)
def intNodeHelper = ComponentAccessor.getOSGiComponentInstanceOfType(intNodeHelperClass) // this is not the nodeHelper of the Exalate API



def exalateNow = {
    Issue issue, String connectionName ->

       
    def connection = relrepo.getConnectionByName(connectionName)
    if (connection == null) {
       throw new IllegalArgumentException("""Connection with name ${connectionName} is not found""")
    }

	log.info("Connection found")

    def exaIssueKey = basicIssueKey.newInstance(issue.id, issue.key)
    log.info("Issue key available ${exaIssueKey.properties}")

    def hubIssue = intNodeHelper.getHubIssueWithInitializedCustomFieldsValues(issue)
    log.info("hubIssue built")
    
    ess.schedulePairEvent(exaIssueKey, connection, hubIssue,null )
    log.info("Pair event scheduled")
    
}

def subTasks = event.issue.subTaskObjects

subTasks.each {
    log.info("Subtask trigger - ${it.key}")
    exalateNow(it, "auto_subtask")
}