How to sync comments in jira cloud with impersonate user and with role level?

Originally asked by Sonal Otwani on 17 June 2024 (original question)


Hi,

I wanted to set executor of the comment which is working fine using the script described here:
https://docs.exalate.com/docs/how-to-impersonate-a-comment-in-jira-cloud

Now if i am setting the role level using following snippet:

replica.addedComments.each { 
    it.executor = nodeHelper.getUserByEmail("abc@demo.com")
    it.roleLevel="administrators"
    it
 }
issue.comments     = commentHelper.mergeComments(issue, replica)

Then it is not setting the role level. So what can i achieve both the things to gather ?

Thanks,

Sonal


Answer by Sonal Otwani on 17 June 2024

Hi,

Here the script to achieve this:

issue.summary      = replica.summary
issue.description  = replica.description
.....
store(issue)
def await = { f -> scala.concurrent.Await$.MODULE$.result(f, scala.concurrent.duration.Duration$.MODULE$.Inf()) }
nodeHelper.jCloudClient.generalSettingsService
def orNull = { scala.Option<?> opt -> opt.isDefined() ? opt.get() : null }
def ec = nodeHelper.jCloudClient.ec
def gsOptFuture = nodeHelper.jCloudClient.generalSettingsService.get()
def gs = orNull(await(gsOptFuture))
//getOAuthHeaderValue(generalSettings: JCloudGeneralSettings, accountId: String)(implicit ex: ExecutionContext)


def jiraUrl = gs.issueTrackerUrl+"/rest/api/3/issue/${issue.key}/comment"
def url = new URL(jiraUrl)
def http = url.openConnection()


replica.addedComments.each { 

//def email=it.author?.email==null ? "<default user email id>" : it.author?.email
def email= "abc@demo.com"
def accountId = nodeHelper.getUserByEmail(email)?.key
def authHeaderValueFuture = nodeHelper.jCloudClient.getOAuthHeaderValue(gs, accountId,ec)
def authHeaderValue = await(authHeaderValueFuture)

http.setRequestProperty("Authorization",authHeaderValue)
http.setRequestProperty("Content-Type", "application/json")
http.setDoOutput(true)
http.setRequestMethod('POST')  // Change to PUT if needed


def json = new groovy.json.JsonBuilder([

  body: [
    content: [
      [
        content: [
          [
            text:it.body,
            type: "text"
          ]
        ],
        type: "paragraph"
      ]
    ],
    type: "doc",
    version: 1
    
  ],
  visibility: [
                type: "role",
                value: "Administrators"
    ]



]).toString()

// Send the request
def outputStream = http.getOutputStream()
outputStream.write(json.bytes)
outputStream.close()

// Get the response
def responseCode = http.getResponseCode()
//debug.error("Response Code: $responseCode")
if (responseCode >= 300) {
   def response=http.getErrorStream().text
    debug.error("Error while creating comment : $response")
}
else{
    def js = new groovy.json.JsonSlurper()
    def jsonRes = js.parseText(http.getInputStream().text)
                    def localCommentId = jsonRes.id
if(localCommentId){
                    def trace = new com.exalate.basic.domain.BasicNonPersistentTrace()
                            .setType(com.exalate.api.domain.twintrace.TraceType.COMMENT)
                            .setToSynchronize(true)
                            .setLocalId(localCommentId as String)
                            .setRemoteId(it.remoteId as String)
                            .setAction(com.exalate.api.domain.twintrace.TraceAction.NONE)
                    traces.add(trace)
}

   
} 

 }
 return new scala.Tuple2(issueKey, scala.collection.JavaConverters.asScalaBuffer(traces))