Skip to content

Commit

Permalink
JENKINS-39704 retry fetching the changeset when the repository appear…
Browse files Browse the repository at this point in the history
…s inconsistent
  • Loading branch information
i386 committed Nov 14, 2016
1 parent 26e65d4 commit 57cd6bd
Showing 1 changed file with 39 additions and 20 deletions.
Expand Up @@ -19,15 +19,18 @@
import hudson.scm.SCMRevisionState;
import jenkins.branch.MultiBranchProject;
import org.apache.commons.io.IOUtils;
import org.eclipse.jgit.errors.MissingObjectException;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

import javax.annotation.CheckForNull;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -52,28 +55,20 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
return;
}

GitClient git = Git.with(TaskListener.NULL, new EnvVars())
.in(new File(workspace.getRemote()))
.getClient();

List<GitChangeSet> changeSets;
try (StringWriter writer = new StringWriter()) {
// Fetch the first commit
git.changelog()
.includes(lastBuiltRevision.getSha1())
.to(writer)
.max(1)
.execute();

// Parse the changelog
GitChangeLogParser parser = new GitChangeLogParser(true);
try (StringReader input = new StringReader(writer.toString())) {
List<String> lines = IOUtils.readLines(input);
changeSets = parser.parse(lines);
// Sometimes the Git repository isn't consistent so we need to retry (JENKINS-39704)
GitChangeSet first;
try {
first = getChangeSet(workspace, lastBuiltRevision);
} catch (MissingObjectException e) {
// Wait before we retry...
Thread.sleep(TimeUnit.SECONDS.toMillis(2));
try {
first = getChangeSet(workspace, lastBuiltRevision);
} catch (MissingObjectException ex) {
logger.log(Level.SEVERE, "Git repository is not consistent. Can't get the changeset that was just checked out.", ex);
first = null;
}
}

GitChangeSet first = Iterables.getOnlyElement(changeSets, null);
if (first == null) {
return;
}
Expand All @@ -94,4 +89,28 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
Favorites.addFavorite(author, job);
logger.log(Level.INFO, "Automatically favorited " + job.getFullName() + " for " + author);
}

private GitChangeSet getChangeSet(FilePath workspace, Revision lastBuiltRevision) throws IOException, InterruptedException {
GitClient git = Git.with(TaskListener.NULL, new EnvVars())
.in(new File(workspace.getRemote()))
.getClient();

List<GitChangeSet> changeSets;
try (StringWriter writer = new StringWriter()) {
// Fetch the first commit
git.changelog()
.includes(lastBuiltRevision.getSha1())
.to(writer)
.max(1)
.execute();

// Parse the changelog
GitChangeLogParser parser = new GitChangeLogParser(true);
try (StringReader input = new StringReader(writer.toString())) {
List<String> lines = IOUtils.readLines(input);
changeSets = parser.parse(lines);
}
}
return Iterables.getOnlyElement(changeSets, null);
}
}

0 comments on commit 57cd6bd

Please sign in to comment.