JIRA SERVER

Challenge

Whenever you sync an issue from JIRA A to JIRA B, changes are high that the issue key on the remote issue is different from the local one (unless you implemented Is it possible to keep issuekey same on both sides?

What you would like to achieve is that once that the JIRA A issue is exalated, you can store the remote issue key locally in a custom field 'Remote  Issuekey'

Solution

For this solution you will require an add-on like Script Runner for JIra to capture Exalate events that JIRANode emits:  these are events when an issue has been Exalated (but there are a couple others)

  • Step 1: Customfield 'Remote Issuekey'
    You create a 'Text field - read only' with the name 'Remote Issuekey' and ensure it is visible on the appropriate screens and contexts
  • Step 2: Add a file 'ExalateListener.groovy' to your <jira-home>/scripts directory containing the script listener code below
  • Step 3: You create a custom script listener as explained here

    it should look like



  • Step 4: Validate by exalating an issue and see that the customfield is updated

Script Listener code (script file version)

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventTypeManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class ExalateListener extends AbstractIssueEventListener {

    private final Logger log = LoggerFactory.getLogger("com.exalate.custom")

    private static final String EXALATED_EVENT_TYPE = "com.exalate.api.domain.trigger.EXALATED"
    private static final String EXALATE_UPDATED_EVENT_TYPE = "com.exalate.api.domain.trigger.UPDATED"
    private static final String CUSTOMFIELDNAME = "Remote Issuekey"
    private static final EventTypeManager eventTypeManager  = ComponentAccessor.getEventTypeManager()

    @Override
    void customEvent(IssueEvent event) {
        Issue issue = event.issue
        EventType eventType = eventTypeManager.getEventType(event.getEventTypeId())
        log.debug("EXALATED event caught for issue ${issue.key} - referring remote issue " + event.params.get("REMOTE_ISSUE_URN"))


        def changeHolder = new DefaultIssueChangeHolder()
        def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(CUSTOMFIELDNAME)
        cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), event.params.get("REMOTE_ISSUE_URN")), changeHolder)
        log.debug "Field Remote Issuekey updated"
    }
}


Script Listener code (Inline version)

For the inline version, you don't need to declare a class, nor define a customEvent method. 


The code is

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventTypeManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
 
import org.slf4j.Logger
import org.slf4j.LoggerFactory
 
 
Logger log = LoggerFactory.getLogger("com.exalate.custom")
 
String EXALATED_EVENT_TYPE = "com.exalate.api.domain.trigger.EXALATED"
String EXALATE_UPDATED_EVENT_TYPE = "com.exalate.api.domain.trigger.UPDATED"
String CUSTOMFIELDNAME = "Remote Issueeyk"

EventTypeManager eventTypeManager  = ComponentAccessor.getEventTypeManager()
 
 Issue issue = event.issue
EventType eventType = eventTypeManager.getEventType(event.getEventTypeId())
log.debug("EXALATED event caught for issue ${issue.key} - referring remote issue " + event.params.get("REMOTE_ISSUE_URN"))
 
 
def changeHolder = new DefaultIssueChangeHolder()
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(CUSTOMFIELDNAME)
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), event.params.get("REMOTE_ISSUE_URN")), changeHolder)

 

See Also