Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-47167] - Handle null build when serializing the object to the disk, patch FindBugs #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
}
}

}