Skip to content
Permalink
Browse files

[JENKINS-28188] fix plugin compatibility with anonymous class cause f…

…or build trigger

- added test for Issue JENKINS-28188
- fixes exception `StringIndexOutOfBoundsException: String index out of range: -1`
  • Loading branch information
lanwen committed May 4, 2015
1 parent ebd77a2 commit 3d62b4f4505112883d058cab12a86b12e34949dd
@@ -5,7 +5,6 @@
import hudson.model.CauseAction;
import hudson.triggers.SCMTrigger;
import hudson.triggers.TimerTrigger;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
@@ -15,6 +14,10 @@
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Joiner.on;
import static org.apache.commons.lang.StringUtils.isNotBlank;


/**
* @author Gregory Boissinot
*/
@@ -47,7 +50,7 @@ private static void insertRootCauseNames(Set<String> causeNames, Cause cause, in
if (depth == MAX_UPSTREAM_DEPTH) {
causeNames.add("DEEPLYNESTEDCAUSES");
} else {
Cause.UpstreamCause c = (Cause.UpstreamCause)cause;
Cause.UpstreamCause c = (Cause.UpstreamCause) cause;
List<Cause> upstreamCauses = c.getUpstreamCauses();
for (Cause upstreamCause : upstreamCauses)
insertRootCauseNames(causeNames, upstreamCause, depth + 1);
@@ -59,16 +62,15 @@ private static void insertRootCauseNames(Set<String> causeNames, Cause cause, in

private static Map<String, String> buildCauseEnvironmentVariables(String envBase, Collection<String> causeNames) {
Map<String, String> triggerVars = new HashMap<String, String>();
StringBuilder all = new StringBuilder();
List<String> nonEmptyNames = new ArrayList<String>();
for (String name : causeNames) {
if (!StringUtils.isBlank(name)) {
triggerVars.put(envBase + "_" + name, "true");
all.append(",");
all.append(name);
if (isNotBlank(name)) {
triggerVars.put(on("_").join(envBase, name), "true");
nonEmptyNames.add(name);
}
}
// add variable containing all the trigger names
triggerVars.put(envBase, all.toString().substring(1));
triggerVars.put(envBase, on(",").join(nonEmptyNames));
return triggerVars;
}

@@ -7,9 +7,9 @@
import hudson.model.Run;
import hudson.triggers.SCMTrigger;
import hudson.triggers.TimerTrigger;
import org.jenkinsci.plugins.envinject.CustomTestCause;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;

import static com.google.common.base.Joiner.on;
@@ -123,6 +123,22 @@ public void shouldWriteInfoAboutMultipleBuildCauses() throws Exception {
);
}

@Test
@Bug(28188)
public void shouldWriteInfoAboutAnonymousClassCause() throws Exception {
FreeStyleBuild build = jenkins.createFreeStyleProject().scheduleBuild2(0, new Cause() {
@Override
public String getShortDescription() {
return "This build was started by a hobbit Bilbo. Bilbo Baggins";
}
}).get();

assertThat(build.getResult(), is(SUCCESS));

assertThat(build, withCause(BUILD_CAUSE, ""));
assertThat(build, withCause(ROOT_BUILD_CAUSE, ""));
}

private String sub(String first, String second) {
return on("_").join(first, second);
}

0 comments on commit 3d62b4f

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