Skip to content
Permalink
Browse files

Fixed JENKINS-24284

  • Loading branch information...
Andrius Ordojan
Andrius Ordojan committed Aug 15, 2014
1 parent 9bdbb3a commit 7d0bf3c81dc098582da6a433d86c281abc7f1787
@@ -12,29 +12,26 @@
import hudson.model.BuildListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.util.BuildData;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.pretestedintegration.AbstractSCMBridge;
import org.jenkinsci.plugins.pretestedintegration.Commit;
import org.jenkinsci.plugins.pretestedintegration.*;
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegationFailedExeception;
import org.jenkinsci.plugins.pretestedintegration.IntegrationStrategy;
import org.jenkinsci.plugins.pretestedintegration.IntegrationStrategyDescriptor;
import org.jenkinsci.plugins.pretestedintegration.PretestedIntegrationAction;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Mads
*/
public class SquashCommitStrategy extends IntegrationStrategy {

private static final String B_NAME = "Squashed commit";
private static final Logger logger = Logger.getLogger(SquashCommitStrategy.class.getName());

@DataBoundConstructor
public SquashCommitStrategy() { }

@@ -43,21 +40,21 @@ public void integrate(AbstractBuild<?,?> build, Launcher launcher, BuildListener
int exitCode = -999;
int exitCodeCommit = -999;
GitBridge gitbridge = (GitBridge)bridge;

ByteArrayOutputStream out = new ByteArrayOutputStream();
BuildData gitBuildData = build.getAction(BuildData.class);
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();

String integrationSHA = "Not specified";
try {
integrationSHA = (String)build.getAction(PretestedIntegrationAction.class).getCurrentIntegrationTip().getId();
} catch (Exception ex) {

}
listener.getLogger().println( String.format( "Preparing to merge changes in commit %s to integration branch %s(%s)", gitDataBranch.getSHA1String(), bridge.getBranch(), integrationSHA) );
} catch (Exception ex) {}

listener.getLogger().println( String.format( "Preparing to merge changes in commit %s to integration branch %s(%s)", gitDataBranch.getSHA1String(), bridge.getBranch(), integrationSHA) );
boolean found = false;
try {
GitClient client = Git.with(listener, build.getEnvironment(listener)).in(build.getWorkspace()).getClient();

for(Branch b : client.getRemoteBranches()) {
if(b.getName().equals(gitDataBranch.getName())) {
found = true;
@@ -68,38 +65,44 @@ public void integrate(AbstractBuild<?,?> build, Launcher launcher, BuildListener
logger.log(Level.SEVERE, "GitClient error", ex);
throw new IntegationFailedExeception("Unspecified GitClient error",ex);
}

if(!found) {
try {
build.setDescription(String.format("Noting to do"));
} catch (IOException ex) {
logger.log(Level.FINE, "Failed to update description", ex);
}
} catch (IOException ex) {
logger.log(Level.FINE, "Failed to update description", ex);
}
throw new NothingToDoException();
}

try {

String[] split = gitDataBranch.getName().split("/");
String branchNameWithNoRemote = "";
for (int i = 1; i < split.length; i++) {
branchNameWithNoRemote += split[i] + "/";
}
branchNameWithNoRemote = branchNameWithNoRemote.substring(0, branchNameWithNoRemote.length() - 1);


exitCode = gitbridge.git(build, launcher, listener, out, "merge", "--squash", gitDataBranch.getName());
exitCodeCommit = gitbridge.git(build, launcher, listener, out, "commit", "-m", String.format("Integrated %s", gitDataBranch.getName()));

exitCodeCommit = gitbridge.git(build, launcher, listener, out, "commit", "-m", String.format("Integrated %s", branchNameWithNoRemote));
} catch (Exception ex) { /*Handled below */ }

if (exitCode != 0) {
listener.getLogger().println("Failed to merge changes. Error message below");
listener.getLogger().println(out.toString());
try {
try {
build.setDescription(String.format("Merge conflict"));
} catch (IOException ex) {
logger.log(Level.FINE, "Failed to update description", ex);
}
throw new IntegationFailedExeception();
}

if (exitCodeCommit != 0 && exitCodeCommit != -999 ) {
listener.getLogger().println("Failed to commit merged changes. Error message below");
listener.getLogger().println(out.toString());

try {
if(out.toString().contains("nothing to commit")) {
build.setDescription(String.format("Nothing to do"));
@@ -109,11 +112,11 @@ public void integrate(AbstractBuild<?,?> build, Launcher launcher, BuildListener
} catch (IOException ex ) {
logger.log(Level.FINE, "Failed to update description", ex);
}

if(out.toString().contains("nothing to commit")) {
throw new NothingToDoException();
}

throw new IntegationFailedExeception("Could commit merges. Git output: " + out.toString());
}
}
@@ -124,15 +127,15 @@ public void integrate(AbstractBuild<?,?> build, Launcher launcher, BuildListener
public DescriptorImpl() {
load();
}

public String getDisplayName() {
return B_NAME;
}
}

@Override
public boolean isApplicable(Class<? extends AbstractSCMBridge> bridge) {
return GitBridge.class.equals(bridge);
public boolean isApplicable(Class<? extends AbstractSCMBridge> bridge) {
return GitBridge.class.equals(bridge);
}
}

}

0 comments on commit 7d0bf3c

Please sign in to comment.
You can’t perform that action at this time.