From 52367891ab1c51eb3d5ac3d8c378812c9710158a Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Fri, 13 Oct 2023 11:48:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E8=84=9A=E6=9C=AC=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=AE=A1=E7=90=86=E7=B1=BBAPI=20#2318?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决创建脚本版本 API 报错的问题 --- .../api/inner/ServiceScriptResource.java | 4 +- .../api/web/WebPublicScriptResource.java | 15 ++-- .../job/manage/api/web/WebScriptResource.java | 14 ++-- .../web/ScriptVersionCreateUpdateReq.java | 69 +++++++++++++++++++ ...ateUpdateReq.java => ScriptCreateReq.java} | 57 +++++++-------- .../manage/api/common/ScriptDTOBuilder.java | 26 +++++-- .../inner/impl/ServiceScriptResourceImpl.java | 8 +-- .../web/impl/WebPublicScriptResourceImpl.java | 55 ++++++++++----- .../api/web/impl/WebScriptResourceImpl.java | 54 ++++++++++----- .../service/impl/ScriptManagerImpl.java | 32 --------- 10 files changed, 216 insertions(+), 118 deletions(-) create mode 100644 src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java rename src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/{ScriptCreateUpdateReq.java => ScriptCreateReq.java} (60%) diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java index f082f11b72..c9cc2456c7 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/ServiceScriptResource.java @@ -27,7 +27,7 @@ import com.tencent.bk.job.common.annotation.InternalAPI; import com.tencent.bk.job.common.model.InternalResponse; import com.tencent.bk.job.manage.model.inner.ServiceScriptDTO; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tentent.bk.job.common.api.feign.annotation.SmartFeignClient; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -66,7 +66,7 @@ InternalResponse> createScriptWithVersionId( @ApiParam("脚本状态") @RequestHeader(value = "X-Script-Status", required = false) Integer scriptStatus, @ApiParam(value = "业务ID", required = true, example = "2") @PathVariable("appId") Long appId, @ApiParam(value = "新增/更新的脚本对象", name = "scriptCreateUpdateReq", - required = true) @RequestBody ScriptCreateUpdateReq scriptCreateUpdateReq); + required = true) @RequestBody ScriptCreateReq scriptCreateReq); @ApiOperation(value = "获取脚本基本信息", produces = "application/json") @GetMapping("/service/script/{scriptId}") diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java index 7c20b62190..c425ad8288 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java @@ -27,7 +27,10 @@ import com.tencent.bk.job.common.annotation.WebAPI; import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.model.Response; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.common.validation.Create; +import com.tencent.bk.job.common.validation.Update; +import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; @@ -41,6 +44,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -193,7 +197,8 @@ Response saveScript( String username, @ApiParam(value = "新增/更新的脚本对象", name = "request", required = true) @RequestBody - ScriptCreateUpdateReq request + @Validated + ScriptCreateReq request ); @ApiOperation(value = "新增脚本版本", produces = "application/json") @@ -206,7 +211,8 @@ Response saveScriptVersion( String scriptId, @ApiParam(value = "新增脚本版本请求", name = "request", required = true) @RequestBody - ScriptCreateUpdateReq request + @Validated(Create.class) + ScriptVersionCreateUpdateReq request ); @ApiOperation(value = "更新脚本版本", produces = "application/json") @@ -221,7 +227,8 @@ Response updateScriptVersion( Long scriptVersionId, @ApiParam(value = "更新脚本版本请求", name = "request", required = true) @RequestBody - ScriptCreateUpdateReq request + @Validated(Update.class) + ScriptVersionCreateUpdateReq request ); @ApiOperation(value = "上线脚本", produces = "application/json") diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java index 8f27a1aeb3..c19ec5723c 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java @@ -28,8 +28,11 @@ import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.model.Response; import com.tencent.bk.job.common.model.dto.AppResourceScope; +import com.tencent.bk.job.common.validation.Create; +import com.tencent.bk.job.common.validation.Update; +import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCheckReq; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; @@ -44,6 +47,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -285,7 +289,7 @@ Response saveScript( String scopeId, @ApiParam(value = "新增/更新的脚本对象", name = "request", required = true) @RequestBody - ScriptCreateUpdateReq request + ScriptCreateReq request ); @ApiOperation(value = "新增脚本版本", produces = "application/json") @@ -307,7 +311,8 @@ Response saveScriptVersion( String scriptId, @ApiParam(value = "新增脚本版本请求", name = "request", required = true) @RequestBody - ScriptCreateUpdateReq request + @Validated(Create.class) + ScriptVersionCreateUpdateReq request ); @ApiOperation(value = "更新脚本版本", produces = "application/json") @@ -331,7 +336,8 @@ Response updateScriptVersion( Long scriptVersionId, @ApiParam(value = "更新脚本版本请求", name = "request", required = true) @RequestBody - ScriptCreateUpdateReq request + @Validated(Update.class) + ScriptVersionCreateUpdateReq request ); @ApiOperation(value = "上线脚本", produces = "application/json") diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java new file mode 100644 index 0000000000..ff904e9a9b --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java @@ -0,0 +1,69 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.model.web; + +import com.tencent.bk.job.common.validation.Create; +import com.tencent.bk.job.common.validation.Update; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.Pattern; + +/** + * 脚本版本新增、更新请求 + */ +@Data +@ApiModel("脚本版本新增、更新请求报文") +public class ScriptVersionCreateUpdateReq { + + /** + * 脚本内容 + */ + @ApiModelProperty(value = "脚本内容,新增脚本/脚本版本时需要传入,BASE64编码") + @NotEmpty(message = "{validation.constraints.ScriptContent_empty.message}", groups = {Create.class, Update.class}) + private String content; + + /** + * 脚本的版本号 + */ + @ApiModelProperty(value = "版本号,新增脚本版本时需要传入") + @NotEmpty(message = "{validation.constraints.ScriptVersion_empty.message}", groups = Create.class) + @Length(max = 60, message = "{validation.constraints.ScriptVersion_outOfLength.message}", groups = Create.class) + @Pattern(regexp = "^[A-Za-z0-9_\\-#@.]+$", message = "{validation.constraints.ScriptVersion_illegal.message}", + groups = Create.class) + private String version; + + /** + * 脚本版本描述 + */ + @ApiModelProperty(value = "脚本版本描述") + private String versionDesc; + +} + + diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptCreateUpdateReq.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptCreateReq.java similarity index 60% rename from src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptCreateUpdateReq.java rename to src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptCreateReq.java index a9091b3f25..de81b99574 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptCreateUpdateReq.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptCreateReq.java @@ -24,58 +24,60 @@ package com.tencent.bk.job.manage.model.web.request; +import com.tencent.bk.job.common.validation.CheckEnum; +import com.tencent.bk.job.manage.common.consts.script.ScriptTypeEnum; import com.tencent.bk.job.manage.model.web.vo.TagVO; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import org.hibernate.validator.constraints.Length; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; import java.util.List; /** - * 脚本新增、更新请求 + * 脚本新增请求 */ @Data -@ApiModel("脚本新增、更新请求报文") -public class ScriptCreateUpdateReq { - /** - * 脚本版本ID,对应某个版本的脚本的ID - */ - @Deprecated - @ApiModelProperty(value = "脚本版本ID,对应某个版本的脚本的ID", hidden = true) - private Long scriptVersionId; - /** - * 脚本ID - */ - @Deprecated - @ApiModelProperty(value = "脚本ID", hidden = true) - private String id; +@ApiModel("脚本新增请求") +public class ScriptCreateReq { + /** * 脚本名称 */ @ApiModelProperty(value = "脚本名称", required = true, example = "scriptName") + @NotEmpty(message = "{validation.constraints.ScriptName_empty.message}") + @Length(max = 60, message = "{validation.constraints.ScriptName_outOfLength.message}") + @Pattern(regexp = "^[^\\\\|/:*<>\"?]+$", message = "{validation.constraints.ScriptName_illegal.message}") private String name; + /** * 脚本类型 */ - @ApiModelProperty(value = "脚本类型,新增脚本/脚本版本时需要传入") + @ApiModelProperty(value = "脚本类型,创建脚本时需要传入") + @NotNull(message = "{validation.constraints.ScriptType_empty.message}") + @CheckEnum(enumClass = ScriptTypeEnum.class, enumMethod = "isValid", + message = "{validation.constraints.ScriptType_illegal.message}") private Integer type; + /** * 脚本内容 */ - @ApiModelProperty(value = "脚本内容,新增脚本/脚本版本时需要传入,BASE64编码") + @ApiModelProperty(value = "脚本内容,创建脚本时需要传入,BASE64编码") + @NotEmpty(message = "{validation.constraints.ScriptContent_empty.message}") private String content; - /** - * 创建者 - */ - @ApiModelProperty(value = "创建者,新增脚本/脚本版本时需要传入") - private String creator; - /** * 脚本的版本号 */ - @ApiModelProperty(value = "版本号,新增脚本/脚本版本时需要传入") + @ApiModelProperty(value = "版本号,新增脚本时需要传入") + @NotEmpty(message = "{validation.constraints.ScriptVersion_empty.message}") + @Length(max = 60, message = "{validation.constraints.ScriptVersion_outOfLength.message}") + @Pattern(regexp = "^[A-Za-z0-9_\\-#@.]+$", message = "{validation.constraints.ScriptVersion_illegal.message}") private String version; + /** * 脚本标签 */ @@ -88,13 +90,6 @@ public class ScriptCreateUpdateReq { @ApiModelProperty(value = "脚本描述") private String description; - /** - * 脚本版本描述 - */ - @ApiModelProperty(value = "脚本版本描述") - private String versionDesc; - - } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java index f71eb99892..957ce3a38e 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java @@ -35,7 +35,8 @@ import com.tencent.bk.job.manage.model.esb.v3.request.EsbCreateScriptVersionV3Req; import com.tencent.bk.job.manage.model.esb.v3.request.EsbUpdatePublicScriptVersionV3Req; import com.tencent.bk.job.manage.model.esb.v3.request.EsbUpdateScriptVersionV3Req; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.vo.TagVO; import org.springframework.stereotype.Service; @@ -45,10 +46,14 @@ @Service public class ScriptDTOBuilder { - public ScriptDTO buildFromCreateUpdateReq(ScriptCreateUpdateReq req) { + /** + * ScriptCreateReq -> ScriptDTO 转换 + * + * @param req 请求 + * @return 转换后的脚本 + */ + public ScriptDTO buildFromScriptCreateReq(ScriptCreateReq req) { ScriptDTO scriptDTO = new ScriptDTO(); - scriptDTO.setId(req.getId()); - scriptDTO.setScriptVersionId(req.getScriptVersionId()); scriptDTO.setName(req.getName()); scriptDTO.setType(req.getType()); if (req.getType() != null) { @@ -71,6 +76,19 @@ public ScriptDTO buildFromCreateUpdateReq(ScriptCreateUpdateReq req) { } scriptDTO.setVersion(req.getVersion()); scriptDTO.setDescription(req.getDescription()); + return scriptDTO; + } + + /** + * ScriptVersionCreateUpdateReq -> ScriptDTO 转换 + * + * @param req 请求 + * @return 转换后的脚本 + */ + public ScriptDTO buildFromScriptVersionCreateUpdateReq(ScriptVersionCreateUpdateReq req) { + ScriptDTO scriptDTO = new ScriptDTO(); + scriptDTO.setContent(Base64Util.decodeContentToStr(req.getContent())); + scriptDTO.setVersion(req.getVersion()); scriptDTO.setVersionDesc(req.getVersionDesc()); return scriptDTO; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java index de72d694c9..3f4290c50c 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/inner/impl/ServiceScriptResourceImpl.java @@ -35,7 +35,7 @@ import com.tencent.bk.job.manage.model.dto.ScriptDTO; import com.tencent.bk.job.manage.model.dto.converter.ScriptConverter; import com.tencent.bk.job.manage.model.inner.ServiceScriptDTO; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.service.ScriptManager; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -103,12 +103,12 @@ public InternalResponse getScriptByScriptVersionId(Long script public InternalResponse> createScriptWithVersionId(String username, Long createTime, Long lastModifyTime, String lastModifyUser, Integer scriptStatus, Long appId, - ScriptCreateUpdateReq scriptCreateUpdateReq) { + ScriptCreateReq scriptCreateReq) { if (log.isDebugEnabled()) { log.debug("createScriptWithVersionId,operator={},appId={},script={},status={}", username, appId, - scriptCreateUpdateReq, scriptStatus); + scriptCreateReq, scriptStatus); } - ScriptDTO script = scriptDTOBuilder.buildFromCreateUpdateReq(scriptCreateUpdateReq); + ScriptDTO script = scriptDTOBuilder.buildFromScriptCreateReq(scriptCreateReq); script.setAppId(appId); script.setCreator(username); if (StringUtils.isNotBlank(lastModifyUser)) { diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java index 244f592937..202e64ddd1 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java @@ -25,6 +25,7 @@ package com.tencent.bk.job.manage.api.web.impl; import com.tencent.bk.audit.annotations.AuditEntry; +import com.tencent.bk.audit.annotations.AuditRequestBody; import com.tencent.bk.job.common.constant.ErrorCode; import com.tencent.bk.job.common.constant.JobResourceTypeEnum; import com.tencent.bk.job.common.exception.InvalidParamException; @@ -45,7 +46,8 @@ import com.tencent.bk.job.manage.model.dto.TemplateStepIDDTO; import com.tencent.bk.job.manage.model.dto.converter.ScriptConverter; import com.tencent.bk.job.manage.model.query.ScriptQuery; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; @@ -192,7 +194,7 @@ private void processPermissionForList(String username, @AuditEntry(actionId = ActionId.MANAGE_PUBLIC_SCRIPT_INSTANCE) public Response updateScriptInfo(String username, String scriptId, - ScriptInfoUpdateReq request) { + @AuditRequestBody ScriptInfoUpdateReq request) { String updateField = request.getUpdateField(); boolean isUpdateDesc = "scriptDesc".equals(updateField); boolean isUpdateName = "scriptName".equals(updateField); @@ -278,21 +280,20 @@ public Response> listScriptVersion(String username, @Override @AuditEntry(actionId = ActionId.CREATE_PUBLIC_SCRIPT) public Response saveScript(String username, - ScriptCreateUpdateReq request) { + @AuditRequestBody ScriptCreateReq request) { - ScriptDTO script = buildCreateOrUpdateScript(request, username); - script.setCreator(username); + ScriptDTO script = buildCreateScriptDTO(request, username); ScriptDTO savedScript = publicScriptService.saveScript(username, script); ScriptVO scriptVO = ScriptConverter.convertToScriptVO(savedScript); return Response.buildSuccessResp(scriptVO); } - - private ScriptDTO buildCreateOrUpdateScript(ScriptCreateUpdateReq request, - String username) { - ScriptDTO script = scriptDTOBuilder.buildFromCreateUpdateReq(request); + private ScriptDTO buildCreateScriptDTO(ScriptCreateReq request, + String username) { + ScriptDTO script = scriptDTOBuilder.buildFromScriptCreateReq(request); script.setAppId(PUBLIC_APP_ID); script.setPublicScript(true); + script.setCreator(username); script.setLastModifyUser(username); return script; } @@ -301,25 +302,41 @@ private ScriptDTO buildCreateOrUpdateScript(ScriptCreateUpdateReq request, @AuditEntry(actionId = ActionId.MANAGE_PUBLIC_SCRIPT_INSTANCE) public Response saveScriptVersion(String username, String scriptId, - ScriptCreateUpdateReq request) { - ScriptDTO script = buildCreateOrUpdateScript(request, username); - script.setId(scriptId); - script.setCreator(username); + @AuditRequestBody ScriptVersionCreateUpdateReq request) { + ScriptDTO script = buildCreateOrUpdateScriptVersion(true, request, scriptId, + null, username); ScriptDTO savedScript = publicScriptService.saveScriptVersion(username, script); ScriptVO scriptVO = ScriptConverter.convertToScriptVO(savedScript); return Response.buildSuccessResp(scriptVO); } + private ScriptDTO buildCreateOrUpdateScriptVersion(boolean isCreate, + ScriptVersionCreateUpdateReq request, + String scriptId, + Long scriptVersionId, + String username) { + ScriptDTO script = scriptDTOBuilder.buildFromScriptVersionCreateUpdateReq(request); + script.setId(scriptId); + script.setAppId(PUBLIC_APP_ID); + script.setPublicScript(true); + script.setLastModifyUser(username); + if (isCreate) { + script.setCreator(username); + } else { + script.setScriptVersionId(scriptVersionId); + } + return script; + } + @Override @AuditEntry(actionId = ActionId.MANAGE_PUBLIC_SCRIPT_INSTANCE) public Response updateScriptVersion(String username, String scriptId, Long scriptVersionId, - ScriptCreateUpdateReq request) { - ScriptDTO script = buildCreateOrUpdateScript(request, username); - script.setId(scriptId); - script.setScriptVersionId(scriptVersionId); + @AuditRequestBody ScriptVersionCreateUpdateReq request) { + ScriptDTO script = buildCreateOrUpdateScriptVersion(false, request, scriptId, + scriptVersionId, username); ScriptDTO savedScriptVersion = publicScriptService.updateScriptVersion(username, script); ScriptVO scriptVO = ScriptConverter.convertToScriptVO(savedScriptVersion); @@ -399,7 +416,7 @@ public Response> listScriptSyncTemplateSteps(S public Response> syncScripts(String username, String scriptId, Long scriptVersionId, - ScriptSyncReq scriptSyncReq) { + @AuditRequestBody ScriptSyncReq scriptSyncReq) { List templateStepIDs = new ArrayList<>(scriptSyncReq.getSteps().size()); scriptSyncReq.getSteps().forEach(step -> templateStepIDs.add(new TemplateStepIDDTO(step.getTemplateId(), step.getStepId()))); @@ -430,7 +447,7 @@ public Response getPublicScriptCiteCount(String username, @Override @AuditEntry(actionId = ActionId.MANAGE_PUBLIC_SCRIPT_INSTANCE) public Response batchUpdatePublicScriptTags(String username, - ScriptTagBatchPatchReq req) { + @AuditRequestBody ScriptTagBatchPatchReq req) { // 校验 req.validate(); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java index 3de2bdce3a..87e3e2dd0e 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java @@ -54,8 +54,9 @@ import com.tencent.bk.job.manage.model.dto.TemplateStepIDDTO; import com.tencent.bk.job.manage.model.dto.converter.ScriptConverter; import com.tencent.bk.job.manage.model.query.ScriptQuery; +import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCheckReq; -import com.tencent.bk.job.manage.model.web.request.ScriptCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; @@ -76,6 +77,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -348,10 +350,9 @@ public Response saveScript(String username, AppResourceScope appResourceScope, String scopeType, String scopeId, - @AuditRequestBody ScriptCreateUpdateReq request) { + @AuditRequestBody @Validated ScriptCreateReq request) { - ScriptDTO script = buildCreateOrUpdateScript(request, appResourceScope, username); - script.setCreator(username); + ScriptDTO script = buildCreateScriptDTO(request, appResourceScope, username); ScriptDTO savedScript = scriptService.createScript(username, script); ScriptVO scriptVO = ScriptConverter.convertToScriptVO(savedScript); @@ -359,12 +360,13 @@ public Response saveScript(String username, } - private ScriptDTO buildCreateOrUpdateScript(ScriptCreateUpdateReq request, - AppResourceScope appResourceScope, - String username) { - ScriptDTO script = scriptDTOBuilder.buildFromCreateUpdateReq(request); + private ScriptDTO buildCreateScriptDTO(ScriptCreateReq request, + AppResourceScope appResourceScope, + String username) { + ScriptDTO script = scriptDTOBuilder.buildFromScriptCreateReq(request); script.setAppId(appResourceScope.getAppId()); script.setPublicScript(false); + script.setCreator(username); script.setLastModifyUser(username); return script; } @@ -376,30 +378,46 @@ public Response saveScriptVersion(String username, String scopeType, String scopeId, String scriptId, - @AuditRequestBody ScriptCreateUpdateReq request) { + @AuditRequestBody ScriptVersionCreateUpdateReq request) { - ScriptDTO script = buildCreateOrUpdateScript(request, appResourceScope, username); - script.setId(scriptId); - script.setCreator(username); + ScriptDTO script = buildCreateOrUpdateScriptVersion(true, request, scriptId, null, + appResourceScope, username); ScriptDTO savedScript = scriptService.createScriptVersion(username, script); ScriptVO scriptVO = ScriptConverter.convertToScriptVO(savedScript); return Response.buildSuccessResp(scriptVO); } + private ScriptDTO buildCreateOrUpdateScriptVersion(boolean isCreate, + ScriptVersionCreateUpdateReq request, + String scriptId, + Long scriptVersionId, + AppResourceScope appResourceScope, + String username) { + ScriptDTO script = scriptDTOBuilder.buildFromScriptVersionCreateUpdateReq(request); + script.setId(scriptId); + script.setAppId(appResourceScope.getAppId()); + script.setPublicScript(false); + script.setLastModifyUser(username); + if (isCreate) { + script.setCreator(username); + } else { + script.setScriptVersionId(scriptVersionId); + } + return script; + } + @Override @AuditEntry(actionId = ActionId.MANAGE_SCRIPT) public Response updateScriptVersion(String username, AppResourceScope appResourceScope, - String scopeType, - String scopeId, + String scopeType, String scopeId, String scriptId, Long scriptVersionId, - @AuditRequestBody ScriptCreateUpdateReq request) { + @AuditRequestBody ScriptVersionCreateUpdateReq request) { - ScriptDTO script = buildCreateOrUpdateScript(request, appResourceScope, username); - script.setId(scriptId); - script.setScriptVersionId(scriptVersionId); + ScriptDTO script = buildCreateOrUpdateScriptVersion(false, request, scriptId, scriptVersionId, + appResourceScope, username); ScriptDTO savedScriptVersion = scriptService.updateScriptVersion(username, script); ScriptVO scriptVO = ScriptConverter.convertToScriptVO(savedScriptVersion); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java index a3fc54f196..21cf66fbb3 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/ScriptManagerImpl.java @@ -38,13 +38,6 @@ import com.tencent.bk.job.common.model.PageData; import com.tencent.bk.job.common.model.dto.AppResourceScope; import com.tencent.bk.job.common.util.JobUUID; -import com.tencent.bk.job.common.util.check.IlegalCharChecker; -import com.tencent.bk.job.common.util.check.MaxLengthChecker; -import com.tencent.bk.job.common.util.check.NotEmptyChecker; -import com.tencent.bk.job.common.util.check.StringCheckHelper; -import com.tencent.bk.job.common.util.check.TrimChecker; -import com.tencent.bk.job.common.util.check.WhiteCharChecker; -import com.tencent.bk.job.common.util.check.exception.StringCheckException; import com.tencent.bk.job.common.util.date.DateUtils; import com.tencent.bk.job.common.util.json.JsonUtils; import com.tencent.bk.job.manage.auth.TemplateAuthService; @@ -292,7 +285,6 @@ public ScriptDTO createScript(ScriptDTO script) { log.info("Begin to create script: {}", script); long appId = script.getAppId(); - checkScript(script); boolean isNameDuplicate = scriptDAO.isExistDuplicateName(appId, script.getName()); if (isNameDuplicate) { log.warn("The script name:{} is exist for app:{}", script.getName(), appId); @@ -313,26 +305,6 @@ public ScriptDTO createScript(ScriptDTO script) { return getScriptVersion(scriptVersionId); } - private void checkScript(ScriptDTO script) { - try { - StringCheckHelper scriptNameCheckHelper = new StringCheckHelper(new TrimChecker(), new NotEmptyChecker(), - new IlegalCharChecker(), new MaxLengthChecker(60)); - script.setName(scriptNameCheckHelper.checkAndGetResult(script.getName())); - } catch (StringCheckException e) { - log.warn("Script name [{}] is invalid", script.getName()); - throw new InvalidParamException(ErrorCode.SCRIPT_NAME_INVALID); - } - try { - StringCheckHelper scriptVersionCheckHelper = new StringCheckHelper(new TrimChecker(), new NotEmptyChecker(), - new WhiteCharChecker("A-Za-z0-9_\\-#@\\."), new MaxLengthChecker(60)); - script - .setVersion(scriptVersionCheckHelper.checkAndGetResult(script.getVersion())); - } catch (StringCheckException e) { - log.warn("Script version [{}] is invalid", script.getVersion()); - throw new InvalidParamException(ErrorCode.SCRIPT_VERSION_ILLEGAL); - } - } - private void saveScriptTags(Long appId, ScriptDTO script) { saveScriptTags(script.getLastModifyUser(), appId, script.getId(), script.getTags()); } @@ -362,8 +334,6 @@ private Long getTimeOrDefault(Long time) { public ScriptDTO createScriptVersion(ScriptDTO scriptVersion) { log.info("Begin to save scriptVersion: {}", scriptVersion); - // 检查 - checkScript(scriptVersion); if (scriptDAO.isExistDuplicateVersion(scriptVersion.getId(), scriptVersion.getVersion())) { log.warn("Script version:{} is exist, scriptId:{}", scriptVersion.getVersion(), scriptVersion.getId()); throw new AlreadyExistsException(ErrorCode.SCRIPT_VERSION_NAME_EXIST); @@ -383,8 +353,6 @@ public ScriptDTO createScriptVersion(ScriptDTO scriptVersion) { public ScriptDTO updateScriptVersion(ScriptDTO scriptVersion) { log.info("Begin to update scriptVersion: {}", scriptVersion); - // 检查 - checkScript(scriptVersion); ScriptDTO scriptVersionToBeUpdate = scriptDAO.getScriptVersionById(scriptVersion.getScriptVersionId()); if (scriptVersionToBeUpdate == null) { throw new NotFoundException(ErrorCode.SCRIPT_NOT_EXIST); From b17add35a53406e1d4ce9f26851826d6d2914e75 Mon Sep 17 00:00:00 2001 From: wangyu096 Date: Fri, 13 Oct 2023 12:26:54 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E8=84=9A=E6=9C=AC=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E7=AE=A1=E7=90=86=E7=B1=BBAPI=20#2318?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决创建脚本版本 API 报错的问题 --- .../tencent/bk/job/manage/api/web/WebPublicScriptResource.java | 2 +- .../com/tencent/bk/job/manage/api/web/WebScriptResource.java | 2 +- .../model/web/{ => request}/ScriptVersionCreateUpdateReq.java | 2 +- .../com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java | 2 +- .../bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java | 2 +- .../bk/job/manage/api/web/impl/WebScriptResourceImpl.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) rename src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/{ => request}/ScriptVersionCreateUpdateReq.java (98%) diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java index c425ad8288..16974c18da 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebPublicScriptResource.java @@ -29,11 +29,11 @@ import com.tencent.bk.job.common.model.Response; import com.tencent.bk.job.common.validation.Create; import com.tencent.bk.job.common.validation.Update; -import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; +import com.tencent.bk.job.manage.model.web.request.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.vo.BasicScriptVO; import com.tencent.bk.job.manage.model.web.vo.ScriptVO; import com.tencent.bk.job.manage.model.web.vo.TagCountVO; diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java index c19ec5723c..c8070b9d7c 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebScriptResource.java @@ -30,12 +30,12 @@ import com.tencent.bk.job.common.model.dto.AppResourceScope; import com.tencent.bk.job.common.validation.Create; import com.tencent.bk.job.common.validation.Update; -import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCheckReq; import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; +import com.tencent.bk.job.manage.model.web.request.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.vo.BasicScriptVO; import com.tencent.bk.job.manage.model.web.vo.ScriptCheckResultItemVO; import com.tencent.bk.job.manage.model.web.vo.ScriptVO; diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptVersionCreateUpdateReq.java similarity index 98% rename from src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java rename to src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptVersionCreateUpdateReq.java index ff904e9a9b..80abdebf31 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/ScriptVersionCreateUpdateReq.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/request/ScriptVersionCreateUpdateReq.java @@ -22,7 +22,7 @@ * IN THE SOFTWARE. */ -package com.tencent.bk.job.manage.model.web; +package com.tencent.bk.job.manage.model.web.request; import com.tencent.bk.job.common.validation.Create; import com.tencent.bk.job.common.validation.Update; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java index 957ce3a38e..ab7f909892 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/common/ScriptDTOBuilder.java @@ -35,8 +35,8 @@ import com.tencent.bk.job.manage.model.esb.v3.request.EsbCreateScriptVersionV3Req; import com.tencent.bk.job.manage.model.esb.v3.request.EsbUpdatePublicScriptVersionV3Req; import com.tencent.bk.job.manage.model.esb.v3.request.EsbUpdateScriptVersionV3Req; -import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; +import com.tencent.bk.job.manage.model.web.request.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.vo.TagVO; import org.springframework.stereotype.Service; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java index 202e64ddd1..c31872c7a1 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebPublicScriptResourceImpl.java @@ -46,11 +46,11 @@ import com.tencent.bk.job.manage.model.dto.TemplateStepIDDTO; import com.tencent.bk.job.manage.model.dto.converter.ScriptConverter; import com.tencent.bk.job.manage.model.query.ScriptQuery; -import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; +import com.tencent.bk.job.manage.model.web.request.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.vo.BasicScriptVO; import com.tencent.bk.job.manage.model.web.vo.ScriptVO; import com.tencent.bk.job.manage.model.web.vo.TagCountVO; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java index 87e3e2dd0e..67be814682 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebScriptResourceImpl.java @@ -54,12 +54,12 @@ import com.tencent.bk.job.manage.model.dto.TemplateStepIDDTO; import com.tencent.bk.job.manage.model.dto.converter.ScriptConverter; import com.tencent.bk.job.manage.model.query.ScriptQuery; -import com.tencent.bk.job.manage.model.web.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptCheckReq; import com.tencent.bk.job.manage.model.web.request.ScriptCreateReq; import com.tencent.bk.job.manage.model.web.request.ScriptInfoUpdateReq; import com.tencent.bk.job.manage.model.web.request.ScriptSyncReq; import com.tencent.bk.job.manage.model.web.request.ScriptTagBatchPatchReq; +import com.tencent.bk.job.manage.model.web.request.ScriptVersionCreateUpdateReq; import com.tencent.bk.job.manage.model.web.vo.BasicScriptVO; import com.tencent.bk.job.manage.model.web.vo.ScriptCheckResultItemVO; import com.tencent.bk.job.manage.model.web.vo.ScriptVO;