Skip to content

Commit

Permalink
Update to gh-133. Retry counter
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsNielsen committed Aug 15, 2018
1 parent 73cd54f commit c7219e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,35 @@ 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));
throw new PushFailedException(String.format("Failed to push changes to branch: " + expandedBranch + ", message was:%n%s", ex));
}
}

public static void pushToBranch(TaskListener listener, GitClient client, String sourceLocalBranch, String targetRemoteBranch, String expandedRepo) throws PushFailedException {
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()) {
LOGGER.log(Level.WARNING, LOG_PREFIX + "Failed to push...retying in 5 seconds");
listener.getLogger().println(LOG_PREFIX + "Failed to push...retying in 5 seconds");
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);
GitBridge.pushToBranch(listener, client, sourceLocalBranch, targetRemoteBranch, expandedRepo, --retries);
} else {
throw gex;
}
Expand Down Expand Up @@ -224,7 +224,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,32 +12,29 @@
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;
import org.jenkinsci.plugins.pretestedintegration.IntegrationStrategyDescriptor;
import org.jenkinsci.plugins.pretestedintegration.exceptions.*;
import org.jenkinsci.plugins.pretestedintegration.exceptions.EstablishingWorkspaceFailedException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegrationFailedException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.UnsupportedConfigurationException;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
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 java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

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

Expand Down Expand Up @@ -140,7 +137,7 @@ public Revision decorateRevisionToBuild(
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 c7219e8

Please sign in to comment.