1
0
Просмотр исходного кода

refactor: 删除commit到具体门户用户的映射,因为webhook无法准确获取其映射关系

370774330@qq.com 5 лет назад
Родитель
Сommit
27497a5f9f

+ 17 - 19
web/src/main/java/cn/seecoder/web/controller/gitlab/HookController.java

@@ -10,8 +10,6 @@ import cn.seecoder.web.service.bug_list.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;
@@ -21,10 +19,6 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
 /**
  * @author chenyz
  * @date 2021/2/4
@@ -60,22 +54,27 @@ public class HookController {
      *  因为gitlab官方,考虑性能原因导致
      *  详见 https://docs.gitlab.com/ee/user/project/integrations/webhooks.html push event内容
      *  暂时无解
+     * 只关注push event
+     * 所有分支的push都分析
      */
     @ApiOperation(value = "push回调接口", httpMethod = "POST")
     @PostMapping
     @ApiImplicitParam(name = "gitWebHookVO",dataType = "object", paramType = "body")
     public void pushHook(@RequestBody GitWebHookVO gitWebHookVO) throws ServiceException {
+
+//
+//        List<String> emails = new ArrayList<>();
+//        emails.add(gitWebHookVO.getUser_email());
+//        List<GitlabUserEmailDTO> users;
+//        try {
+//            users = seecoderGitlabApi.findUserByEmails(emails);
+//        } catch (SeecoderGitlabException e) {
+//            throw new ServiceException("统一代码服务出现访问错误,无法正确分析webhook信息");
+//        }
+//        Integer userId = users.size()!=0 ? users.get(0).getUserId() : null;
+
         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){
+        for (GitWebHookVO.Commit commit: gitWebHookVO.getCommits()){
             if (commitService.isResolved(commit.getId())){
                 //已经解析过了
                 continue;
@@ -85,15 +84,14 @@ 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, CommitRelatedEnum.BUG_ID,userId));
+                    commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID));
                     break;
                 case FIX:
                     bugListService.updateState(rule.getState(), rule.getId());
-                    commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID,userId));
+                    commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID));
                     break;
                 case UNKNOWN:
                     break;

+ 5 - 4
web/src/main/java/cn/seecoder/web/model/po/project/CommitPO.java

@@ -23,7 +23,8 @@ public class CommitPO {
     private String message;
     private String title;
     private String timestamp;
-    private String gitlabUsername;
+    private String username;
+    private String email;
     private CommitRelatedEnum relatedType;
     /**
      * 关联的id,详见 CommitRelatedEnum
@@ -40,11 +41,11 @@ public class CommitPO {
      */
     private Integer userId;
 
-    public CommitPO(GitWebHookVO.Commit commit, Integer projectId, CommitRelatedEnum type, Integer userId){
+    public CommitPO(GitWebHookVO.Commit commit, Integer projectId, CommitRelatedEnum type){
         BeanUtils.copyProperties(commit,this);
-        this.gitlabUsername = commit.getAuthor().getName();
+        this.username = commit.getAuthor().getName();
+        this.email = commit.getAuthor().getEmail();
         this.projectId = projectId;
         relatedType = type;
-        this.userId = userId;
     }
 }

+ 2 - 2
web/src/test/java/cn/seecoder/web/dao/project/CommitMapperTest.java

@@ -31,7 +31,7 @@ class CommitMapperTest {
     void insert() {
         CommitPO commitPO = CommitPO.builder()
                 .id("esdadafs")
-                .gitlabUsername("cool")
+                .username("cool")
                 .message("fefefefeat")
                 .title("feat: nononono")
                 .projectId(2)
@@ -46,7 +46,7 @@ class CommitMapperTest {
     void update() {
         CommitPO commitPO = CommitPO.builder()
                 .id("esdadafs")
-                .gitlabUsername("not cool")
+                .username("not cool")
                 .build();
         commitMapper.update(commitPO);
     }