|
|
@@ -1,229 +0,0 @@
|
|
|
-package cn.seecoder.web.controller.gitlab;
|
|
|
-
|
|
|
-import cn.seecoder.common.exceptions.ServiceException;
|
|
|
-import cn.seecoder.common.util.LogTrackingUtil;
|
|
|
-import cn.seecoder.common.util.OpType;
|
|
|
-import cn.seecoder.web.core.commit.CommitResolver;
|
|
|
-import cn.seecoder.web.core.commit.model.CommitRule;
|
|
|
-import cn.seecoder.web.dao.user.UserMapper;
|
|
|
-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.alibaba.fastjson.JSONObject;
|
|
|
-import com.nju.edu.gitlab.SeecoderGitlabApi;
|
|
|
-import com.nju.edu.gitlab.vo.DiffVO;
|
|
|
-import io.swagger.annotations.Api;
|
|
|
-import io.swagger.annotations.ApiImplicitParam;
|
|
|
-import io.swagger.annotations.ApiImplicitParams;
|
|
|
-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;
|
|
|
-import java.util.Set;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author chenyz
|
|
|
- * @date 2021/2/4
|
|
|
- * @description: 与GitLab进行交互的接口
|
|
|
- */
|
|
|
-@Api(tags = "GitLab Hook API")
|
|
|
-@RestController
|
|
|
-@Slf4j
|
|
|
-public class CommitController {
|
|
|
-
|
|
|
- private final static String LINK_FAIL_ERROR = "Commit Link Failed! No Such Bug or Task Found!";
|
|
|
-
|
|
|
- private final BugListService bugListService;
|
|
|
-
|
|
|
- private final TreeNodeService treeNodeService;
|
|
|
-
|
|
|
- private final CommitService commitService;
|
|
|
-
|
|
|
- private final SeecoderGitlabApi seecoderGitlabApi;
|
|
|
-
|
|
|
- // 赶时间暂时这么写
|
|
|
- @Autowired
|
|
|
- UserMapper userMapper;
|
|
|
-
|
|
|
- @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;
|
|
|
- }
|
|
|
- //ANA 日志需要打出用户的commit信息
|
|
|
- JSONObject object = new JSONObject();
|
|
|
- try{
|
|
|
- object.put("commit_id",commit.getId());
|
|
|
- object.put("user_id",userMapper.selectIdByEmail(commit.getAuthor().getEmail()));
|
|
|
- object.put("project_id",projectId);
|
|
|
- object.put("commit_time",commit.getTimestamp());
|
|
|
- object.put("message",commit.getMessage());
|
|
|
- }catch (Exception ignored){}
|
|
|
-
|
|
|
-
|
|
|
- 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());
|
|
|
- //ANA
|
|
|
- try {
|
|
|
- object.put("type","不合规范");
|
|
|
- String data = JSONObject.toJSONString(object);
|
|
|
- LogTrackingUtil.log(data, OpType.COMMIT);
|
|
|
- } catch (Exception ignored) {}
|
|
|
- continue;
|
|
|
- }
|
|
|
- switch (rule.getType()){
|
|
|
- case FEAT:
|
|
|
- treeNodeService.updateNodeState(rule.getId(),rule.getState());
|
|
|
- commitService.save(new CommitPO(commit,projectId, CommitRelatedEnum.TREE_ID, rule.getId()));
|
|
|
- log.info("Commits 关联tree节点, id: {}", commit.getId());
|
|
|
- //ANA
|
|
|
- try {
|
|
|
- object.put("type","关联tree");
|
|
|
- String data = JSONObject.toJSONString(object);
|
|
|
- LogTrackingUtil.log(data, OpType.COMMIT);
|
|
|
- } catch (Exception ignored) {}
|
|
|
- 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());
|
|
|
- //ANA
|
|
|
- try {
|
|
|
- object.put("type","关联bug");
|
|
|
- String data = JSONObject.toJSONString(object);
|
|
|
- LogTrackingUtil.log(data, OpType.COMMIT);
|
|
|
- } catch (Exception ignored) {}
|
|
|
- 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));
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation(value = "获取指定项目指定分支所有的commit信息", httpMethod = "GET")
|
|
|
- @GetMapping("/commits/by_branch")
|
|
|
- @ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "branchName", dataType = "String", paramType = "query")
|
|
|
- })
|
|
|
- public Response<List<CommitVO>> getCommitsByBranch(@RequestParam("projectId") Integer projectId,
|
|
|
- @RequestParam("branchName") String branchName){
|
|
|
- return Response.buildSuccess(commitService.getCommitsByBranchName(projectId, branchName));
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation(value = "获得一个commit的信息", httpMethod = "GET")
|
|
|
- @GetMapping("/commit")
|
|
|
- @ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "hash", dataType = "String", paramType = "query")
|
|
|
- })
|
|
|
- public Response<CommitVO> getCommitByHash(@RequestParam("projectId") Integer projectId,
|
|
|
- @RequestParam("hash") String hash)
|
|
|
- {
|
|
|
- return Response.buildSuccess(commitService.getCommitByHash(projectId, hash));
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation(value = "获得两个提交之间的差异", httpMethod = "GET")
|
|
|
- @GetMapping("/commits/diff")
|
|
|
- @ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "from", dataType = "String", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "to", dataType = "String", paramType = "query")
|
|
|
- })
|
|
|
- public Response<Set<DiffVO>> getDiff(@RequestParam("projectId") Integer projectId,
|
|
|
- @RequestParam("from") String from,
|
|
|
- @RequestParam("to") String to)
|
|
|
- {
|
|
|
- return Response.buildSuccess(commitService.getDiff(projectId, from, to));
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation(value = "关联commit和缺陷、任务节点", httpMethod = "GET")
|
|
|
- @GetMapping("/commits/link")
|
|
|
- @ApiImplicitParams({
|
|
|
- @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "commitId", dataType = "String", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "relatedType", dataType = "String", paramType = "query"),
|
|
|
- @ApiImplicitParam(name = "relatedId", dataType = "int", paramType = "query")
|
|
|
- })
|
|
|
- public Response<Object> linkCommit(@RequestParam("projectId") Integer projectId,
|
|
|
- @RequestParam("commitId") String commitId,
|
|
|
- @RequestParam("relatedType") CommitRelatedEnum relatedType,
|
|
|
- @RequestParam("relatedId") Integer relatedId)
|
|
|
- {
|
|
|
- if (commitService.link(projectId, commitId, relatedType, relatedId)){
|
|
|
- return Response.buildSuccess(null);
|
|
|
- } else {
|
|
|
- return Response.buildFailure(404, LINK_FAIL_ERROR);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|