Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #33 from tsondergaard/jenkins-24785-root-cause
[FIXED JENKINS-24785] - Introduce ROOT_BUILD_CAUSE
  • Loading branch information
tsondergaard committed Oct 8, 2014
2 parents f28c26b + 7ae492d commit 2fb243d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.445</version>
<version>1.482</version>
</parent>

<artifactId>envinject</artifactId>
Expand Down
Expand Up @@ -7,37 +7,71 @@
import hudson.triggers.TimerTrigger;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author Gregory Boissinot
*/
public class BuildCauseRetriever {

/**
* Maximum depth of transitive upstream causes we want to record.
*/
private static final int MAX_UPSTREAM_DEPTH = 10;
public static final String ENV_CAUSE = "BUILD_CAUSE";
public static final String ENV_ROOT_CAUSE = "ROOT_BUILD_CAUSE";

public Map<String, String> getTriggeredCause(AbstractBuild<?, ?> build) {

Map<String, String> triggerVars = new HashMap<String, String>();
StringBuilder all = new StringBuilder();
CauseAction causeAction = build.getAction(CauseAction.class);
List<Cause> buildCauses = causeAction.getCauses();
Map<String, String> env = new HashMap<String, String>();
List<String> directCauseNames = new ArrayList<String>();
Set<String> rootCauseNames = new LinkedHashSet<String>();
for (Cause cause : buildCauses) {
String name = getTriggerName(cause);
directCauseNames.add(getTriggerName(cause));
insertRootCauseNames(rootCauseNames, cause, 0);
}
env.putAll(buildCauseEnvironmentVariables(ENV_CAUSE, directCauseNames));
env.putAll(buildCauseEnvironmentVariables(ENV_ROOT_CAUSE, rootCauseNames));
return env;
}

private static void insertRootCauseNames(Set<String> causeNames, Cause cause, int depth) {
if (cause instanceof Cause.UpstreamCause) {
if (depth == MAX_UPSTREAM_DEPTH) {
causeNames.add("DEEPLYNESTEDCAUSES");
} else {
Cause.UpstreamCause c = (Cause.UpstreamCause)cause;
List<Cause> upstreamCauses = c.getUpstreamCauses();
for (Cause upstreamCause : upstreamCauses)
insertRootCauseNames(causeNames, upstreamCause, depth + 1);
}
} else {
causeNames.add(getTriggerName(cause));
}
}

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


@SuppressWarnings(value = "deprecation")
private static String getTriggerName(Cause cause) {
if (SCMTrigger.SCMTriggerCause.class.isInstance(cause)) {
Expand Down
Expand Up @@ -48,11 +48,12 @@ public void testTIMERBuildCause() throws Exception {

public void testUPSTREAMBuildCause() throws Exception {
FreeStyleProject upProject = createFreeStyleProject();
FreeStyleBuild upBuild = upProject.scheduleBuild2(0).get();
FreeStyleBuild upBuild = upProject.scheduleBuild2(0, new Cause.UserCause()).get();
Cause.UpstreamCause upstreamCause = new Cause.UpstreamCause((Run) upBuild);
FreeStyleBuild build = project.scheduleBuild2(0, upstreamCause).get();
Assert.assertEquals(Result.SUCCESS, build.getResult());
checkCauseArguments(upstreamCause);
checkBuildCauses(build, "UPSTREAMTRIGGER", "MANUALTRIGGER",
"BUILD_CAUSE_UPSTREAMTRIGGER" , "ROOT_BUILD_CAUSE_MANUALTRIGGER");
}

public void testCustomBuildCause() throws Exception {
Expand All @@ -70,7 +71,9 @@ public void testMultipleBuildCause() throws Exception {
Assert.assertEquals(Result.SUCCESS, build.getResult());

String customCauseName = CustomTestCause.class.getSimpleName().toUpperCase();
checkBuildCauses(build, "CUSTOMTESTCAUSE,SCMTRIGGER", "BUILD_CAUSE_" + customCauseName, "BUILD_CAUSE_SCMTRIGGER");
checkBuildCauses(build, "CUSTOMTESTCAUSE,SCMTRIGGER", "CUSTOMTESTCAUSE,SCMTRIGGER",
"BUILD_CAUSE_" + customCauseName, "BUILD_CAUSE_SCMTRIGGER",
"ROOT_BUILD_CAUSE_" + customCauseName, "ROOT_BUILD_CAUSE_SCMTRIGGER");
}

private void checkCauseArguments(Class<? extends Cause> causeClass) throws Exception {
Expand All @@ -80,21 +83,21 @@ private void checkCauseArguments(Class<? extends Cause> causeClass) throws Excep
private void checkCauseArguments(Cause cause) throws Exception {
FreeStyleBuild build = project.scheduleBuild2(0, cause).get();
Assert.assertEquals(Result.SUCCESS, build.getResult());
Assert.assertEquals(Result.SUCCESS, build.getResult());
checkCauseArgumentsWithBuild(build, cause.getClass());
checkCauseArgumentsWithBuild(build, causeMatchingNames.get(cause.getClass()));
}

private void checkCauseArgumentsWithBuild(FreeStyleBuild build, Class<? extends Cause> causeClass) throws Exception {
String causeValue = causeMatchingNames.get(causeClass);
private void checkCauseArgumentsWithBuild(FreeStyleBuild build, String causeValue) throws Exception {
if (causeValue != null) {
checkBuildCauses(build, causeValue, "BUILD_CAUSE_" + causeValue);
checkBuildCauses(build, causeValue, causeValue, "BUILD_CAUSE_" + causeValue, "ROOT_BUILD_CAUSE_" + causeValue);
} else {
String customCauseName = CustomTestCause.class.getSimpleName().toUpperCase();
checkBuildCauses(build, customCauseName, "BUILD_CAUSE_" + customCauseName);
checkBuildCauses(build, customCauseName, customCauseName,
"BUILD_CAUSE_" + customCauseName, "ROOT_BUILD_CAUSE_" + customCauseName);
}
}

private void checkBuildCauses(FreeStyleBuild build, String expectedMainCauseValue, String... expectedCauseKeys) {
private void checkBuildCauses(FreeStyleBuild build, String expectedMainCauseValue,
String expectedRootMainCauseValue, String... expectedCauseKeys) {

EnvInjectAction envInjectAction = build.getAction(EnvInjectAction.class);
Assert.assertNotNull(envInjectAction);
Expand All @@ -106,6 +109,10 @@ private void checkBuildCauses(FreeStyleBuild build, String expectedMainCauseValu
Assert.assertNotNull(causeValue);
Assert.assertEquals(expectedMainCauseValue, causeValue);

String rootCauseValue = envVars.get("ROOT_BUILD_CAUSE");
Assert.assertNotNull(rootCauseValue);
Assert.assertEquals(expectedRootMainCauseValue, rootCauseValue);

for (String causeKey : expectedCauseKeys) {
Assert.assertEquals("true", envVars.get(causeKey));
}
Expand Down

0 comments on commit 2fb243d

Please sign in to comment.