Skip to content

Commit

Permalink
[FIXED JENKINS-11563]
Browse files Browse the repository at this point in the history
It would be better to find out why the nextBuildNumber is at times empty.
In the mean time this will find the appropriate number and write it to the file.
  • Loading branch information
emanuelez authored and kohsuke committed Nov 2, 2011
1 parent b20ef41 commit 8affa6d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -66,6 +66,9 @@ <h3><a name=v1.438>What's new in 1.438</a> <!--=DATE=--></h3>
<li class=bug>
Repeated ids, expandTextArea() and multiple "Invoke Ant" build steps.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-10989">issue 10989</a>)
<li class=bug>
Improve the resilience to the missing 'nextBuildNumber' file.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-11563">issue 11563</a>)
<li class=bug>
NPE when running Maven 3 jobs with -T.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-11458">issue 11458</a>)
Expand Down
33 changes: 22 additions & 11 deletions core/src/main/java/hudson/model/Job.java
Expand Up @@ -23,6 +23,8 @@
*/
package hudson.model;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import hudson.Extension;
import hudson.ExtensionPoint;
Expand Down Expand Up @@ -78,18 +80,10 @@

import javax.servlet.ServletException;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;

import static javax.servlet.http.HttpServletResponse.*;

Expand Down Expand Up @@ -166,7 +160,24 @@ public void onLoad(ItemGroup<? extends Item> parent, String name)
this.nextBuildNumber = Integer.parseInt(f.readTrim());
}
} catch (NumberFormatException e) {
throw new IOException2(f + " doesn't contain a number", e);
// try to infer the value of the next build number from the existing build records. See JENKINS-11563
File[] folders = this.getBuildDir().listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isDirectory() && file.getName().matches("[0-9]+");
}
});

if (folders == null || folders.length == 0) {
this.nextBuildNumber = 1;
} else {
Collection<Integer> foldersInt = Collections2.transform(Arrays.asList(folders), new Function<File, Integer>() {
public Integer apply(File file) {
return Integer.parseInt(file.getName());
}
});
this.nextBuildNumber = Collections.max(foldersInt) + 1;
}
saveNextBuildNumber();
}
} else {
// From the old Hudson, or doCreateItem. Create this file now.
Expand Down

0 comments on commit 8affa6d

Please sign in to comment.