package com.tw.go.plugin.provider.gerrit;

import com.tw.go.plugin.PluginSettings;
import com.tw.go.plugin.provider.Provider;
import com.tw.go.plugin.provider.gerrit.response.ResponseParser;
import com.tw.go.plugin.provider.gerrit.response.model.CommitDetails;
import com.tw.go.plugin.util.AuthenticationType;
import com.tw.go.plugin.util.HTTPUtils;
import com.tw.go.plugin.util.JSONUtils;

import java.util.HashMap;
import java.util.Map;

public class GerritProvider implements Provider {
    public static final String PLUGIN_ID = "gerrit.cs.status";
    public static final String GERRIT_CS_POLLER_PLUGIN_ID = "gerrit.cs";

    public static final int IN_PROGRESS_VALUE = 0;
    public static final int SUCCESS_VALUE = 1;
    public static final int FAILURE_VALUE = -1;

    @Override
    public String pluginId() {
        return PLUGIN_ID;
    }

    @Override
    public String pollerPluginId() {
        return GERRIT_CS_POLLER_PLUGIN_ID;
    }

    @Override
    public void updateStatus(String url, PluginSettings pluginSettings, String branch, String revision, String pipelineInstance,
                             String result, String trackbackURL) throws Exception {
        String commitDetailsURL = String.format("%s/a/changes/?q=commit:%s", pluginSettings.getEndPoint(), revision);
        String commitDetailsResponse = HTTPUtils.getRequest(commitDetailsURL, AuthenticationType.DIGEST, pluginSettings.getUsername(), pluginSettings.getPassword());
        CommitDetails commitDetails = new ResponseParser().parseCommitDetails(commitDetailsResponse);

        Map<String, Object> request = new HashMap<String, Object>();
        request.put("message", String.format("%s: %s", pipelineInstance, trackbackURL));
        Map<String, Object> labels = new HashMap<String, Object>();
        request.put("labels", labels);
        labels.put("Code-Review", getCodeReviewValue(result));
        String updateStatusURL = String.format("%s/a/changes/%s/revisions/%s/review", pluginSettings.getEndPoint(), commitDetails.getId(), revision);
        HTTPUtils.postRequest(updateStatusURL, AuthenticationType.DIGEST, pluginSettings.getUsername(), pluginSettings.getPassword(), JSONUtils.toJSON(request));
    }

    int getCodeReviewValue(String result) {
        result = result == null ? "" : result;
        int value = IN_PROGRESS_VALUE;
        if (result.equalsIgnoreCase("Passed")) {
            value = SUCCESS_VALUE;
        } else if (result.equalsIgnoreCase("Failed")) {
            value = FAILURE_VALUE;
        } else if (result.equalsIgnoreCase("Cancelled")) {
            value = FAILURE_VALUE;
        }
        return value;
    }
}

@@ -382,16 +382,58 @@
 ONUtils;
+%0Aimport com.tw.go.plugin.util.StringUtils;
 %0A%0Aimport
@@ -1215,16 +1215,651 @@
 ption %7B%0A
+        String endPointToUse = pluginSettings.getEndPoint();%0A        String usernameToUse = pluginSettings.getUsername();%0A        String passwordToUse = pluginSettings.getPassword();%0A%0A        if (StringUtils.isEmpty(endPointToUse)) %7B%0A            endPointToUse = System.getProperty(%22go.plugin.build.status.gerrit.endpoint%22);%0A        %7D%0A        if (StringUtils.isEmpty(usernameToUse)) %7B%0A            usernameToUse = System.getProperty(%22go.plugin.build.status.gerrit.username%22);%0A        %7D%0A        if (StringUtils.isEmpty(passwordToUse)) %7B%0A            passwordToUse = System.getProperty(%22go.plugin.build.status.gerrit.password%22);%0A        %7D%0A%0A
         
@@ -1931,36 +1931,21 @@
 s%22, 
-pluginSettings.getE
+e
 ndPoint
-()
+ToUse
 , re
@@ -2062,66 +2062,36 @@
 ST, 
-pluginSettings.getUsername(), pluginSettings.getP
+usernameToUse, p
 assword
-()
+ToUse
 );%0A 
@@ -2610,36 +2610,21 @@
 w%22, 
-pluginSettings.getE
+e
 ndPoint
-()
+ToUse
 , co
@@ -2733,66 +2733,36 @@
 ST, 
-pluginSettings.getUsername(), pluginSettings.getP
+usernameToUse, p
 assword
-()
+ToUse
 , JS

Add system property support for gerrit plugin