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 aea10ca commit abd5c44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 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, expandedBranch, expandedRepo);
pushToBranch(listener, client, expandedBranch, expandedBranch, expandedRepo, 0);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to branch: " + expandedBranch +". Exception:", ex);
listener.getLogger().println(GitMessages.LOG_PREFIX + String.format("Failed to push changes to branch: \" + expandedBranch +\". Exception: %s", ex));
Expand All @@ -75,16 +76,33 @@ public static void pushToIntegrationBranchGit(Run<?, ?> run, TaskListener listen
}

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 + "/",""));
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");
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));
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;
}
}
}

Expand Down Expand Up @@ -210,7 +228,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, 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,13 +12,12 @@
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.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.URIish;
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 @@ -31,14 +30,11 @@
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URISyntaxException;
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 Expand Up @@ -139,10 +135,9 @@ public Revision decorateRevisionToBuild(
if(run.getActions(PretestTriggerCommitAction.class).isEmpty() ) {
GitIntegrationStrategy gitStrategy = (GitIntegrationStrategy)gitBridge.integrationStrategy;
gitStrategy.integrateAsGitPluginExt(scm, run, git, listener, marked, triggeredBranch, gitBridge);

//For AccumulatedCommit and in pipeline, immediately push back the merge result
if(gitStrategy instanceof AccumulatedCommitStrategy && run instanceof WorkflowRun) {
GitBridge.pushToBranch(listener, git, expandedIntegrationBranch, triggeredBranch.getName(), repoName);
GitBridge.pushToBranch(listener, git, expandedIntegrationBranch, triggeredBranch.getName(), repoName, 3);
}
run.addAction(new PretestTriggerCommitAction(triggeredBranch, expandedIntegrationBranch, expandedRepo, ucCredentialsId));
} else {
Expand Down

0 comments on commit abd5c44

Please sign in to comment.