ZD Jira: Customized comment sync + many to one mapping

Originally asked by Syed Majid Hassan on 01 September 2022 (original question)


Requirements for ZD-Jira integration:

1. Any comments arriving back from Jira must appear as Internal Notes in Zendesk
2. Only certain comments from Jira should be synced back to ZD i.e. comments starting with [ZD:]
3. The organization and Zendesk ticket number should be reflected in the Environment field in Jira.

Multiple to one sync requirements:

1. Multiple Zendesk tickets to be linked to same Jira issue.
2. The Environment field in Jira should have information of all the linked tickets.
3. Any updates from Jira should follow the general rules stated above.


Answer by Syed Majid Hassan on 01 September 2022

The code which achieves the aforementioned requirements is the following:

Send ORG info from Zendesk Expand source

def orgId = issue.organization_id
if(orgId){
  def orgName = httpClient.get("/api/v2/organizations/"+(long)orgId+".json").organization.name
  replica.customKeys."Organization" = orgName
}

Send Jira ticket num from ZD (for many to one) Expand source

replica.customFields."Jira Ticket" = issue.customFields."Jira Ticket"

Ensure that all comments received in ZD are treated as Internal Expand source

issue.comments = commentHelper.mergeComments(issue, replica, {
		  comment ->
			comment.internal = true
		    comment
		  }
		)

Send only specific comment from Jira Expand source

replica.comments       = issue.comments.findAll {it.body =~ /\[ZD:\]*/ }

Handle the many to one mapping Expand source

if(firstSync){
   issue.projectKey   = "CM" 
   issue.typeName     = nodeHelper.getIssueType(replica.type?.name, issue.projectKey)?.name ?: "Task"
      
   def remoteIssueUrn = replica.customFields."Jira Ticket"?.value
   if(remoteIssueUrn){
        def localIssue = httpClient.get("/rest/api/2/issue/"+remoteIssueUrn)
        if(localIssue == null) 
            throw new com.exalate.api.exception.IssueTrackerException("Issue with key "+remoteIssueUrn+" was not found")
        //debug.error(localIssue)
        issue.id = localIssue?.id
        issue.key = localIssue?.key
        def newData = httpClient.get("/rest/api/latest/issue/"+issue.key)

        //debug.error("${newData.fields.customfield_10092}")
        issue.customFields."Environment".value = newData.fields.customfield_10092 + "\nOrg:" + replica.customKeys."Organization" + "\t\tTicket num from ZD: " + replica.key 
        return;
    }
    issue.summary      = replica.summary
    issue.description  = replica.description
    issue.customFields."Environment".value = "Org:" + replica.customKeys."Organization" + "\t\tTicket num from ZD: " + replica.key 
}

The entire code is attached here (old community).

A demonstration of how this works can be found in the attached video:

ZD Jira many to one + comment customization.mp4 (old community)

Thanks
Majid


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.