Explorar o código

feat: add branch and commit API

claws %!s(int64=4) %!d(string=hai) anos
pai
achega
e29645ed70

+ 46 - 0
web/src/main/java/cn/seecoder/web/controller/gitlab/BranchController.java

@@ -0,0 +1,46 @@
+package cn.seecoder.web.controller.gitlab;
+
+import cn.seecoder.web.model.vo.Response;
+import cn.seecoder.web.service.project.BranchService;
+import com.nju.edu.gitlab.vo.BranchVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * @author claws
+ * @date 2022/3/19
+ * @description API about branch
+ */
+@Api(tags = "Branch API")
+@RestController
+@Slf4j
+public class BranchController {
+    final static String GET_BRANCHES_ERROR = "Fail to search all branches";
+
+    final BranchService branchService;
+
+    @Autowired
+    public BranchController(BranchService branchService) {
+        this.branchService = branchService;
+    }
+
+    @ApiOperation(value = "获取指定项目的所有branch信息", httpMethod = "GET")
+    @GetMapping("/branch/list")
+    @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query")
+    public Response<Object> getAllBranches(@RequestParam("projectId") Integer projectId){
+        List<BranchVO> allBranches = branchService.getAllBranches(projectId);
+        if (allBranches != null) {
+            return Response.buildSuccess(allBranches);
+        } else {
+            return Response.buildFailure(500, GET_BRANCHES_ERROR);
+        }
+    }
+}

+ 29 - 1
web/src/main/java/cn/seecoder/web/controller/gitlab/CommitController.java

@@ -16,6 +16,7 @@ import cn.seecoder.web.service.project.CommitService;
 import cn.seecoder.web.service.tree.TreeNodeService;
 import com.alibaba.fastjson.JSONObject;
 import com.nju.edu.gitlab.SeecoderGitlabApi;
+import com.nju.edu.gitlab.vo.DiffVO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiImplicitParams;
@@ -25,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * @author chenyz
@@ -172,8 +174,34 @@ public class CommitController {
             @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
             @ApiImplicitParam(name = "branchName", dataType = "String", paramType = "query")
     })
-    public Response<List<com.nju.edu.gitlab.vo.commit.CommitVO>> getCommitsByBranch(@RequestParam("projectId") Integer projectId,
+    public Response<List<CommitVO>> getCommitsByBranch(@RequestParam("projectId") Integer projectId,
                                                                                     @RequestParam("branchName") String branchName){
         return Response.buildSuccess(commitService.getCommitsByBranchName(projectId, branchName));
     }
+
+    @ApiOperation(value = "获得一个commit的信息", httpMethod = "GET")
+    @GetMapping("/commit")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
+            @ApiImplicitParam(name = "hash", dataType = "String", paramType = "query")
+    })
+    public Response<CommitVO> getCommitByHash(@RequestParam("projectId") Integer projectId,
+                                              @RequestParam("hash") String hash)
+    {
+        return Response.buildSuccess(commitService.getCommitByHash(projectId, hash));
+    }
+
+    @ApiOperation(value = "获得两个提交之间的差异", httpMethod = "GET")
+    @GetMapping("/commits/diff")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
+            @ApiImplicitParam(name = "from", dataType = "String", paramType = "query"),
+            @ApiImplicitParam(name = "to", dataType = "String", paramType = "query")
+    })
+    public Response<Set<DiffVO>> getDiff(@RequestParam("projectId") Integer projectId,
+                                         @RequestParam("from") String from,
+                                         @RequestParam("to") String to)
+    {
+        return Response.buildSuccess(commitService.getDiff(projectId, from, to));
+    }
 }

+ 5 - 0
web/src/main/java/cn/seecoder/web/infrastructure/config/WebSecurityConfig.java

@@ -88,6 +88,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers(GET, "/commits/tree").access("@authTools.checkTreeNodeOwnershipParam(request)")
                 .antMatchers(GET, "/commits/bug_list").access("@authTools.checkBugOwnershipParam(request)")
                 .antMatchers(GET, "/commits/list").access("@authTools.checkProjOwnershipParam(request)")
+                .antMatchers(GET, "/commits/by_branch").access("@authTools.checkProjOwnershipParam(request)")
+                .antMatchers(GET, "/commit").access("@authTools.checkProjOwnershipParam(request)")
+                .antMatchers(GET, "/commits/diff").access("@authTools.checkProjOwnershipParam(request)")
+                // Branch Controller
+                .antMatchers(GET, "/branch/list").access("@authTools.checkProjOwnershipParam(request)")
                 // Deployment Controller
                 .antMatchers(GET, "/deployments/list").access("@authTools.checkProjOwnershipParam(request)")
                 .antMatchers(GET, "/deployments/log").access("@authTools.checkProjPipelineParam(request)")

+ 14 - 1
web/src/main/java/cn/seecoder/web/model/vo/project/CommitVO.java

@@ -7,6 +7,8 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import org.springframework.beans.BeanUtils;
 
