Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replaced CLI calls with GitClient implementations. [JENKINS-29104]
Merged in master.
  • Loading branch information
MadsNielsen authored and Thierry Lacour committed Oct 30, 2015
1 parent 0ce224f commit 7d9f16e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -456,7 +456,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>2.4.0</version>
<version>2.4.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.jenkinsci.plugins.pretestedintegration.exceptions.NextCommitFailureException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import org.jenkinsci.plugins.pretestedintegration.exceptions.UnsupportedConfigurationException;
import org.kohsuke.stapler.DataBoundConstructor;

public abstract class AbstractSCMBridge implements Describable<AbstractSCMBridge>, ExtensionPoint {

Expand Down
Expand Up @@ -11,6 +11,7 @@
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegationFailedExeception;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import org.eclipse.jgit.lib.PersonIdent;
import org.jenkinsci.plugins.pretestedintegration.exceptions.UnsupportedConfigurationException;

public abstract class IntegrationStrategy implements Describable<IntegrationStrategy>, ExtensionPoint {
Expand All @@ -31,5 +32,16 @@ public static DescriptorExtensionList<IntegrationStrategy, IntegrationStrategyDe
logger.exiting("IntegrationStrategy", "all");// Generated code DONT TOUCH! Bookmark: 4a32ea823412bf7eb75d28dd9edca807
return Jenkins.getInstance().<IntegrationStrategy, IntegrationStrategyDescriptor<IntegrationStrategy>>getDescriptorList(IntegrationStrategy.class);
}


protected PersonIdent getPersonIdent(String commitAuthor) {
//john Doe <Joh@praqma.net> 1442321765 +0200
int endOfName = commitAuthor.indexOf("<");
String authorName = commitAuthor.substring(0, endOfName-1);
int endOfMail = commitAuthor.indexOf(">");
String authorMail = commitAuthor.substring(endOfName + 1, endOfMail);
int endOfTime = commitAuthor.indexOf(" ", endOfMail+2);
long time = Long.parseLong(commitAuthor.substring(endOfMail + 2, endOfTime));
int zone = Integer.parseInt(commitAuthor.substring(commitAuthor.indexOf(" ", commitAuthor.indexOf(">")+2)+1));
return new PersonIdent(authorName, authorMail, time, zone);
}
}
Expand Up @@ -10,8 +10,9 @@
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.gitclient.Git;
import org.eclipse.jgit.lib.PersonIdent;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.MergeCommand;
import org.jenkinsci.plugins.pretestedintegration.AbstractSCMBridge;
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegationFailedExeception;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void integrate(AbstractBuild<?, ?> build, Launcher launcher, BuildListene
logger.log(Level.INFO, String.format("Preparing to merge changes in commit %s on development branch %s to integration branch %s", commit, gitDataBranch.getName(), gitbridge.getExpandedBranch(build.getEnvironment(listener))));
listener.getLogger().println(String.format(LOG_PREFIX + "Preparing to merge changes in commit %s on development branch %s to integration branch %s", commit, gitDataBranch.getName(), gitbridge.getExpandedBranch(build.getEnvironment(listener))));
logger.fine("Resolving and getting git client from workspace:");
client = Git.with(listener, build.getEnvironment(listener)).in(gitbridge.resolveWorkspace(build, listener)).getClient();
client = gitbridge.findScm(build, listener).createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
logger.fine("Finding remote branches:");
for (Branch b : client.getRemoteBranches()) {
logger.fine(String.format("Found remote branch %s", b.getName()));
Expand Down Expand Up @@ -112,8 +113,14 @@ public void integrate(AbstractBuild<?, ?> build, Launcher launcher, BuildListene
logger.info("Starting accumulated merge (no-ff) - without commit:");
listener.getLogger().println(String.format(LOG_PREFIX + "Starting accumulated merge (no-ff) - without commit:"));
String commitMsg = String.format("%s%n%s", headerLine, commits);
String modifiedCommitMsg = commitMsg.replaceAll("\"", "'");
exitCodeMerge = gitbridge.git(build, launcher, listener, out, "merge", "--no-ff", "--no-commit", "-m", modifiedCommitMsg, commit);
String modifiedCommitMsg = commitMsg.replaceAll("\"","'");
client.merge()
.setMessage(modifiedCommitMsg)
.setCommit(false)
.setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwardMode.NO_FF)
.setRevisionToMerge(gitBuildData.lastBuild.revision.getSha1())
.execute();
exitCodeMerge = 0;
logger.info("Accumulated merge done");
listener.getLogger().println(String.format(LOG_PREFIX + "Accumulated merge done"));
} catch (Exception ex) {
Expand Down Expand Up @@ -157,7 +164,12 @@ public void integrate(AbstractBuild<?, ?> build, Launcher launcher, BuildListene
try {
logger.info("Starting to commit accumulated merge changes:");
listener.getLogger().println(String.format(LOG_PREFIX + "Starting to commit accumulated merge changes:"));
exitCodeCommit = gitbridge.git(build, launcher, listener, out, "commit", "--no-edit", "--author=" + commitAuthor);
String message = client.getWorkTree().child(".git/MERGE_MSG").readToString();
PersonIdent author = getPersonIdent(commitAuthor);
client.setAuthor(author);
client.commit(message);
exitCodeCommit = 0;
//exitCodeCommit = gitbridge.git(build, launcher, listener, out, "commit", "--no-edit", "--author=" + commitAuthor);
logger.info("Commit of accumulated merge done");
listener.getLogger().println(String.format(LOG_PREFIX + "Commit of accumulated merge done"));
} catch (Exception ex) {
Expand Down
Expand Up @@ -30,6 +30,7 @@
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.transport.RefSpec;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.multiplescms.MultiSCM;
Expand Down Expand Up @@ -79,7 +80,7 @@ public GitBridge(IntegrationStrategy integrationStrategy, final String branch) {
* @throws UnsupportedConfigurationException
* When multiple, ambiguous relevant BuildDatas are found.
*/
private GitSCM findScm(AbstractBuild<?, ?> build, BuildListener listener) throws InterruptedException, NothingToDoException, UnsupportedConfigurationException {
public GitSCM findScm(AbstractBuild<?, ?> build, BuildListener listener) throws InterruptedException, NothingToDoException, UnsupportedConfigurationException {
LOGGER.entering("GitBridge", "findScm");
BuildData buildData = findRelevantBuildData(build, listener);

Expand Down Expand Up @@ -190,9 +191,10 @@ public void ensureBranch(AbstractBuild<?, ?> build, Launcher launcher, BuildList
LOGGER.entering("GitBridge", "ensureBranch", new Object[]{build, branch, listener, launcher});// Generated code DONT TOUCH! Bookmark: eb203ba8b33b4c38087310c398984c1a
try {
EnvVars environment = build.getEnvironment(listener);
listener.getLogger().println(String.format(LOGGER_PREFIX + "Checking out integration branch %s:", getExpandedBranch(environment)));
//We need to explicitly checkout the remote we have configured
git(build, launcher, listener, "checkout", "-B", getExpandedBranch(environment), getExpandedRepository(environment) + "/" + getExpandedBranch(environment));
String expandedBranch = getExpandedBranch(environment);
listener.getLogger().println(String.format(LOGGER_PREFIX + "Checking out integration branch %s:", expandedBranch));
GitClient client = findScm(build, listener).createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
client.checkout().branch(branch).ref(getExpandedRepository(environment) + "/" + expandedBranch).deleteBranchIfExist(true).execute();
update(build, launcher, listener);
} catch (IOException | InterruptedException ex) {
LOGGER.log(Level.SEVERE, "ensureBranch", ex);
Expand All @@ -203,10 +205,16 @@ public void ensureBranch(AbstractBuild<?, ?> build, Launcher launcher, BuildList
protected void update(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
try {
EnvVars environment = build.getEnvironment(listener);
String expandedRepo = getExpandedRepository(environment);
String expandedBranch = getExpandedBranch(environment);
GitClient client = findScm(build, listener).createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
client.fetch(expandedRepo, new RefSpec("refs/heads/" + expandedBranch));
client.merge().setRevisionToMerge(client.revParse(expandedRepo + "/" + expandedBranch)).execute();
} catch (InterruptedException ex) {
LOGGER.exiting("GitBridge", "ensureBranch - InterruptedException");// Generated code DONT TOUCH! Bookmark: 775c55327ca90d7a3b1889cb1547bc4c
throw new EstablishWorkspaceException(ex);
} catch (IOException ex) {
LOGGER.exiting("GitBridge", "ensureBranch - IOException");// Generated code DONT TOUCH! Bookmark: 775c55327ca90d7a3b1889cb1547bc4c
git(build, launcher, listener, "pull", getExpandedRepository(environment), getExpandedBranch(environment));
} catch (InterruptedException | IOException ex) {
LOGGER.exiting("GitBridge", "ensureBranch - EstablishWorkspaceException");
throw new EstablishWorkspaceException(ex);
}
}
Expand All @@ -218,12 +226,13 @@ public void commit(AbstractBuild<?, ?> build, Launcher launcher, BuildListener l
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
EnvVars environment = build.getEnvironment(listener);
GitClient client = findScm(build, listener).createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
LOGGER.log(Level.INFO, "Pushing changes to integration branch:");
listener.getLogger().println(LOGGER_PREFIX + "Pushing changes to integration branch:");
returncode = git(build, launcher, listener, output, "push", getExpandedRepository(environment), getExpandedBranch(environment));
client.push(getExpandedRepository(environment), "refs/heads/" + getExpandedBranch(environment));
LOGGER.log(Level.INFO, "Done pushing changes");
listener.getLogger().println(LOGGER_PREFIX + "Done pushing changes");
} catch (IOException | InterruptedException ex) {
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Failed to push changes to integration branch. Exception:", ex);
listener.getLogger().println(LOGGER_PREFIX + String.format("Failed to push changes to integration branch. Exception %s", ex));
}
Expand Down Expand Up @@ -347,26 +356,20 @@ public void deleteIntegratedBranch(AbstractBuild<?, ?> build, Launcher launcher,

//At this point in time the lastBuild is also the latests. So thats what we use
Branch gitDataBranch = gitBuildData.lastBuild.revision.getBranches().iterator().next();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int delRemote = unlikelyExitCode;

if (build.getResult().isBetterOrEqualTo(getRequiredResult())) {
try {
LOGGER.log(Level.INFO, "Deleting development branch:");
String expandedRepo = getExpandedRepository(build.getEnvironment(listener));
listener.getLogger().println(LOGGER_PREFIX + "Deleting development branch:");
delRemote = git(build, launcher, listener, out, "push", getExpandedRepository(build.getEnvironment(listener)), ":" + removeRepository(gitDataBranch.getName()));
GitClient client = findScm(build, listener).createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());
client.push(expandedRepo, ":" + removeRepository(gitDataBranch.getName()));
listener.getLogger().println("push " + expandedRepo + " :" + removeRepository(gitDataBranch.getName()));
LOGGER.log(Level.INFO, "Done deleting development branch");
listener.getLogger().println(LOGGER_PREFIX + "Done deleting development branch");
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Failed to delete development branch. Exception:", ex);
listener.getLogger().println(LOGGER_PREFIX + "Failed to delete development branch. Exception:" + ex.getMessage());
}

if (delRemote != 0) {
listener.getLogger().println(LOGGER_PREFIX + String.format("Failed to delete development branch. Git command exit code %d, message was:%n%s", delRemote, out.toString()));
LOGGER.log(Level.SEVERE, String.format("Failed to delete development branch. Git command exit code %d, message was:%n%s", delRemote, out.toString()));
LOGGER.exiting("GitBridge", "deleteIntegratedBranch");
throw new DeleteIntegratedBranchException(String.format("Failed to delete development branch %s with the following error:%n%s", gitDataBranch.getName(), out.toString()));
listener.getLogger().println(LOGGER_PREFIX + String.format("Failed to delete development branch. Exception:", ex));
}
}
}
Expand All @@ -389,7 +392,6 @@ public void updateBuildDescription(AbstractBuild<?, ?> build, Launcher launcher,
LOGGER.log(Level.FINE, "Failed to update description", ex); /* Dont care */ }
}
LOGGER.exiting("GitBridge", "updateBuildDescription");// Generated code DONT TOUCH! Bookmark: 4b67859f8914c1ec862b51d3a63b0c88

}

/**
Expand Down
Expand Up @@ -7,17 +7,18 @@
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
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.eclipse.jgit.lib.ObjectId;
import org.jenkinsci.plugins.gitclient.MergeCommand;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.pretestedintegration.*;
import org.jenkinsci.plugins.pretestedintegration.exceptions.IntegationFailedExeception;
import org.jenkinsci.plugins.pretestedintegration.exceptions.NothingToDoException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jgit.lib.PersonIdent;
import org.jenkinsci.plugins.pretestedintegration.exceptions.UnsupportedConfigurationException;
import org.kohsuke.stapler.DataBoundConstructor;

Expand Down Expand Up @@ -55,7 +56,7 @@ public void integrate(AbstractBuild<?, ?> build, Launcher launcher, BuildListene
logger.log(Level.INFO, String.format("Preparing to merge changes in commit %s on development branch %s to integration branch %s", gitDataBranch.getSHA1String(), gitDataBranch.getName(), gitbridge.getExpandedBranch(build.getEnvironment(listener))));
listener.getLogger().println(String.format(LOG_PREFIX + "Preparing to merge changes in commit %s on development branch %s to integration branch %s", gitDataBranch.getSHA1String(), gitDataBranch.getName(), gitbridge.getExpandedBranch(build.getEnvironment(listener))));
logger.fine("Resolving and getting git client from workspace:");
client = Git.with(listener, build.getEnvironment(listener)).in(gitbridge.resolveWorkspace(build, listener)).getClient();
client = gitbridge.findScm(build, listener).createClient(listener, build.getEnvironment(listener), build, build.getWorkspace());

logger.fine("Finding remote branches:");
for (Branch b : client.getRemoteBranches()) {
Expand Down Expand Up @@ -102,7 +103,16 @@ public void integrate(AbstractBuild<?, ?> build, Launcher launcher, BuildListene

logger.info("Starting squash merge - without commit:");
listener.getLogger().println(String.format(LOG_PREFIX + "Starting squash merge - without commit:"));
exitCodeMerge = gitbridge.git(build, launcher, listener, out, "merge", "--squash", gitDataBranch.getName());
listener.getLogger().println( String.format("%smerge --squash %s",LOG_PREFIX, gitDataBranch.getName()) );
client.merge().setSquash(true).setRevisionToMerge(gitDataBranch.getSHA1()).execute();
/*
TODO: We do not have an exit code for the merge anymore. We get exceptions
*/
//exitCodeMerge = gitbridge.git(build, launcher, listener, out, "merge", "--squash", gitDataBranch.getName());



exitCodeMerge = 0;
logger.info("Squash merge done");
listener.getLogger().println(String.format(LOG_PREFIX + "Squash merge done"));
} catch (Exception ex) {
Expand Down Expand Up @@ -147,7 +157,12 @@ public void integrate(AbstractBuild<?, ?> build, Launcher launcher, BuildListene
try {
logger.info("Starting to commit squash merge changes:");
listener.getLogger().println(String.format(LOG_PREFIX + "Starting to commit squash merge changes:"));
exitCodeCommit = gitbridge.git(build, launcher, listener, out, "commit", "--no-edit", "--author=" + commitAuthor);
PersonIdent author = getPersonIdent(commitAuthor);
String message = client.getWorkTree().child(".git/SQUASH_MSG").readToString();
client.setAuthor(author);
client.commit(message);
exitCodeCommit = 0;
//exitCodeCommit = gitbridge.git(build, launcher, listener, out, "commit", "--no-edit", "--author=" + commitAuthor);
logger.info("Commit of squashed merge done");
listener.getLogger().println(String.format(LOG_PREFIX + "Commit of squashed merge done"));

Expand Down Expand Up @@ -240,10 +255,8 @@ private boolean tryRebase(AbstractBuild<?, ?> build, Launcher launcher, BuildLis
String expandedBranch = bridge.getExpandedBranch(build.getEnvironment(listener));

//Rebase the commit, then checkout master for a fast-forward merge.
int rebaseCode = bridge.git(build, launcher, listener, "rebase", expandedBranch, commitId.getName());
if (rebaseCode != 0) {
throw new IntegationFailedExeception("Rebase failed with exit code " + rebaseCode);
}
client.checkout().ref(commitId.getName()).execute();
client.rebase().setUpstream(expandedBranch).execute();
ObjectId rebasedCommit = client.revParse("HEAD");
logger.log(Level.INFO, String.format(LOG_PREFIX + "Rebase successful. Attempting fast-forward merge."));
client.checkout().ref(expandedBranch).execute();
Expand Down
Expand Up @@ -74,7 +74,6 @@ public void integrateMasterBranch() throws Exception {
System.out.println("===CONSOLE===");
System.out.println(console);
System.out.println("===CONSOLE===");

System.out.println("===Result check 1===");
String msg = "Using the master or integration branch for polling and development is not "
+ "allowed since it will attempt to merge it to other branches and delete it after. Failing build.";
Expand Down

0 comments on commit 7d9f16e

Please sign in to comment.