@ -1,23 +1,29 @@
package net.educoder.ecsonar.services ;
import cn.hutool.http.HttpUtil ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;
import net.educoder.ecsonar.dao.CloudTaskInfoDao ;
import net.educoder.ecsonar.dao.CloudTaskInfoDetailDao ;
import net.educoder.ecsonar.dao.ProjectDao ;
import net.educoder.ecsonar.exception.BusinessException ;
import net.educoder.ecsonar.model.CloudTaskInfo ;
import net.educoder.ecsonar.model.CloudTaskInfoDetail ;
import net.educoder.ecsonar.model.IssuesMetrics ;
import net.educoder.ecsonar.model.Project ;
import net.educoder.ecsonar.model.api.QualityInspect ;
import net.educoder.ecsonar.model.api.QualityInspectIsCompleted ;
import net.educoder.ecsonar.model.vo. CloudBrainQualityInspectVO ;
import net.educoder.ecsonar.model.vo. * ;
import net.educoder.ecsonar.task.CloudBrainQualityInspectRunnable ;
import net.educoder.ecsonar.utils.IdUtils ;
import net.educoder.ecsonar.utils.SystemUtil ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.concurrent.ExecutorService ;
@ -41,6 +47,9 @@ public class CloudBrainService {
@Autowired
private DbOperateService dbOperateService ;
@Autowired
private ProjectDao projectDao ;
@Autowired
@Qualifier ( "sonarScannerPool" )
private ExecutorService sonarScannerPool ;
@ -49,6 +58,9 @@ public class CloudBrainService {
@Qualifier ( "sonarQueryResultPool" )
private ExecutorService sonarQueryResultPool ;
@Value ( "${sonar.host}" )
private String sonarHost ;
@Transactional ( rollbackFor = Exception . class )
public QualityInspect startQualityInspect ( List < CloudBrainQualityInspectVO > cloudBrainQualityInspectVO ) {
@ -108,4 +120,130 @@ public class CloudBrainService {
QualityInspectIsCompleted qualityInspectIsCompleted = new QualityInspectIsCompleted ( taskId , completed ) ;
return qualityInspectIsCompleted ;
}
public CloudQualityInspectResultVO cloudQualityInspectResult ( String taskId ) {
QualityInspectIsCompleted qualityInspectIsCompleted = cloudQualityInspectIsCompleted ( taskId ) ;
CloudQualityInspectResultVO resultVO = new CloudQualityInspectResultVO ( ) ;
resultVO . setTaskId ( taskId ) ;
resultVO . setCompleted ( qualityInspectIsCompleted . getCompleted ( ) ) ;
if ( qualityInspectIsCompleted . getCompleted ( ) = = 0 ) {
return resultVO ;
}
CloudTaskInfo cloudTaskInfo = cloudTaskInfoDao . queryById ( taskId ) ;
if ( cloudTaskInfo = = null ) {
return resultVO ;
}
List < String > projectNames = cloudTaskInfoDetailDao . selectProjectNamesByTaskId ( taskId ) ;
List < CloudQualityInspectMetricsVO > metricsVOList = new ArrayList < > ( projectNames . size ( ) ) ;
for ( String projectName : projectNames ) {
Project project = projectDao . findByName ( projectName ) ;
IssuesMetrics issuesMetrics = projectDao . selectIssuesMetrics ( project . getProject_uuid ( ) ) ;
CloudQualityInspectMetricsVO metricsVO = new CloudQualityInspectMetricsVO ( ) ;
metricsVO . setProjectName ( projectName ) ;
metricsVO . setBugs ( issuesMetrics . getBugs ( ) ) ;
metricsVO . setVulnerability ( issuesMetrics . getVulnerability ( ) ) ;
metricsVO . setCodeSmell ( issuesMetrics . getCodeSmell ( ) ) ;
metricsVO . setCodeLines ( issuesMetrics . getCodeLines ( ) ) ;
metricsVOList . add ( metricsVO ) ;
}
resultVO . setMetricsVOList ( metricsVOList ) ;
return resultVO ;
}
/ * *
* issues 搜 索
* @param searchVO
* @return
* /
public List < IssuesVO > issuesSearch ( IssuesSearchVO searchVO ) {
List < IssuesVO > issues = doGetIssues ( searchVO . getProjectName ( ) , searchVO . getType ( ) ) ;
return issues ;
}
/ * *
* 获 取 源 码
* @param component
* @return
* /
public List < SourceCodeVO > getSourceCode ( String component ) {
return doGetSourceCode ( component ) ;
}
/ * *
* 获 取 issues
*
* @param projectName
* @param types
* @return
* /
private List < IssuesVO > doGetIssues ( String projectName , String types ) {
StringBuilder sb = new StringBuilder ( ) ;
sb . append ( "/api/issues/search?componentKeys=" ) . append ( projectName )
. append ( "&s=FILE_LINE&resolved=false&types=" ) . append ( types )
. append ( "&ps=500&organization=default-organization&facets=severities%2Ctypes&additionalFields=_all" ) ;
String url = sonarHost + sb . toString ( ) ;
String result = HttpUtil . get ( url ) ;
if ( StringUtils . isEmpty ( result ) ) {
return new ArrayList < > ( 0 ) ;
}
JSONArray issuesArray = JSONObject . parseObject ( result ) . getJSONArray ( "issues" ) ;
List < IssuesVO > issuesVOS = new ArrayList < > ( issuesArray . size ( ) ) ;
for ( Object o : issuesArray ) {
JSONObject issueObj = ( JSONObject ) o ;
IssuesVO vo = new IssuesVO ( ) ;
vo . setComponent ( issueObj . getString ( "component" ) ) ;
vo . setLine ( issueObj . getInteger ( "line" ) ) ;
vo . setMessage ( issueObj . getString ( "message" ) ) ;
vo . setSeverity ( issueObj . getString ( "severity" ) ) ;
vo . setStatus ( issueObj . getString ( "status" ) ) ;
issuesVOS . add ( vo ) ;
}
return issuesVOS ;
}
private List < SourceCodeVO > doGetSourceCode ( String component ) {
StringBuilder sb = new StringBuilder ( ) ;
sb . append ( "/api/sources/lines?key=" ) . append ( component )
. append ( "&from=1&to=50100" ) ;
String url = sonarHost + sb . toString ( ) ;
String result = HttpUtil . get ( url ) ;
if ( StringUtils . isEmpty ( result ) ) {
return new ArrayList < > ( 0 ) ;
}
JSONArray sourcesArrays = JSONObject . parseObject ( result ) . getJSONArray ( "sources" ) ;
List < SourceCodeVO > sourceCodeVOS = new ArrayList < > ( sourcesArrays . size ( ) ) ;
for ( Object o : sourcesArrays ) {
JSONObject issueObj = ( JSONObject ) o ;
SourceCodeVO vo = new SourceCodeVO ( ) ;
vo . setCode ( issueObj . getString ( "code" ) ) ;
vo . setLine ( issueObj . getInteger ( "line" ) ) ;
sourceCodeVOS . add ( vo ) ;
}
return sourceCodeVOS ;
}
}