
// Configure gerrit trigger servers
def add_gerrit_trigger = { instance, serverName, noConnectionOnStartup, gerritHostName, gerritSshPort, gerritUserName, gerritAuthKeyFile, gerritFrontEndUrl ->

    def gerritPlugin = instance.getPlugin(com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl.class)
    gerritPlugin.getPluginConfig().setNumberOfReceivingWorkerThreads(3)
    gerritPlugin.getPluginConfig().setNumberOfSendingWorkerThreads(1)

    println "  :: Set gerrit trigger server " + serverName
    def server = new com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer(serverName, noConnectionOnStartup)
    def config = server.getConfig()

    def triggerConfig = [
      'gerritHostName':gerritHostName,
      'gerritSshPort':gerritSshPort,
      'gerritUserName':gerritUserName,
      'gerritFrontEndUrl':gerritFrontEndUrl,
      'gerritAuthKeyFile':gerritAuthKeyFile,
      'gerritBuildCurrentPatchesOnly':true,
      'gerritBuildStartedVerifiedValue':0,
      'gerritBuildStartedCodeReviewValue':0,
      'gerritBuildSuccessfulVerifiedValue':1,
      'gerritBuildSuccessfulCodeReviewValue':0,
      'gerritBuildFailedVerifiedValue':-1,
      'gerritBuildFailedCodeReviewValue':0,
      'gerritBuildUnstableVerifiedValue':-1,
      'gerritBuildUnstableCodeReviewValue':0,
      'gerritBuildNotBuiltVerifiedValue':0,
      'gerritBuildNotBuiltCodeReviewValue':0,
      'enableManualTrigger':true,
      'enablePluginMessages':true,
      'buildScheduleDelay':3,
      'dynamicConfigRefreshInterval':30,
      'watchdogTimeoutMinutes':0,
      'verdictCategories': [
        [ 'verdictValue':'CRVW', 'verdictDescription':'Code Review'],
        [ 'verdictValue':'VRIF', 'verdictDescription':'Verified']
      ] as LinkedList
    ]

    config.setValues(net.sf.json.JSONObject.fromObject(triggerConfig))
    server.setConfig(config)

    // avoid duplicate servers on the server list
    if ( gerritPlugin.containsServer(serverName) ) {
      gerritPlugin.removeServer(gerritPlugin.getServer(serverName))
    }
    gerritPlugin.addServer(server)

}

