Skip to content
Permalink
Browse files

Fix changeIdAnnotator when annotating a build configured with anyServer

ChangeIdAnnotator was using the serverName configured at the job level
to find the front end url of the gerrit server but it is failing when
job is configured with "Any Server" as server name.

Use the gerritCause of the build instead to find the url of the gerrit
server.

Change-Id: I08332e788c5f23417425599d2ab60fb41fde0f92
  • Loading branch information...
hugares committed Feb 6, 2014
1 parent 8129c64 commit f8e50a297e9a00551928abfcf66df18bcea180ad
@@ -1,18 +1,16 @@
package com.sonyericsson.hudson.plugins.gerrit.trigger;

import com.sonyericsson.hudson.plugins.gerrit.trigger.config.IGerritHudsonTriggerConfig;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import hudson.Extension;
import hudson.MarkupText;
import hudson.MarkupText.SubText;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.scm.ChangeLogAnnotator;
import hudson.scm.ChangeLogSet.Entry;
import hudson.triggers.Trigger;

import java.util.regex.Pattern;

import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause;

/**
* Turns "Change-ID: XXXX" into a hyperlink to Gerrit.
*
@@ -22,38 +20,15 @@
public class ChangeIdAnnotator extends ChangeLogAnnotator {
@Override
public void annotate(AbstractBuild<?, ?> build, Entry change, MarkupText text) {
String serverName = GerritTrigger.getTrigger(build.getProject()).getServerName();
IGerritHudsonTriggerConfig config = PluginImpl.getInstance().getServer(serverName).getConfig();
annotate(build.getProject(), text, config);
}

/**
* Annotates Gerrit change IDs in changelogs.
* @param project The project
* @param text The initial text
* @param config The Gerrit trigger config
*/
public void annotate(AbstractProject<?, ?> project, MarkupText text, IGerritHudsonTriggerConfig config) {
for (SubText token : text.findTokens(CHANGE_ID)) {
if (!hasGerritTrigger(project)) {
return; // not configured with Gerrit
}
token.href(config.getGerritFrontEndUrl() + "r/" + token.getText());
}
}

/**
* Does this project have the Gerrit trigger configured?
* @param project The project
* @return True if the gerrit trigger is configured.
*/
private boolean hasGerritTrigger(AbstractProject<?, ?> project) {
for (Trigger t : project.getTriggers().values()) {
if (t instanceof GerritTrigger) {
return true;
GerritCause gerritCause = build.getCause(GerritCause.class);
if (gerritCause != null
&& gerritCause.getEvent().getProvider() != null
&& gerritCause.getEvent().getProvider().getUrl() != null
&& !gerritCause.getEvent().getProvider().getUrl().trim().isEmpty()) {
token.href(gerritCause.getEvent().getProvider().getUrl() + "r/" + token.getText());
}
}
return false;
}

private static final Pattern CHANGE_ID = Pattern.compile("(?<=\\bChange-Id: )I[0-9a-fA-F]{40}\\b");
@@ -1,65 +1,62 @@
package com.sonyericsson.hudson.plugins.gerrit.trigger;

import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger.DescriptorImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.MockGerritHudsonTriggerConfig;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.MarkupText;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.AbstractBuild;
import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

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

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.sonyericsson.hudson.plugins.gerrit.gerritevents.dto.events.PatchsetCreated;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause;
import com.sonyericsson.hudson.plugins.gerrit.trigger.mock.Setup;

/**
* Test for {@link ChangeIdAnnotator}.
* @author Kohsuke Kawaguchi
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Hudson.class })
public class ChangeIdAnnotatorTest {

/**
* the test.
*/
@Test
public void testFoo() {
Map m = new HashMap();
m.put(new DescriptorImpl(), mock(GerritTrigger.class));

AbstractProject p = mock(AbstractProject.class);
when(p.getTriggers()).thenReturn(m);

annotateAndVerify(p,
AbstractBuild<?, ?> b = mock(AbstractBuild.class);
when(b.getCause(GerritCause.class)).thenReturn(null);
annotateAndVerify(b,
"test\ntest\nChange-Id: I1234567890123456789012345678901234567890",
"test\ntest\nChange-Id: I1234567890123456789012345678901234567890");

PatchsetCreated patchsetCreated = Setup.createPatchsetCreated();
GerritCause gerritCause = new GerritCause();
gerritCause.setEvent(patchsetCreated);
when(b.getCause(GerritCause.class)).thenReturn(gerritCause);
annotateAndVerify(b,
"test\ntest\nChange-Id: <a href='http://gerrit/r/I1234567890123456789012345678901234567890'>"
+ "I1234567890123456789012345678901234567890</a>",
"test\ntest\nChange-Id: I1234567890123456789012345678901234567890");

annotateAndVerify(p,
annotateAndVerify(b,
"xxxChange-Id: I1234567890123456789012345678901234567890",
"xxxChange-Id: I1234567890123456789012345678901234567890");

annotateAndVerify(p,
annotateAndVerify(b,
"Change-Id: I1234567890123456789012345678901234567890ffff",
"Change-Id: I1234567890123456789012345678901234567890ffff");
}

/**
* Utility method.
* @param p p
* @param b b
* @param expected expected
* @param plain plain
*/
private void annotateAndVerify(AbstractProject p, String expected, String plain) {
private void annotateAndVerify(AbstractBuild<?, ?> b, String expected, String plain) {
MarkupText t = new MarkupText(plain);
new ChangeIdAnnotator().annotate(p, t, new MockGerritHudsonTriggerConfig());
new ChangeIdAnnotator().annotate(b, null, t);
System.out.println(t.toString(true));
Assert.assertEquals(expected, t.toString(true));
}

0 comments on commit f8e50a2

Please sign in to comment.
You can’t perform that action at this time.