How to Synchronize User Mentions in Comments Between Jira Cloud and Jira On-premise

Jira to Jira integration

This article was originally published on the Atlassian Community.

If your organization uses Jira Cloud and Jira on-premise concurrently, then syncing user mentions in comments between the two systems is challenging. 

With Exalate, it’s possible to synchronize user mentions bi-directionally, making it easier for teams to collaborate seamlessly across both Jira instances. 

In this blog post, we’ll dive in and implement this use case with Exalate. 

The Use Case

The following are the use case requirements: 

  • Synchronizing summary, description, attachments, etc between Jira Cloud and Jira on-premise. 
  • Synchronizing comments bi-directionally between Jira Cloud and Jira on-premise. This includes
    • synchronizing simple text comments.
    • synchronizing user mentions when users exist in both systems.
    • synchronizing user mentions when the user does not exist in one of the systems. In this case, a custom message along with the user mention must be synced. 

There are a few challenges to address. 

The Challenges

The real challenge is that Jira Cloud and Jira on-premise handle comments differently. 

They also have different formats internally. 

Both systems handle user mentions as follows: 

  • Jira Cloud uses the format [~accountid:account-id-string] to represent mentions. You can read more about this in the Atlassian community
  • Jira Server uses the format [~username] to represent mentions. You can read more about this here.

Considering the advanced nature of the use case, it’s recommended to use integration solutions to implement it. 

We have chosen Exalate

Exalate: A Bi-directional Integration Solution

Exalate is a fully customizable bi-directional synchronization solution. It can set up integrations for popular platforms like Jira Cloud, Jira on-premise, Azure DevOps, Zendesk, ServiceNow, Salesforce, GitHub, etc. 

The scripting engine that Exalate supports makes it an ideal solution for implementing advanced use cases. It also has an intuitive visual drag-and-drop interface having pre-built sync rules and mappings. 

It is the only decentralized integration solution in the market. This feature allows you to handle user mentions on both systems differently and independently without messing with each other’s sync. 

Let’s see how to implement this use case with Exalate.

Synchronize User Mentions Bi-directionally Between Jira Cloud and Jira On-premise

Prerequisites

You must install Exalate on both platforms: Jira Cloud and Jira on-premise.

Then create a connection between them. 

A connection works as a secure gateway to start sending information back and forth. 

You can find a lot of help online regarding this on the Exalate Academy and the Getting Started Guide.

Implementation using Exalate

After setting up the connection, you need to configure the sync rules to control what information must be sent and received at both ends. 

Then you can set up triggers to enable the automatic exchange of information. 

Jira Jira connection using Exalate

You can reach the configuration screen by clicking the “Configure Sync” button after the connection is established. Or you can even click the edit button on the connection list.

Sync rules in Jira for Exalate

The sync rules (“Rules”) tab consists of the following two sections: the Incoming sync and the Outgoing sync. 

In Jira Cloud: 

  • The “Incoming sync” denotes information send from the Jira Cloud to Jira on-premise. 
  • The “Outgoing sync” denotes how to interpret information coming from Jira on-premise.

These syncs exist on the Jira on-premise instance as well. 

Scripts to Implement the Use Case

Let’s see how to can handle the situation when information goes from Jira Cloud to on-premise. 

Jira Cloud Outgoing Sync Script

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"
	    newCommentBody = newCommentBody.replace(it[0],target)
    }
    comment.body = newCommentBody
    comment
}

A regular expression finds comments having user mentions in them. 

We replace the user mention with the email address of the corresponding user for each such comment.

To do this, we use the nodeHelper.getUser() method. So the replica sent to the on-premise instance contains the actual email address instead of the user mention. 

Note: Replica in Exalate context is the payload used to transfer the required information between the two systems.

Jira On-premise Incoming Sync Script

for(comment in replica.addedComments){
    def newCommentBody=comment.body
    def matcher  = comment.body =~ /[a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+/
	 matcher.each {
	    x->
	    if (nodeHelper.getUserByEmail(x)){
	        def target = "[~" + nodeHelper.getUserByEmail(x).username + "]"
	        newCommentBody = newCommentBody.replace(x,target)
	    }
	    else
	            newCommentBody = newCommentBody.replace(x,"[External user with email ${x} mentioned]")
	  }
    comment.body=  newCommentBody
}
issue.comments     = commentHelper.mergeComments(issue, replica)

Once the information arrives at the on-premise instance, we perform a reverse process.

We deploy a regular expression to check whether an email address is present within the comment body. 

If it is present, you can find the user account corresponding to the email address using the nodeHelper.getUserByEmail() method.

 Then replace the email address with the username. This operation ensures that the on-premise format of the user mention is taken care of. 

If the on-premise instance does not find the user, it replaces them with a custom message. This message can also state that the user mentioned on the source side does not have an associated account in the instance. 

Now we’ll consider the use case in the other direction, from Jira on-premise to Jira cloud. 

The logic remains the same; only the format of what we search for changes. 

Jira On-premise Outgoing Sync Script

replica.comments = issue.comments.collect {
    comment ->
    if (comment.roleLevel != "Developers"){
        def matcher  = comment.body =~ /[~(w+)]/
        def newCommentBody = comment.body
        matcher.each {
            target = nodeHelper.getUserByUsername(it[1])?.email ?: "Stranger"
            newCommentBody = newCommentBody.replace(it[0],target)
        }
        comment.body = newCommentBody
        comment
    }
}

A regular expression finds the comments with user mentions in them. 

We replace the user mention with the email address of the corresponding user for each comment.

To do this, we use the nodeHelper.getUserByUsername() method. 

So effectively, the comment added to the replica doesn’t contain the actual mention; but rather the email address. 

Jira Cloud Incoming Sync Script

for(comment in replica.addedComments){
    def newCommentBody=comment.body
    def matcher  = comment.body =~ /[a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9_-]+/
     matcher.each {
        x->
        if (nodeHelper.getUserByEmail(x)){
            def target = "[~accountid:" + nodeHelper.getUserByEmail(x).key + "]"
            newCommentBody = newCommentBody.replace(x,target)
        }
        else
                newCommentBody = newCommentBody.replace(x,"[External user with email ${x} mentioned]")
        }
    comment.body=  newCommentBody
}
issue.comments 	= commentHelper.mergeComments(issue, replica)

Once the replica arrives at the Jira Cloud instance, we deploy a regular expression to check whether an email address is present within the comment body.

If it does, we use the nodeHelper.getUserByEmail() method to find the corresponding email address. 

Then the email address is finally replaced by the username. 

We take care to maintain the cloud format of user mentions throughout the process

If the Jira cloud instance does not find the user, it replaces them with a custom message. This message can also state that the user mentioned on the source side does not have an associated account on this instance. 

Conclusion

Exalate provides a powerful solution for synchronizing user mentions bi-directionally between Jira Cloud and Jira on-premise. 

By following the steps outlined in this use case, you can ensure that team members can communicate and collaborate seamlessly, regardless of which Jira instance they are working in. 

If you want to do more than sync user mentions, book a demo with Exalate engineers and see it in action!

Recommended Reading:

Comments are closed.