diff --git a/pom.xml b/pom.xml index 14d51486..9c611a0c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ envinject - 1.92.0-beta-2-SNAPSHOT + 1.93-SNAPSHOT hpi Environment Injector Plugin https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin @@ -41,6 +41,15 @@ Maintainer + + recena + Manuel Recena + recena@gmail.com + +1 + + Contributor + + @@ -60,16 +69,17 @@ + org.jenkins-ci.tools maven-hpi-plugin 1.98 true - + - + @@ -120,6 +130,6 @@ - - + + diff --git a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectComputerListener.java b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectComputerListener.java index 22ee643b..9cd94244 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectComputerListener.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectComputerListener.java @@ -20,6 +20,8 @@ import java.io.IOException; import java.io.Serializable; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -53,10 +55,13 @@ public Map call() throws IOException { globalPropertiesEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars()); } - if (nodeProperty instanceof EnvInjectNodeProperty) { + final Node node = c.getNode(); + if (node != null && nodeProperty instanceof EnvInjectNodeProperty) { EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty); unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables(); - globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars)); + globalPropertiesEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty( + node.getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), + null, nodeEnvVars)); } } @@ -80,6 +85,11 @@ private EnvVars getNewSlaveEnvironmentVariables(Computer c, FilePath nodePath, T EnvInjectLogger logger = new EnvInjectLogger(listener); EnvInjectEnvVars envInjectEnvVarsService = new EnvInjectEnvVars(logger); + final Node node = c.getNode(); + if (node == null) { + throw new EnvInjectException("Node is removed, but the computer has not gone yet"); + } + //Get env vars for the current node Map nodeEnvVars = nodePath.act( new Callable, IOException>() { @@ -90,7 +100,7 @@ public Map call() throws IOException { // -- Process slave properties boolean unsetSystemVariables = false; - for (NodeProperty nodeProperty : c.getNode().getNodeProperties()) { + for (NodeProperty nodeProperty : node.getNodeProperties()) { if (nodeProperty instanceof EnvironmentVariablesNodeProperty) { currentEnvVars.putAll(((EnvironmentVariablesNodeProperty) nodeProperty).getEnvVars()); @@ -99,7 +109,7 @@ public Map call() throws IOException { if (nodeProperty instanceof EnvInjectNodeProperty) { EnvInjectNodeProperty envInjectNodeProperty = ((EnvInjectNodeProperty) nodeProperty); unsetSystemVariables = envInjectNodeProperty.isUnsetSystemVariables(); - currentEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(c.getNode().getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars)); + currentEnvVars.putAll(envInjectEnvVarsService.getEnvVarsFileProperty(node.getRootPath(), logger, envInjectNodeProperty.getPropertiesFilePath(), null, nodeEnvVars)); } } @@ -121,7 +131,8 @@ public Map call() throws IOException { public void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException { //Get node path - FilePath nodePath = c.getNode().getRootPath(); + final Node node = c.getNode(); + final FilePath nodePath = node != null ? node.getRootPath() : null; if (nodePath == null) { return; } diff --git a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobProperty.java b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobProperty.java index e7f58e5a..2cbbac7d 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobProperty.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectJobProperty.java @@ -16,6 +16,7 @@ import org.kohsuke.stapler.StaplerRequest; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import javax.annotation.CheckForNull; @@ -69,7 +70,7 @@ public EnvInjectJobPropertyContributor[] getContributors() { contributors = contributorsComputed; } - return contributors; + return Arrays.copyOf(contributors, contributors.length); } private EnvInjectJobPropertyContributor[] computeEnvInjectContributors() throws org.jenkinsci.lib.envinject.EnvInjectException { diff --git a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java index a75ce111..271df8b4 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectListener.java @@ -127,7 +127,9 @@ public void beforeUse(AbstractBuild build, FilePath ws, BuildListener listener) Map cleanVariables = envInjectEnvVars.removeUnsetVars(previousEnvVars); //Set new env vars - new EnvInjectActionSetter(build.getBuiltOn().getRootPath()).addEnvVarsToEnvInjectBuildAction(build, cleanVariables); + final Node builtOn = build.getBuiltOn(); + new EnvInjectActionSetter(builtOn != null ? builtOn.getRootPath() : null) + .addEnvVarsToEnvInjectBuildAction(build, cleanVariables); } catch (EnvInjectException e) { throw new RuntimeException(e); diff --git a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectNodeProperty.java b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectNodeProperty.java index 5d3f2734..8d32b6c1 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectNodeProperty.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectNodeProperty.java @@ -5,6 +5,7 @@ import hudson.model.Node; import hudson.slaves.NodeProperty; import hudson.slaves.NodePropertyDescriptor; +import java.util.Arrays; import net.sf.json.JSONObject; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; @@ -63,7 +64,7 @@ public String getHelpFile() { @SuppressWarnings("unused") public EnvInjectGlobalPasswordEntry[] getEnvInjectGlobalPasswordEntries() { - return envInjectGlobalPasswordEntries; + return Arrays.copyOf(envInjectGlobalPasswordEntries, envInjectGlobalPasswordEntries.length); } @Override diff --git a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectPasswordWrapper.java b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectPasswordWrapper.java index 35071b1d..cc3f8def 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectPasswordWrapper.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectPasswordWrapper.java @@ -178,7 +178,7 @@ class EnvInjectPasswordsOutputStream extends LineTransformationOutputStream { nbMaskedPasswords++; } } - if (nbMaskedPasswords++ >= 1) { // is there at least one password to mask? + if (nbMaskedPasswords >= 1) { // is there at least one password to mask? regex.deleteCharAt(regex.length() - 1); // removes the last unuseful pipe regex.append(')'); passwordsAsPattern = Pattern.compile(regex.toString()); diff --git a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectVarList.java b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectVarList.java index 77f5a8d7..efb55952 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectVarList.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/EnvInjectVarList.java @@ -105,7 +105,7 @@ private void writeTextResponse(StaplerResponse response) throws IOException { response.setContentType("plain/text"); StringWriter stringWriter = new StringWriter(); for (Map.Entry entry : envVars.entrySet()) { - stringWriter.write(String.format("%s%s%s\n", entry.getKey(), "=", entry.getValue())); + stringWriter.write(String.format("%s%s%s%n", entry.getKey(), "=", entry.getValue())); } response.getOutputStream().write(stringWriter.toString().getBytes()); } diff --git a/src/main/java/org/jenkinsci/plugins/envinject/service/BuildCauseRetriever.java b/src/main/java/org/jenkinsci/plugins/envinject/service/BuildCauseRetriever.java index 4cb84994..93ac50a7 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/service/BuildCauseRetriever.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/service/BuildCauseRetriever.java @@ -15,6 +15,7 @@ import java.util.Set; import static com.google.common.base.Joiner.on; +import java.util.Locale; import static org.apache.commons.lang.StringUtils.isNotBlank; @@ -87,7 +88,7 @@ private static String getTriggerName(Cause cause) { } else if (Cause.UpstreamCause.class.isInstance(cause)) { return "UPSTREAMTRIGGER"; } else if (cause != null) { - return cause.getClass().getSimpleName().toUpperCase(); + return cause.getClass().getSimpleName().toUpperCase(Locale.ENGLISH); } return null; diff --git a/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectActionSetter.java b/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectActionSetter.java index d953f336..3db8d4d0 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectActionSetter.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectActionSetter.java @@ -11,6 +11,7 @@ import java.io.Serializable; import java.util.HashMap; import java.util.Map; +import javax.annotation.CheckForNull; /** @@ -18,9 +19,10 @@ */ public class EnvInjectActionSetter implements Serializable { + @CheckForNull private FilePath rootPath; - public EnvInjectActionSetter(FilePath rootPath) { + public EnvInjectActionSetter(@CheckForNull FilePath rootPath) { this.rootPath = rootPath; } diff --git a/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java b/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java index f1145e2e..d4c5fec4 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java @@ -58,6 +58,7 @@ public Map getEnvVarsPropertiesJobProperty(FilePath rootPath, return resultMap; } + @Nonnull public Map getEnvVarsFileProperty(@Nonnull FilePath rootPath, EnvInjectLogger logger, String propertiesFilePath, @@ -120,7 +121,7 @@ public Map executeAndGetMapGroovyScript(EnvInjectLogger logger, return new HashMap(); } - logger.info(String.format("Evaluation the following Groovy script content: \n%s\n", scriptContent)); + logger.info(String.format("Evaluation the following Groovy script content: %n%s%n", scriptContent)); Binding binding = new Binding(); String jobName = envVars.get("JOB_NAME"); diff --git a/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectScriptExecutor.java b/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectScriptExecutor.java index ea7fe7e0..784797e3 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectScriptExecutor.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectScriptExecutor.java @@ -86,7 +86,7 @@ private int executeScriptContent(FilePath scriptExecutionRoot, String scriptCont } FilePath tmpFile = batchRunner.createScriptFile(scriptExecutionRoot); - logger.info(String.format("Executing and processing the following script content: \n%s\n", scriptContent)); + logger.info(String.format("Executing and processing the following script content: %n%s%n", scriptContent)); int cmdCode = launcher.launch().cmds(batchRunner.buildCommandLine(tmpFile)).stdout(launcher.getListener()).envs(scriptExecutionEnvVars).pwd(scriptExecutionRoot).join(); if (cmdCode != 0) { logger.info(String.format("Script executed. The exit code is %s.", cmdCode)); diff --git a/src/main/java/org/jenkinsci/plugins/envinject/service/PropertiesVariablesRetriever.java b/src/main/java/org/jenkinsci/plugins/envinject/service/PropertiesVariablesRetriever.java index 7a9446c3..1400d8e5 100644 --- a/src/main/java/org/jenkinsci/plugins/envinject/service/PropertiesVariablesRetriever.java +++ b/src/main/java/org/jenkinsci/plugins/envinject/service/PropertiesVariablesRetriever.java @@ -58,7 +58,7 @@ public Map invoke(File base, VirtualChannel channel) throws IOEx //Add the properties content if (propertiesContent != null) { PropertiesGetter propertiesGetter = new PropertiesGetter(); - logger.info(String.format("Injecting as environment variables the properties content \n%s\n", propertiesGetter.getPropertiesContentFromMapObject(propertiesContent))); + logger.info(String.format("Injecting as environment variables the properties content %n%s%n", propertiesGetter.getPropertiesContentFromMapObject(propertiesContent))); result.putAll(propertiesContent); logger.info("Variables injected successfully."); }