|
|
|
|
@ -28,6 +28,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@ -48,13 +50,22 @@ public class SonarService {
|
|
|
|
|
@Value("${extract.path}")
|
|
|
|
|
String extractProgramPath;
|
|
|
|
|
|
|
|
|
|
@Value("${sonar.scan.max-concurrent:1}")
|
|
|
|
|
int sonarScanMaxConcurrent;
|
|
|
|
|
|
|
|
|
|
@Value("${sonar.scan.retry-times:3}")
|
|
|
|
|
int sonarScanRetryTimes;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ExecutorService executorService;
|
|
|
|
|
|
|
|
|
|
private ConcurrentHashMap<String, Consumer<SonarScannerParam>> concurrentHashMap = new ConcurrentHashMap(8);
|
|
|
|
|
|
|
|
|
|
private Semaphore sonarScanSemaphore;
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
sonarScanSemaphore = new Semaphore(Math.max(1, sonarScanMaxConcurrent));
|
|
|
|
|
concurrentHashMap.put(Constant.JAVA, param -> {
|
|
|
|
|
String command = "sonar-scanner " +
|
|
|
|
|
"-Dsonar.host.url=" + sonarUrl + " " +
|
|
|
|
|
@ -184,7 +195,7 @@ public class SonarService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void sonar(String projectPath, String key) {
|
|
|
|
|
public boolean sonar(String projectPath, String key) {
|
|
|
|
|
String command = "sonar-scanner " +
|
|
|
|
|
"-Dsonar.host.url=" + sonarUrl + " " +
|
|
|
|
|
"-Dsonar.sourceEncoding=utf-8 " +
|
|
|
|
|
@ -192,8 +203,42 @@ public class SonarService {
|
|
|
|
|
"-Dsonar.java.binaries=./ " +
|
|
|
|
|
"-Dsonar.projectBaseDir=" + projectPath;
|
|
|
|
|
log.info("projectPath:{},key:{}, command: {}", projectPath, key, command);
|
|
|
|
|
SystemUtil.ExecuteResp executeResp = SystemUtil.executeAndGetExitStatus(command);
|
|
|
|
|
log.info("projectPath:{},key:{}, result: {}", projectPath, key, executeResp.getStatus() != 0 ? executeResp.getOutput() : "success");
|
|
|
|
|
boolean acquired = false;
|
|
|
|
|
try {
|
|
|
|
|
sonarScanSemaphore.acquire();
|
|
|
|
|
acquired = true;
|
|
|
|
|
for (int i = 1; i <= Math.max(1, sonarScanRetryTimes); i++) {
|
|
|
|
|
SystemUtil.ExecuteResp executeResp = SystemUtil.executeAndGetExitStatus(command);
|
|
|
|
|
if (executeResp.getStatus() == 0) {
|
|
|
|
|
log.info("projectPath:{},key:{}, result: success", projectPath, key);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String output = executeResp.getOutput();
|
|
|
|
|
boolean retryable = isRetryableSonarError(output);
|
|
|
|
|
log.warn("projectPath:{},key:{}, sonar scanner failed, attempt:{}/{}, retryable:{}, result:{}",
|
|
|
|
|
projectPath, key, i, sonarScanRetryTimes, retryable, output);
|
|
|
|
|
if (!retryable || i >= sonarScanRetryTimes) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
TimeUnit.SECONDS.sleep(20L * i);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
|
log.error("projectPath:{},key:{}, sonar scanner interrupted", projectPath, key, e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (acquired) {
|
|
|
|
|
sonarScanSemaphore.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isRetryableSonarError(String output) {
|
|
|
|
|
return StringUtils.contains(output, "Error 500")
|
|
|
|
|
|| StringUtils.contains(output, "/api/rules/search.protobuf")
|
|
|
|
|
|| StringUtils.contains(output, "Unable to load component class org.sonar.scanner.report.ActiveRulesPublisher")
|
|
|
|
|
|| StringUtils.contains(output, "Unable to load component interface org.sonar.api.batch.rule.ActiveRules");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -258,7 +303,7 @@ public class SonarService {
|
|
|
|
|
*/
|
|
|
|
|
public void scanExcel(String excelPath) {
|
|
|
|
|
|
|
|
|
|
int homeworkId = 202505142;
|
|
|
|
|
int homeworkId = 202605112;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
List<Person> personList = ExcelUtil.readExcel(excelPath);
|
|
|
|
|
|