Jelajahi Sumber

遵循设计文档,调整优化构建部署模块结构

reghao 5 tahun lalu
induk
melakukan
d94a4940bb

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/BuilderUtil.java

@@ -67,7 +67,7 @@ public class BuilderUtil {
         if ("svn".equals(reposAuth.getReposType())) {
             return new SvnImpl(reposAuth.getUsername(), reposAuth.getPassword());
         } else if ("git".equals(reposAuth.getReposType())) {
-            return new GitImpl(reposAuth.getUsername(), reposAuth.getPassword());
+            return new GitImpl("");
         } else {
             return null;
         }

+ 23 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/CommitInfo.java

@@ -0,0 +1,23 @@
+package cn.reghao.autodop.dmaster.app.service.tools.updater;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * 代码提交信息
+ *
+ * @author reghao
+ * @date 2020-05-13 22:50:38
+ */
+@Data
+public class CommitInfo {
+    private String repoUrl;
+    private String branch;
+    private String commitId;
+    private String commitAuthor;
+    // 毫秒级时间戳
+    private long commitTime;
+    private String commitMsg;
+    private List<String> commitFiles;
+}

+ 86 - 20
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/GitImpl.java

@@ -3,12 +3,19 @@ package cn.reghao.autodop.dmaster.app.service.tools.updater;
 import org.eclipse.jgit.api.CloneCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.PullCommand;
+import org.eclipse.jgit.api.PullResult;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.internal.storage.file.FileRepository;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 
 import java.io.File;
+import java.io.IOException;
+import java.util.List;
 
 /**
  * git 实现
@@ -17,47 +24,106 @@ import java.io.File;
  * @date 2019-10-12 22:22:00
  */
 public class GitImpl implements CodeUpdater {
-    private String GIT = "/.git";
-    private String branch = "master";
+    private String branch;
 
-    private final UsernamePasswordCredentialsProvider credentials;
+    //private final UsernamePasswordCredentialsProvider credentials;
 
-    public GitImpl(String username, String password) {
-        this.credentials = new UsernamePasswordCredentialsProvider(username, password);
+    public GitImpl(String branch) {
+        this.branch = branch;
+        //this.credentials = new UsernamePasswordCredentialsProvider(username, password);
     }
 
+    /**
+     * 不存在则 clone,存在则 pull
+     *
+     * @param
+     * @return
+     * @date 2020-05-13 下午9:22
+     */
     @Override
     public String update(String remote, String local) throws Exception {
-        File localRepo = new File(local + GIT);
+        File localRepo = new File(local + "/.git");
         if (!localRepo.exists()) {
-            clone(remote, local);
+            CommitInfo commitInfo = clone(remote, local);
         } else {
-            Repository repo = new FileRepository(localRepo.getAbsolutePath());
-            PullCommand gitPull = new Git(repo).pull();
-            gitPull.call();
+            try (Repository repo = new FileRepository(localRepo.getAbsolutePath())) {
+                PullCommand gitPull = new Git(repo).pull();
+                gitPull.call();
+                CommitInfo commitInfo = commitInfo(repo);
+                commitInfo.setRepoUrl(remote);
+            } catch (Exception e) {
+
+            }
         }
 
         return null;
     }
 
-    private void clone(String remote, String local) throws GitAPIException {
-        CloneCommand gitClone = Git.cloneRepository().setURI(remote).setBranch(branch);
-        Git git = gitClone.setDirectory(new File(local)).call();
-        System.out.println();
+    private CommitInfo clone(String remote, String local) {
+        CloneCommand gitClone = Git.cloneRepository()
+                .setURI(remote)
+                .setBranch(branch)
+                .setDirectory(new File(local));
+
+        try (Git git = gitClone.call()) {
+            Repository repo = git.getRepository();
+            Ref head = repo.findRef("refs/heads/master");
+            ObjectId objectId = head.getObjectId();
+
+            RevWalk walk = new RevWalk(repo);
+            RevCommit lastCommit = walk.parseCommit(objectId);
+
+            CommitInfo commitInfo = new CommitInfo();
+            commitInfo.setCommitId(objectId.name());
+            commitInfo.setRepoUrl(remote);
+
+            return commitInfo;
+        } catch (Exception e) {
+            return null;
+        }
     }
 
-    @Override
-    public String localVersion(String local) throws Exception {
+    private CommitInfo commitInfo(Repository repo) throws IOException {
+        Ref head = repo.findRef("refs/heads/master");
+        ObjectId objectId = head.getObjectId();
+        try (RevWalk walk = new RevWalk(repo)) {
+            RevCommit lastCommit = walk.parseCommit(objectId);
+            int commitTime = lastCommit.getCommitTime();
+            String commitAuthor = lastCommit.getAuthorIdent().getName();
+            String commitMsg = lastCommit.getFullMessage();
+
+            CommitInfo commitInfo = new CommitInfo();
+            commitInfo.setCommitId(objectId.name());
+            commitInfo.setCommitTime(commitTime*1000);
+            commitInfo.setCommitAuthor(commitAuthor);
+            commitInfo.setCommitMsg(commitMsg);
+            commitInfo.setBranch(repo.getBranch());
+            return commitInfo;
+        }
+    }
+
+    List<String> changedFileList() {
         return null;
     }
 
+    @Override
+    public String localVersion(String local) {
+        File localRepo = new File(local + "/.git");
+        try (Repository repo = new FileRepository(localRepo.getAbsolutePath())) {
+            Ref head = repo.findRef("refs/heads/master");
+            ObjectId objectId = head.getObjectId();
+            return objectId.name();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
     public static void main(String[] args) throws Exception {
-        GitImpl git = new GitImpl("reghao", "gjs");
+        String branch = "master";
+        GitImpl git = new GitImpl(branch);
         String remote = "http://git.reghao.cn/reghao/spiderlab.git";
         String local = "/home/reghao/tmp/autodop/opt/data/git/spiderlab";
-
-        /*CloneCommand clone = Git.cloneRepository().setURI(remote);
-        clone.setDirectory(new File(local)).call();*/
         git.update(remote, local);
     }
 }