Skip to content

Instantly share code, notes, and snippets.

@ieure
Created July 2, 2010 22:14
Show Gist options
  • Save ieure/461982 to your computer and use it in GitHub Desktop.
Save ieure/461982 to your computer and use it in GitHub Desktop.
diff -u DefaultBuildChooser.java RetryBuildChooser.java
--- DefaultBuildChooser.java 2010-07-02 13:34:57.000000000 -0700
+++ RetryBuildChooser.java 2010-07-02 14:46:22.000000000 -0700
@@ -5,10 +5,9 @@
import hudson.plugins.git.Branch;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitException;
-import hudson.plugins.git.IGitAPI;
import hudson.plugins.git.Revision;
+import hudson.plugins.git.util.DefaultBuildChooser;
import org.kohsuke.stapler.DataBoundConstructor;
-import org.spearce.jgit.lib.ObjectId;
import java.io.IOException;
import java.util.Collection;
@@ -16,77 +15,9 @@
import java.util.Iterator;
import java.util.Set;
-import static java.util.Collections.emptyList;
-
-public class DefaultBuildChooser extends BuildChooser {
+public class RetryBuildChooser extends DefaultBuildChooser {
@DataBoundConstructor
- public DefaultBuildChooser() {
- }
-
- /**
- * Determines which Revisions to build.
- *
- * If only one branch is chosen and only one repository is listed, then
- * just attempt to find the latest revision number for the chosen branch.
- *
- * If multiple branches are selected or the branches include wildcards, then
- * use the advanced usecase as defined in the getAdvancedCandidateRevisons
- * method.
- *
- * @throws IOException
- * @throws GitException
- */
- public Collection<Revision> getCandidateRevisions(boolean isPollCall, String singleBranch,
- IGitAPI git, TaskListener listener, BuildData data)
- throws GitException, IOException {
- // if the branch name contains more wildcards then the simple usecase
- // does not apply and we need to skip to the advanced usecase
- if (singleBranch == null || singleBranch.contains("*"))
- return getAdvancedCandidateRevisions(isPollCall,new GitUtils(listener,git),data);
-
- // check if we're trying to build a specific commit
- // this only makes sense for a build, there is no
- // reason to poll for a commit
- if (!isPollCall && singleBranch.matches("[0-9a-f]{6,40}")) {
- try {
- ObjectId sha1 = git.revParse(singleBranch);
- Revision revision = new Revision(sha1);
- revision.getBranches().add(new Branch("detached", sha1));
- return Collections.singletonList(revision);
- }
- catch (GitException e) {
- // revision does not exist, may still be a branch
- // for example a branch called "badface" would show up here
- }
- }
-
- // if it doesn't contain '/' then it could be either a tag or an unqualified branch
- if (!singleBranch.contains("/")) {
- // the 'branch' could actually be a tag:
- Set<String> tags = git.getTagNames(singleBranch);
- if(tags.size() == 0) {
- // its not a tag, so lets fully qualify the branch
- String repository = gitSCM.getRepositories().get(0).getName();
- singleBranch = repository + "/" + singleBranch;
- }
- }
-
- try {
- ObjectId sha1 = git.revParse(singleBranch);
-
- // if polling for changes don't select something that has
- // already been built as a build candidate
- if (isPollCall && data.hasBeenBuilt(sha1))
- return emptyList();
-
- Revision revision = new Revision(sha1);
- revision.getBranches().add(new Branch(singleBranch, sha1));
- return Collections.singletonList(revision);
- }
- catch (GitException e) {
- // branch does not exist, there is nothing to build
- return emptyList();
- }
+ public RetryBuildChooser() {
}
/**
@@ -98,7 +29,9 @@
* Any Revisions with no interesting branches are dropped.
* 3. Get rid of any revisions that are wholly subsumed by another
* revision we're considering.
- * 4. Get rid of any revisions that we've already built.
+ * 4. Don't get rid of any revisions that we've already
+ * built. Merging will be a no-op if it was a successful
+ * build, and will cause a retry if it failed.
*
* NB: Alternate BuildChooser implementations are possible - this
* may be beneficial if "only 1" branch is to be built, as much of
@@ -126,25 +59,25 @@
}
}
- if (!keep) j.remove();
-
+ if (!keep) {
+ j.remove();
+ }
}
- if (r.getBranches().size() == 0) i.remove();
-
+ if (r.getBranches().size() == 0) {
+ i.remove();
+ }
}
// 3. We only want 'tip' revisions
revs = utils.filterTipBranches(revs);
- // 4. Finally, remove any revisions that have already been built.
- for (Iterator<Revision> i = revs.iterator(); i.hasNext();) {
- Revision r = i.next();
-
- if (data.hasBeenBuilt(r.getSha1())) {
- i.remove();
- }
- }
+ // DefaultBuildChooser filters out hashes we've already built;
+ // however, this will fail to retry builds which weren't
+ // successful. We don't do that here. Ideally, we'd filter
+ // out revisions that had been built successfully, instead of
+ // including everything, but merging code which has already
+ // been merged is a noop.
// if we're trying to run a build (not an SCM poll) and nothing new
// was found then just run the last build again
@@ -159,12 +92,12 @@
public static final class DescriptorImpl extends BuildChooserDescriptor {
@Override
public String getDisplayName() {
- return "Default";
+ return "Retry";
}
@Override
public String getLegacyId() {
- return "Default";
+ return "Retry";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment