Skip to content

Commit

Permalink
Fix various Spotbugs detected issues, mostly nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
lacostej committed Oct 30, 2023
1 parent d7fa311 commit 41ac304
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
36 changes: 33 additions & 3 deletions src/main/java/hudson/plugins/batch_task/BatchRun.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.batch_task;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.*;
import hudson.model.*;
import hudson.model.Queue.Executable;
Expand All @@ -21,6 +22,7 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -91,9 +93,9 @@ public File getLogFile() {
return new File(parent.owner.getRootDir(), "task-" + id + ".log");
}

@NonNull
public BatchTask getParent() {
BatchTaskAction jta = parent.owner.getProject().getAction(BatchTaskAction.class);
if (jta == null) return null;
return jta.getTask(taskName);
}

Expand Down Expand Up @@ -206,13 +208,21 @@ public void run() {
try {
long start = System.currentTimeMillis();
listener = new StreamBuildListener(new FileOutputStream(getLogFile()));
Node node = Executor.currentExecutor().getOwner().getNode();
Executor executor = Executor.currentExecutor();
if (executor == null)
throw new AbortException("ERROR: no executor");
Node node = executor.getOwner().getNode();
if (node == null)
throw new AbortException("ERROR: no node present");

Launcher launcher = node.createLauncher(listener);

BatchTask task = getParent();
if (task == null)
throw new AbortException("ERROR: undefined task \"" + taskName + "\"");
AbstractBuild<?, ?> lb = task.owner.getLastBuild();
if (lb == null)
throw new AbortException("ERROR: task \"" + taskName + "\" doesn't have a last build");
FilePath ws = lb.getWorkspace();
if (ws == null)
throw new AbortException(lb.getFullDisplayName() + " doesn't have a workspace.");
Expand Down Expand Up @@ -266,7 +276,14 @@ public String getUrlName() {
Lease wsLease = null;
try {
// Lock the workspace
wsLease = lb.getBuiltOn().toComputer().getWorkspaceList().acquire(ws,
Node builtOn = lb.getBuiltOn();
if (builtOn == null)
throw new AbortException("ERROR: no node for last build");
Computer computer = builtOn.toComputer();
if (computer == null)
throw new AbortException("ERROR: no computer");

wsLease = computer.getWorkspaceList().acquire(ws,
!task.owner.isConcurrentBuild());
// Add environment to build so it will apply when task runs
lb.getActions().add(envAct);
Expand Down Expand Up @@ -321,6 +338,19 @@ public int compareTo(BatchRun that) {
return that.timestamp.compareTo(this.timestamp);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BatchRun batchRun = (BatchRun) o;
return id == batchRun.id && duration == batchRun.duration && Objects.equals(result, batchRun.result) && Objects.equals(timestamp, batchRun.timestamp) && Objects.equals(parent, batchRun.parent) && Objects.equals(taskName, batchRun.taskName);
}

@Override
public int hashCode() {
return Objects.hash(result, timestamp, parent, id, taskName, duration);
}

public long getEstimatedDuration() {
return getParent().getEstimatedDuration();
}
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/hudson/plugins/batch_task/BatchTaskInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,14 @@ public boolean isEvenIfUnstable() {
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
HashSet<String> seenJobs = new HashSet<String>();
if (build.getResult().isBetterOrEqualTo(threshold)) {
for (Config config : configs)
config.invoke(build, listener, seenJobs);
if (build != null) {
Result result = build.getResult();
if (result != null) {
if (result.isBetterOrEqualTo(threshold)) {
for (Config config : configs)
config.invoke(build, listener, seenJobs);
}
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.batch_task;

import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Action;
Expand Down Expand Up @@ -107,8 +108,8 @@ public String getDisplayName() {
}

@Override
public BatchTaskProperty newInstance(StaplerRequest req, JSONObject formData) throws FormException {
if(req.getParameter("batch-tasks.on")!=null)
public BatchTaskProperty newInstance(@Nullable StaplerRequest req, JSONObject formData) throws FormException {
if(req != null && req.getParameter("batch-tasks.on")!=null)
return new BatchTaskProperty(req.bindParametersToList(BatchTask.class, "batch-task."));
else
return null;
Expand Down

0 comments on commit 41ac304

Please sign in to comment.