Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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
MerkushevKirill committed May 4, 2015
1 parent ebd77a2 commit 3d62b4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Expand Up @@ -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;
Expand All @@ -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
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 3d62b4f

Please sign in to comment.