diff --git a/src/main/java/net/educoder/ecsonar/model/Metrics.java b/src/main/java/net/educoder/ecsonar/model/Metrics.java index dfaf52c..f1713c1 100644 --- a/src/main/java/net/educoder/ecsonar/model/Metrics.java +++ b/src/main/java/net/educoder/ecsonar/model/Metrics.java @@ -2,6 +2,7 @@ package net.educoder.ecsonar.model; public class Metrics { + // 复杂度 private String complexity; private int lines; diff --git a/src/main/java/net/educoder/ecsonar/services/ReportService.java b/src/main/java/net/educoder/ecsonar/services/ReportService.java index 219327d..02eba48 100644 --- a/src/main/java/net/educoder/ecsonar/services/ReportService.java +++ b/src/main/java/net/educoder/ecsonar/services/ReportService.java @@ -31,7 +31,7 @@ public class ReportService { public Metrics getMetrics(String name) { Project project = projectDao.findByName(name); if (project==null){ - return null; + return getDefault(); } String uuid = project.getProject_uuid(); @@ -57,6 +57,29 @@ public class ReportService { return metrics; } + public Metrics getDefault(){ + Metrics metrics = new Metrics(); + metrics.setComplexity("0"); + metrics.setLines(0); + metrics.setBlock_bugs(0); + metrics.setBlock_code_smells(0); + metrics.setBlock_violations(0); + metrics.setCritical_bugs(0); + metrics.setCritical_code_smells(0); + metrics.setCritical_violations(0); + metrics.setMajor_bugs(0); + metrics.setMajor_code_smells(0); + metrics.setMajor_violations(0); + metrics.setMinor_bugs(0); + metrics.setMinor_code_smells(0); + metrics.setMinor_violations(0); + metrics.setBugs("A"); + metrics.setViolations("A"); + metrics.setCode_smells("A"); + + return metrics; + } + /** * 将结果写进excel @@ -70,6 +93,22 @@ public class ReportService { ExcelUtil.saveExcel(workbook,outPath); } + /** + * 写入指定模板 + * @param stuId + * @param name + * @param metrics + * @param templatePath + * @param outPath + * @throws Exception + */ + public void writeSpecifyTemplateToExcel(String stuId, String name, + Metrics metrics, String templatePath, String outPath) throws Exception { + Workbook workbook = ExcelUtil.getWorkbok(new File(templatePath)); + ExcelUtil.writeToTemplateOne(metrics, stuId, name,workbook); + ExcelUtil.saveExcel(workbook,outPath); + } + /** * 获取复杂度 * diff --git a/src/main/java/net/educoder/ecsonar/services/SonarService.java b/src/main/java/net/educoder/ecsonar/services/SonarService.java index e430e5b..f0d70cf 100644 --- a/src/main/java/net/educoder/ecsonar/services/SonarService.java +++ b/src/main/java/net/educoder/ecsonar/services/SonarService.java @@ -36,8 +36,8 @@ public class SonarService { @Value("${sonar.url}") String sonarUrl; -// @Value("${sonar.token}") -// String sonarToken; + @Value("${sonar.token}") + String sonarToken; @Value("${extract.path}") String extractProgramPath; @@ -120,12 +120,14 @@ public class SonarService { public void sonar(String projectPath, String key) { - String command = "source ~/.bash_profile && sonar-scanner " + + String command = "cppcheck --xml --xml-version=2 --enable=all "+projectPath+" 2> /tmp/"+key+"-result.xml &&" + + "source ~/.bash_profile && sonar-scanner " + "-Dsonar.host.url=" + sonarUrl + " " + "-Dsonar.sourceEncoding=utf-8 " + "-Dsonar.projectKey=" + key + " " + -// "-Dsonar.login=" + sonarToken + " " + + "-Dsonar.login=" + sonarToken + " " + "-Dsonar.java.binaries=./ " + + "-Dsonar.cxx.cppcheck.reportPath=/tmp/"+ key+"-result.xml " + "-Dsonar.projectBaseDir=" + projectPath; log.info("projectPath:{},key:{}, command: {}", projectPath,key, command); SystemUtil.executeAndGetExitStatus(command); diff --git a/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java b/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java index c2022c2..860d3a8 100644 --- a/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java +++ b/src/main/java/net/educoder/ecsonar/task/ReadExcelRunnable.java @@ -44,15 +44,15 @@ public class ReadExcelRunnable implements Runnable{ if(!file.exists()){ file.mkdir(); } - for (Person person : sonarRequest.getPersonList()) { - String[] split = StringUtils.split(person.getDownloadUrl(), "/"); - String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]); - try { - download(person.getDownloadUrl(),path+zipFilename); - } catch (IOException e) { - e.printStackTrace(); - } - } +// for (Person person : sonarRequest.getPersonList()) { +// String[] split = StringUtils.split(person.getDownloadUrl(), "/"); +// String zipFilename = UrlUtil.getURLDecoderString(split[split.length - 1]); +// try { +// download(person.getDownloadUrl(),path+zipFilename); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } String key = String.format("%d-%s", homeworkId, uid); log.info("开始调用sonar:{}",key); sonarService.sonar(path,key); diff --git a/src/main/java/net/educoder/ecsonar/utils/ExcelUtil.java b/src/main/java/net/educoder/ecsonar/utils/ExcelUtil.java index 300ddc6..a92847c 100644 --- a/src/main/java/net/educoder/ecsonar/utils/ExcelUtil.java +++ b/src/main/java/net/educoder/ecsonar/utils/ExcelUtil.java @@ -3,6 +3,7 @@ package net.educoder.ecsonar.utils; import net.educoder.ecsonar.model.Metrics; import net.educoder.ecsonar.model.api.Person; import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; @@ -18,6 +19,11 @@ public class ExcelUtil { private static final String EXCEL_XLS = "xls"; private static final String EXCEL_XLSX = "xlsx"; + private static final String SCORE ="IFERROR(分数转化说明!B$4*V#line+分数转化说明!G$4*W#line+分数转化说明!L$4*X#line,\" \")"; + private static final String BUG ="IFERROR(100-SUMPRODUCT(代码检测结果!D#line:G#line,分数转化说明!B$6:E$6)/代码检测结果!$T#line*100,\" \")"; + private static final String VULNERABILITY ="IFERROR(100-SUMPRODUCT(代码检测结果!I#line:L#line,分数转化说明!G$6:J$6)/代码检测结果!$T#line*100,\" \")"; + private static final String CODE_SMELL ="IFERROR(100-SUMPRODUCT(代码检测结果!N#line:Q#line,分数转化说明!L$6:O$6)/代码检测结果!$T#line*100,\" \")"; + /** * 检查此用户是否查验过 @@ -60,121 +66,279 @@ public class ExcelUtil { Workbook workBook, int index) throws IOException { - // sheet 对应一个工作页 - Sheet sheet = workBook.getSheetAt(0); - /** - * 删除原有数据,除了属性列 - */ - int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算 - - //从第三行开始 - Row newRow = null; - - for (int i = 3; i < rowNumber+1; ++i) { - Row row = sheet.getRow(i); - - if (row != null) { - Cell cell = row.getCell(1); - cell.setCellType(CellType.STRING); - if (StringUtils.isEmpty(cell.getStringCellValue()) - || StringUtils.equals(cell.getStringCellValue(), uid)) { - - newRow = row; - break; - } + // sheet 对应一个工作页 + Sheet sheet = workBook.getSheetAt(0); + + + /** + * 删除原有数据,除了属性列 + */ + int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算 + + //从第三行开始 + Row newRow = null; + + for (int i = 3; i < rowNumber+1; ++i) { + Row row = sheet.getRow(i); + + if (row != null) { + Cell cell = row.getCell(1); + + cell.setCellType(CellType.STRING); + if (StringUtils.isEmpty(cell.getStringCellValue()) + || StringUtils.equals(cell.getStringCellValue(), uid)) { + + newRow = row; + break; } } + } - if (newRow == null) { - newRow = sheet.createRow(rowNumber+1); - } + if (newRow == null) { + newRow = sheet.createRow(rowNumber+1); + } + + //一直往下找,直到找到空的,或者与学号匹配的行,进行写入 + //学号 + Cell uidCell = newRow.createCell(1); + uidCell.setCellType(CellType.STRING); + uidCell.setCellValue(uid); + + Cell cell2 = newRow.createCell(2); + cell2.setCellType(CellType.STRING); + cell2.setCellValue(name); + + int offset = 0; + if (index == 2) { + offset = 17; + } + Cell cell3 = newRow.createCell(3 + offset); + cell3.setCellType(CellType.NUMERIC); + cell3.setCellValue(metrics.getBlock_bugs()); + + Cell cell4 = newRow.createCell(4 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getCritical_bugs()); + + cell4 = newRow.createCell(5 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMajor_bugs()); + + cell4 = newRow.createCell(6 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMinor_bugs()); + + cell4 = newRow.createCell(7 + offset); + cell4.setCellType(CellType.STRING); + cell4.setCellValue(metrics.getBugs()); + + + cell4 = newRow.createCell(8 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getBlock_violations()); + + cell4 = newRow.createCell(9 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getCritical_violations()); + + cell4 = newRow.createCell(10 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMajor_violations()); + + cell4 = newRow.createCell(11 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMinor_violations()); + + cell4 = newRow.createCell(12 + offset); + cell4.setCellType(CellType.STRING); + cell4.setCellValue(metrics.getViolations()); + + + cell4 = newRow.createCell(13 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getBlock_code_smells()); + + + cell4 = newRow.createCell(14 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getCritical_code_smells()); + + cell4 = newRow.createCell(15 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMajor_violations()); + + cell4 = newRow.createCell(16 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMinor_code_smells()); - //一直往下找,直到找到空的,或者与学号匹配的行,进行写入 - //学号 - Cell uidCell = newRow.createCell(1); - uidCell.setCellType(CellType.STRING); - uidCell.setCellValue(uid); + cell4 = newRow.createCell(17 + offset); + cell4.setCellType(CellType.STRING); + cell4.setCellValue(metrics.getCode_smells()); - Cell cell2 = newRow.createCell(2); - cell2.setCellType(CellType.STRING); - cell2.setCellValue(name); - int offset = 0; - if (index == 2) { - offset = 17; + cell4 = newRow.createCell(18 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getComplexity()); + + cell4 = newRow.createCell(19 + offset); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getLines()); + + } + + + /** + * 根据template1写入excel + * @param metrics + * @param uid + * @param name + * @param workBook + * @throws IOException + */ + public static void writeToTemplateOne(Metrics metrics, String uid, + String name, + Workbook workBook) throws IOException { + + + // sheet 对应一个工作页 + Sheet sheet = workBook.getSheetAt(0); + sheet.setForceFormulaRecalculation(true); + CellStyle cellStyle = workBook.createCellStyle(); + cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); + + + /** + * 删除原有数据,除了属性列 + */ + int rowNumber = sheet.getLastRowNum(); // 第一行从0开始算 + + //从第三行开始 + Row newRow = null; + + for (int i = 3; i < rowNumber+1; ++i) { + Row row = sheet.getRow(i); + + if (row != null) { + Cell cell = row.getCell(1); + + cell.setCellType(CellType.STRING); + if (StringUtils.isEmpty(cell.getStringCellValue()) + || StringUtils.equals(cell.getStringCellValue(), uid)) { + + newRow = row; + break; + } } - Cell cell3 = newRow.createCell(3 + offset); - cell3.setCellType(CellType.NUMERIC); - cell3.setCellValue(metrics.getBlock_bugs()); + } + + if (newRow == null) { + newRow = sheet.createRow(rowNumber+1); + } + + //一直往下找,直到找到空的,或者与学号匹配的行,进行写入 + //学号 + Cell uidCell = newRow.createCell(1); + uidCell.setCellType(CellType.STRING); + uidCell.setCellValue(uid); + + Cell cell2 = newRow.createCell(2); + cell2.setCellType(CellType.STRING); + cell2.setCellValue(name); + + + Cell cell3 = newRow.createCell(3); + cell3.setCellType(CellType.NUMERIC); + cell3.setCellValue(metrics.getBlock_bugs()); + + Cell cell4 = newRow.createCell(4); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getCritical_bugs()); + + cell4 = newRow.createCell(5); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMajor_bugs()); + + cell4 = newRow.createCell(6); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMinor_bugs()); + + cell4 = newRow.createCell(7); + cell4.setCellType(CellType.STRING); + cell4.setCellValue(metrics.getBugs()); - Cell cell4 = newRow.createCell(4 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getCritical_bugs()); - cell4 = newRow.createCell(5 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getMajor_bugs()); + cell4 = newRow.createCell(8); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getBlock_violations()); - cell4 = newRow.createCell(6 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getMinor_bugs()); + cell4 = newRow.createCell(9); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getCritical_violations()); - cell4 = newRow.createCell(7 + offset); - cell4.setCellType(CellType.STRING); - cell4.setCellValue(metrics.getBugs()); + cell4 = newRow.createCell(10 ); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMajor_violations()); + cell4 = newRow.createCell(11 ); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMinor_violations()); - cell4 = newRow.createCell(8 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getBlock_violations()); + cell4 = newRow.createCell(12); + cell4.setCellType(CellType.STRING); + cell4.setCellValue(metrics.getViolations()); - cell4 = newRow.createCell(9 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getCritical_violations()); - cell4 = newRow.createCell(10 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getMajor_violations()); + cell4 = newRow.createCell(13); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getBlock_code_smells()); - cell4 = newRow.createCell(11 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getMinor_violations()); - cell4 = newRow.createCell(12 + offset); - cell4.setCellType(CellType.STRING); - cell4.setCellValue(metrics.getViolations()); + cell4 = newRow.createCell(14); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getCritical_code_smells()); + cell4 = newRow.createCell(15); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMajor_violations()); - cell4 = newRow.createCell(13 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getBlock_code_smells()); + cell4 = newRow.createCell(16); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getMinor_code_smells()); + cell4 = newRow.createCell(17); + cell4.setCellType(CellType.STRING); + cell4.setCellValue(metrics.getCode_smells()); - cell4 = newRow.createCell(14 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getCritical_code_smells()); - cell4 = newRow.createCell(15 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getMajor_violations()); + cell4 = newRow.createCell(18); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getComplexity()); - cell4 = newRow.createCell(16 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getMinor_code_smells()); + cell4 = newRow.createCell(19); + cell4.setCellType(CellType.NUMERIC); + cell4.setCellValue(metrics.getLines()); - cell4 = newRow.createCell(17 + offset); - cell4.setCellType(CellType.STRING); - cell4.setCellValue(metrics.getCode_smells()); + String rowStr = String.valueOf(rowNumber+2); + cell4 = newRow.createCell(20); + cell4.setCellType(CellType.FORMULA); + cell4.setCellStyle(cellStyle); + cell4.setCellFormula(SCORE.replaceAll("#line", rowStr)); - cell4 = newRow.createCell(18 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getComplexity()); + cell4 = newRow.createCell(21); + cell4.setCellType(CellType.FORMULA); + cell4.setCellStyle(cellStyle); + cell4.setCellFormula(BUG.replaceAll("#line", rowStr)); - cell4 = newRow.createCell(19 + offset); - cell4.setCellType(CellType.NUMERIC); - cell4.setCellValue(metrics.getLines()); + cell4 = newRow.createCell(22); + cell4.setCellType(CellType.FORMULA); + cell4.setCellStyle(cellStyle); + cell4.setCellFormula(VULNERABILITY.replaceAll("#line", rowStr)); + cell4 = newRow.createCell(23); + cell4.setCellType(CellType.FORMULA); + cell4.setCellStyle(cellStyle); + cell4.setCellFormula(CODE_SMELL.replaceAll("#line", rowStr)); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 07d38f3..0b02acd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ -#spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/sonar7.7 -spring.datasource.url=jdbc:postgresql://117.50.14.123:5432/sonar -spring.datasource.username=sonar -spring.datasource.password=sonar +spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/sonar7.7 +#spring.datasource.url=jdbc:postgresql://117.50.14.123:5432/sonar +spring.datasource.username=root +spring.datasource.password=root spring.datasource.driver-class-name=org.postgresql.Driver diff --git a/src/main/resources/template1.xlsx b/src/main/resources/template1.xlsx index 6e80e04..b828e40 100644 Binary files a/src/main/resources/template1.xlsx and b/src/main/resources/template1.xlsx differ diff --git a/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java b/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java index 9112652..dd4056a 100644 --- a/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java +++ b/src/test/java/net/educoder/ecsonar/EcsonarApplicationTests.java @@ -100,7 +100,7 @@ public class EcsonarApplicationTests { Metrics metrics = reportService.getMetrics(projectName); String templatePath = this.getClass().getClassLoader().getResource("template1.xlsx").getPath(); - String outPath = "/Users/youyongsheng/Desktop/20210101.xlsx"; + String outPath = "/Users/youyongsheng/Desktop/202100000004.xlsx"; System.out.println("第"+(i++)+"个学生," + uid); if (CollectionUtils.isEmpty(collect.get(uid))) { @@ -109,12 +109,12 @@ public class EcsonarApplicationTests { continue; } - reportService.writeToExcel(uid, + + reportService.writeSpecifyTemplateToExcel(uid, collect.get(uid).get(0).getName(), metrics, - outPath, - outPath, - 1); + i == 2? templatePath : outPath, + outPath); }