Sync user mentions in comments between Jira Cloud and ADO

Originally asked by Sonal Otwani on 03 October 2022 (original question)


Hi,

In ADO and JIRA, we are using the @mention control to start or continue a discussion within work item discussion or any rich-text field.

We are using ADO and JIRA systems. We want to sync comments with user mentions provided in comments in both the directions.

Does Exalate support this feature?

Thanks,


Answer by Sonal Otwani on 03 October 2022

Hi,

With Exalate script mode, you can achieve this use case, the only pre-requisite is user property should be same like email id should be same in both the system.

  1. ADO to JIRA Sync:

ADO Outgoing Sync Expand source

def newComment
def allComments = workItem.comments.collect {
    comment ->
def comment1=comment.body
def matcher  = comment1 =~ /(?<=data-vss-mention="version:2.0,).*?(?=\")/
matcher.each {
 x->
def userId=nodeHelper.getUser(x,"project_key")?.email
 if (userId) 
{
   def matcher1  = comment =~ /<a href="#" data-vss-mention="version:2.0,${x}.*?<\/a>/

matcher1.each{
    y->
    comment1=comment1.replaceAll(y,"[~accountid:"+userId+"]")
  }
}
        
}
    comment.body=comment1
    comment
}
replica.comments       = nodeHelper.stripHtmlFromComments(allComments)


Here is the high level notes:

  • In ADO, user mentions are written in following HTML code:

<div><a href=“#” data-vss-mention=“version:2.0,{user id}”>@John Doe<a/> Testing mentioning</div>

  • We need to fetch the {user id} from this HTML code and using this user id, find the email id of that user
  • For that, we are using getUser method with ADO Project Key
  • Replace this whole <a> tag with following code:

[~accountid:{email id}]

Jira InComing Sync Expand source

for(comment in replica.addedComments){
    def newCommentBody=comment.body
    def matcher  = comment.body =~ /\[~accountid:([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)\]/
 matcher.each {
 x->
    def target = nodeHelper.getUserByEmail(x[1])?.key ?:x[1]
    newCommentBody = newCommentBody.replace(x[1],target)
}
 comment.body=  newCommentBody
}

def addedComments = commentHelper.mergeComments(issue, replica)


Here is the high level notes:

  • From ADO, we are receiving comment in following format:

[~accountid:{email id}]

  • We are fetching these emails using regex (of course there might be multiple mentions)
  • Using getUserByEmail method, we are fetching user account key using that email id
  • Then replacing email id with account id

2. JIRA to ADO Sync:

JIRA Outgoing Sync Expand source

String start1="#exalate_comment#"
String end1="#exalate_comment_end#"
replica.comments = issue.comments.collect {
comment ->
     
    def matcher  = comment.body =~ /\[~accountid:([\w:-]+)\]/
    def newCommentBody = comment.body
     
    matcher.each {
     target = nodeHelper.getUser(it[1])?.email ?: "Stranger"          
     target = start1+target+end1
     newCommentBody = newCommentBody.replace(it[0],target)
    }
 
    comment.body = newCommentBody
    comment
}


Here is the high level notes:

  • JIRA stores user mentions in following format:[~accountid:{account id}]
  • So we are fetching account id using the regex and for that account id we are fetching email id of the user
  • Now, at target side we need to replace Jira’s format with ADO format, to avoid other email id (may be written in plain text) we are sending User mentions between specified string
  • We are sending email ids in following format:

#exalate_comment#{Email_id}#exalate_comment_end#

  • Instead of “Stranger” we can use email id of default user

ADO Incoming Sync Expand source

String start1="#exalate_comment#"
String end1="#exalate_comment_end#"
for(comment in replica.addedComments)
{
  def matcher  = comment.body =~ /(?<=#exalate_comment#).*?(?=#exalate_comment_end#)/

 matcher.each {
 x->
   def userId=nodeHelper.getUserByEmail(x,"Project_key")?.key
   if(userId){
     def string = "<a href=\"#\" data-vss-mention=\"version:2.0,"+userId+"\"></a>"
     def test = comment.body.replaceAll(start1+ x + end1,string)
     comment.body = test
   }     
 } 
}


Here is the high level notes:

  • From JIRA we are receiving user mention information as follows:

#exalate_comment#{Email_id}#exalate_comment_end#

  • Following is the format in ADO for user mentions:

<div><a href=“#” data-vss-mention=“version:2.0,{user id}”>@John Doe<a/> Testing mentioning</div>

  • So, from each comment we are getting email id, we use getUserByEmail method to get ADO user account key corresponding to that email id
  • Then replace the format coming from JIRA with ADO user mention format

Note: Comment or remove script which is already there for comment

Thanks,

Sonal