package com.cadrlife.devsearch.agent.service.git; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.Date; import java.util.List; import com.google.common.collect.Iterators; import org.eclipse.jgit.api.AddCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.PullResult; import org.eclipse.jgit.api.errors.GitAPIException; import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.transport.PushResult; import org.elasticsearch.common.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class GitService { private static final Logger LOG = LoggerFactory.getLogger(GitService.class); public Git pull(File file) { try { Git gitHandle = Git.open(file); PullResult result = gitHandle.pull().call(); Preconditions.checkState(result.isSuccessful(), "Pull was not succesfull"); return gitHandle; } catch (GitAPIException | IOException e) { throw new RuntimeException("Problem pulling " + file.getPath(), e); } } public Git clone(File cloneDir, String repoUrl, String branch) { LOG.info("git clone {} {}", repoUrl, cloneDir); try { Git.cloneRepository().setURI(repoUrl). setDirectory(cloneDir). setBranch(branch).setBare(false).setRemote("origin"). setNoCheckout(false).call(); return Git.open(cloneDir); } catch (GitAPIException | IOException e) { Throwables.propagate(e); return null; } } public Date findLastChangedDate(Git gitHandle, File file) { try { RevCommit revCommit = Iterators.get(Git.open(file).log().call().iterator(), 0); return new Date(revCommit.getCommitTime() * 1000L); } catch (GitAPIException | IOException e) { LOG.info("Could not find last commit: {}", e.getMessage()); return null; } } public boolean gitRepoLooksValid(Path projectDir) { return projectDir.resolve(".git/refs/remotes/origin").toFile().exists(); } public Git push(File file) { try { Git gitHandle = Git.open(file); Iterable<PushResult> pushResults = gitHandle.push().call(); int pushResultCount = 0; for (PushResult pushResult : pushResults) { LOG.info("Push result remote updates: {}", pushResult.getRemoteUpdates()); LOG.info("Push result messages: {}", pushResult.getMessages()); pushResultCount++; } Preconditions.checkState(pushResultCount > 0, "Push was not successful, got no results"); return gitHandle; } catch (GitAPIException | IOException e) { throw new RuntimeException("Problem pushing " + file.getPath(), e); } } public void commit(File repo, List<String> fileUpdates) { if (fileUpdates.isEmpty()) { return; } try { Git gitHandle = Git.open(repo); AddCommand addCommand = gitHandle.add(); for (String fileUpdate : fileUpdates) { addCommand = addCommand.addFilepattern(fileUpdate); LOG.info("git add'ing {}", fileUpdate); } DirCache addResult = addCommand.call(); RevCommit commitResult = gitHandle.commit().setCommitter("dev search agent", "noreply@cadrlife.com").setMessage("Automated change performed by dev-search-agent").call(); LOG.info("git commit'd: {}", commitResult.toString()); } catch (GitAPIException | IOException e) { throw new RuntimeException("Problem pushing " + repo.getPath(), e); } } }