Skip to content

Commit

Permalink
Protect PlaybackManager from null EventCreatedOn
Browse files Browse the repository at this point in the history
It is possible that Gerrit might return a null eventCreated attribute for some events.

This protects the persistence of events from this situation.

[FIXED JENKINS-30975]
  • Loading branch information
Scott Hebert committed Nov 27, 2015
1 parent 3bfe912 commit 7fd0896
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Expand Up @@ -405,12 +405,18 @@ protected String buildEventsLogURL(IGerritHudsonTriggerConfig config, Date date1
* @param evt Gerrit Event to persist.
* @return true if was able to persist event.
*/
private synchronized boolean persist(GerritTriggeredEvent evt) {
synchronized boolean persist(GerritTriggeredEvent evt) {

long ts = evt.getEventCreatedOn().getTime();
if (evt == null || evt.getEventCreatedOn() == null) {
logger.warn("Event CreatedOn is null...Gerrit Server might not support attribute eventCreatedOn. "
+ "Will NOT persist this event and Missed Events will be disabled!");
isSupported = false;
return false;
}

long ts = evt.getEventCreatedOn().getTime();
if (ts == 0) {
logger.warn("Event CreatedOn is 0...Gerrit Server does not support attribute eventCreateOn. "
logger.warn("Event CreatedOn is 0...Gerrit Server does not support attribute eventCreatedOn. "
+ "Will NOT persist this event and Missed Events will be disabled!");
isSupported = false;
return false;
Expand Down
Expand Up @@ -30,12 +30,17 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonyericsson.hudson.plugins.gerrit.trigger.utils.GerritPluginChecker;
import com.sonymobile.tools.gerrit.gerritevents.GerritHandler;
import com.sonymobile.tools.gerrit.gerritevents.GerritJsonEventFactory;
import com.sonymobile.tools.gerrit.gerritevents.dto.GerritEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.GerritTriggeredEvent;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;

import hudson.XmlFile;
import jenkins.model.Jenkins;
import junit.framework.TestCase;
import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -47,6 +52,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Random;
Expand Down Expand Up @@ -133,6 +139,25 @@ public void setUp() throws IOException {
, anyString())).thenReturn(true);
}

/**
* Test if Gerrit returns a null eventCreated attribute.
* @throws IOException if occurs.
*/
@Test
public void testNullEventCreatedOn() throws IOException {
InputStream stream = getClass().getResourceAsStream("DeserializeEventCreatedOnTest.json");
String json = IOUtils.toString(stream);
JSONObject jsonObject = JSONObject.fromObject(json);
GerritEvent evt = GerritJsonEventFactory.getEvent(jsonObject);
GerritTriggeredEvent gEvt = (GerritTriggeredEvent)evt;
assertNull(gEvt.getEventCreatedOn());

GerritMissedEventsPlaybackManager missingEventsPlaybackManager
= new GerritMissedEventsPlaybackManager("defaultServer");

assertTrue(!missingEventsPlaybackManager.persist(gEvt));

}
/**
* Given a non-existing timestamp file
* When we attempt to load it
Expand Down
@@ -0,0 +1,11 @@
{"type":"change-restored","change":{"project":"playback-test","branch":"master"
,"id":"Ibafad9613934fdec650d6186dc1021bc2cf5d2f5","number":"654","subject":"Mon Dec 8 20:53:56 EST 2014"
,"owner":{"name":"Scott","email":"scott@company.com","username":"scott"
},"url":"http://localhost:9090/654"
,"commitMessage":"Mon Dec 8 20:53:56 EST 2014\n\nChange-Id: Ibafad9613934fdec650d6186dc1021bc2cf5d2f5\n"
,"status":"NEW"},"patchSet":{"number":"1","revision":"b2b5f7c8fb8f96312d629d4d4f379f34f5b9b651"
,"parents":["6cf2d28cc07d22a49e0e614c7bde0a4e5fd0c8f5"],"ref":"refs/changes/54/654/1"
,"uploader":{"name":"Scott","email":"scott@company.com","username":"scott"}
,"createdOn":1418090036,"author":{"name":"Scott","email":"scott@company.com","username":"scott"}
,"isDraft":false,"sizeInsertions":1,"sizeDeletions":0},"restorer":{"name":"Scott"
,"email":"scott@company.com","username":"scott"}}

0 comments on commit 7fd0896

Please sign in to comment.