diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java index b5eb53ac4c..2ce942c37f 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -6,17 +6,24 @@ import io.metersphere.api.dto.APIReportResult; import io.metersphere.api.dto.ApiTestImportRequest; import io.metersphere.api.dto.automation.ApiScenarioRequest; import io.metersphere.api.dto.automation.ReferenceDTO; +import io.metersphere.api.dto.datacount.request.ScheduleInfoRequest; +import io.metersphere.api.dto.datacount.response.TaskInfoResult; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; +import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest; +import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult; +import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.base.domain.ApiDefinition; import io.metersphere.base.domain.Schedule; import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.json.JSONSchemaGenerator; +import io.metersphere.commons.utils.CronUtils; import io.metersphere.commons.utils.PageUtils; import io.metersphere.commons.utils.Pager; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.service.CheckPermissionService; +import io.metersphere.service.ScheduleService; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresRoles; @@ -24,6 +31,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import java.util.Date; import java.util.List; @@ -31,6 +39,8 @@ import java.util.List; @RequestMapping(value = "/api/definition") @RequiresRoles(value = {RoleConstants.TEST_MANAGER, RoleConstants.TEST_USER, RoleConstants.TEST_VIEWER}, logical = Logical.OR) public class ApiDefinitionController { + @Resource + private ScheduleService scheduleService; @Resource private ApiDefinitionService apiDefinitionService; @Resource @@ -139,7 +149,7 @@ public class ApiDefinitionController { public ApiDefinitionImport testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) { return apiDefinitionService.apiTestImport(file, request); } - + //定时任务创建 @PostMapping(value = "/schedule/create") public void createSchedule(@RequestBody Schedule request) { apiDefinitionService.createSchedule(request); @@ -148,7 +158,38 @@ public class ApiDefinitionController { public void updateSchedule(@RequestBody Schedule request){ apiDefinitionService.updateSchedule(request); } - + //查找定时任务资源Id + @PostMapping(value = "/getResourceId") + public String getResourceId(@RequestBody SwaggerUrlRequest swaggerUrlRequest){ + return apiDefinitionService.getResourceId(swaggerUrlRequest); + } + //查找定时任务列表 + @GetMapping("/scheduleTask/{projectId}") + public List getSwaggerScheduleList(@PathVariable String projectId) { + List resultList = apiDefinitionService.getSwaggerScheduleList(projectId); + int dataIndex = 1; + for (SwaggerTaskResult swaggerTaskResult : + resultList) { + swaggerTaskResult.setIndex(dataIndex++); + Date nextExecutionTime = CronUtils.getNextTriggerTime(swaggerTaskResult.getRule()); + if(nextExecutionTime!=null){ + swaggerTaskResult.setNextExecutionTime(nextExecutionTime.getTime()); + } + } + return resultList; + } + //更新定时任务 + @PostMapping(value = "/schedule/updateByPrimyKey") + public void updateScheduleEnableByPrimyKey(@RequestBody ScheduleInfoSwaggerUrlRequest request) { + Schedule schedule = scheduleService.getSchedule(request.getTaskId()); + schedule.setEnable(request.getTaskStatus()); + apiDefinitionService.updateSchedule(schedule); + } + //删除定时任务和swaggereUrl + @PostMapping("/schedule/deleteByPrimyKey") + public void deleteSchedule(@RequestBody ScheduleInfoSwaggerUrlRequest request) { + apiDefinitionService.deleteSchedule(request); + } @PostMapping("/getReference") public ReferenceDTO getReference(@RequestBody ApiScenarioRequest request) { return apiDefinitionService.getReference(request); diff --git a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java index 28980a6993..932c0ac674 100644 --- a/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/ApiTestImportRequest.java @@ -16,4 +16,9 @@ public class ApiTestImportRequest { // 来自场景的导入不需要存储 private boolean saved = true; private String swaggerUrl; + //导入策略 + private String modeId; + private String userId; + //调用类型 + private String type; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/ApiSwaggerUrlDTO.java b/backend/src/main/java/io/metersphere/api/dto/definition/ApiSwaggerUrlDTO.java new file mode 100644 index 0000000000..eea386de38 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/definition/ApiSwaggerUrlDTO.java @@ -0,0 +1,18 @@ +package io.metersphere.api.dto.definition; + +import io.metersphere.base.domain.Schedule; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ApiSwaggerUrlDTO extends Schedule { + //序号 + private int index; + private String id; + private String projectId; + private String swaggerUrl; + private String scheduleId; + //下次执行时间 + private Long nextExecutionTime; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ScheduleInfoSwaggerUrlRequest.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ScheduleInfoSwaggerUrlRequest.java new file mode 100644 index 0000000000..7ab32b1a0f --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ScheduleInfoSwaggerUrlRequest.java @@ -0,0 +1,12 @@ +package io.metersphere.api.dto.definition.request; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ScheduleInfoSwaggerUrlRequest { + private String taskId; + private Boolean taskStatus; + private String id; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/swaggerurl/SwaggerTaskResult.java b/backend/src/main/java/io/metersphere/api/dto/swaggerurl/SwaggerTaskResult.java new file mode 100644 index 0000000000..0601d0e587 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/swaggerurl/SwaggerTaskResult.java @@ -0,0 +1,27 @@ +package io.metersphere.api.dto.swaggerurl; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SwaggerTaskResult { + //序号 + private int index; + //定时任务号 + private String taskId; + //SwaggerUrlId + private String SwaggerUrlId; + //SwaggerUrl + private String swaggerUrl; + //导入模块 + private String modulePath; + //同步规则 + private String rule; + //下次同步时间 + private Long nextExecutionTime; + //同步开关 + private boolean taskStatus; + //定时任务类型 swaggerUrlType + private String taskType; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/swaggerurl/SwaggerUrlRequest.java b/backend/src/main/java/io/metersphere/api/dto/swaggerurl/SwaggerUrlRequest.java new file mode 100644 index 0000000000..3433d4c3c5 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/swaggerurl/SwaggerUrlRequest.java @@ -0,0 +1,12 @@ +package io.metersphere.api.dto.swaggerurl; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SwaggerUrlRequest { + private String projectId; + private String swaggerUrl; + private String moduleId; +} diff --git a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java index 27add6d374..7fec48540f 100644 --- a/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/ApiImportAbstractParser.java @@ -125,7 +125,7 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { return bodyType; } - protected ApiDefinitionResult buildApiDefinition(String id, String name, String path, String method) { + protected ApiDefinitionResult buildApiDefinition(String id, String name, String path, String method,ApiTestImportRequest importRequest) { ApiDefinitionResult apiDefinition = new ApiDefinitionResult(); apiDefinition.setName(name); apiDefinition.setPath(formatPath(path)); @@ -133,7 +133,11 @@ public abstract class ApiImportAbstractParser implements ApiImportParser { apiDefinition.setMethod(method); apiDefinition.setId(id); apiDefinition.setProjectId(this.projectId); - apiDefinition.setUserId(SessionUtils.getUserId()); + if(StringUtils.equalsIgnoreCase("schedule",importRequest.getType())){ + apiDefinition.setUserId(importRequest.getUserId()); + }else{ + apiDefinition.setUserId(SessionUtils.getUserId()); + } return apiDefinition; } diff --git a/backend/src/main/java/io/metersphere/api/parse/MsParser.java b/backend/src/main/java/io/metersphere/api/parse/MsParser.java index 9f700f6501..db9727b8e5 100644 --- a/backend/src/main/java/io/metersphere/api/parse/MsParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/MsParser.java @@ -76,7 +76,7 @@ public class MsParser extends ApiImportAbstractParser { String method = requestObject.getString("method"); MsHTTPSamplerProxy request = buildRequest(requestName, path, method); - ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), requestName, path, method); + ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), requestName, path, method,importRequest); apiDefinition.setModuleId(module.getId()); apiDefinition.setProjectId(this.projectId); parseBody(requestObject, request.getBody()); diff --git a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java index 13d7f9b9f0..e30127233c 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanParser.java @@ -60,7 +60,7 @@ public class PostmanParser extends ApiImportAbstractParser { PostmanUrl url = requestDesc.getUrl(); MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url.getRaw(), requestDesc.getMethod()); ApiDefinitionResult apiDefinition = - buildApiDefinition(request.getId(), requestItem.getName(), url.getRaw(), requestDesc.getMethod()); + buildApiDefinition(request.getId(), requestItem.getName(), url.getRaw(), requestDesc.getMethod(),new ApiTestImportRequest()); if (StringUtils.isNotBlank(request.getPath())) { String path = request.getPath().split("\\?")[0]; path = path.replace("{{", "${"); diff --git a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java index ad8a7ab7e0..80c18fce35 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java @@ -66,7 +66,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { for (HttpMethod method : httpMethods) { Operation operation = operationMap.get(method); MsHTTPSamplerProxy request = buildRequest(operation, pathName, method.name()); - ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method.name()); + ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method.name(),importRequest); parseParameters(operation, request); apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation, operation.getResponses()))); @@ -79,7 +79,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { return results; } - private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method) { + private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) { String name = ""; if (StringUtils.isNotBlank(operation.getSummary())) { name = operation.getSummary(); @@ -88,7 +88,7 @@ public class Swagger2Parser extends SwaggerAbstractParser { } else { name = path; } - return buildApiDefinition(id, name, path, method); + return buildApiDefinition(id, name, path, method,importRequest); } private MsHTTPSamplerProxy buildRequest(Operation operation, String path, String method) { diff --git a/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java index 0498ec4ec2..1fd4033d76 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger3Parser.java @@ -97,7 +97,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { Operation operation = operationsMap.get(method); if (operation != null) { MsHTTPSamplerProxy request = buildRequest(operation, pathName, method); - ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method); + ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method,importRequest); parseParameters(operation, request); parseRequestBody(operation.getRequestBody(), request.getBody()); apiDefinition.setRequest(JSON.toJSONString(request)); @@ -111,7 +111,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { return results; } - private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method) { + private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method,ApiTestImportRequest importRequest) { String name = ""; if (StringUtils.isNotBlank(operation.getSummary())) { name = operation.getSummary(); @@ -120,7 +120,7 @@ public class Swagger3Parser extends SwaggerAbstractParser { } else { name = path; } - return buildApiDefinition(id, name, path, method); + return buildApiDefinition(id, name, path, method,importRequest); } private MsHTTPSamplerProxy buildRequest(Operation operation, String path, String method) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index a69f77668b..4e836d4afb 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -9,19 +9,17 @@ import io.metersphere.api.dto.automation.ReferenceDTO; import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.definition.*; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; +import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest; import io.metersphere.api.dto.scenario.request.RequestType; +import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult; +import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.TestResult; import io.metersphere.api.parse.ApiImportParser; import io.metersphere.api.parse.ApiImportParserFactory; import io.metersphere.base.domain.*; -import io.metersphere.base.mapper.ApiDefinitionMapper; -import io.metersphere.base.mapper.ApiTestFileMapper; -import io.metersphere.base.mapper.ProjectMapper; -import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; -import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper; -import io.metersphere.base.mapper.ext.ExtApiScenarioMapper; -import io.metersphere.base.mapper.ext.ExtTestPlanMapper; +import io.metersphere.base.mapper.*; +import io.metersphere.base.mapper.ext.*; import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ScheduleGroup; @@ -79,6 +77,12 @@ public class ApiDefinitionService { private ProjectMapper projectMapper; @Resource private ScheduleService scheduleService; + @Resource + private SwaggerUrlProjectMapper swaggerUrlProjectMapper; + @Resource + private ExtSwaggerUrlScheduleMapper extSwaggerUrlScheduleMapper; + @Resource + private ScheduleMapper scheduleMapper; private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24); @@ -293,7 +297,7 @@ public class ApiDefinitionService { } } - private ApiDefinition importCreate(ApiDefinitionResult request, ApiDefinitionMapper batchMapper) { + private ApiDefinition importCreate(ApiDefinitionResult request, ApiDefinitionMapper batchMapper,ApiTestImportRequest apiTestImportRequest) { SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); BeanUtils.copyBean(saveReq, request); final ApiDefinitionWithBLOBs apiDefinition = new ApiDefinitionWithBLOBs(); @@ -309,13 +313,26 @@ public class ApiDefinitionService { apiDefinition.setDescription(request.getDescription()); List sameRequest = getSameRequest(saveReq); - if (CollectionUtils.isEmpty(sameRequest)) { + if(StringUtils.equals("fullCoverage",apiTestImportRequest.getModeId())){ + if (CollectionUtils.isEmpty(sameRequest)) { + batchMapper.insert(apiDefinition); + } else { + //如果存在则修改 + apiDefinition.setId(sameRequest.get(0).getId()); + apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); + } + }else if(StringUtils.equals("incrementalMerge",apiTestImportRequest.getModeId())){ batchMapper.insert(apiDefinition); - } else { - //如果存在则修改 - apiDefinition.setId(sameRequest.get(0).getId()); - apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); + }else{ + if (CollectionUtils.isEmpty(sameRequest)) { + batchMapper.insert(apiDefinition); + } else { + //如果存在则修改 + apiDefinition.setId(sameRequest.get(0).getId()); + apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); + } } + return apiDefinition; } @@ -434,7 +451,7 @@ public class ApiDefinitionService { item.setName(item.getName().substring(0, 255)); } item.setNum(num++); - importCreate(item, batchMapper); + importCreate(item, batchMapper,request); if (i % 300 == 0) { sqlSession.flushStatements(); } @@ -596,6 +613,16 @@ public class ApiDefinitionService { /*swagger定时导入*/ public void createSchedule(Schedule request) { + /*保存swaggerUrl*/ + SwaggerUrlProject swaggerUrlProject=new SwaggerUrlProject(); + swaggerUrlProject.setId(UUID.randomUUID().toString()); + swaggerUrlProject.setProjectId(request.getProjectId()); + swaggerUrlProject.setSwaggerUrl(request.getResourceId()); + swaggerUrlProject.setModuleId(request.getModuleId()); + swaggerUrlProject.setModulePath(request.getModulePath()); + swaggerUrlProject.setModeId(request.getModeId()); + scheduleService.addSwaggerUrlSchedule(swaggerUrlProject); + request.setResourceId(swaggerUrlProject.getId()); Schedule schedule = scheduleService.buildApiTestSchedule(request); schedule.setJob(SwaggerUrlImportJob.class.getName()); schedule.setGroup(ScheduleGroup.SWAGGER_IMPORT.name()); @@ -604,12 +631,37 @@ public class ApiDefinitionService { this.addOrUpdateSwaggerImportCronJob(request); } + //关闭 public void updateSchedule(Schedule request){ scheduleService.editSchedule(request); this.addOrUpdateSwaggerImportCronJob(request); } + //删除 + public void deleteSchedule(ScheduleInfoSwaggerUrlRequest request){ + swaggerUrlProjectMapper.deleteByPrimaryKey(request.getId()); + scheduleMapper.deleteByPrimaryKey(request.getTaskId()); + + } + //查询swaggerUrl详情 + public SwaggerUrlProject getSwaggerInfo(String resourceId){ + return swaggerUrlProjectMapper.selectByPrimaryKey(resourceId); + } + public String getResourceId(SwaggerUrlRequest swaggerUrlRequest){ + SwaggerUrlProjectExample swaggerUrlProjectExample=new SwaggerUrlProjectExample(); + SwaggerUrlProjectExample.Criteria criteria=swaggerUrlProjectExample.createCriteria(); + criteria.andProjectIdEqualTo(swaggerUrlRequest.getProjectId()).andSwaggerUrlEqualTo(swaggerUrlRequest.getSwaggerUrl()).andModuleIdEqualTo(swaggerUrlRequest.getModuleId()); + List list=swaggerUrlProjectMapper.selectByExample(swaggerUrlProjectExample); + String resourceId=""; + if(list.size()==1){ + resourceId=list.get(0).getId(); + } + return resourceId; + } + public List getSwaggerScheduleList(String projectId){ + return extSwaggerUrlScheduleMapper.getSwaggerTaskList(projectId); + } private void addOrUpdateSwaggerImportCronJob(Schedule request) { scheduleService.addOrUpdateCronJob(request, SwaggerUrlImportJob.getJobKey(request.getResourceId()), SwaggerUrlImportJob.getTriggerKey(request.getResourceId()), SwaggerUrlImportJob.class); } -} \ No newline at end of file +} diff --git a/backend/src/main/java/io/metersphere/base/domain/AuthSource.java b/backend/src/main/java/io/metersphere/base/domain/AuthSource.java index 97575da1f2..0d24e19a5c 100644 --- a/backend/src/main/java/io/metersphere/base/domain/AuthSource.java +++ b/backend/src/main/java/io/metersphere/base/domain/AuthSource.java @@ -1,8 +1,7 @@ package io.metersphere.base.domain; -import lombok.Data; - import java.io.Serializable; +import lombok.Data; @Data public class AuthSource implements Serializable { diff --git a/backend/src/main/java/io/metersphere/base/domain/Schedule.java b/backend/src/main/java/io/metersphere/base/domain/Schedule.java index 0dcf9e8966..8033b6f759 100644 --- a/backend/src/main/java/io/metersphere/base/domain/Schedule.java +++ b/backend/src/main/java/io/metersphere/base/domain/Schedule.java @@ -37,5 +37,11 @@ public class Schedule implements Serializable { //定时任务来源: 测试计划/测试场景 private String scheduleFrom; - private String swaggerUrl; + private String projectId; + + private String moduleId; + + private String modulePath; + + private String modeId; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/AuthSourceMapper.java b/backend/src/main/java/io/metersphere/base/mapper/AuthSourceMapper.java index f00eec594a..3cdb4beb0b 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/AuthSourceMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/AuthSourceMapper.java @@ -2,9 +2,8 @@ package io.metersphere.base.mapper; import io.metersphere.base.domain.AuthSource; import io.metersphere.base.domain.AuthSourceExample; -import org.apache.ibatis.annotations.Param; - import java.util.List; +import org.apache.ibatis.annotations.Param; public interface AuthSourceMapper { long countByExample(AuthSourceExample example); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java index e4576036f1..7c0156407f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.java @@ -4,12 +4,14 @@ import io.metersphere.api.dto.datacount.ApiDataCountResult; import io.metersphere.api.dto.definition.ApiComputeResult; import io.metersphere.api.dto.definition.ApiDefinitionRequest; import io.metersphere.api.dto.definition.ApiDefinitionResult; +import io.metersphere.api.dto.definition.ApiSwaggerUrlDTO; import io.metersphere.base.domain.ApiDefinition; import org.apache.ibatis.annotations.Param; import java.util.List; public interface ExtApiDefinitionMapper { + List selectScheduleList(@Param("projectId") String projectId); List list(@Param("request") ApiDefinitionRequest request); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml index 6751e067fd..cc3ead40b4 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml @@ -421,4 +421,19 @@ + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.java index fc94d7b4fe..236a0ec067 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.java @@ -1,6 +1,7 @@ package io.metersphere.base.mapper.ext; import io.metersphere.api.dto.datacount.response.TaskInfoResult; +import io.metersphere.api.dto.definition.ApiSwaggerUrlDTO; import io.metersphere.controller.request.QueryScheduleRequest; import io.metersphere.dto.ScheduleDao; import org.apache.ibatis.annotations.Param; @@ -15,4 +16,9 @@ public interface ExtScheduleMapper { long countTaskByProjectIdAndCreateTimeRange(@Param("projectId")String projectId, @Param("startTime") long startTime, @Param("endTime") long endTime); List findRunningTaskInfoByProjectID(String workspaceID); + + void insert(@Param("apiSwaggerUrlDTO") ApiSwaggerUrlDTO apiSwaggerUrlDTO); + + ApiSwaggerUrlDTO select(String id); + } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.xml index f17df6bc52..b7e236df7d 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtScheduleMapper.xml @@ -1,6 +1,12 @@ + + insert into swagger_url_project (id, `project_id`, `swagger_url`, + `schedule_id`) + values (#{apiSwaggerUrlDTO.id,jdbcType=VARCHAR}, #{apiSwaggerUrlDTO.projectId,jdbcType=VARCHAR}, #{apiSwaggerUrlDTO.swaggerUrl,jdbcType=VARCHAR}, + #{apiSwaggerUrlDTO.scheduleId,jdbcType=VARCHAR}) + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.java new file mode 100644 index 0000000000..98f26fc8ca --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.java @@ -0,0 +1,11 @@ +package io.metersphere.base.mapper.ext; + +import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult; +import org.springframework.data.repository.query.Param; + +import java.util.List; + +public interface ExtSwaggerUrlScheduleMapper { + //接口列表定时导入 + List getSwaggerTaskList(@Param("projectId") String projectId); +} diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml new file mode 100644 index 0000000000..652f4a4808 --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtSwaggerUrlScheduleMapper.xml @@ -0,0 +1,16 @@ + + + + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/job/sechedule/ScheduleManager.java b/backend/src/main/java/io/metersphere/job/sechedule/ScheduleManager.java index d0575bb36a..7c64968203 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/ScheduleManager.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/ScheduleManager.java @@ -290,12 +290,11 @@ public class ScheduleManager { addOrUpdateCronJob(jobKey, triggerKey, jobClass, cron, null); } - public JobDataMap getDefaultJobDataMap(String resourceId, String expression, String userId,String swaggerUrl) { + public JobDataMap getDefaultJobDataMap(String resourceId, String expression, String userId) { JobDataMap jobDataMap = new JobDataMap(); jobDataMap.put("resourceId", resourceId); jobDataMap.put("expression", expression); jobDataMap.put("userId", userId); - jobDataMap.put("swaggerUrl",swaggerUrl); return jobDataMap; } diff --git a/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java b/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java index 0e517e8843..ba8ab0c3ea 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/SwaggerUrlImportJob.java @@ -1,9 +1,12 @@ package io.metersphere.job.sechedule; import io.metersphere.api.dto.ApiTestImportRequest; +import io.metersphere.api.dto.definition.ApiSwaggerUrlDTO; import io.metersphere.api.service.ApiDefinitionService; +import io.metersphere.base.domain.SwaggerUrlProject; import io.metersphere.commons.constants.ScheduleGroup; import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.service.ScheduleService; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobKey; @@ -20,12 +23,15 @@ public class SwaggerUrlImportJob extends MsScheduleJob { void businessExecute(JobExecutionContext context) { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); String resourceId = jobDataMap.getString("resourceId"); - String swaggerUrl = jobDataMap.getString("swaggerUrl"); + SwaggerUrlProject swaggerUrlProject=apiDefinitionService.getSwaggerInfo(resourceId); ApiTestImportRequest request = new ApiTestImportRequest(); - request.setProjectId(resourceId); - request.setSwaggerUrl(swaggerUrl); + request.setProjectId(swaggerUrlProject.getProjectId()); + request.setSwaggerUrl(swaggerUrlProject.getSwaggerUrl()); + request.setModuleId(swaggerUrlProject.getModuleId()); request.setPlatform("Swagger2"); request.setSaved(true); + request.setUserId(jobDataMap.getString("userId")); + request.setType("schedule"); apiDefinitionService.apiTestImport(null, request); } diff --git a/backend/src/main/java/io/metersphere/service/ScheduleService.java b/backend/src/main/java/io/metersphere/service/ScheduleService.java index 2da372b26f..d7d002c4e4 100644 --- a/backend/src/main/java/io/metersphere/service/ScheduleService.java +++ b/backend/src/main/java/io/metersphere/service/ScheduleService.java @@ -2,11 +2,10 @@ package io.metersphere.service; import com.alibaba.fastjson.JSON; import io.metersphere.api.dto.datacount.response.TaskInfoResult; -import io.metersphere.base.domain.Schedule; -import io.metersphere.base.domain.ScheduleExample; -import io.metersphere.base.domain.User; -import io.metersphere.base.domain.UserExample; +import io.metersphere.api.dto.definition.ApiSwaggerUrlDTO; +import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ScheduleMapper; +import io.metersphere.base.mapper.SwaggerUrlProjectMapper; import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.ext.ExtScheduleMapper; import io.metersphere.commons.constants.ScheduleGroup; @@ -49,6 +48,8 @@ public class ScheduleService { private ExtScheduleMapper extScheduleMapper; @Resource private UserMapper userMapper; + @Resource + private SwaggerUrlProjectMapper swaggerUrlProjectMapper; public void addSchedule(Schedule schedule) { schedule.setId(UUID.randomUUID().toString()); @@ -57,6 +58,12 @@ public class ScheduleService { schedule.setUpdateTime(System.currentTimeMillis()); scheduleMapper.insert(schedule); } + public void addSwaggerUrlSchedule(SwaggerUrlProject swaggerUrlProject) { + swaggerUrlProjectMapper.insert(swaggerUrlProject); + } + public ApiSwaggerUrlDTO selectApiSwaggerUrlDTO(String id){ + return extScheduleMapper.select(id); + } public Schedule getSchedule(String ScheduleId) { return scheduleMapper.selectByPrimaryKey(ScheduleId); @@ -111,7 +118,7 @@ public class ScheduleService { LogUtil.error("初始化任务:" + JSON.toJSONString(schedule)); scheduleManager.addOrUpdateCronJob(new JobKey(schedule.getKey(), schedule.getGroup()), new TriggerKey(schedule.getKey(), schedule.getGroup()), Class.forName(schedule.getJob()), schedule.getValue(), - scheduleManager.getDefaultJobDataMap(schedule.getResourceId(), schedule.getValue(), schedule.getUserId(),schedule.getSwaggerUrl())); + scheduleManager.getDefaultJobDataMap(schedule.getResourceId(), schedule.getValue(), schedule.getUserId())); } } catch (Exception e) { LogUtil.error("初始化任务失败", e); @@ -140,7 +147,7 @@ public class ScheduleService { if (enable != null && enable && StringUtils.isNotBlank(cronExpression)) { try { scheduleManager.addOrUpdateCronJob(jobKey, triggerKey, clazz, cronExpression, - scheduleManager.getDefaultJobDataMap(request.getResourceId(), cronExpression, SessionUtils.getUser().getId(),request.getSwaggerUrl())); + scheduleManager.getDefaultJobDataMap(request.getResourceId(), cronExpression, SessionUtils.getUser().getId())); } catch (SchedulerException e) { LogUtil.error(e.getMessage(), e); MSException.throwException("定时任务开启异常"); diff --git a/backend/src/main/resources/db/migration/V69__swagger_url_project.sql b/backend/src/main/resources/db/migration/V69__swagger_url_project.sql new file mode 100644 index 0000000000..db6cb0d5c4 --- /dev/null +++ b/backend/src/main/resources/db/migration/V69__swagger_url_project.sql @@ -0,0 +1,12 @@ +create table swagger_url_project +( + id varchar(255) not null, + project_id varchar(255) null, + swagger_url varchar(255) null, + module_id varchar(255) null, + module_path varchar(255) null, + mode_id varchar(255) null, + primary key (id) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4; + diff --git a/backend/src/main/resources/generatorConfig.xml b/backend/src/main/resources/generatorConfig.xml index ee85158201..afacb48f72 100644 --- a/backend/src/main/resources/generatorConfig.xml +++ b/backend/src/main/resources/generatorConfig.xml @@ -65,6 +65,7 @@ +
diff --git a/frontend/src/business/components/api/definition/components/import/ApiImport.vue b/frontend/src/business/components/api/definition/components/import/ApiImport.vue index 0c264eecfa..655fbdde6f 100644 --- a/frontend/src/business/components/api/definition/components/import/ApiImport.vue +++ b/frontend/src/business/components/api/definition/components/import/ApiImport.vue @@ -20,7 +20,11 @@ - + + + + + @@ -33,10 +37,27 @@ + + + + + + + + - + + + + {{ $t('api_test.api_import.file_size_limit') }} - - - - - - - - - - - - - - - -
@@ -84,7 +84,7 @@ {{ $t('api_test.api_import.export_tip') }}:{{ selectedPlatform.exportTip }}
- + @@ -180,11 +180,13 @@ export default { }, methods: { scheduleEdit() { - if (this.swaggerSynchronization) { - this.$refs.scheduleEdit.open(this.buildParam()); + if (!this.formData.swaggerUrl) { + this.$warning(this.$t('commons.please_upload')); + this.swaggerSynchronization = !this.swaggerSynchronization } else { - this.result = this.$post("/api/definition/schedule/update", this.schedule, response => { - }); + if (this.swaggerSynchronization) { + this.$refs.scheduleEdit.open(this.buildParam()); + } } }, open(module) { @@ -238,9 +240,15 @@ export default { Object.assign(param, this.formData); param.platform = this.selectedPlatformValue; param.saved = this.saved; + console.log(this.formData.moduleId) if (this.currentModule) { - param.moduleId = this.currentModule.id; - param.modulePath = this.currentModule.path; + param.moduleId = this.formData.moduleId + this.moduleOptions.filter(item => { + if (item.id === this.formData.moduleId) { + param.modulePath = item.path + } + }) + param.modeId = this.formData.modeId } param.projectId = getCurrentProjectID(); if (!this.swaggerUrlEable) { @@ -326,4 +334,8 @@ export default { margin-left: 80px; } +.el-divider { + height: 200px; +} + diff --git a/frontend/src/business/components/api/definition/components/import/ImportScheduleEdit.vue b/frontend/src/business/components/api/definition/components/import/ImportScheduleEdit.vue index e8a74cb502..df5887ec4e 100644 --- a/frontend/src/business/components/api/definition/components/import/ImportScheduleEdit.vue +++ b/frontend/src/business/components/api/definition/components/import/ImportScheduleEdit.vue @@ -28,6 +28,9 @@ ref="crontab"/> + + + @@ -35,10 +38,17 @@ + + +