+import java.util.List;
+
 /**
  * @author PuHong Weng
  * @date 2021/3/23
@@ -17,7 +19,7 @@ import org.springframework.beans.BeanUtils;
 public class CommitVO {
     @ApiModelProperty("commit id")
     private String id;
-    @ApiModelProperty("commit i")
+    @ApiModelProperty("commit information")
     private String message;
     @ApiModelProperty("commit summary")
     private String title;
@@ -27,8 +29,19 @@ public class CommitVO {
     private String gitlabUsername;
     @ApiModelProperty("commit type")
     private CommitRelatedEnum relatedType;
+    @ApiModelProperty("commit parents")
+    private List<String> parents;
 
     public CommitVO(CommitPO po) {
         BeanUtils.copyProperties(po,this);
     }
+
+    public CommitVO(com.nju.edu.gitlab.vo.commit.CommitVO vo){
+        id = vo.getId();
+        message = vo.getMessage();
+        title = vo.getMessage();
+        timestamp = vo.getCommittedDate().toString();
+        gitlabUsername = vo.getCommitterName();
+        parents = vo.getParentIds();
+    }
 }

+ 29 - 0
web/src/main/java/cn/seecoder/web/service/impl/project/BranchServiceImpl.java

@@ -0,0 +1,29 @@
+package cn.seecoder.web.service.impl.project;
+
+import cn.seecoder.web.service.project.BranchService;
+import com.nju.edu.gitlab.SeecoderGitlabApi;
+import com.nju.edu.gitlab.SeecoderGitlabException;
+import com.nju.edu.gitlab.vo.BranchVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class BranchServiceImpl implements BranchService {
+
+    @Autowired
+    SeecoderGitlabApi seecoderGitlabApi;
+
+    @Override
+    public List<BranchVO> getAllBranches(Integer projectId) {
+        List<BranchVO> branches = null;
+        try {
+            branches = seecoderGitlabApi.getProjectBranches(projectId);
+        } catch (SeecoderGitlabException e) {
+            e.printStackTrace();
+            return null;
+        }
+        return branches;
+    }
+}

+ 33 - 10
web/src/main/java/cn/seecoder/web/service/impl/project/CommitServiceImpl.java

@@ -1,21 +1,20 @@
 package cn.seecoder.web.service.impl.project;
 
-import cn.seecoder.common.util.LogTrackingUtil;
-import cn.seecoder.common.util.OpType;
 import cn.seecoder.web.dao.user.UserMapper;
 import cn.seecoder.web.model.enums.CommitRelatedEnum;
 import cn.seecoder.web.model.vo.project.CommitVO;
-import com.alibaba.fastjson.JSONObject;
 import com.nju.edu.gitlab.SeecoderGitlabApi;
+import com.nju.edu.gitlab.vo.DiffVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import cn.seecoder.web.dao.project.CommitMapper;
 import cn.seecoder.web.model.po.project.CommitPO;
 import cn.seecoder.web.service.project.CommitService;
-import com.nju.edu.gitlab.SeecoderGitlabApi;
-import com.nju.edu.gitlab.SeecoderGitlabException;
 
+import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -77,18 +76,42 @@ public class CommitServiceImpl implements CommitService {
     }
 
     @Override
-    public List<com.nju.edu.gitlab.vo.commit.CommitVO> getCommitsByBranchName(Integer projectId, String BranchName) {
-        List<com.nju.edu.gitlab.vo.commit.CommitVO> allCommits;
+    public List<CommitVO> getCommitsByBranchName(Integer projectId, String BranchName) {
+        List<CommitVO> allCommits = new ArrayList<CommitVO>();
 
-        //TODO
         try{
-            allCommits = seecoderGitlabApi.getCommitsByBranch(projectId, "master");
+            List<com.nju.edu.gitlab.vo.commit.CommitVO> commitsResponse = seecoderGitlabApi.getCommitsByBranch(projectId, "master");
+            for (com.nju.edu.gitlab.vo.commit.CommitVO commit : commitsResponse){
+                allCommits.add(new CommitVO(commit));
+            }
 
         } catch (Exception e){
             e.printStackTrace();
-            return null;
+            return new ArrayList<>();
         }
 
         return allCommits;
     }
+
+    @Override
+    public CommitVO getCommitByHash(Integer projectId, String hash) {
+        try {
+            com.nju.edu.gitlab.vo.commit.CommitVO commit = seecoderGitlabApi.getCommitByHash(projectId, hash);
+            return new CommitVO(commit);
+        } catch (Exception e){
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    @Override
+    public Set<DiffVO> getDiff(Integer projectId, String fromHash, String toHash) {
+        try{
+            Set<DiffVO> diff = seecoderGitlabApi.compare(projectId, fromHash, toHash);
+            return diff;
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        return new HashSet<>();
+    }
 }

+ 9 - 0
web/src/main/java/cn/seecoder/web/service/project/BranchService.java

@@ -0,0 +1,9 @@
+package cn.seecoder.web.service.project;
+
+import com.nju.edu.gitlab.vo.BranchVO;
+
+import java.util.List;
+
+public interface BranchService {
+    List<BranchVO> getAllBranches(Integer projectId);
+}

+ 7 - 1
web/src/main/java/cn/seecoder/web/service/project/CommitService.java

@@ -3,8 +3,10 @@ package cn.seecoder.web.service.project;
 import cn.seecoder.web.model.enums.CommitRelatedEnum;
 import cn.seecoder.web.model.po.project.CommitPO;
 import cn.seecoder.web.model.vo.project.CommitVO;
+import com.nju.edu.gitlab.vo.DiffVO;
 
 import java.util.List;
+import java.util.Set;
 
 /**
  * @author PuHong Weng
@@ -30,5 +32,9 @@ public interface CommitService {
 
     List<CommitVO> getCommits(Integer projectId);
 
-    List<com.nju.edu.gitlab.vo.commit.CommitVO> getCommitsByBranchName(Integer projectId, String branchName);
+    List<CommitVO> getCommitsByBranchName(Integer projectId, String branchName);
+
+    CommitVO getCommitByHash(Integer projectId, String hash);
+
+    Set<DiffVO> getDiff(Integer projectId, String fromHash, String toHash);
 }

+ 1 - 1
web/src/main/resources/application-lzl.yml

@@ -1,6 +1,6 @@
 spring:
   datasource:
-    url: jdbc:mysql://127.0.0.1:30006/devcloud?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
+    url: jdbc:mysql://127.0.0.1:3306/devcloud?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
     username: root
     password: root
     sql-script-encoding: utf-8