| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package cn.seecoder.web.controller.gitlab;
- import cn.seecoder.common.exceptions.ServiceException;
- import cn.seecoder.web.core.commit.CommitResolver;
- import cn.seecoder.web.core.commit.model.CommitRule;
- import cn.seecoder.web.model.enums.CommitRelatedEnum;
- import cn.seecoder.web.model.po.project.CommitPO;
- import cn.seecoder.web.model.vo.GitWebHookVO;
- import cn.seecoder.web.model.vo.Response;
- import cn.seecoder.web.model.vo.project.CommitVO;
- 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 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.*;
- import java.util.List;
- /**
- * @author chenyz
- * @date 2021/2/4
- * @description: 与GitLab进行交互的接口
- */
- @Api(tags = "GitLab Hook API")
- @RestController
- @Slf4j
- public class CommitController {
- private final BugListService bugListService;
- private final TreeNodeService treeNodeService;
- private final CommitService commitService;
- private final SeecoderGitlabApi seecoderGitlabApi;
- @Autowired
- public CommitController(BugListService bugListService, TreeNodeService treeNodeService, CommitService commitService, SeecoderGitlabApi seecoderGitlabApi) {
- this.bugListService = bugListService;
- this.treeNodeService = treeNodeService;
- this.commitService = commitService;
- this.seecoderGitlabApi = seecoderGitlabApi;
- }
- /**
- * @author chenyz
- * @date 2021/2/4
- * @description: 此接口挂载在webhooks的push回调中
- * todo webhook最多只能获取20个commit
- * 因为gitlab官方,考虑性能原因导致
- * 详见 https://docs.gitlab.com/ee/user/project/integrations/webhooks.html push event内容
- * 暂时无解
- * 只关注push event
- * 所有分支的push都分析
- */
- @ApiOperation(value = "push回调接口", httpMethod = "POST")
- @PostMapping("/hook")
- @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();
- for (GitWebHookVO.Commit commit: gitWebHookVO.getCommits()){
- if (commitService.isResolved(commit.getId())){
- //已经解析过了
- log.info("Commits 已经解析过了, id: {}", commit.getId());
- continue;
- }
- log.info("resolving commit id : {}, title : {}, message : {}", commit.getId(), commit.getTitle(), commit.getMessage());
- //todo 注意目前分析的是 message
- // 应该分析 title 还是 message 与学生的提交规范有关
- CommitRule rule = CommitResolver.resolve(commit.getMessage());
- if (rule == null){
- log.info("Commits 不符合规范, id: {}", commit.getId());
- continue;
- }
- switch (rule.getType()){
- case FEAT:
- treeNodeService.updateNodeState(rule.getId(),rule.getState());
- commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID, rule.getId()));
- log.info("Commits 关联tree节点, id: {}", commit.getId());
- break;
- case FIX:
- bugListService.updateState(rule.getState(), rule.getId());
- commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.BUG_ID, rule.getId()));
- log.info("Commits 关联bug节点, id: {}", commit.getId());
- break;
- case UNKNOWN:
- break;
- }
- }
- }
- @ApiOperation(value = "获取某个需求树结点的所有commit信息", httpMethod = "GET")
- @GetMapping("/commits/tree")
- @ApiImplicitParam(name = "treeId",dataType = "int", paramType = "query")
- public Response<List<CommitVO>> getTreeCommits(@RequestParam("treeId") Integer treeId){
- return Response.buildSuccess(commitService.getTreeCommit(treeId));
- }
- @ApiOperation(value = "获取bug list中一个bug关联的所有commit信息", httpMethod = "GET")
- @GetMapping("/commits/bug_list")
- @ApiImplicitParam(name = "bugId",dataType = "int", paramType = "query")
- public Response<List<CommitVO>> getBugCommits(@RequestParam("bugId") Integer bugId){
- return Response.buildSuccess(commitService.getBugCommit(bugId));
- }
- @ApiOperation(value = "获取指定项目的所有commit信息", httpMethod = "GET")
- @GetMapping("/commits/list")
- @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query")
- public Response<List<CommitVO>> getCommits(@RequestParam("projectId") Integer projectId) {
- return Response.buildSuccess(commitService.getCommits(projectId));
- }
- }
|