com.codacy.scoobydoo.GitWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scooby-doo-fwk Show documentation
Show all versions of scooby-doo-fwk Show documentation
Automated testing framework
package com.codacy.scoobydoo;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.RebaseCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.RebaseTodoLine;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File;
import java.io.IOException;
import java.util.List;
// TODO: create GitWrapperTests.
public class GitWrapper {
private org.eclipse.jgit.api.Git git;
private final String localPath;
public GitWrapper(String localPath, String remotePath) throws GitAPIException, IOException {
this.localPath = localPath;
File tempGitDirectory = new File(localPath);
CloneCommand git = org.eclipse.jgit.api.Git.cloneRepository()
.setURI(remotePath)
.setDirectory(new File(localPath));
if (tempGitDirectory.exists()) {
FileUtils.deleteDirectory(tempGitDirectory);
}
this.git = git.call();
}
public String getLatestCommitHash() throws GitAPIException {
RevCommit latestCommit = git
.log()
.setMaxCount(1)
.call()
.iterator()
.next();
return latestCommit.getName();
}
public GitWrapper gitCreateBranch(String branchName, String username, String pwd) throws GitAPIException {
if (git
.branchList()
.setListMode(ListBranchCommand.ListMode.ALL)
.call()
.stream()
.map(Ref::getName)
.toList()
.contains("refs/remotes/origin/" + branchName
)){
LoggingHelper.warn("Branch already exists. Deleting branch [" + branchName + "].");
try {
gitDeleteBranchWithForce(branchName, username, pwd, true);
} catch (Exception e) {
LoggingHelper.error("Error deleting branch.", e);
throw e;
}
}
LoggingHelper.info("Creating branch [" + branchName + "].");
git.branchCreate()
.setName(branchName)
.call();
return this;
}
public GitWrapper gitCheckout(String branchName) throws GitAPIException {
LoggingHelper.info("Checking out to branch [" + branchName + "].");
git.checkout()
.setName(branchName)
.call();
return this;
}
public void gitAddFile(String fileName) throws GitAPIException {
LoggingHelper.info("Adding file [" + fileName + "].");
git.add()
.addFilepattern(fileName)
.call();
}
public void gitCommit(String message) throws GitAPIException {
LoggingHelper.info("Committing.");
git.commit()
.setMessage(message)
.call();
}
public void gitPush(String username, String password) throws GitAPIException {
gitPush(username, password, false);
}
public void gitPushForce(String username, String password) throws GitAPIException {
LoggingHelper.info("You are pushing with force, I hope you know what you're doing.");
gitPush(username, password, true);
}
private void gitPush(String username, String password, Boolean withForce) throws GitAPIException {
LoggingHelper.info("Pushing.");
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
pushCommand.setForce(withForce);
pushCommand.call();
}
public void gitRebaseToCommit(String commitHash, String commitMessage) throws GitAPIException {
LoggingHelper.info("Rebasing to commit [" + commitHash + "].");
RebaseCommand.InteractiveHandler handler = new RebaseCommand.InteractiveHandler() {
@SneakyThrows
public void prepareSteps(List steps) {
steps.get(0).setAction(RebaseTodoLine.Action.PICK);
for(int i = 1; i < steps.size(); i++) {
steps.get(i).setAction(RebaseTodoLine.Action.SQUASH);
}
}
public String modifyCommitMessage(String oldMessage) {
return commitMessage;
}
};
git.rebase()
.setUpstream(commitHash)
.runInteractively(handler)
.call();
}
public GitWrapper gitDeleteBranchWithForce(String branchName, String username, String password, Boolean withForce) throws GitAPIException {
//delete Local
LoggingHelper.info("Deleting branch [" + branchName + "].");
git.branchDelete()
.setBranchNames("refs/heads/" + branchName)
.setForce(withForce)
.call();
//delete remote
RefSpec refSpec = new RefSpec()
.setSource(null)
.setDestination("refs/heads/" + branchName);
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
pushCommand.setRefSpecs(refSpec).setRemote("origin").call();
return this;
}
public void gitDeleteBranch(String branchName, String username, String password) throws GitAPIException {
gitDeleteBranchWithForce(branchName, username, password, false);
}
public Git gitClient() {
if (git == null) {
final String errorMessage = "Git client is not defined.";
NullPointerException exception = new NullPointerException(errorMessage);
LoggingHelper.error(errorMessage, exception);
throw exception;
}
return git;
}
public void destroyAndClean() {
this.git = null;
try {
FileUtils.deleteDirectory(new File(localPath));
} catch(Exception e) {
LoggingHelper.warn("Couldn't delete directory [" + localPath + "], but this will be ignored and the test will continue. Exception was: " + e);
}
}
}