JENKINS-36489# NPE fix

This commit is contained in:
Vivek Pandey 2016-07-12 12:09:24 -07:00
parent 07c80ed822
commit e28c77e13a
1 changed files with 22 additions and 7 deletions

View File

@ -133,24 +133,39 @@ public class MultiBranchPipelineImpl extends BlueMultiBranchPipeline {
/**
* TODO: this code need cleanup once MultiBranchProject exposes default branch. At present
*
* At present we look for master as primary branch, if not found we find the latest build and return
* its score.
* At present we look for master as primary branch, if not found we find the latest build across all branches and
* return its score.
*
* If there are no builds taken place 0 score is returned.
*/
Job j = mbp.getBranch("master");
Job j = mbp.getItem("master");
if(j == null) {
j = mbp.getBranch("production");
if(j == null){ //get latest
j = mbp.getItem("production");
/**
* If there are no master or production branch then we return weather score of
*
* - Sort latest build of all branches in ascending order
* - Return the latest
*
*/
if(j == null){
Collection<Job> jbs = mbp.getAllJobs();
if(jbs.size() > 0){
Job[] jobs = jbs.toArray(new Job[jbs.size()]);
Arrays.sort(jobs, new Comparator<Job>() {
@Override
public int compare(Job o1, Job o2) {
long t1 = o1.getLastBuild().getTimeInMillis() + o1.getLastBuild().getDuration();
long t2 = o2.getLastBuild().getTimeInMillis() + o2.getLastBuild().getDuration();
long t1 = 0;
if(o1.getLastBuild() != null){
t1 = o1.getLastBuild().getTimeInMillis() + o1.getLastBuild().getDuration();
}
long t2 = 0;
if(o2.getLastBuild() != null){
t2 = o2.getLastBuild().getTimeInMillis() + o2.getLastBuild().getDuration();
}
if(t1<2){
return -1;
}else if(t1 > t2){