1
0

CommitController.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package cn.seecoder.web.controller.gitlab;
  2. import cn.seecoder.common.exceptions.ServiceException;
  3. import cn.seecoder.common.util.LogTrackingUtil;
  4. import cn.seecoder.common.util.OpType;
  5. import cn.seecoder.web.core.commit.CommitResolver;
  6. import cn.seecoder.web.core.commit.model.CommitRule;
  7. import cn.seecoder.web.dao.user.UserMapper;
  8. import cn.seecoder.web.model.enums.CommitRelatedEnum;
  9. import cn.seecoder.web.model.po.project.CommitPO;
  10. import cn.seecoder.web.model.vo.GitWebHookVO;
  11. import cn.seecoder.web.model.vo.Response;
  12. import cn.seecoder.web.model.vo.project.CommitVO;
  13. import cn.seecoder.web.service.bug_list.BugListService;
  14. import cn.seecoder.web.service.project.CommitService;
  15. import cn.seecoder.web.service.tree.TreeNodeService;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.nju.edu.gitlab.SeecoderGitlabApi;
  18. import com.nju.edu.gitlab.vo.DiffVO;
  19. import io.swagger.annotations.Api;
  20. import io.swagger.annotations.ApiImplicitParam;
  21. import io.swagger.annotations.ApiImplicitParams;
  22. import io.swagger.annotations.ApiOperation;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.*;
  26. import java.util.List;
  27. import java.util.Set;
  28. /**
  29. * @author chenyz
  30. * @date 2021/2/4
  31. * @description: 与GitLab进行交互的接口
  32. */
  33. @Api(tags = "GitLab Hook API")
  34. @RestController
  35. @Slf4j
  36. public class CommitController {
  37. private final BugListService bugListService;
  38. private final TreeNodeService treeNodeService;
  39. private final CommitService commitService;
  40. private final SeecoderGitlabApi seecoderGitlabApi;
  41. // 赶时间暂时这么写
  42. @Autowired
  43. UserMapper userMapper;
  44. @Autowired
  45. public CommitController(BugListService bugListService, TreeNodeService treeNodeService, CommitService commitService, SeecoderGitlabApi seecoderGitlabApi) {
  46. this.bugListService = bugListService;
  47. this.treeNodeService = treeNodeService;
  48. this.commitService = commitService;
  49. this.seecoderGitlabApi = seecoderGitlabApi;
  50. }
  51. /**
  52. * @author chenyz
  53. * @date 2021/2/4
  54. * @description: 此接口挂载在webhooks的push回调中
  55. * todo webhook最多只能获取20个commit
  56. * 因为gitlab官方,考虑性能原因导致
  57. * 详见 https://docs.gitlab.com/ee/user/project/integrations/webhooks.html push event内容
  58. * 暂时无解
  59. * 只关注push event
  60. * 所有分支的push都分析
  61. */
  62. @ApiOperation(value = "push回调接口", httpMethod = "POST")
  63. @PostMapping("/hook")
  64. @ApiImplicitParam(name = "gitWebHookVO",dataType = "object", paramType = "body")
  65. public void pushHook(@RequestBody GitWebHookVO gitWebHookVO) throws ServiceException {
  66. //
  67. // List<String> emails = new ArrayList<>();
  68. // emails.add(gitWebHookVO.getUser_email());
  69. // List<GitlabUserEmailDTO> users;
  70. // try {
  71. // users = seecoderGitlabApi.findUserByEmails(emails);
  72. // } catch (SeecoderGitlabException e) {
  73. // throw new ServiceException("统一代码服务出现访问错误,无法正确分析webhook信息");
  74. // }
  75. // Integer userId = users.size()!=0 ? users.get(0).getUserId() : null;
  76. Integer projectId = gitWebHookVO.getProject_id();
  77. for (GitWebHookVO.Commit commit: gitWebHookVO.getCommits()){
  78. if (commitService.isResolved(commit.getId())){
  79. //已经解析过了
  80. log.info("Commits 已经解析过了, id: {}", commit.getId());
  81. continue;
  82. }
  83. //ANA 日志需要打出用户的commit信息
  84. JSONObject object = new JSONObject();
  85. try{
  86. object.put("commit_id",commit.getId());
  87. object.put("user_id",userMapper.selectIdByEmail(commit.getAuthor().getEmail()));
  88. object.put("project_id",projectId);
  89. object.put("commit_time",commit.getTimestamp());
  90. object.put("message",commit.getMessage());
  91. }catch (Exception ignored){}
  92. log.info("resolving commit id : {}, title : {}, message : {}", commit.getId(), commit.getTitle(), commit.getMessage());
  93. //todo 注意目前分析的是 message
  94. // 应该分析 title 还是 message 与学生的提交规范有关
  95. CommitRule rule = CommitResolver.resolve(commit.getMessage());
  96. if (rule == null){
  97. log.info("Commits 不符合规范, id: {}", commit.getId());
  98. //ANA
  99. try {
  100. object.put("type","不合规范");
  101. String data = JSONObject.toJSONString(object);
  102. LogTrackingUtil.log(data, OpType.COMMIT);
  103. } catch (Exception ignored) {}
  104. continue;
  105. }
  106. switch (rule.getType()){
  107. case FEAT:
  108. treeNodeService.updateNodeState(rule.getId(),rule.getState());
  109. commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.TREE_ID, rule.getId()));
  110. log.info("Commits 关联tree节点, id: {}", commit.getId());
  111. //ANA
  112. try {
  113. object.put("type","关联tree");
  114. String data = JSONObject.toJSONString(object);
  115. LogTrackingUtil.log(data, OpType.COMMIT);
  116. } catch (Exception ignored) {}
  117. break;
  118. case FIX:
  119. bugListService.updateState(rule.getState(), rule.getId());
  120. commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID, rule.getId()));
  121. log.info("Commits 关联bug节点, id: {}", commit.getId());
  122. //ANA
  123. try {
  124. object.put("type","关联bug");
  125. String data = JSONObject.toJSONString(object);
  126. LogTrackingUtil.log(data, OpType.COMMIT);
  127. } catch (Exception ignored) {}
  128. break;
  129. case UNKNOWN:
  130. break;
  131. }
  132. }
  133. }
  134. @ApiOperation(value = "获取某个需求树结点的所有commit信息", httpMethod = "GET")
  135. @GetMapping("/commits/tree")
  136. @ApiImplicitParam(name = "treeId",dataType = "int", paramType = "query")
  137. public Response<List<CommitVO>> getTreeCommits(@RequestParam("treeId") Integer treeId){
  138. return Response.buildSuccess(commitService.getTreeCommit(treeId));
  139. }
  140. @ApiOperation(value = "获取bug list中一个bug关联的所有commit信息", httpMethod = "GET")
  141. @GetMapping("/commits/bug_list")
  142. @ApiImplicitParam(name = "bugId",dataType = "int", paramType = "query")
  143. public Response<List<CommitVO>> getBugCommits(@RequestParam("bugId") Integer bugId){
  144. return Response.buildSuccess(commitService.getBugCommit(bugId));
  145. }
  146. @ApiOperation(value = "获取指定项目的所有commit信息", httpMethod = "GET")
  147. @GetMapping("/commits/list")
  148. @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query")
  149. public Response<List<CommitVO>> getCommits(@RequestParam("projectId") Integer projectId) {
  150. return Response.buildSuccess(commitService.getCommits(projectId));
  151. }
  152. @ApiOperation(value = "获取指定项目指定分支所有的commit信息", httpMethod = "GET")
  153. @GetMapping("/commits/by_branch")
  154. @ApiImplicitParams({
  155. @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
  156. @ApiImplicitParam(name = "branchName", dataType = "String", paramType = "query")
  157. })
  158. public Response<List<CommitVO>> getCommitsByBranch(@RequestParam("projectId") Integer projectId,
  159. @RequestParam("branchName") String branchName){
  160. return Response.buildSuccess(commitService.getCommitsByBranchName(projectId, branchName));
  161. }
  162. @ApiOperation(value = "获得一个commit的信息", httpMethod = "GET")
  163. @GetMapping("/commit")
  164. @ApiImplicitParams({
  165. @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
  166. @ApiImplicitParam(name = "hash", dataType = "String", paramType = "query")
  167. })
  168. public Response<CommitVO> getCommitByHash(@RequestParam("projectId") Integer projectId,
  169. @RequestParam("hash") String hash)
  170. {
  171. return Response.buildSuccess(commitService.getCommitByHash(projectId, hash));
  172. }
  173. @ApiOperation(value = "获得两个提交之间的差异", httpMethod = "GET")
  174. @GetMapping("/commits/diff")
  175. @ApiImplicitParams({
  176. @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
  177. @ApiImplicitParam(name = "from", dataType = "String", paramType = "query"),
  178. @ApiImplicitParam(name = "to", dataType = "String", paramType = "query")
  179. })
  180. public Response<Set<DiffVO>> getDiff(@RequestParam("projectId") Integer projectId,
  181. @RequestParam("from") String from,
  182. @RequestParam("to") String to)
  183. {
  184. return Response.buildSuccess(commitService.getDiff(projectId, from, to));
  185. }
  186. }