CommitController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package cn.seecoder.web.controller.gitlab;
  2. import cn.seecoder.common.exceptions.ServiceException;
  3. import cn.seecoder.web.core.commit.CommitResolver;
  4. import cn.seecoder.web.core.commit.model.CommitRule;
  5. import cn.seecoder.web.model.enums.CommitRelatedEnum;
  6. import cn.seecoder.web.model.po.project.CommitPO;
  7. import cn.seecoder.web.model.vo.GitWebHookVO;
  8. import cn.seecoder.web.model.vo.Response;
  9. import cn.seecoder.web.model.vo.project.CommitVO;
  10. import cn.seecoder.web.service.bug_list.BugListService;
  11. import cn.seecoder.web.service.project.CommitService;
  12. import cn.seecoder.web.service.tree.TreeNodeService;
  13. import com.nju.edu.gitlab.SeecoderGitlabApi;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiImplicitParam;
  16. import io.swagger.annotations.ApiOperation;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.bind.annotation.*;
  20. import java.util.List;
  21. /**
  22. * @author chenyz
  23. * @date 2021/2/4
  24. * @description: 与GitLab进行交互的接口
  25. */
  26. @Api(tags = "GitLab Hook API")
  27. @RestController
  28. @Slf4j
  29. public class CommitController {
  30. private final BugListService bugListService;
  31. private final TreeNodeService treeNodeService;
  32. private final CommitService commitService;
  33. private final SeecoderGitlabApi seecoderGitlabApi;
  34. @Autowired
  35. public CommitController(BugListService bugListService, TreeNodeService treeNodeService, CommitService commitService, SeecoderGitlabApi seecoderGitlabApi) {
  36. this.bugListService = bugListService;
  37. this.treeNodeService = treeNodeService;
  38. this.commitService = commitService;
  39. this.seecoderGitlabApi = seecoderGitlabApi;
  40. }
  41. /**
  42. * @author chenyz
  43. * @date 2021/2/4
  44. * @description: 此接口挂载在webhooks的push回调中
  45. * todo webhook最多只能获取20个commit
  46. * 因为gitlab官方,考虑性能原因导致
  47. * 详见 https://docs.gitlab.com/ee/user/project/integrations/webhooks.html push event内容
  48. * 暂时无解
  49. * 只关注push event
  50. * 所有分支的push都分析
  51. */
  52. @ApiOperation(value = "push回调接口", httpMethod = "POST")
  53. @PostMapping("/hook")
  54. @ApiImplicitParam(name = "gitWebHookVO",dataType = "object", paramType = "body")
  55. public void pushHook(@RequestBody GitWebHookVO gitWebHookVO) throws ServiceException {
  56. //
  57. // List<String> emails = new ArrayList<>();
  58. // emails.add(gitWebHookVO.getUser_email());
  59. // List<GitlabUserEmailDTO> users;
  60. // try {
  61. // users = seecoderGitlabApi.findUserByEmails(emails);
  62. // } catch (SeecoderGitlabException e) {
  63. // throw new ServiceException("统一代码服务出现访问错误,无法正确分析webhook信息");
  64. // }
  65. // Integer userId = users.size()!=0 ? users.get(0).getUserId() : null;
  66. Integer projectId = gitWebHookVO.getProject_id();
  67. for (GitWebHookVO.Commit commit: gitWebHookVO.getCommits()){
  68. if (commitService.isResolved(commit.getId())){
  69. //已经解析过了
  70. log.info("Commits 已经解析过了, id: {}", commit.getId());
  71. continue;
  72. }
  73. log.info("resolving commit id : {}, title : {}, message : {}", commit.getId(), commit.getTitle(), commit.getMessage());
  74. //todo 注意目前分析的是 message
  75. // 应该分析 title 还是 message 与学生的提交规范有关
  76. CommitRule rule = CommitResolver.resolve(commit.getMessage());
  77. if (rule == null){
  78. log.info("Commits 不符合规范, id: {}", commit.getId());
  79. continue;
  80. }
  81. switch (rule.getType()){
  82. case FEAT:
  83. treeNodeService.updateNodeState(rule.getId(),rule.getState());
  84. commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID, rule.getId()));
  85. log.info("Commits 关联tree节点, id: {}", commit.getId());
  86. break;
  87. case FIX:
  88. bugListService.updateState(rule.getState(), rule.getId());
  89. commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID, rule.getId()));
  90. log.info("Commits 关联bug节点, id: {}", commit.getId());
  91. break;
  92. case UNKNOWN:
  93. break;
  94. }
  95. }
  96. }
  97. @ApiOperation(value = "获取某个需求树结点的所有commit信息", httpMethod = "GET")
  98. @GetMapping("/commits/tree")
  99. @ApiImplicitParam(name = "treeId",dataType = "int", paramType = "query")
  100. public Response<List<CommitVO>> getTreeCommits(@RequestParam("treeId") Integer treeId){
  101. return Response.buildSuccess(commitService.getTreeCommit(treeId));
  102. }
  103. @ApiOperation(value = "获取bug list中一个bug关联的所有commit信息", httpMethod = "GET")
  104. @GetMapping("/commits/bug_list")
  105. @ApiImplicitParam(name = "bugId",dataType = "int", paramType = "query")
  106. public Response<List<CommitVO>> getBugCommits(@RequestParam("bugId") Integer bugId){
  107. return Response.buildSuccess(commitService.getBugCommit(bugId));
  108. }
  109. @ApiOperation(value = "获取指定项目的所有commit信息", httpMethod = "GET")
  110. @GetMapping("/commits/list")
  111. @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query")
  112. public Response<List<CommitVO>> getCommits(@RequestParam("projectId") Integer projectId) {
  113. return Response.buildSuccess(commitService.getCommits(projectId));
  114. }
  115. }