diff --git a/src/main/java/net/educoder/ecsonar/controller/MainController.java b/src/main/java/net/educoder/ecsonar/controller/MainController.java index d5cf607..4983581 100644 --- a/src/main/java/net/educoder/ecsonar/controller/MainController.java +++ b/src/main/java/net/educoder/ecsonar/controller/MainController.java @@ -14,7 +14,6 @@ import java.io.IOException; public class MainController { private Logger log = LoggerFactory.getLogger(getClass()); - @Autowired EcsonarService ecsonarService; @@ -26,6 +25,13 @@ public class MainController { return "success"; } + @RequestMapping(value = "/sonarSingle", method = RequestMethod.POST) + @CrossOrigin + public String sonarSingle(@RequestBody SonarRequest sonarRequest) throws IOException { + log.info("sonarSingle: {}", sonarRequest); + ecsonarService.sonarSingle(sonarRequest); + return "success"; + } @RequestMapping(value = "/fit", method = RequestMethod.GET) public String fit(@RequestParam String fileName, diff --git a/src/main/java/net/educoder/ecsonar/services/EcsonarService.java b/src/main/java/net/educoder/ecsonar/services/EcsonarService.java index aa82f38..b2a95a0 100644 --- a/src/main/java/net/educoder/ecsonar/services/EcsonarService.java +++ b/src/main/java/net/educoder/ecsonar/services/EcsonarService.java @@ -42,8 +42,6 @@ public class EcsonarService { @Value("${force.parse:false}") boolean forceParse; - - public static class Row { public int i; public Person person; @@ -87,7 +85,7 @@ public class EcsonarService { if (isSonared(key)){ - writeResult(csvPath, row.i, row.person, row.key); + writeResult(csvPath, row.i, row.person, row.key, Boolean.FALSE); } else { sonarService.scan( zipUsePath, @@ -104,7 +102,7 @@ public class EcsonarService { 2); - writeResult(csvPath, row.i, row.person, row.key); + writeResult(csvPath, row.i, row.person, row.key, Boolean.FALSE); } } @@ -115,13 +113,13 @@ public class EcsonarService { person.getUid(), e.getMessage()); - writeFailResult(csvPath, i, person, e.getMessage()); + writeFailResult(csvPath, i, person, e.getMessage(), Boolean.FALSE); } } else { log.error("未发现附件:{}", person); - writeFailResult(csvPath, i, person, "没有上传附件"); + writeFailResult(csvPath, i, person, "没有上传附件", Boolean.FALSE); } } @@ -129,6 +127,65 @@ public class EcsonarService { }); } + public void sonarSingle(SonarRequest sonarRequest) { + String codeUsePath = String.format("%s%d/", zipSavePath, sonarRequest.getHomeworkId()); + + if (!new File(codeUsePath).exists()) { + new File(codeUsePath).mkdir(); + } + + String csvPath = codeUsePath + String.format("%d.csv", sonarRequest.getHomeworkId()); + + List uids = readAllUids(csvPath); + + executorService.execute(() -> { + int i = 0; + for (Person person : sonarRequest.getPersonList()) { + i++; + + log.info("{}/{} 开始处理: {} ", i, sonarRequest.getPersonList().size(), person); + if (person.getDownloadUrl().startsWith("http")) { + try { + + String key = String.format("%d-%s", sonarRequest.getHomeworkId(), person.getUid()); + if (forceParse || (!isChecked(uids, person.getUid())) ) { + + Row row = new Row(); + row.i = i; + row.key = key; + row.person = person; + + if (isSonaredSingle(key)){ + + //数据库如果有就不检测直接写结果 + writeResult(csvPath, row.i, row.person, row.key, Boolean.TRUE); + } else { + sonarService.scanSingle( + codeUsePath, + person.getDownloadUrl(), + sonarRequest.getHomeworkId(), + person.getUid(), key); + + writeResult(csvPath, row.i, row.person, row.key, Boolean.TRUE); + } + } + } catch (Exception e) { + log.error("sonar scan error: ", e); + log.error("检测出错: {}-{}, 原因: {}", + person.getName(), + person.getUid(), + e.getMessage()); + + writeFailResult(csvPath, i, person, e.getMessage(), Boolean.TRUE); + } + } else { + log.error("未发现附件:{}", person); + writeFailResult(csvPath, i, person, "没有上传附件", Boolean.TRUE); + } + } + }); + } + /** * 是否提交过sonar结果 * @param key @@ -139,9 +196,14 @@ public class EcsonarService { return project!=null; } - private void writeFailResult(String filePath, int i, Person person, String message) { + private boolean isSonaredSingle(String key) { + Project project = projectDao.findByName(key); + return project!=null; + } + + private void writeFailResult(String filePath, int i, Person person, String message, Boolean isSingle) { try { - StringBuffer stringBuffer = getFailString(i, person, message); + StringBuffer stringBuffer = getFailString(i, person, message, isSingle); FileWriter fileWritter = new FileWriter(filePath, true); fileWritter.write(stringBuffer.toString()); @@ -154,23 +216,31 @@ public class EcsonarService { } } - private StringBuffer getFailString(int i, Person person, String message) { + private StringBuffer getFailString(int i, Person person, String message, Boolean isSingle) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(i); stringBuffer.append(","); stringBuffer.append(person.getUid()); stringBuffer.append(","); stringBuffer.append(person.getName()); - stringBuffer.append(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"); + if (isSingle) { + stringBuffer.append(",,,,,,,,,,,,,,,,,,"); + } else { + stringBuffer.append(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"); + } stringBuffer.append(message); stringBuffer.append("\n"); return stringBuffer; } - private void writeResult(String filePath, int i, Person person, String key) { + private void writeResult(String filePath, int i, Person person, String key, Boolean isSingle) { try { - StringBuffer stringBuffer = getPersonLine(i, person, key); - + StringBuffer stringBuffer; + if (isSingle) { + stringBuffer = getSinglePersonLine(i, person, key); + } else { + stringBuffer = getPersonLine(i, person, key); + } FileWriter fileWritter = new FileWriter(filePath, true); fileWritter.write(stringBuffer.toString()); @@ -181,10 +251,58 @@ public class EcsonarService { person.getUid(), e.getMessage()); - writeFailResult(filePath, i, person, e.getMessage()); + writeFailResult(filePath, i, person, e.getMessage(), isSingle); } } + private StringBuffer getSinglePersonLine(int i, Person person, String key) { + Metrics metrics = reportService.getMetrics(key); + + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(i); + stringBuffer.append(","); + stringBuffer.append(person.getUid()); + stringBuffer.append(","); + stringBuffer.append(person.getName()); + stringBuffer.append(","); + stringBuffer.append(metrics.getBlock_bugs()); + stringBuffer.append(","); + stringBuffer.append(metrics.getCritical_bugs()); + stringBuffer.append(","); + stringBuffer.append(metrics.getMajor_bugs()); + stringBuffer.append(","); + stringBuffer.append(metrics.getMinor_bugs()); + stringBuffer.append(","); + stringBuffer.append(metrics.getBugs()); + stringBuffer.append(","); + stringBuffer.append(metrics.getBlock_violations()); + stringBuffer.append(","); + stringBuffer.append(metrics.getCritical_violations()); + stringBuffer.append(","); + stringBuffer.append(metrics.getMajor_violations()); + stringBuffer.append(","); + stringBuffer.append(metrics.getMinor_violations()); + stringBuffer.append(","); + stringBuffer.append(metrics.getViolations()); + stringBuffer.append(","); + stringBuffer.append(metrics.getBlock_code_smells()); + stringBuffer.append(","); + stringBuffer.append(metrics.getCritical_code_smells()); + stringBuffer.append(","); + stringBuffer.append(metrics.getMajor_violations()); + stringBuffer.append(","); + stringBuffer.append(metrics.getMinor_code_smells()); + stringBuffer.append(","); + stringBuffer.append(metrics.getCode_smells()); + stringBuffer.append(","); + stringBuffer.append(metrics.getComplexity()); + stringBuffer.append(","); + stringBuffer.append(metrics.getLines()); + stringBuffer.append("\n"); + + return stringBuffer; + } + private StringBuffer getPersonLine(int i, Person person, String key) { Metrics metrics = reportService.getMetrics(key + "-1"); @@ -369,7 +487,7 @@ public class EcsonarService { ); lines.add(stringBuffer.toString()); } catch (Exception e){ - StringBuffer failString = getFailString(i, person, split[split.length - 1]); + StringBuffer failString = getFailString(i, person, split[split.length - 1], Boolean.FALSE); lines.add(failString.toString()); } diff --git a/src/main/java/net/educoder/ecsonar/services/SonarService.java b/src/main/java/net/educoder/ecsonar/services/SonarService.java index 0ba8068..74d1c6e 100644 --- a/src/main/java/net/educoder/ecsonar/services/SonarService.java +++ b/src/main/java/net/educoder/ecsonar/services/SonarService.java @@ -71,6 +71,30 @@ public class SonarService { return key; } + /** + * 提交一个任务 + */ + public String scanSingle(String codeSavePath, String codeUrl, int homeworkId, String uid, String key) throws Exception { + + String[] split = StringUtils.split(codeUrl, "/"); + + String codeFilename = UrlUtil.getURLDecoderString(split[split.length - 1]); + + String savePath = codeSavePath + codeFilename; + if (!new File(savePath).exists()) { + new File(savePath).mkdir(); + } + // 代码文件后缀写死 + String saveFile = savePath + File.separator + codeFilename + ".cpp"; + + // 下载文件 + download(codeUrl, saveFile); + + sonar(savePath, key); + + return key; + } + private void sonar(String projectPath, String key) { SystemUtil.executeAndGetExitStatus("sonar-scanner " +