From 7a861c0c65f35eb54b5a480a468267973639f8c1 Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Fri, 8 Jul 2022 13:49:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=90=9C=E7=B4=A2=E5=85=A8?= =?UTF-8?q?=E9=83=A8issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../educoder/ecsonar/constant/Constant.java | 2 ++ .../net/educoder/ecsonar/dao/ProjectDao.java | 4 ++-- .../ecsonar/model/vo/IssuesSearchVO.java | 6 +++++ .../ecsonar/services/CloudBrainService.java | 22 ++++++++++++++----- .../ecsonar/services/SonarService.java | 11 +++++++++- .../CloudBrainQualityInspectRunnable.java | 21 +++++++++++++++++- 6 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/educoder/ecsonar/constant/Constant.java b/src/main/java/net/educoder/ecsonar/constant/Constant.java index d70d37e..2eb4226 100644 --- a/src/main/java/net/educoder/ecsonar/constant/Constant.java +++ b/src/main/java/net/educoder/ecsonar/constant/Constant.java @@ -14,6 +14,7 @@ public class Constant { public static final String JAVA = "java"; public static final String C = "c"; public static final String CXX = "cpp"; + public static final String CPP = "c++"; public static final String PYTHON = "python"; public static final List language = Arrays.asList(JAVA, C, CXX, PYTHON); @@ -25,4 +26,5 @@ public class Constant { public static final String VULNERABILITY = "VULNERABILITY"; public static final String CODE_SMELL = "CODE_SMELL"; public static final String SECURITY_HOTSPOT = "SECURITY_HOTSPOT"; + public static final String ALL = "ALL"; } diff --git a/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java b/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java index 2ff5db0..9cf13ec 100644 --- a/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java +++ b/src/main/java/net/educoder/ecsonar/dao/ProjectDao.java @@ -53,9 +53,9 @@ public interface ProjectDao { - @Select("select (select count(1) from issues where project_uuid=#{projectUuid} and issue_type=1) vulnerability," + + @Select("select (select count(1) from issues where project_uuid=#{projectUuid} and issue_type=1) codeSmell," + "(select count(1) from issues where project_uuid=#{projectUuid} and issue_type=2) bugs," + - "(select count(1) from issues where project_uuid=#{projectUuid} and issue_type=3) codeSmell," + + "(select count(1) from issues where project_uuid=#{projectUuid} and issue_type=3) vulnerability," + "(select value from project_measures pm where component_uuid=#{projectUuid} and metric_id=3) codeLines") IssuesMetrics selectIssuesMetrics(String projectUuid); } diff --git a/src/main/java/net/educoder/ecsonar/model/vo/IssuesSearchVO.java b/src/main/java/net/educoder/ecsonar/model/vo/IssuesSearchVO.java index b9b51e1..776813d 100644 --- a/src/main/java/net/educoder/ecsonar/model/vo/IssuesSearchVO.java +++ b/src/main/java/net/educoder/ecsonar/model/vo/IssuesSearchVO.java @@ -8,6 +8,12 @@ package net.educoder.ecsonar.model.vo; public class IssuesSearchVO { private String taskName; + /** + * BUG, + * CODE_SMELL, + * VULNERABILITY + * ALL + */ private String type; public String getTaskName() { diff --git a/src/main/java/net/educoder/ecsonar/services/CloudBrainService.java b/src/main/java/net/educoder/ecsonar/services/CloudBrainService.java index 1dc6da7..ba7497a 100644 --- a/src/main/java/net/educoder/ecsonar/services/CloudBrainService.java +++ b/src/main/java/net/educoder/ecsonar/services/CloudBrainService.java @@ -3,6 +3,7 @@ package net.educoder.ecsonar.services; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import net.educoder.ecsonar.constant.Constant; import net.educoder.ecsonar.dao.CloudTaskInfoDao; import net.educoder.ecsonar.dao.CloudTaskInfoDetailDao; import net.educoder.ecsonar.dao.ProjectDao; @@ -23,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -183,15 +185,19 @@ public class CloudBrainService { /** * 获取issues * - * @param projectName - * @param types + * @param taskName 任务名称 + * @param type 类型 * @return */ - private List doGetIssues(String projectName, String types) { + private List doGetIssues(String taskName, String type) { + + if (Constant.ALL.equals(type)) { + type = StringUtils.join(Constant.BUG, ",", Constant.VULNERABILITY, ",", Constant.CODE_SMELL); + } StringBuilder sb = new StringBuilder(); - sb.append("/api/issues/search?componentKeys=").append(projectName) - .append("&s=FILE_LINE&resolved=false&types=").append(types) + sb.append("/api/issues/search?componentKeys=").append(taskName) + .append("&s=FILE_LINE&resolved=false&types=").append(type) .append("&ps=500&organization=default-organization&facets=severities%2Ctypes&additionalFields=_all"); String url = sonarHost + sb.toString(); @@ -217,6 +223,12 @@ public class CloudBrainService { return issuesVOS; } + /** + * 获取问题源代码 + * + * @param component + * @return + */ private List doGetSourceCode(String component) { StringBuilder sb = new StringBuilder(); diff --git a/src/main/java/net/educoder/ecsonar/services/SonarService.java b/src/main/java/net/educoder/ecsonar/services/SonarService.java index d135495..bc66a31 100644 --- a/src/main/java/net/educoder/ecsonar/services/SonarService.java +++ b/src/main/java/net/educoder/ecsonar/services/SonarService.java @@ -176,7 +176,7 @@ public class SonarService { public void sonar(String language, SonarScannerParam sonarScannerParam) { - Consumer consumer = concurrentHashMap.get(language); + Consumer consumer = concurrentHashMap.get(language.toLowerCase()); if (consumer != null) { log.info("语言:{},projectName:{},path:{}找到了consumer", language, sonarScannerParam.getProjectKey(),sonarScannerParam.getProjectPath()); consumer.accept(sonarScannerParam); @@ -195,6 +195,15 @@ public class SonarService { SystemUtil.executeAndGetExitStatus(command); } + /** + * 是否是支持的语言 + * @param language + * @return + */ + public boolean supportLanguage(String language){ + return concurrentHashMap.containsKey(language.toLowerCase()); + } + /** * 解压zip文件 * diff --git a/src/main/java/net/educoder/ecsonar/task/CloudBrainQualityInspectRunnable.java b/src/main/java/net/educoder/ecsonar/task/CloudBrainQualityInspectRunnable.java index 9792988..6955d82 100644 --- a/src/main/java/net/educoder/ecsonar/task/CloudBrainQualityInspectRunnable.java +++ b/src/main/java/net/educoder/ecsonar/task/CloudBrainQualityInspectRunnable.java @@ -1,6 +1,7 @@ package net.educoder.ecsonar.task; import com.alibaba.fastjson.JSONObject; +import net.educoder.ecsonar.constant.Constant; import net.educoder.ecsonar.model.CloudTaskInfoDetail; import net.educoder.ecsonar.model.SonarScannerParam; import net.educoder.ecsonar.services.DbOperateService; @@ -10,6 +11,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.util.concurrent.ExecutorService; /** @@ -54,7 +56,24 @@ public class CloudBrainQualityInspectRunnable implements Runnable { String projectKey = projectName; String projectPath = path; - sonarService.sonar(cloudTaskInfoDetail.getLanguage(), new SonarScannerParam(projectKey, projectPath)); + + String language = cloudTaskInfoDetail.getLanguage(); + // c和c++需要额外做一些处理 + if (sonarService.supportLanguage(language)) { + SonarScannerParam scannerParam = new SonarScannerParam(projectKey, projectPath); + if (Constant.C.equalsIgnoreCase(language) || Constant.CPP.equalsIgnoreCase(language)) { + String resultPath = String.format("/tmp/%s", cloudTaskInfoDetail.getTaskId()); + File f = new File(resultPath); + if (!f.exists()) { + f.mkdirs(); + } + scannerParam.setCppCheckReportPath(resultPath + String.format("/%s-result.xml", projectName)); + + } + sonarService.sonar(cloudTaskInfoDetail.getLanguage(), scannerParam); + } else { + sonarService.sonar(projectPath, projectKey); + } CloudBrainQueryResultRunnable queryResultRunnable = new CloudBrainQueryResultRunnable(projectName, cloudTaskInfoDetail.getId(), dbOperateService); queryResultPool.execute(queryResultRunnable);