Parcourir la source

feat: commit po 加入 user id

370774330@qq.com il y a 5 ans
Parent
commit
8c46267201

+ 24 - 7
web/src/main/java/cn/seecoder/web/controller/gitlab/HookController.java

@@ -1,11 +1,17 @@
 package cn.seecoder.web.controller.gitlab;
 
+import cn.seecoder.common.exceptions.ServiceException;
+import cn.seecoder.core.commit.CommitResolver;
+import cn.seecoder.core.commit.CommitRule;
 import cn.seecoder.web.model.enums.CommitRelatedType;
 import cn.seecoder.web.model.po.project.CommitPO;
 import cn.seecoder.web.model.vo.GitWebHookVO;
 import cn.seecoder.web.service.project.BugListService;
 import cn.seecoder.web.service.project.CommitService;
 import cn.seecoder.web.service.tree.TreeNodeService;
+import com.nju.edu.gitlab.SeecoderGitlabApi;
+import com.nju.edu.gitlab.SeecoderGitlabException;
+import com.nju.edu.gitlab.dto.GitlabUserEmailDTO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
 import io.swagger.annotations.ApiOperation;
@@ -14,10 +20,10 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
-import cn.seecoder.core.commit.CommitResolver;
-import cn.seecoder.core.commit.CommitRule;
 
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @author chenyz
@@ -35,11 +41,14 @@ public class HookController {
 
     private final CommitService commitService;
 
+    private final SeecoderGitlabApi seecoderGitlabApi;
+
     @Autowired
-    public HookController(BugListService bugListService, TreeNodeService treeNodeService, CommitService commitService) {
+    public HookController(BugListService bugListService, TreeNodeService treeNodeService, CommitService commitService, SeecoderGitlabApi seecoderGitlabApi) {
         this.bugListService = bugListService;
         this.treeNodeService = treeNodeService;
         this.commitService = commitService;
+        this.seecoderGitlabApi = seecoderGitlabApi;
     }
 
 
@@ -55,10 +64,17 @@ public class HookController {
     @ApiOperation(value = "push回调接口", httpMethod = "POST")
     @PostMapping
     @ApiImplicitParam(name = "gitWebHookVO",dataType = "object", paramType = "body")
-    public void pushHook(@RequestBody GitWebHookVO gitWebHookVO)
-    {
+    public void pushHook(@RequestBody GitWebHookVO gitWebHookVO) throws ServiceException {
         Integer projectId = gitWebHookVO.getProject_id();
         List<GitWebHookVO.Commit> commits = gitWebHookVO.getCommits();
+        List<String> emails = commits.stream().map(x->x.getAuthor().getEmail()).collect(Collectors.toList());
+        List<GitlabUserEmailDTO> users;
+        try {
+            users = seecoderGitlabApi.findUserByEmails(emails);
+        } catch (SeecoderGitlabException e) {
+            throw new ServiceException("统一代码服务出现访问错误,无法正确分析webhook信息");
+        }
+        Map<String,Integer> userIdMap = users.stream().collect(Collectors.toMap(x->x.getEmail(), x->x.getUserId()));
         for (GitWebHookVO.Commit commit:commits){
             if (commitService.isResolved(commit.getId())){
                 //已经解析过了
@@ -69,14 +85,15 @@ public class HookController {
                 //不符合规范
                 continue;
             }
+            Integer userId = userIdMap.get(commit.getAuthor().getEmail());
             switch (rule.getType()){
                 case FEAT:
                     treeNodeService.updateNodeState(rule.getId(),rule.getState());
-                    commitService.save(new CommitPO(commit,projectId, CommitRelatedType.BUG_ID));
+                    commitService.save(new CommitPO(commit,projectId, CommitRelatedType.BUG_ID,userId));
                     break;
                 case FIX:
                     bugListService.updateState(rule.getState(), rule.getId());
-                    commitService.save(new CommitPO(commit,projectId, CommitRelatedType.BUG_ID));
+                    commitService.save(new CommitPO(commit,projectId, CommitRelatedType.BUG_ID,userId));
                     break;
                 case UNKNOWN:
                     break;

+ 7 - 1
web/src/main/java/cn/seecoder/web/model/po/project/CommitPO.java

@@ -35,10 +35,16 @@ public class CommitPO {
      */
     private Integer projectId;
 
-    public CommitPO(GitWebHookVO.Commit commit, Integer projectId, CommitRelatedType type){
+    /**
+     * 这条commit提交人的门户id
+     */
+    private Integer userId;
+
+    public CommitPO(GitWebHookVO.Commit commit, Integer projectId, CommitRelatedType type, Integer userId){
         BeanUtils.copyProperties(commit,this);
         this.gitlabUsername = commit.getAuthor().getName();
         this.projectId = projectId;
         relatedType = type;
+        this.userId = userId;
     }
 }

+ 2 - 0
web/src/main/java/cn/seecoder/web/model/vo/project/CommitVO.java

@@ -24,6 +24,8 @@ public class CommitVO {
     private String timestamp;
     @ApiModelProperty("commit 用户的gitlab用户名")
     private String gitlabUsername;
+    @ApiModelProperty("门户id")
+    private String userId;
 
     public CommitVO(CommitPO po) {
         BeanUtils.copyProperties(po,this);