How to Sync Watchers Field in Jira On-premise

    Watchers field is one of the standard fields of an issue. You can synchronize it as any other issue fields.

    This article provides description and scripts that help you to synchronize issue watchers and create new users if they do not exist on the destination side.

    Exalate has a helper method to create users with no permissions. Use this method to sync the watchers list.

    More details about watchers issue field.

    Source Side

     Outgoing sync

    Send watchers

     replica.watchers = issue.watchers

    Destination Side (Jira on-premise)

    Add the code below to the Incoming sync to create users if they don't exist on the receiving side.

    // WATCHERS SYNC
    issue.watchers = replica.watchers.each { w ->
        def _localW = nodeHelper.getUserByEmail(w.email)
        if (_localW == null) {
            // We create a user with no permissions, and with randomly generated password
            return nodeHelper.createUser(
                w.username,
                scala.util.Random$.MODULE$.alphanumeric().take(12).mkString(), 
                w.email, 
                w.displayName
            )
        }
        _localW
    }
    // END: WATCHERS SYNC 

    Destination Side (Jira Cloud)

    There's no API on Jira Cloud to create users if they don't exist.

    Exalate can lookup local users in different ways:

    • by email, if the user email is not hidden in the Atlassian account settings
    • by user accountID if the user email is hidden in the Atlassian account settings

    Incoming sync

    Set local watcher based on the received user data from the remote side. If the user is not found locally then don't set any value.

    // WATCHERS SYNC
    def localWatchers = []
    replica.watchers.each { w ->
        def _localW = nodeHelper.getUserByEmail(w.email)
        if (_localW != null) {
            localWatchers << _localW  // Add to the local watchers list if the user exists
        }
    }
    issue.watchers = localWatchers
    // END: WATCHERS SYNC

    Note: Updating watchers on Jira Cloud does not trigger Exalate synchronization event. It means that watchers will sync with the next issue update like new comment, description or other fields update.