sast worker 逻辑完善
This commit is contained in:
parent
9418ad8856
commit
cc6d090327
@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.constraints.Negative;
|
||||
import jakarta.ws.rs.Path;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@ -138,4 +139,9 @@ public class SastController {
|
||||
sastService.reportDelete(reportId);
|
||||
return CommonResult.success();
|
||||
}
|
||||
@GetMapping("/getApplicationStatus/{applicationId}")
|
||||
public CommonResult<SastApplicationStatusResp> getApplicationStatus(@PathVariable String applicationId){
|
||||
SastApplicationStatusResp applicationStatus = sastService.getApplicationStatus(applicationId);
|
||||
return CommonResult.success(applicationStatus);
|
||||
}
|
||||
}
|
||||
|
@ -16,4 +16,5 @@ public class SastUrlConstant {
|
||||
public static final String language = "/invoke/language";
|
||||
public static final String reportList = "/api/report/index/pageList";
|
||||
public static final String reportDelete = "/api/report/index";
|
||||
public static final String getApplicationStatus = "/invoke/application/getStatus/";
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package cd.casic.ci.process.dto.resp.sast;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class SastApplicationStatusResp {
|
||||
|
||||
private String id;
|
||||
private LocalDateTime createdTime;
|
||||
private String createdBy;
|
||||
private LocalDateTime updatedTime;
|
||||
private String updatedBy;
|
||||
private String fileId;
|
||||
private String jobAddBy;
|
||||
private String jobStatus;
|
||||
private String checkStartTime;
|
||||
private String checkEndTime;
|
||||
private String codeQualityMetric;
|
||||
private String engineType;
|
||||
private Object progress; // 如果有具体结构可替换为对应类型
|
||||
private String type;
|
||||
private String applicationId;
|
||||
private boolean whetherUseDetection;
|
||||
private String changeFilePath;
|
||||
}
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||
@Data
|
||||
public class SastLanguageResp {
|
||||
private String type;
|
||||
private List<SastLanguageResp> languageInfos;
|
||||
private List<SastLanguageInfoResp> languageInfos;
|
||||
@Data
|
||||
public static class SastLanguageInfoResp{
|
||||
private String id;
|
||||
|
@ -0,0 +1,12 @@
|
||||
package cd.casic.ci.process.engine.constant;
|
||||
|
||||
public class SastConstant {
|
||||
public static final String JOB_DONE = "DONE";
|
||||
public static final String REPORT_JSON = "reportJSON";
|
||||
public static final String REPORT_URL = "reportUrl";
|
||||
public static final String REPORT_FORMAT_JSON="JSON";
|
||||
public static final String REPORT_FORMAT_PDF="PDF";
|
||||
public static final String REPORT_MODE ="DETAILS";
|
||||
public static final String REPORT_MODE_DETAILS ="DETAILS";
|
||||
public static final String REPORT_MODE_SUMMARY ="SUMMARY";
|
||||
}
|
@ -3,8 +3,11 @@ package cd.casic.ci.process.engine.worker;
|
||||
|
||||
import cd.casic.ci.process.common.WorkAtom;
|
||||
import cd.casic.ci.process.dto.req.sast.SastApplicationCreateReq;
|
||||
import cd.casic.ci.process.dto.req.sast.SastReportCreateReq;
|
||||
import cd.casic.ci.process.dto.resp.sast.SastApplicationCreateResp;
|
||||
import cd.casic.ci.process.dto.resp.sast.SastApplicationEchoResp;
|
||||
import cd.casic.ci.process.dto.resp.sast.SastApplicationStatusResp;
|
||||
import cd.casic.ci.process.engine.constant.SastConstant;
|
||||
import cd.casic.ci.process.engine.runContext.TaskRunContext;
|
||||
import cd.casic.ci.process.engine.worker.base.BaseWorker;
|
||||
import cd.casic.ci.process.process.converter.SastConverter;
|
||||
@ -16,6 +19,14 @@ import com.alibaba.fastjson.JSON;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -50,6 +61,54 @@ public class SastWorker extends BaseWorker {
|
||||
SastApplicationCreateResp sastApplicationCreateResp = sastService.applicationCreate(sastApplicationCreateReq);
|
||||
log.info("SAST启动任务返回值:{}",JSON.toJSONString(sastApplicationCreateResp));
|
||||
append(context,"启动任务完毕");
|
||||
// 检测状态是否完毕
|
||||
SastApplicationStatusResp applicationStatus = sastService.getApplicationStatus(applicationId);
|
||||
while (!SastConstant.JOB_DONE.equals(applicationStatus.getJobStatus())) {
|
||||
try {
|
||||
Thread.sleep(1000L);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("SAST WORKER线程中断");
|
||||
}
|
||||
applicationStatus=sastService.getApplicationStatus(applicationId);
|
||||
append(context,"当前SAST运行中,运行状态:"+applicationStatus.getJobStatus());
|
||||
}
|
||||
String reportJSON = getReportJSON(applicationId, SastConstant.REPORT_MODE_DETAILS);
|
||||
String reportUrl = getReportUrl(applicationId, SastConstant.REPORT_MODE_DETAILS);
|
||||
task.getTaskProperties().put(SastConstant.REPORT_JSON,reportJSON);
|
||||
task.getTaskProperties().put(SastConstant.REPORT_URL,reportUrl);
|
||||
}
|
||||
}
|
||||
private String getReportJSON(String applicationId,String model){
|
||||
String reqJSON = "{\"mode\":\"DETAILS\",\"contacts\":\"admin@clouditera.com\",\"reportType\":\"APPLICATION\",\"format\":\"JSON\",\"projectId\":\"8a863857-bab3-40d9-a79a-2cb6617a9dd3\",\"applicationIds\":[\"9e3e55d6-6d28-4fd1-adc2-15b6cf0702db\"],\"bugLevels\":[\"DEADLY\",\"SEVERITY\",\"POOR_RISK\",\"LOW_RISK\"],\"audRiskLevels\":[\"WAITING\",\"IGNORE\",\"HIGH\",\"MEDIUM\",\"LOW\",\"NONE\"],\"applicationScope\":\"ALL\"}";
|
||||
SastReportCreateReq sastReportCreateReq = JSON.parseObject(reqJSON, SastReportCreateReq.class);
|
||||
sastReportCreateReq.setMode(model);
|
||||
sastReportCreateReq.setFormat(SastConstant.REPORT_FORMAT_JSON);
|
||||
sastReportCreateReq.setApplicationIds(Collections.singletonList(applicationId));
|
||||
String jsonReportUrl = sastService.reportIndex(sastReportCreateReq);
|
||||
return getJSONString(jsonReportUrl);
|
||||
}
|
||||
private String getReportUrl(String applicationId,String model){
|
||||
String reqJSON = "{\"mode\":\"DETAILS\",\"contacts\":\"admin@clouditera.com\",\"reportType\":\"APPLICATION\",\"format\":\"JSON\",\"projectId\":\"8a863857-bab3-40d9-a79a-2cb6617a9dd3\",\"applicationIds\":[\"9e3e55d6-6d28-4fd1-adc2-15b6cf0702db\"],\"bugLevels\":[\"DEADLY\",\"SEVERITY\",\"POOR_RISK\",\"LOW_RISK\"],\"audRiskLevels\":[\"WAITING\",\"IGNORE\",\"HIGH\",\"MEDIUM\",\"LOW\",\"NONE\"],\"applicationScope\":\"ALL\"}";
|
||||
SastReportCreateReq sastReportCreateReq = JSON.parseObject(reqJSON, SastReportCreateReq.class);
|
||||
sastReportCreateReq.setMode(model);
|
||||
sastReportCreateReq.setFormat(SastConstant.REPORT_FORMAT_PDF);
|
||||
sastReportCreateReq.setApplicationIds(Collections.singletonList(applicationId));
|
||||
return sastService.reportIndex(sastReportCreateReq);
|
||||
}
|
||||
private String getJSONString(String reportId){
|
||||
String s = sastService.reportDownload(reportId);
|
||||
URI uri = URI.create(s);
|
||||
URL url = null;
|
||||
try {
|
||||
url = uri.toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
log.error("获取json格式报告失败");
|
||||
}
|
||||
try (InputStream in = url.openStream()) {
|
||||
byte[] bytes = in.readAllBytes();
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
log.error("获取json格式报告失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,4 +29,6 @@ public interface SastService {
|
||||
public PageResult<SastReportResp> reportList(PageParam pageParam);
|
||||
|
||||
public void reportDelete(String reportId);
|
||||
|
||||
public SastApplicationStatusResp getApplicationStatus(String applicationId);
|
||||
}
|
||||
|
@ -232,6 +232,15 @@ public class SastServiceImpl implements SastService {
|
||||
System.out.println(uriString);
|
||||
ResponseEntity<Void> exchange = restTemplate.exchange(uriString, HttpMethod.DELETE,entity, Void.class,new HashMap<>());
|
||||
}
|
||||
@Override
|
||||
public SastApplicationStatusResp getApplicationStatus(String applicationId){
|
||||
HttpHeaders httpHeaders = getHeaders();
|
||||
Map<String, String> entityMap = new HashMap<>();
|
||||
HttpEntity<Map<String, String>> entity = new HttpEntity<>(entityMap,httpHeaders);
|
||||
String uriString=sastProperties.getBaseUrl()+getApplicationStatus+applicationId;
|
||||
ResponseEntity<SastApplicationStatusResp> exchange = restTemplate.exchange(uriString, HttpMethod.GET,entity, SastApplicationStatusResp.class,new HashMap<>());
|
||||
return exchange.getBody();
|
||||
}
|
||||
|
||||
private HttpHeaders getHeaders(){
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
|
@ -18,6 +18,13 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
@ -98,7 +105,7 @@ public class SastTest {
|
||||
|
||||
@Test
|
||||
public void getIdentify(){
|
||||
SastIdentifiedLanguageTaskResp resp = sastService.getIdentifiedLanguageTaskStatus("ab8c8ff1-7c07-4787-9107-6624ba0ede02");
|
||||
SastIdentifiedLanguageTaskResp resp = sastService.getIdentifiedLanguageTaskStatus("2c18c2f4-2c31-41d7-8199-e08bb7b36378");
|
||||
System.out.println(JSON.toJSONString(resp));
|
||||
}
|
||||
@Test
|
||||
@ -137,4 +144,17 @@ public class SastTest {
|
||||
public void reportDelete(){
|
||||
sastService.reportDelete("4af3380a-de08-476b-b1bc-b86f8d2e1339");
|
||||
}
|
||||
@Test
|
||||
public void reportDownload() throws MalformedURLException {
|
||||
String s = sastService.reportDownload("3300b586-52d7-4a46-b656-e653a0b2a024");
|
||||
URI uri = URI.create(s);
|
||||
URL url = uri.toURL();
|
||||
try (InputStream in = url.openStream()) {
|
||||
byte[] bytes = in.readAllBytes();
|
||||
String s1 = new String(bytes, StandardCharsets.UTF_8);
|
||||
System.out.println(s1);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user