GitTest.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package devops;
  2. import cn.reghao.bnt.web.devops.build.model.po.RepoAuthConfig;
  3. import cn.reghao.bnt.web.devops.builder.model.constant.RepoAuthType;
  4. import cn.reghao.bnt.web.devops.builder.tool.repo.GitImpl;
  5. import cn.reghao.bnt.web.devops.deployer.model.po.RemoteHost;
  6. import cn.reghao.bnt.web.devops.deployer.util.Sftp;
  7. import cn.reghao.jutil.jdk.shell.ShellExecutor;
  8. import cn.reghao.jutil.jdk.shell.ShellResult;
  9. import org.eclipse.jgit.api.Git;
  10. import org.eclipse.jgit.api.errors.GitAPIException;
  11. import org.eclipse.jgit.internal.storage.file.FileRepository;
  12. import org.eclipse.jgit.lib.Ref;
  13. import org.eclipse.jgit.lib.Repository;
  14. import org.eclipse.jgit.revwalk.RevCommit;
  15. import org.eclipse.jgit.revwalk.RevWalk;
  16. import org.junit.jupiter.api.Test;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * @author reghao
  23. * @date 2022-05-07 09:32:40
  24. */
  25. public class GitTest {
  26. @Test
  27. public void gitTest() throws Exception {
  28. String repo = "";
  29. String username = "";
  30. String password = "";
  31. RepoAuthConfig repoAuthConfig = new RepoAuthConfig();
  32. repoAuthConfig.setAuthType(RepoAuthType.http.getName());
  33. repoAuthConfig.setUsername(username);
  34. repoAuthConfig.setPassword(password);
  35. GitImpl git = new GitImpl(repoAuthConfig.getRepoAuth());
  36. List<String> list = git.remoteBranches(repo);
  37. System.out.println();
  38. }
  39. @Test
  40. public void repoTest() throws IOException, GitAPIException {
  41. String localRepo = "/home/reghao/code/java/jutil/.git";
  42. //checkout(localRepo);
  43. Repository repo = new FileRepository(localRepo);
  44. String branch = null;
  45. String commit = null;
  46. getLogsSinceCommit(repo, branch, commit);
  47. }
  48. private List<String> getLogsSinceCommit(Repository repository, String branch, String commit) throws IOException, GitAPIException {
  49. if (branch == null) {
  50. branch = repository.getBranch();
  51. }
  52. List<String> commits = new ArrayList<>();
  53. Ref head = repository.findRef("refs/heads/" + branch);
  54. if (head != null) {
  55. try (RevWalk revWalk = new RevWalk(repository)) {
  56. revWalk.markStart(revWalk.parseCommit(head.getObjectId()));
  57. for (RevCommit revCommit : revWalk) {
  58. String commitId = revCommit.getId().getName();
  59. if (commitId.equals(commit)) {
  60. break;
  61. }
  62. long commitTime = revCommit.getCommitTime()*1000L;
  63. String fullMessage = revCommit.getFullMessage();
  64. commits.add(fullMessage);
  65. System.out.println("\nCommit-Message: " + fullMessage);
  66. }
  67. revWalk.dispose();
  68. }
  69. }
  70. return commits;
  71. }
  72. private void checkout(String localRepo) throws IOException, GitAPIException {
  73. Git git = Git.open(new File(localRepo));
  74. String branch = git.getRepository().getBranch();
  75. for (RevCommit revCommit : git.log().call()) {
  76. String commitId = revCommit.getId().getName();
  77. git.checkout().setStartPoint(revCommit);
  78. }
  79. }
  80. @Test
  81. public void compilerTest() {
  82. ShellExecutor shell = new ShellExecutor();
  83. String versionCmd = "/opt/env/java/maven/maven-3.6.0/bin/mvn -v";
  84. versionCmd = "/opt/env/dotnet/dotnet-sdk-7.0.203-linux-x64/dotnet --info";
  85. versionCmd = "/opt/env/node/node-v14.19.3-linux-x64/bin/node -v";
  86. versionCmd = "/usr/bin/python --version";
  87. versionCmd = "/usr/bin/java -version";
  88. versionCmd = "/usr/bin/cmake --version";
  89. versionCmd = "/usr/bin/g++ -v";
  90. versionCmd = "/usr/bin/gcc -v";
  91. ShellResult shellResult = shell.exec(versionCmd.split("\\s+"));
  92. if (shellResult.getExitCode() == 0) {
  93. System.out.println(shellResult.getResult());
  94. }
  95. }
  96. @Test
  97. public void sftpTest() throws Exception {
  98. String host = "127.0.0.1";
  99. int port = 22;
  100. String username = "root";
  101. String password = "gsh";
  102. RemoteHost remoteHost = new RemoteHost(host, port, username, password);
  103. Sftp sftp = new Sftp();
  104. String command = "docker ps -a";
  105. sftp.exec(remoteHost, command);
  106. }
  107. }