JIRA CLOUD

Recently we had a customer with a request to synchronize JIRA software custom field (single select drop-down list) with JIRA Service Desk Organizations field.

Please use scripts below to sync custom field to Service Desk field. 

Sending side

Data Filter

replica.customFields."Custom field name" = issue.customFields."Custom field name"
 

Receiving side

Create Processor

import scala.concurrent.Await$
import scala.concurrent.duration.Duration$
import play.api.libs.ws.WSResponse
import groovy.json.JsonOutput
import scala.Option$
import com.exalate.api.exception.IssueTrackerException

def slurper = new groovy.json.JsonSlurper() //JSON parser
def await = { future -> Await$.MODULE$.result(future, Duration$.MODULE$.Inf()) }

def getOrganization = { name ->
    if (name == null) {
        return null
    }

    def request = httpClient.thisJira("/rest/servicedeskapi/organization", "GET", null, null)
    WSResponse response = await(request.execute())
    if(response.status() != 200) {
        throw new IssueTrackerException("Please review the connection script - Failed to get organizations via Atlassian REST API: \n" +
                "Request: "+ request.method() +" "+ request.url() +"\n" +
                "Response: status: "+ response.status() + " body: "+ response.body()
        )
    }
    def organizationsJsonStr = response.body()

    def organizationsJson
    try {
        organizationsJson = slurper.parseText(organizationsJsonStr)
    } catch (e) {
        throw new IssueTrackerException("Please review the connection script - Failed to read organizations text received from Atlassian REST API to proper JSON: \n" +
                "Organizations text: "+ response.body()+"\n" +
                "Error: "+ e?.message,
                e
        )
    }
    def orgId = organizationsJson?.values?.findAll{ org -> name.equalsIgnoreCase(org?.name) }?.find{it}?.id
    if (orgId == null) {
        throw new IssueTrackerException("Please create organization "+ name +" " +
                "or review the connection script - " +
                "Failed to find organization by name "+ name +", " +
                "available organizations: "+ organizationsJson?.values?.collect { org -> org?.name })
    }
    orgId as Long
}

def newOrgFromRemote = getOrganization(replica.customFields."Custom field name"?.value?.value)
if (newOrgFromRemote != null) {
  issue.customFields."Organizations".value = [newOrgFromRemote]
}
 

Change processor

import scala.concurrent.Await$
import scala.concurrent.duration.Duration$
import play.api.libs.ws.WSResponse
import groovy.json.JsonOutput
import scala.Option$
import com.exalate.api.exception.IssueTrackerException

def slurper = new groovy.json.JsonSlurper() //JSON parser
def await = { future -> Await$.MODULE$.result(future, Duration$.MODULE$.Inf()) }

def getOrganization = { name ->
    if (name == null) {
        return null
    }

    def request = httpClient.thisJira("/rest/servicedeskapi/organization", "GET", null, null)
    WSResponse response = await(request.execute())
    if(response.status() != 200) {
        throw new IssueTrackerException("Please review the connection script - Failed to get organizations via Atlassian REST API: \n" +
                "Request: "+ request.method() +" "+ request.url() +"\n" +
                "Response: status: "+ response.status() + " body: "+ response.body()
        )
    }
    def organizationsJsonStr = response.body()

    def organizationsJson
    try {
        organizationsJson = slurper.parseText(organizationsJsonStr)
    } catch (e) {
        throw new IssueTrackerException("Please review the connection script - Failed to read organizations text received from Atlassian REST API to proper JSON: \n" +
                "Organizations text: "+ response.body()+"\n" +
                "Error: "+ e?.message,
                e
        )
    }
    def orgId = organizationsJson?.values?.findAll{ org -> name.equalsIgnoreCase(org?.name) }?.find{it}?.id
    if (orgId == null) {
        throw new IssueTrackerException("Please create organization "+ name +" " +
                "or review the connection script - " +
                "Failed to find organization by name "+ name +", " +
                "available organizations: "+ organizationsJson?.values?.collect { org -> org?.name })
    }
    orgId as Long
}

def newOrgFromRemote = getOrganization(replica.customFields."Custom field name"?.value?.value)
if (newOrgFromRemote != null) {
  issue.customFields."Organizations".value = [newOrgFromRemote]
}

See Also