Example of code of an Connection operation in a JIRA incomoing script

Hi There,
i have an exalate connection in place between Salesforce and a JIRA DC, and any new opportunity created in SF is correctly replicated to JIRA as a ticket creation.
We have customized the JIRA incoming script to manage some data conversion (dates, users, …) , and so on, this works well.
In some cases, it happens that a new or updated SF opportunity has already a correspondance in JIRA, but is not yet sync. to it. In such a case, i would like in the incoming script to perform a connect operation, instead of an exalate operation. I have a JIRA custom field to identify the correspondance, and i am able to verify it in the incoming, thanks to an external API call running a JQL.
However, i cannot find any code example to perfom the connect operation itself, could you help ?
Thanks a lot, and kind regards
Laurent

Hi,

I will not be dwelling on how you get to the issue in Jira i.e. when the SF opportunity reaches the Jira Incoming script, you are able to identify the Jira ticket that you need to connect to. In the following script, I have hard coded that Jira key to MAJ-23. The following is the full firstSync block I used on the Jira OnPrem side:

import com.atlassian.jira.component.ComponentAccessor
 
if(firstSync){
        def issueManager = ComponentAccessor.issueManager
        def mainIssue = issueManager.getIssueByCurrentKey("MAJ-23")
        def issueKey = new com.exalate.basic.domain.BasicIssueKey(mainIssue.id, mainIssue.key)
        def hubObjVersion = new com.exalate.basic.domain.BasicSemanticVersion(1, 0, 0);
        def hubObjectHelperFactory = com.atlassian.jira.component.ComponentAccessor
          .getOSGiComponentInstanceOfType(com.exalate.api.hubobject.IHubObjectHelperFactory.class)
        def hubObjectHelper = hubObjectHelperFactory.get(hubObjVersion)
        def hubIssue = hubObjectHelper.getHubIssueOnOutgoingProcessor(issueKey)
        hubIssue.getProperties().each { k, v ->
            issueBeforeScript[k] = v
            if (v instanceof Map) {
                def newV = [:]
                newV.putAll(v)
                issue[k] = newV
            } else {
                issue[k] = v
            }
        }
        def iterator = hubIssue.entrySet().iterator()
        while (iterator.hasNext()) {
            def entry = iterator.next()
            def k = entry.key
            def v = entry.value
            // Update issueBeforeScript and issue
            issueBeforeScript.put(k, v)
            issue.put(k, v)
        }
        issueBeforeScript.id = null
        issue.id = mainIssue.id
        issue.key = mainIssue.key
        issue.customKeys.somethingIsChanged = true
     
          //debug.error("linked!")
          //def fakeTraces = com.exalate.util.TraceUtils.indexFakeTraces(traces);
          //def hubIssueAfterScripts = hubObjectHelper.prepareLocalHubIssueForApplication(hubIssue, issue, fakeTraces);
          //debug.error("hubIssue.getProperties()=${hubIssue.getProperties().keySet()} hubIssueAfterScripts.removedComments=${hubIssueAfterScripts.removedComments}")
     
          // take care of comments, attahchments and workLogs:
          replica.comments.each { replicaComment ->
          // Check if the comment already exists in the local issue based on author email, creation date, and content
          def existingComment = issue.comments.find { 
                // Import tool ideally should manage the dates and the authors so we should compare them
                //it.created?.time == replicaComment.created?.time &&
                it.body == replicaComment.body
          }
          //debug.error("#comments replica=${replica.comments.collect { ["created":it.created?.time, "body":it.body, "author":it.author?.email] }.join("; ")} issue=${issue.comments.collect { ["created":it.created?.time, "body":it.body, "author":it.author?.email] }.join("; ")}")
          if (existingComment) {
                //debug.error("#comments, creating a trace between: remoteId=${replicaComment.remoteId as String} and localId=${existingComment.id as String}")
                def trace = new com.exalate.basic.domain.BasicNonPersistentTrace()
                .setType(com.exalate.api.domain.twintrace.TraceType.COMMENT)
                .setToSynchronize(true)
                .setLocalId(existingComment.id as String) 
                .setRemoteId(replicaComment.remoteId as String)
                .setAction(com.exalate.api.domain.twintrace.TraceAction.NONE)
                traces.add(trace)
          }
          }
          replica.attachments.each { replicaAttachment ->
     
            def existingAttachment = issue.attachments.find {
                  it.filename == replicaAttachment.filename &&
                  it.filesize == replicaAttachment.filesize// &&
                  //it.created?.time == replicaAttachment.created?.time
            }
      
            if (existingAttachment) {
                  def trace = new com.exalate.basic.domain.BasicNonPersistentTrace()
                     .setType(com.exalate.api.domain.twintrace.TraceType.ATTACHMENT)
                     .setToSynchronize(true)
                     .setLocalId(existingAttachment.id as String) 
                     .setRemoteId(replicaAttachment.remoteId as String)
                     .setAction(com.exalate.api.domain.twintrace.TraceAction.NONE)
                  traces.add(trace)
            }
          }
            syncHelper.syncBackAfterProcessing() 
      
            return issueKey
        
}

I just quickly tested this and here is a short video of this in action:

Hope it helps.

Thanks
Majid