Skip to content

Commit

Permalink
Merge pull request #500
Browse files Browse the repository at this point in the history
rsandell committed Feb 20, 2024

Verified

This commit was signed with the committer’s verified signature. The key has expired.
vespian Paweł Rozlach
2 parents 79344e7 + 7a8ec7d commit d3ed11d
Showing 2 changed files with 80 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.parameters.Base64EncodedStringParameterValue;
import com.sonyericsson.hudson.plugins.gerrit.trigger.utils.StringUtil;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Account;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Approval;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Provider;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeAbandoned;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeBasedEvent;
@@ -44,6 +46,7 @@
import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;
import hudson.model.TextParameterValue;
import net.sf.json.JSONObject;
import org.jvnet.localizer.Localizable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -246,7 +249,11 @@ public enum GerritTriggerParameters {
/**
* Comment posted to Gerrit in a comment-added event.
*/
GERRIT_EVENT_COMMENT_TEXT;
GERRIT_EVENT_COMMENT_TEXT,
/**
* Updated approvals.
*/
GERRIT_EVENT_UPDATED_APPROVALS;

private static final Logger logger = LoggerFactory.getLogger(GerritTriggerParameters.class);

@@ -490,6 +497,8 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Job p
commentTextMode.setOrCreateParameterValue(GERRIT_EVENT_COMMENT_TEXT,
parameters, comment, ParameterMode.PlainMode.TEXT, escapeQuotes);
}
GERRIT_EVENT_UPDATED_APPROVALS.setOrCreateStringParameterValue(parameters,
getUpdatedApprovals((CommentAdded)event), false);
}
} else if (gerritEvent instanceof RefUpdated) {
RefUpdated event = (RefUpdated)gerritEvent;
@@ -526,6 +535,23 @@ public static void setOrCreateParameters(GerritTriggeredEvent gerritEvent, Job p
}
}

/**
* Get the updated approvals as json string from a CommentAddedEvent.
*
* @param event the event
* @return json string of updated approvals
*/
static String getUpdatedApprovals(CommentAdded event) {
JSONObject updatedApprovals = event.getApprovals().stream()
.filter(Approval::isUpdated).collect(JSONObject::new, (JSONObject json, Approval apr) -> {
JSONObject j = new JSONObject();
j.put(GerritEventKeys.VALUE, apr.getValue());
j.put(GerritEventKeys.OLD_VALUE, apr.getOldValue());
json.put(apr.getType(), j);
}, (jsonObject, jsonObject2) -> { });
return updatedApprovals.toString();
}

/**
* Get the front end url from a ChangeBasedEvent.
*
Original file line number Diff line number Diff line change
@@ -27,20 +27,27 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.MockGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.CommentAdded;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;
import hudson.model.AbstractProject;
import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.WithoutJenkins;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

/**
@@ -64,12 +71,14 @@ public class GerritTriggerParametersTest {
*/
@Before
public void setup() {
config = Setup.createConfig();
GerritServer server = new GerritServer(PluginImpl.DEFAULT_SERVER_NAME);
server.setConfig(config);
PluginImpl plugin = PluginImpl.getInstance();
assertNotNull(plugin);
plugin.setServers(Collections.singletonList(server));
if (j.jenkins != null) {
config = Setup.createConfig();
GerritServer server = new GerritServer(PluginImpl.DEFAULT_SERVER_NAME);
server.setConfig(config);
PluginImpl plugin = PluginImpl.getInstance();
assertNotNull(plugin);
plugin.setServers(Collections.singletonList(server));
} //else running @WithoutJenkins
}

// CS IGNORE LineLength FOR NEXT 3 LINES. REASON: JavaDoc.
@@ -131,6 +140,44 @@ public void setOrCreateParametersUrlNoProviderAnyServer() throws Exception {
assertTrue(param.value.startsWith(config.getGerritFrontEndUrl()));
}

@Test @WithoutJenkins
public void testGetUpdatedApprovals() {
JSONArray approvals = new JSONArray();
JSONObject approval = new JSONObject();
approval
.accumulate(GerritEventKeys.TYPE, "CODE")
.accumulate(GerritEventKeys.VALUE, "+1")
.accumulate(GerritEventKeys.OLD_VALUE, "0");
approvals.add(approval);
approval = new JSONObject();
approval
.accumulate(GerritEventKeys.TYPE, "CI")
.accumulate(GerritEventKeys.VALUE, "0")
.accumulate(GerritEventKeys.OLD_VALUE, "-1");
approvals.add(approval);
approval = new JSONObject();
approval
.accumulate(GerritEventKeys.TYPE, "VRF")
.accumulate(GerritEventKeys.VALUE, "0");
approvals.add(approval);

JSONObject e = new JSONObject()
.accumulate(GerritEventKeys.COMMENT, "Test")
.accumulate(GerritEventKeys.APPROVALS, approvals);
CommentAdded event = new CommentAdded();
event.fromJson(e);
String updatedApprovalsStr = GerritTriggerParameters.getUpdatedApprovals(event);
JSONObject updatedApprovals = JSONObject.fromObject(updatedApprovalsStr);
JSONObject code = updatedApprovals.getJSONObject("CODE");
assertEquals("+1", code.optString(GerritEventKeys.VALUE));
assertEquals("0", code.optString(GerritEventKeys.OLD_VALUE));
JSONObject ci = updatedApprovals.getJSONObject("CI");
assertEquals("0", ci.optString(GerritEventKeys.VALUE));
assertEquals("-1", ci.optString(GerritEventKeys.OLD_VALUE));
assertNull(updatedApprovals.optJSONObject("VRF"));

}

/**
* Finds the given parameter in the list.
*

0 comments on commit d3ed11d

Please sign in to comment.