From abd5c44cbd6d7afb30fa77d28dfa615518135543 Mon Sep 17 00:00:00 2001 From: Mads Nielsen Date: Wed, 15 Aug 2018 12:32:26 +0200 Subject: [PATCH] Implemented #132. Retry push --- .../scm/git/GitBridge.java | 30 +++++++++++++++---- .../PretestedIntegrationAsGitPluginExt.java | 9 ++---- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java index 9540859d..4fa3d911 100644 --- a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java +++ b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/GitBridge.java @@ -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. @@ -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)); @@ -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; + } } } @@ -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)); diff --git a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java index b25f1f2e..a59739a5 100644 --- a/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java +++ b/src/main/java/org/jenkinsci/plugins/pretestedintegration/scm/git/PretestedIntegrationAsGitPluginExt.java @@ -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; @@ -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; @@ -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 {