Skip to content

Commit

Permalink
(fix) Build Monitor no longer throws a NPE when the jobs are ordered …
Browse files Browse the repository at this point in the history
…by status and one or more of them is in progress.

Fixes #181
  • Loading branch information
jan-molak committed Nov 5, 2015
1 parent 48fb9c7 commit fdb6d40
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.smartcodeltd.jenkinsci.plugins.buildmonitor.order;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
Expand All @@ -11,33 +9,33 @@
public class ByStatus implements Comparator<Job<?, ?>> {
@Override
public int compare(Job<?, ?> a, Job<?, ?> b) {
return bothProjectsHaveBuildHistory(a, b) ?
compareLastBuilds(a, b) :
compareProjects(a, b);
return bothProjectsHaveBuildHistory(a, b)
? compareRecentlyCompletedBuilds(a, b)
: compareProjects(a, b);
}

// --

private boolean bothProjectsHaveBuildHistory(Job<?, ?> a, Job<?, ?> b) {
return a.getLastBuild() != null && b.getLastBuild() != null;
return a.getLastCompletedBuild() != null && b.getLastCompletedBuild() != null;
}

private int compareProjects(Job<?, ?> a, Job<?, ?> b) {
Run<?, ?> lastBuildOfA = a.getLastBuild();
Run<?, ?> lastBuildOfB = b.getLastBuild();
Run<?, ?> recentBuildOfA = a.getLastCompletedBuild();
Run<?, ?> recentBuildOfB = b.getLastCompletedBuild();

if (lastBuildOfA == null && lastBuildOfB != null) {
if (recentBuildOfA == null && recentBuildOfB != null) {
return -1;
} else if (lastBuildOfA != null && lastBuildOfB == null) {
} else if (recentBuildOfA != null && recentBuildOfB == null) {
return 1;
} else {
return 0;
}
}

private int compareLastBuilds(Job<?, ?> a, Job<?, ?> b) {
Result lastResultOfA = a.getLastBuild().getResult();
Result lastResultOfB = b.getLastBuild().getResult();
private int compareRecentlyCompletedBuilds(Job<?, ?> a, Job<?, ?> b) {
Result lastResultOfA = a.getLastCompletedBuild().getResult();
Result lastResultOfB = b.getLastCompletedBuild().getResult();

if (lastResultOfA.isWorseThan(lastResultOfB)) {
return -1;
Expand All @@ -47,4 +45,4 @@ private int compareLastBuilds(Job<?, ?> a, Job<?, ?> b) {
return 0;
}
}
}
}

0 comments on commit fdb6d40

Please sign in to comment.