Skip to content

Commit

Permalink
Merge pull request #13 from oleg-nenashev/bug/JENKINS-47167-diagnostics
Browse files Browse the repository at this point in the history
[JENKINS-47167] - Handle null build when serializing the object to the disk, patch FindBugs
  • Loading branch information
oleg-nenashev committed Oct 24, 2017
2 parents 781b257 + fb78337 commit e2d0a75
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -61,6 +61,7 @@
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
<failOnError>true</failOnError>
</configuration>
<executions>
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/org/jenkinsci/lib/envinject/EnvInjectAction.java
Expand Up @@ -10,6 +10,7 @@
import org.kohsuke.stapler.StaplerProxy;

import java.io.File;
import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -140,18 +141,21 @@ private Object writeReplace() throws ObjectStreamException {
toWrite = Collections.<String, String>emptyMap();
}

if (rootDir == null) {
if (build == null && rootDir == null) {
throw new InvalidObjectException("Cannot save the environment file. Action " + this + " has no associated run instance. Target root dir is unknown");
}

if (rootDir == null) { // New logic
dao.saveEnvironment(build.getRootDir(), Maps.transformEntries(toWrite,
new Maps.EntryTransformer<String, String, String>() {
public String transformEntry(String key, String value) {
return (sensibleVariables != null && sensibleVariables.contains(key))
? "********" : value;
}
}));
return this;
} else { // Fall-back to the legacy logic
dao.saveEnvironment(rootDir, toWrite);
}

dao.saveEnvironment(rootDir, toWrite);
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.lib.envinject;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.TaskListener;

import java.io.Serializable;
Expand All @@ -8,6 +9,7 @@
/**
* @author Gregory Boissinot
*/
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID", justification = "Legacy code")
public class EnvInjectLogger implements Serializable {

@Nonnull
Expand Down
Expand Up @@ -11,6 +11,8 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;

/**
* @author Gregory Boissinot
* @deprecated The actual version of this API class is located in EnvInject API Plugin
Expand Down Expand Up @@ -53,7 +55,7 @@ public Map<String, String> getEnvironment(File envInjectBaseDir) throws EnvInjec
}

@SuppressFBWarnings(value = "DM_DEFAULT_ENCODING", justification = "Deprecated class")
public void saveEnvironment(File rootDir, Map<String, String> envMap) throws EnvInjectException {
public void saveEnvironment(@Nonnull File rootDir,@Nonnull Map<String, String> envMap) throws EnvInjectException {
FileWriter fileWriter = null;
try {
File f = new File(rootDir, ENVINJECT_TXT_FILENAME);
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.lib.envinject.service;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Util;
Expand Down Expand Up @@ -29,6 +30,7 @@
*/
@Deprecated
@Restricted(NoExternalUse.class)
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID", justification = "Deprecated code")
public class EnvVarsResolver implements Serializable {

public Map<String, String> getPollingEnvVars(AbstractProject project, /*can be null*/ Node node) throws EnvInjectException {
Expand Down Expand Up @@ -204,11 +206,7 @@ private Map<String, String> gatherEnvVarsNode(@Nonnull AbstractProject project,
}

try {
Map<String, String> envVars = new EnvVars(p.act(new MasterToSlaveCallable<Map<String, String>, EnvInjectException>() {
public Map<String, String> call() throws EnvInjectException {
return EnvVars.masterEnvVars;
}
}));
Map<String, String> envVars = new EnvVars(p.act(new SystemEnvVarsGetter()));

envVars.put("NODE_NAME", node.getNodeName());
envVars.put("NODE_LABELS", Util.join(node.getAssignedLabels(), " "));
Expand All @@ -226,5 +224,11 @@ public Map<String, String> call() throws EnvInjectException {
}
}

private static final class SystemEnvVarsGetter extends MasterToSlaveCallable<Map<String, String>, EnvInjectException> {
public Map<String, String> call() throws EnvInjectException {
return EnvVars.masterEnvVars;
}
}

}

0 comments on commit e2d0a75

Please sign in to comment.