Skip to content
Permalink
Browse files
Merge pull request #218 from ndeloof/master
[FIXED JENKINS-27651] handle job rename
  • Loading branch information
rsandell committed Apr 1, 2015
2 parents 1287381 + bad831f commit 80e0abba6b4473b32472387a9da4337321a88588
@@ -70,7 +70,16 @@ public final class EventListener implements GerritEventListener {
* @param job the job to handle.
*/
EventListener(@Nonnull AbstractProject job) {
this.job = job.getFullName();
this(job.getFullName());
}

/**
* Standard constructor.
*
* @param fullName the job to handle full name.
*/
EventListener(@Nonnull String fullName) {
this.job = fullName;
}

@Override
@@ -70,6 +70,27 @@ public void onDeleted(Item item) {
}
}

/**
* trigger get stopped/started when a job is configured, but rename is a special operation
* and uses a two phase confirmation, the second one doing the actual rename does not
* stop/start the trigger, so we end up with misconfigured EventListener.
*
* Also see JENKINS-22936
* @param item an item whose absolute position is now different
* @param oldFullName the former {@link Item#getFullName}
* @param newFullName the current {@link Item#getFullName}
*/
@Override
public void onLocationChanged(Item item, String oldFullName, String newFullName) {
if (item instanceof AbstractProject<?, ?>) {
AbstractProject<?, ?> project = (AbstractProject<?, ?>)item;
GerritTrigger gerritTrigger = project.getTrigger(GerritTrigger.class);
if (gerritTrigger != null) {
gerritTrigger.onJobRenamed(oldFullName, newFullName);
}
}
}

/**
* Called by Jenkins when all items are loaded.
*/
@@ -344,6 +344,23 @@ public String getGerritSlaveId() {
return gerritSlaveId;
}

/**
* Notify trigger for job being renamed
* @param oldFullName the former {@link Item#getFullName}
* @param newFullName the current {@link Item#getFullName}
*/
void onJobRenamed(String oldFullName, String newFullName) {
PluginImpl plugin = PluginImpl.getInstance();
if (plugin != null) {
GerritHandler handler = plugin.getHandler();
if (handler != null) {
handler.removeListener(new EventListener(oldFullName));
handler.addListener(createListener());
}
}
}


/**
* Finds the GerritTrigger in a project.
*
@@ -23,13 +23,16 @@
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger;

import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.doNothing;

import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.TestUtils;
import com.sonymobile.tools.gerrit.gerritevents.GerritHandler;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;
import hudson.model.FreeStyleProject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
@@ -39,6 +42,14 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.doReturn;


/**
* Tests for {@link GerritItemListener}.
*/
@@ -68,7 +79,7 @@ public void setup() {
}

/**
* Tests {@link GerritItemListener#onLoad()} gets connection.
* Tests {@link GerritItemListener#onLoaded()} gets connection.
*
* @throws Exception if so.
*/
@@ -81,7 +92,7 @@ public void testOnLoadedWithConnection() throws Exception {
}

/**
* Tests {@link GerritItemListener#onLoad()} does not get connection.
* Tests {@link GerritItemListener#onLoaded()} does not get connection.
*
* @throws Exception if so.
*/
@@ -92,4 +103,35 @@ public void testOnLoadedWithNoConnection() throws Exception {
listener.onLoaded();
Mockito.verify(gerritServer, Mockito.times(0)).startConnection();
}

/**
* Test {@link GerritItemListener#onLocationChanged(hudson.model.Item, String, String)} do handle renamed job.
*
* @throws Exception if so.
*/
@Test
@Issue("JENKINS-27651")
public void testOnJobRenamed() throws Exception {
FreeStyleProject job = j.createFreeStyleProject("MyJob");
GerritHandler handler = PluginImpl.getInstance().getHandler();
PatchsetCreated event = Setup.createManualPatchsetCreated();

GerritTrigger trigger = spy(new GerritTrigger(
null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
true, true, true, false, false, "", "", "", "", "", "", "", null, null, null,
null, false, false, "", null));

doReturn(new GerritTrigger.DescriptorImpl()).when(trigger, "getDescriptor");
job.addTrigger(trigger);
trigger.start(job, true);
int before = handler.getEventListenersCount();

when(trigger.isInteresting(event)).thenReturn(true);
job.renameTo("MyJobRenamed");
assertEquals("We leak some listeners", before, handler.getEventListenersCount());
handler.notifyListeners(event);
TestUtils.waitForBuilds(job, 1);
assertNotNull(job.getLastBuild());
}

}

0 comments on commit 80e0abb

Please sign in to comment.