Skip to content

Commit

Permalink
Implemented #132. Retry push
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsNielsen committed Aug 16, 2018
1 parent a2b4a86 commit cc19768
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
* The Git SCM Bridge.
Expand Down Expand Up @@ -66,7 +67,7 @@ public GitBridge(IntegrationStrategy integrationStrategy, final String integrati

public static void pushToIntegrationBranchGit(Run<?, ?> run, TaskListener listener, GitClient client, String expandedRepo, String expandedBranch) throws PushFailedException {
try {
pushToBranch(listener, client, expandedBranch, expandedRepo);
pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo, 0);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to integration branch. Exception:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to integration branch. Exception %s", ex));
Expand All @@ -83,11 +84,42 @@ public static void pushToBranch(TaskListener listener, GitClient client, String
listener.getLogger().println(GitMessages.LOG_PREFIX+ "Done pushing changes");
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to: " + branchToPush + ".\nException:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to: " + branchToPush + ".\nException: %s", ex));
listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to: " + branchToPush + ".\nException: %s", ex));
throw new PushFailedException(String.format("Failed to push changes to integration branch, message was:%n%s", ex));
}
}

public static void pushToBranch(TaskListener listener, GitClient client, String sourceLocalBranch, String targetRemoteBranch, String expandedRepo) throws PushFailedException {
pushToBranch(listener, client, sourceLocalBranch, targetRemoteBranch, expandedRepo, 0);
}

public static void pushToBranch(TaskListener listener, GitClient client, String sourceLocalBranch, String targetRemoteBranch, String expandedRepo, int retries) throws PushFailedException {
try {
LOGGER.log(Level.INFO, "Pushing changes from local branch: " + sourceLocalBranch + " to remote branch: " + targetRemoteBranch);
listener.getLogger().println(GitMessages.LOG_PREFIX + "Pushing changes to branch:");
client.push(expandedRepo, "refs/heads/" + sourceLocalBranch + ":refs/heads/" + targetRemoteBranch.replace(expandedRepo + "/", ""));
LOGGER.log(Level.INFO, "Done pushing changes");
listener.getLogger().println(GitMessages.LOG_PREFIX + "Done pushing changes");
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to: " + targetRemoteBranch + ".\nException:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to: " + targetRemoteBranch + ".\nException: %s", ex));
throw new PushFailedException(String.format("Failed to push changes to branch, message was:%n%s", ex));
} catch (GitException gex) {
final Pattern nonFastForward = Pattern.compile(".*[rejected].*\\(non-fast-forward\\).*", Pattern.DOTALL);
//Something is wrong on the remote and it's not a fast forward issue...try again
if (gex.getMessage() != null && !nonFastForward.matcher(gex.getMessage()).matches() && retries > 0) {
LOGGER.log(Level.WARNING, LOG_PREFIX + "Failed to push...retrying in 5 seconds");
listener.getLogger().println(LOG_PREFIX + "Failed to push...retrying in 5 seconds");
try {
Thread.sleep(5000); //Wait 5 seconds
} catch (InterruptedException e) { /* NOOP */ }
GitBridge.pushToBranch(listener, client, sourceLocalBranch, targetRemoteBranch, expandedRepo, --retries);
} else {
throw gex;
}
}
}

public static void deleteBranch(Run<?, ?> run, TaskListener listener, GitClient client, String branchToBeDeleted, String expandedRepo) throws BranchDeletionFailedException, IOException {
try {
LOGGER.log(Level.INFO, "Deleting branch:");
Expand Down Expand Up @@ -210,7 +242,7 @@ public void pushToIntegrationBranch(AbstractBuild<?, ?> build, BuildListener lis

GitSCM gitSCM = findScm(build, listener);
GitClient client = gitSCM.createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
pushToBranch(listener, client, expandedBranch, expandedRepo);
pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo, 0);
} catch (IOException | InterruptedException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to integration branch. Exception:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX+ String.format("Failed to push changes to integration branch. Exception %s", ex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.GitUtils;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.gitclient.ChangelogCommand;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.MergeCommand;
import org.jenkinsci.plugins.pretestedintegration.IntegrationStrategy;
Expand All @@ -28,13 +27,10 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

import static org.eclipse.jgit.lib.Constants.HEAD;

Expand Down

0 comments on commit cc19768

Please sign in to comment.