Skip to content

Commit

Permalink
[JENKINS-43845] - Move Cause EnvVars logic from EnvInject plugin to E…
Browse files Browse the repository at this point in the history
…nvInject API
  • Loading branch information
oleg-nenashev committed Apr 26, 2017
1 parent bdc9dd7 commit a933c47
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
@@ -0,0 +1,93 @@
package org.jenkinsci.plugins.envinjectapi.util;

import hudson.model.Cause;
import hudson.triggers.SCMTrigger;
import hudson.triggers.TimerTrigger;

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

import java.util.Locale;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static com.google.common.base.Joiner.on;

// Moved from EnvInject plugin
/**
* Contains helper methods for working with run {@link Cause}s.
* @author Gregory Boissinot
* @author Oleg Nenashev
*/
/*package*/ class CauseHelper {

/**
* 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";

/**
* Inserts root cause names to the specified target container.
* @param causeNamesTarget Target set. May receive null items
* @param cause Cause to be added. For {@code Cause.UstreamCause} there will be in-depth search
* @param depth Current search depth. {@link #MAX_UPSTREAM_DEPTH} is a limit
*/
static void insertRootCauseNames(@Nonnull Set<String> causeNamesTarget, @CheckForNull Cause cause, int depth) {
if (cause instanceof Cause.UpstreamCause) {
if (depth == MAX_UPSTREAM_DEPTH) {
causeNamesTarget.add("DEEPLYNESTEDCAUSES");
} else {
Cause.UpstreamCause c = (Cause.UpstreamCause) cause;
List<Cause> upstreamCauses = c.getUpstreamCauses();
for (Cause upstreamCause : upstreamCauses)
insertRootCauseNames(causeNamesTarget, upstreamCause, depth + 1);
}
} else {
//TODO: Accordig to the current design this list may receive null for unknown trigger. Bug?
// Should actually return UNKNOWN
causeNamesTarget.add(getTriggerName(cause));
}
}

@Nonnull
static Map<String, String> buildCauseEnvironmentVariables(@Nonnull String envBase, @Nonnull Collection<String> causeNames) {
Map<String, String> triggerVars = new HashMap<>();
List<String> nonEmptyNames = new ArrayList<>();
for (String name : causeNames) {
if (isNotBlank(name)) {
triggerVars.put(on("_").join(envBase, name), "true");
nonEmptyNames.add(name);
}
}
// add variable containing all the trigger names
triggerVars.put(envBase, on(",").join(nonEmptyNames));
return triggerVars;
}

@CheckForNull
@SuppressWarnings(value = "deprecation")
static String getTriggerName(Cause cause) {
if (SCMTrigger.SCMTriggerCause.class.isInstance(cause)) {
return "SCMTRIGGER";
} else if (TimerTrigger.TimerTriggerCause.class.isInstance(cause)) {
return "TIMERTRIGGER";
} else if (Cause.UserIdCause.class.isInstance(cause)) {
return "MANUALTRIGGER";
} else if (Cause.UserCause.class.isInstance(cause)) {
return "MANUALTRIGGER";
} else if (Cause.UpstreamCause.class.isInstance(cause)) {
return "UPSTREAMTRIGGER";
} else if (cause != null) {
return cause.getClass().getSimpleName().toUpperCase(Locale.ENGLISH);
}

return null;
}

}
Expand Up @@ -6,6 +6,8 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Computer;
import hudson.model.Job;
import hudson.model.Node;
Expand All @@ -18,12 +20,19 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import org.jenkinsci.lib.envinject.EnvInjectException;
import static org.jenkinsci.plugins.envinjectapi.util.CauseHelper.ENV_CAUSE;
import static org.jenkinsci.plugins.envinjectapi.util.CauseHelper.ENV_ROOT_CAUSE;

/**
* Provides utility methods for resolving environment variables.
Expand Down Expand Up @@ -224,6 +233,34 @@ public Map<String, String> call() throws EnvInjectException {
throw new EnvInjectException(ie);
}
}

// Moved from EnvInject

/**
* Retrieves variables describing the Run cause.
* @param run Run
* @return Set of environment variables, which depends on the cause type.
*/
@Nonnull
public static Map<String, String> getCauseEnvVars(@Nonnull Run<?, ?> run) {
CauseAction causeAction = run.getAction(CauseAction.class);
Map<String, String> env = new HashMap<>();
List<String> directCauseNames = new ArrayList<>();
Set<String> rootCauseNames = new LinkedHashSet<>();

if (causeAction != null) {
List<Cause> buildCauses = causeAction.getCauses();
for (Cause cause : buildCauses) {
directCauseNames.add(CauseHelper.getTriggerName(cause));
CauseHelper.insertRootCauseNames(rootCauseNames, cause, 0);
}
} else {
directCauseNames.add("UNKNOWN");
rootCauseNames.add("UNKNOWN");
}
env.putAll(CauseHelper.buildCauseEnvironmentVariables(ENV_CAUSE, directCauseNames));
env.putAll(CauseHelper.buildCauseEnvironmentVariables(ENV_ROOT_CAUSE, rootCauseNames));
return env;
}
}

0 comments on commit a933c47

Please sign in to comment.