diff --git a/src/main/java/net/educoder/ecsonar/constant/Constant.java b/src/main/java/net/educoder/ecsonar/constant/Constant.java index 2eb4226..69e637b 100644 --- a/src/main/java/net/educoder/ecsonar/constant/Constant.java +++ b/src/main/java/net/educoder/ecsonar/constant/Constant.java @@ -16,8 +16,9 @@ public class Constant { public static final String CXX = "cpp"; public static final String CPP = "c++"; public static final String PYTHON = "python"; + public static final String OTHER = "other"; - public static final List language = Arrays.asList(JAVA, C, CXX, PYTHON); + public static final List language = Arrays.asList(JAVA, C, CXX, PYTHON, OTHER); public static final String SUCCESS = "SUCCESS"; diff --git a/src/main/java/net/educoder/ecsonar/controller/QualityInspectController.java b/src/main/java/net/educoder/ecsonar/controller/QualityInspectController.java index 751fb36..ad5cac5 100644 --- a/src/main/java/net/educoder/ecsonar/controller/QualityInspectController.java +++ b/src/main/java/net/educoder/ecsonar/controller/QualityInspectController.java @@ -9,6 +9,7 @@ import net.educoder.ecsonar.model.api.QualityInspectResultData; import net.educoder.ecsonar.model.dto.AnalyseDetailDTO; import net.educoder.ecsonar.model.dto.AnalyseDetailListDTO; import net.educoder.ecsonar.model.dto.CodeDetailDTO; +import net.educoder.ecsonar.model.dto.ProblemAnalysisDTO; import net.educoder.ecsonar.model.vo.*; import net.educoder.ecsonar.services.QualityInspectService; import net.educoder.ecsonar.utils.ResponseResult; @@ -129,8 +130,8 @@ public class QualityInspectController { @GetMapping("/problemAnalysis") @ResponseBody public ResponseResult problemAnalysis(@RequestParam Integer ruleId) { - String description = qualityInspectService.getProblemAnalysis(ruleId); - return ResponseResult.success(description); + ProblemAnalysisDTO problemAnalysis = qualityInspectService.getProblemAnalysis(ruleId); + return ResponseResult.success(problemAnalysis); } diff --git a/src/main/java/net/educoder/ecsonar/dao/IssuesDao.java b/src/main/java/net/educoder/ecsonar/dao/IssuesDao.java index a3d24c0..37e8c1a 100644 --- a/src/main/java/net/educoder/ecsonar/dao/IssuesDao.java +++ b/src/main/java/net/educoder/ecsonar/dao/IssuesDao.java @@ -41,4 +41,12 @@ public interface IssuesDao { * @return */ DegreeDTO queryDegree(String projectUuid, Integer issueType); + + + /** + * 根据id查询 + * @param issueId + * @return + */ + Issues queryById(Long issueId); } diff --git a/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java b/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java index ccb1482..c7d9da1 100644 --- a/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java +++ b/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java @@ -43,12 +43,12 @@ public interface ProjectDao { "(SELECT COALESCE(count(*),0) critical_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='CRITICAL')," + "(SELECT COALESCE(count(*),0) major_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MAJOR')," + "(SELECT COALESCE(count(*),0) minor_bugs FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=2 and severity='MINOR')," + - "(select text_value bugs from project_measures where component_uuid = #{projectUuid} and metric_id = 89 order by id desc limit 1)," + + "(select text_value bugs from project_measures where component_uuid = #{projectUuid} and metric_id = 80 order by id desc limit 1)," + "(SELECT COALESCE(count(*),0) block_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='BLOCKER'), " + "(SELECT COALESCE(count(*),0) critical_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='CRITICAL')," + "(SELECT COALESCE(count(*),0) major_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MAJOR')," + "(SELECT COALESCE(count(*),0) minor_violations FROM issues where project_uuid=#{projectUuid} and status='OPEN' and issue_type=3 and severity='MINOR')," + - "(select text_value violations from project_measures where component_uuid = #{projectUuid} and metric_id = 93 order by id desc limit 1)") + "(select text_value violations from project_measures where component_uuid = #{projectUuid} and metric_id = 80 order by id desc limit 1)") Metrics selectMetricsByProjectUuid(String projectUuid); diff --git a/src/main/java/net/educoder/ecsonar/enums/DegreeEnum.java b/src/main/java/net/educoder/ecsonar/enums/DegreeEnum.java index a72b3c1..d506c6e 100644 --- a/src/main/java/net/educoder/ecsonar/enums/DegreeEnum.java +++ b/src/main/java/net/educoder/ecsonar/enums/DegreeEnum.java @@ -44,6 +44,15 @@ public enum DegreeEnum { throw new RuntimeException("Not Found DegreeEnum by type=" + type); } + public static DegreeEnum getDegreeEnumByValue(String value) { + for (DegreeEnum de : values()) { + if (de.value.equals(value)) { + return de; + } + } + throw new RuntimeException("Not Found DegreeEnum by value=" + value); + } + public Integer getType() { return type; diff --git a/src/main/java/net/educoder/ecsonar/model/Issues.java b/src/main/java/net/educoder/ecsonar/model/Issues.java index cab2c5c..8ecf9d5 100644 --- a/src/main/java/net/educoder/ecsonar/model/Issues.java +++ b/src/main/java/net/educoder/ecsonar/model/Issues.java @@ -1,11 +1,14 @@ package net.educoder.ecsonar.model; +import lombok.Data; + /** * @Author: youys * @Date: 2022/9/19 * @Description: */ +@Data public class Issues { private Long id; @@ -16,6 +19,7 @@ public class Issues { private String status; private String projectUuid; private Integer issueType; + private Integer lines; private byte[] locations = new byte[0]; /** @@ -37,108 +41,9 @@ public class Issues { */ private String uuid; + /** + * 语言 + */ + private String language; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getKee() { - return kee; - } - - public void setKee(String kee) { - this.kee = kee; - } - - public Integer getRuleId() { - return ruleId; - } - - public void setRuleId(Integer ruleId) { - this.ruleId = ruleId; - } - - public String getSeverity() { - return severity; - } - - public void setSeverity(String severity) { - this.severity = severity; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getProjectUuid() { - return projectUuid; - } - - public void setProjectUuid(String projectUuid) { - this.projectUuid = projectUuid; - } - - public Integer getIssueType() { - return issueType; - } - - public void setIssueType(Integer issueType) { - this.issueType = issueType; - } - - public byte[] getLocations() { - return locations; - } - - public void setLocations(byte[] locations) { - this.locations = locations; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String getUuid() { - return uuid; - } - - public void setUuid(String uuid) { - this.uuid = uuid; - } } diff --git a/src/main/java/net/educoder/ecsonar/model/Rule.java b/src/main/java/net/educoder/ecsonar/model/Rule.java index 44e598b..f31d294 100644 --- a/src/main/java/net/educoder/ecsonar/model/Rule.java +++ b/src/main/java/net/educoder/ecsonar/model/Rule.java @@ -2,6 +2,8 @@ package net.educoder.ecsonar.model; import lombok.Data; +import java.util.Date; + /** * @Author: youys * @Date: 2022/10/17 @@ -13,4 +15,24 @@ public class Rule { private Long id; private String name; private String description; + + /** + * 语言 + */ + private String language; + + /** + * 20min + */ + private String defRemediationBaseEffort; + + /** + * 标签, ","分割 + */ + private String tags; + + /** + * 创建时间 + */ + private Long createTime; } diff --git a/src/main/java/net/educoder/ecsonar/model/dto/AnalyseDetailListDTO.java b/src/main/java/net/educoder/ecsonar/model/dto/AnalyseDetailListDTO.java index 84067ad..ec63d5d 100644 --- a/src/main/java/net/educoder/ecsonar/model/dto/AnalyseDetailListDTO.java +++ b/src/main/java/net/educoder/ecsonar/model/dto/AnalyseDetailListDTO.java @@ -3,7 +3,6 @@ package net.educoder.ecsonar.model.dto; import lombok.Data; -import java.util.Date; /** * @Author: youys @@ -28,15 +27,16 @@ public class AnalyseDetailListDTO { /** * 行号 */ - private String rowNumber; + private Integer rowNumber; + /** - * 级别 + * 语言 */ - private String level; + private String language; /** - * 检测时间 + * 级别 */ - private Date detectTime; + private String level; /** * 找文件唯一标识 @@ -45,4 +45,6 @@ public class AnalyseDetailListDTO { private Integer ruleId; + private Long issueId; + } diff --git a/src/main/java/net/educoder/ecsonar/model/dto/CodeDetailDTO.java b/src/main/java/net/educoder/ecsonar/model/dto/CodeDetailDTO.java index 935525a..b849558 100644 --- a/src/main/java/net/educoder/ecsonar/model/dto/CodeDetailDTO.java +++ b/src/main/java/net/educoder/ecsonar/model/dto/CodeDetailDTO.java @@ -21,4 +21,14 @@ public class CodeDetailDTO { */ private String example; + /** + * 标题 + */ + private String title; + + /** + * 错误消息 + */ + private String errMessage; + } diff --git a/src/main/java/net/educoder/ecsonar/model/dto/ProblemAnalysisDTO.java b/src/main/java/net/educoder/ecsonar/model/dto/ProblemAnalysisDTO.java new file mode 100644 index 0000000..8394d33 --- /dev/null +++ b/src/main/java/net/educoder/ecsonar/model/dto/ProblemAnalysisDTO.java @@ -0,0 +1,38 @@ +package net.educoder.ecsonar.model.dto; + +import lombok.Data; + +import java.util.List; + +/** + * @Author: youys + * @Date: 2022/12/22 + * @Description: + */ +@Data +public class ProblemAnalysisDTO { + + private String title; + private String example; + + + /** + * 语言 + */ + private String language; + + /** + * 20min + */ + private String constantIssue; + + /** + * 标签 + */ + private List tags; + + /** + * 创建时间 + */ + private String createTime; +} diff --git a/src/main/java/net/educoder/ecsonar/model/vo/AnalyseDetailListVO.java b/src/main/java/net/educoder/ecsonar/model/vo/AnalyseDetailListVO.java index aa7de35..ffb1fde 100644 --- a/src/main/java/net/educoder/ecsonar/model/vo/AnalyseDetailListVO.java +++ b/src/main/java/net/educoder/ecsonar/model/vo/AnalyseDetailListVO.java @@ -14,9 +14,9 @@ public class AnalyseDetailListVO extends PageVO { /** * 类型 - * 1 bug - * 2 漏洞 - * 3 代码规范 + * 1 代码规范 + * 2 bug + * 3 漏洞 */ private Integer type; diff --git a/src/main/java/net/educoder/ecsonar/model/vo/CodeDetailVO.java b/src/main/java/net/educoder/ecsonar/model/vo/CodeDetailVO.java index c892060..7319900 100644 --- a/src/main/java/net/educoder/ecsonar/model/vo/CodeDetailVO.java +++ b/src/main/java/net/educoder/ecsonar/model/vo/CodeDetailVO.java @@ -13,4 +13,5 @@ public class CodeDetailVO { private String uuid; private Integer ruleId; + private Long issueId; } diff --git a/src/main/java/net/educoder/ecsonar/services/QualityInspectService.java b/src/main/java/net/educoder/ecsonar/services/QualityInspectService.java index 0e0f9f3..16955f3 100644 --- a/src/main/java/net/educoder/ecsonar/services/QualityInspectService.java +++ b/src/main/java/net/educoder/ecsonar/services/QualityInspectService.java @@ -1,5 +1,6 @@ package net.educoder.ecsonar.services; +import cn.hutool.core.date.DateUtil; import com.google.protobuf.InvalidProtocolBufferException; import net.educoder.ecsonar.dao.*; import net.educoder.ecsonar.enums.AnalyseTypeEnum; @@ -28,6 +29,8 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; import java.util.List; import java.util.concurrent.ExecutorService; @@ -201,7 +204,7 @@ public class QualityInspectService { private BigDecimal calcQualityScore(QualityInspectResultData resultData) { if (resultData.getTotalRowNumber() <= 0) { - return null; + return BigDecimal.ZERO; } // 100 -(阻断 * 10 + 严重 * 5 + 主要* 3 + 次要 * 1)/ 总行数 * 100 @@ -300,17 +303,33 @@ public class QualityInspectService { return rollPage; } - public String getProblemAnalysis(Integer ruleId) { + public ProblemAnalysisDTO getProblemAnalysis(Integer ruleId) { Rule rule = projectDao.findRuleById(ruleId); + ProblemAnalysisDTO problemAnalysisDTO = new ProblemAnalysisDTO(); if (rule != null) { String example = rule.getDescription().replaceAll("

", "

") .replaceAll("Noncompliant Code Example", "错误代码示范") .replaceAll("Compliant Solution", "正确代码示范") .replaceAll("Exceptions", "异常代码") .replaceAll("See", "链接"); - return example; + + + problemAnalysisDTO.setTitle(rule.getName()); + problemAnalysisDTO.setExample(example); + problemAnalysisDTO.setLanguage(rule.getLanguage()); + problemAnalysisDTO.setConstantIssue(rule.getDefRemediationBaseEffort()); + problemAnalysisDTO.setTags(Arrays.asList(rule.getTags().split(","))); + problemAnalysisDTO.setCreateTime(DateUtil.formatDateTime(new Date(rule.getCreateTime()))); + }else{ + problemAnalysisDTO.setTitle(""); + problemAnalysisDTO.setExample(""); + problemAnalysisDTO.setLanguage(""); + problemAnalysisDTO.setConstantIssue(""); + problemAnalysisDTO.setTags(new ArrayList<>()); + problemAnalysisDTO.setCreateTime(DateUtil.formatTime(new Date())); } - return ""; + + return problemAnalysisDTO; } /** @@ -336,7 +355,13 @@ public class QualityInspectService { CodeDetailDTO codeDetail = new CodeDetailDTO(); codeDetail.setCodes(fileSourceDTOList); - codeDetail.setExample(getProblemAnalysis(codeDetailVO.getRuleId())); + + Issues issues = issuesDao.queryById(codeDetailVO.getIssueId()); + + ProblemAnalysisDTO problemAnalysis = getProblemAnalysis(codeDetailVO.getRuleId()); + codeDetail.setExample(problemAnalysis.getExample()); + codeDetail.setTitle(problemAnalysis.getTitle()); + codeDetail.setErrMessage(issues.getMessage()); return codeDetail; @@ -353,14 +378,17 @@ public class QualityInspectService { detailListDTO.setName(pageIssue.getName()); detailListDTO.setDescription(pageIssue.getMessage()); + detailListDTO.setIssueId(pageIssue.getId()); detailListDTO.setUuid(pageIssue.getUuid()); detailListDTO.setRuleId(pageIssue.getRuleId()); detailListDTO.setFilePath(pageIssue.getPath()); + detailListDTO.setLanguage(pageIssue.getLanguage()); + detailListDTO.setLevel(DegreeEnum.getDegreeEnumByValue(pageIssue.getSeverity()).getDesc()); try { DbIssues.Locations locations = DbIssues.Locations.parseFrom(pageIssue.getLocations()); - detailListDTO.setRowNumber(String.valueOf(locations.getTextRange().getStartLine())); + detailListDTO.setRowNumber(locations.getTextRange().getStartLine()); } catch (InvalidProtocolBufferException e) { - detailListDTO.setRowNumber("0"); + detailListDTO.setRowNumber(0); logger.error("Fail to read ISSUES.LOCATIONS [KEE=%s]", e); } } diff --git a/src/main/java/net/educoder/ecsonar/task/QualityInspectRunnable.java b/src/main/java/net/educoder/ecsonar/task/QualityInspectRunnable.java index 339f1b1..c0c2b77 100644 --- a/src/main/java/net/educoder/ecsonar/task/QualityInspectRunnable.java +++ b/src/main/java/net/educoder/ecsonar/task/QualityInspectRunnable.java @@ -59,6 +59,7 @@ public class QualityInspectRunnable implements Runnable { String path = String.format("/tmp/%s/%s/", homeworkId, projectName); List codeIds = userData.getCodeIds(); + boolean constantCFlag = true,constantCPPFlag = true,constantJavaFlag = true, constantPyFlag = true; for (Long codeId : codeIds) { File file = new File(path); try { @@ -72,6 +73,27 @@ public class QualityInspectRunnable implements Runnable { continue; } FileUtil.writeString(gameCodes.getCode(), path + gameCodes.getPath(), Charset.forName("UTF-8")); + + if (constantCFlag && gameCodes.getPath().toLowerCase().contains(".c")) { + constantCFlag = false; + } else if (constantCPPFlag && gameCodes.getPath().toLowerCase().contains(".cpp")) { + constantCPPFlag = false; + } else if (constantJavaFlag && gameCodes.getPath().toLowerCase().contains(".java")) { + constantJavaFlag = false; + } else if (constantPyFlag && gameCodes.getPath().toLowerCase().contains(".py")) { + constantPyFlag = false; + } + } + + // 需要自己判别语言,Java优先 + if(!constantJavaFlag){ + language = Constant.JAVA; + }else if(!constantPyFlag){ + language = Constant.PYTHON; + } else if(!constantCFlag){ + language = Constant.C; + }else if(!constantCPPFlag){ + language = Constant.CXX; } // 写完所有文件开始用sonar进行质量分析 diff --git a/src/main/resources/mapper/IssuesMapper.xml b/src/main/resources/mapper/IssuesMapper.xml index b50d13a..8150707 100644 --- a/src/main/resources/mapper/IssuesMapper.xml +++ b/src/main/resources/mapper/IssuesMapper.xml @@ -23,7 +23,7 @@ + + + diff --git a/src/main/resources/mapper/ProjectMapper.xml b/src/main/resources/mapper/ProjectMapper.xml index 5c37ca1..c30008b 100644 --- a/src/main/resources/mapper/ProjectMapper.xml +++ b/src/main/resources/mapper/ProjectMapper.xml @@ -7,7 +7,11 @@ select id, name, - description + description, + language, + def_remediation_base_effort defRemediationBaseEffort, + system_tags tags, + created_at createTime from rules where id=#{id,jdbcType=INTEGER}