diff --git a/src/backend/ci/core/common/common-api/src/main/kotlin/com/tencent/devops/common/api/check/Preconditions.kt b/src/backend/ci/core/common/common-api/src/main/kotlin/com/tencent/devops/common/api/check/Preconditions.kt index a35e11a61d3b..b452fd68916a 100644 --- a/src/backend/ci/core/common/common-api/src/main/kotlin/com/tencent/devops/common/api/check/Preconditions.kt +++ b/src/backend/ci/core/common/common-api/src/main/kotlin/com/tencent/devops/common/api/check/Preconditions.kt @@ -27,6 +27,8 @@ package com.tencent.devops.common.api.check +import com.tencent.devops.common.api.exception.ParamBlankException + /** * 前置条件校验工具类 */ @@ -36,10 +38,27 @@ object Preconditions { * 检查对象[obj]不为空,否则抛出指定的异常[exception] */ @Throws(Exception::class) - fun checkNotNull(obj: Any?, exception: Exception) { + fun checkNotNull(obj: T?, exception: () -> Exception): T { if (obj == null) { - throw exception + throw exception() } + return obj + } + + /** + * 检查对象[obj]不为空,抛出默认错误内容 + */ + @Throws(Exception::class) + fun checkNotNull(obj: T?): T { + return checkNotNull(obj) { ParamBlankException("Required value was null.") } + } + + /** + * 检查对象[obj]不为空, 为空则抛出[message]内容提醒上游 + */ + @Throws(Exception::class) + fun checkNotNull(obj: T?, message: String): T { + return checkNotNull(obj) { ParamBlankException(message) } } /** diff --git a/src/backend/ci/core/common/common-pipeline/src/main/kotlin/com/tencent/devops/common/pipeline/PipelineVersionWithModelRequest.kt b/src/backend/ci/core/common/common-pipeline/src/main/kotlin/com/tencent/devops/common/pipeline/PipelineVersionWithModelRequest.kt index 54049f3f171b..4c634c21308c 100644 --- a/src/backend/ci/core/common/common-pipeline/src/main/kotlin/com/tencent/devops/common/pipeline/PipelineVersionWithModelRequest.kt +++ b/src/backend/ci/core/common/common-pipeline/src/main/kotlin/com/tencent/devops/common/pipeline/PipelineVersionWithModelRequest.kt @@ -37,7 +37,7 @@ data class PipelineVersionWithModelRequest( @get:Schema(title = "草稿的来源版本(前端保存时传递)", required = true) val baseVersion: Int, @get:Schema(title = "流水线模型", required = true) - val modelAndSetting: PipelineModelAndSetting, + val modelAndSetting: PipelineModelAndSetting?, @get:Schema(title = "流水线YAML编排(不为空时以YAML为准)", required = false) val yaml: String?, @get:Schema(title = "存储格式", required = false) diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/vmbuild/EngineVMBuildService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/vmbuild/EngineVMBuildService.kt index 783445ad2992..845bae89dc43 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/vmbuild/EngineVMBuildService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/engine/service/vmbuild/EngineVMBuildService.kt @@ -110,12 +110,12 @@ import com.tencent.devops.process.utils.PIPELINE_VMSEQ_ID import com.tencent.devops.process.utils.PipelineVarUtil import com.tencent.devops.store.api.container.ServiceContainerAppResource import com.tencent.devops.store.pojo.app.BuildEnv -import org.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.stereotype.Service import java.time.LocalDateTime import java.util.concurrent.TimeUnit import javax.ws.rs.NotFoundException +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Service @Suppress( "LongMethod", @@ -193,7 +193,7 @@ class EngineVMBuildService @Autowired(required = false) constructor( ): BuildVariables { val buildInfo = pipelineRuntimeService.getBuildInfo(projectId, buildId) ?: throw NotFoundException("Fail to find build: buildId($buildId)") - Preconditions.checkNotNull(buildInfo, NotFoundException("Pipeline build ($buildId) is not exist")) + Preconditions.checkNotNull(buildInfo) { NotFoundException("Pipeline build ($buildId) is not exist") } LOG.info("ENGINE|$buildId|BUILD_VM_START|j($vmSeqId)|vmName($vmName)") // var表中获取环境变量,并对老版本变量进行兼容 val pipelineId = buildInfo.pipelineId @@ -204,7 +204,7 @@ class EngineVMBuildService @Autowired(required = false) constructor( val asCodeSettings = pipelineAsCodeService.getPipelineAsCodeSettings( projectId = projectId, pipelineId = buildInfo.pipelineId ) - Preconditions.checkNotNull(model, NotFoundException("Build Model ($buildId) is not exist")) + Preconditions.checkNotNull(model) { NotFoundException("Build Model ($buildId) is not exist") } model!!.stages.forEachIndexed { index, s -> if (index == 0) { diff --git a/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchBuildLessDockerStartupTaskAtom.kt b/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchBuildLessDockerStartupTaskAtom.kt index 6af7c45c5e1f..e8af3cfbdc99 100644 --- a/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchBuildLessDockerStartupTaskAtom.kt +++ b/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchBuildLessDockerStartupTaskAtom.kt @@ -146,28 +146,32 @@ class DispatchBuildLessDockerStartupTaskAtom @Autowired constructor( val vmSeqId = task.containerId val pipelineInfo = pipelineRepositoryService.getPipelineInfo(projectId, pipelineId) - Preconditions.checkNotNull(pipelineInfo, BuildTaskException( - errorType = ErrorType.SYSTEM, - errorCode = ERROR_PIPELINE_NOT_EXISTS.toInt(), - errorMsg = - I18nUtil.getCodeLanMessage(messageCode = ERROR_PIPELINE_NOT_EXISTS, params = arrayOf(pipelineId)), - pipelineId = pipelineId, - buildId = buildId, - taskId = taskId - )) + Preconditions.checkNotNull(pipelineInfo) { + BuildTaskException( + errorType = ErrorType.SYSTEM, + errorCode = ERROR_PIPELINE_NOT_EXISTS.toInt(), + errorMsg = + I18nUtil.getCodeLanMessage(messageCode = ERROR_PIPELINE_NOT_EXISTS, params = arrayOf(pipelineId)), + pipelineId = pipelineId, + buildId = buildId, + taskId = taskId + ) + } val container = containerBuildDetailService.getBuildModel(projectId, buildId)?.getContainer(vmSeqId) - Preconditions.checkNotNull(container, BuildTaskException( - errorType = ErrorType.SYSTEM, - errorCode = ERROR_PIPELINE_NODEL_CONTAINER_NOT_EXISTS.toInt(), - errorMsg = I18nUtil.getCodeLanMessage( - messageCode = ERROR_PIPELINE_NOT_EXISTS, - params = arrayOf(vmSeqId) - ), - pipelineId = pipelineId, - buildId = buildId, - taskId = taskId - )) + Preconditions.checkNotNull(container) { + BuildTaskException( + errorType = ErrorType.SYSTEM, + errorCode = ERROR_PIPELINE_NODEL_CONTAINER_NOT_EXISTS.toInt(), + errorMsg = I18nUtil.getCodeLanMessage( + messageCode = ERROR_PIPELINE_NOT_EXISTS, + params = arrayOf(vmSeqId) + ), + pipelineId = pipelineId, + buildId = buildId, + taskId = taskId + ) + } containerBuildRecordService.containerPreparing( projectId = projectId, diff --git a/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchVMStartupTaskAtom.kt b/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchVMStartupTaskAtom.kt index 3aedb903f5f3..af09a497935e 100644 --- a/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchVMStartupTaskAtom.kt +++ b/src/backend/ci/core/process/biz-engine/src/main/kotlin/com/tencent/devops/process/engine/atom/vm/DispatchVMStartupTaskAtom.kt @@ -67,15 +67,15 @@ import com.tencent.devops.process.engine.service.record.ContainerBuildRecordServ import com.tencent.devops.process.pojo.mq.PipelineAgentShutdownEvent import com.tencent.devops.process.pojo.mq.PipelineAgentStartupEvent import com.tencent.devops.process.service.PipelineContextService -import com.tencent.devops.process.utils.PIPELINE_DIALECT import com.tencent.devops.process.utils.BK_CI_AUTHORIZER +import com.tencent.devops.process.utils.PIPELINE_DIALECT import com.tencent.devops.store.api.container.ServiceContainerAppResource +import java.util.concurrent.TimeUnit import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.config.ConfigurableBeanFactory import org.springframework.context.annotation.Scope import org.springframework.stereotype.Component -import java.util.concurrent.TimeUnit /** * @@ -190,9 +190,8 @@ class DispatchVMStartupTaskAtom @Autowired constructor( val vmNames = param.vmNames.joinToString(",") val pipelineInfo = pipelineRepositoryService.getPipelineInfo(projectId, pipelineId) - Preconditions.checkNotNull( - obj = pipelineInfo, - exception = BuildTaskException( + Preconditions.checkNotNull(pipelineInfo) { + BuildTaskException( errorType = ErrorType.SYSTEM, errorCode = ERROR_PIPELINE_NOT_EXISTS.toInt(), errorMsg = MessageUtil.getMessageByLocale( @@ -203,12 +202,11 @@ class DispatchVMStartupTaskAtom @Autowired constructor( buildId = buildId, taskId = taskId ) - ) + } val container = containerBuildDetailService.getBuildModel(projectId, buildId)?.getContainer(vmSeqId) - Preconditions.checkNotNull( - obj = container, - exception = BuildTaskException( + Preconditions.checkNotNull(container) { + BuildTaskException( errorType = ErrorType.SYSTEM, errorCode = ERROR_PIPELINE_NODEL_CONTAINER_NOT_EXISTS.toInt(), errorMsg = MessageUtil.getMessageByLocale( @@ -220,7 +218,7 @@ class DispatchVMStartupTaskAtom @Autowired constructor( buildId = buildId, taskId = taskId ) - ) + } // 这个任务是在构建子流程启动的,所以必须使用根流程进程ID // 注意区分buildId和vmSeqId,BuildId是一次构建整体的ID, diff --git a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/ServicePipelineVersionResourceImpl.kt b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/ServicePipelineVersionResourceImpl.kt index 27193e34666b..a231830d3bf3 100644 --- a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/ServicePipelineVersionResourceImpl.kt +++ b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/ServicePipelineVersionResourceImpl.kt @@ -246,7 +246,7 @@ class ServicePipelineVersionResourceImpl @Autowired constructor( Audit( resourceType = AuthResourceType.PIPELINE_DEFAULT.value, resourceId = result.pipelineId, - resourceName = modelAndYaml.modelAndSetting.model.name, + resourceName = result.pipelineName, userId = userId, action = "edit", actionContent = "Save Ver.${result.version}", diff --git a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/UserPipelineVersionResourceImpl.kt b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/UserPipelineVersionResourceImpl.kt index 6689df2ffd2c..2faeba52118b 100644 --- a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/UserPipelineVersionResourceImpl.kt +++ b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/api/UserPipelineVersionResourceImpl.kt @@ -247,7 +247,7 @@ class UserPipelineVersionResourceImpl @Autowired constructor( Audit( resourceType = AuthResourceType.PIPELINE_DEFAULT.value, resourceId = result.pipelineId, - resourceName = modelAndYaml.modelAndSetting.model.name, + resourceName = result.pipelineName, userId = userId, action = "edit", actionContent = "Save Ver.${result.version}", diff --git a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/service/PipelineVersionFacadeService.kt b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/service/PipelineVersionFacadeService.kt index 8c44b6eff606..0c7426da0629 100644 --- a/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/service/PipelineVersionFacadeService.kt +++ b/src/backend/ci/core/process/biz-process/src/main/kotlin/com/tencent/devops/process/service/PipelineVersionFacadeService.kt @@ -27,6 +27,7 @@ package com.tencent.devops.process.service +import com.tencent.devops.common.api.check.Preconditions import com.tencent.devops.common.api.constant.CommonMessageCode import com.tencent.devops.common.api.exception.ErrorCodeException import com.tencent.devops.common.api.model.SQLLimit @@ -78,11 +79,11 @@ import com.tencent.devops.process.template.service.TemplateService import com.tencent.devops.process.utils.PipelineVersionUtils import com.tencent.devops.process.yaml.PipelineYamlFacadeService import com.tencent.devops.process.yaml.transfer.PipelineTransferException +import javax.ws.rs.core.Response import org.jooq.DSLContext import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service -import javax.ws.rs.core.Response @Suppress("ALL") @Service @@ -174,6 +175,7 @@ class PipelineVersionFacadeService @Autowired constructor( VersionStatus.COMMITTING -> { draftVersion?.version } + VersionStatus.BRANCH -> { val branchVersion = pipelineRepositoryService.getBranchVersionResource( projectId, pipelineId, null @@ -181,6 +183,7 @@ class PipelineVersionFacadeService @Autowired constructor( versionName = branchVersion?.versionName branchVersion?.version } + else -> { draftVersion?.version } @@ -541,7 +544,8 @@ class PipelineVersionFacadeService @Autowired constructor( id = "T-1-1-1", name = I18nUtil.getCodeLanMessage( CommonMessageCode.BK_MANUAL_TRIGGER, - language = I18nUtil.getLanguage(userId + language = I18nUtil.getLanguage( + userId ) ) ) @@ -647,12 +651,14 @@ class PipelineVersionFacadeService @Autowired constructor( ) Triple(true, response, null) } catch (e: PipelineTransferException) { - Triple(false, null, I18nUtil.getCodeLanMessage( - messageCode = e.errorCode, - params = e.params, - language = I18nUtil.getLanguage(I18nUtil.getRequestUserId()), - defaultMessage = e.defaultMessage - )) + Triple( + false, null, I18nUtil.getCodeLanMessage( + messageCode = e.errorCode, + params = e.params, + language = I18nUtil.getLanguage(I18nUtil.getRequestUserId()), + defaultMessage = e.defaultMessage + ) + ) } return PipelineVersionWithModel( modelAndSetting = modelAndSetting, @@ -739,16 +745,28 @@ class PipelineVersionFacadeService @Autowired constructor( logger.warn("TRANSFER_YAML|$projectId|$userId|${ignore.message}|modelAndYaml=\n${modelAndYaml.yaml}") newYaml = null } - model = modelAndYaml.modelAndSetting.model - setting = modelAndYaml.modelAndSetting.setting + model = Preconditions.checkNotNull( + modelAndYaml.modelAndSetting?.model, + "modelAndYaml.modelAndSetting.model must not be null" + ) + setting = Preconditions.checkNotNull( + modelAndYaml.modelAndSetting?.setting, + "modelAndYaml.modelAndSetting.setting must not be null" + ) } return if (pipelineId.isNullOrBlank()) { // 新建流水线产生草稿 pipelineInfoFacadeService.createPipeline( userId = userId, projectId = projectId, - model = model ?: modelAndYaml.modelAndSetting.model, - setting = setting ?: modelAndYaml.modelAndSetting.setting, + model = model ?: Preconditions.checkNotNull( + modelAndYaml.modelAndSetting?.model, + "The transfer data is incorrect, so the modelAndYaml.modelAndSetting.model must not be null" + ), + setting = setting ?: Preconditions.checkNotNull( + modelAndYaml.modelAndSetting?.setting, + "The transfer data is incorrect, so the modelAndYaml.modelAndSetting.setting must not be null" + ), channelCode = ChannelCode.BS, checkPermission = true, versionStatus = versionStatus, @@ -762,7 +780,10 @@ class PipelineVersionFacadeService @Autowired constructor( userId = userId, projectId = projectId, pipelineId = pipelineId, - setting = setting ?: modelAndYaml.modelAndSetting.setting, + setting = setting ?: Preconditions.checkNotNull( + modelAndYaml.modelAndSetting?.setting, + "The transfer data is incorrect, so the modelAndYaml.modelAndSetting.setting must not be null" + ), checkPermission = false, versionStatus = versionStatus, dispatchPipelineUpdateEvent = false, @@ -776,7 +797,10 @@ class PipelineVersionFacadeService @Autowired constructor( release?.model } else { model - } ?: modelAndYaml.modelAndSetting.model, + } ?: Preconditions.checkNotNull( + modelAndYaml.modelAndSetting?.model, + "The transfer data is incorrect, so the modelAndYaml.modelAndSetting.model must not be null" + ), channelCode = ChannelCode.BS, checkPermission = true, checkTemplate = false, diff --git a/src/backend/ci/core/worker/worker-common/src/main/kotlin/com/tencent/devops/worker/common/Runner.kt b/src/backend/ci/core/worker/worker-common/src/main/kotlin/com/tencent/devops/worker/common/Runner.kt index 9fa3abb45827..38cad025680f 100644 --- a/src/backend/ci/core/worker/worker-common/src/main/kotlin/com/tencent/devops/worker/common/Runner.kt +++ b/src/backend/ci/core/worker/worker-common/src/main/kotlin/com/tencent/devops/worker/common/Runner.kt @@ -67,11 +67,11 @@ import com.tencent.devops.worker.common.task.TaskFactory import com.tencent.devops.worker.common.utils.CredentialUtils import com.tencent.devops.worker.common.utils.KillBuildProcessTree import com.tencent.devops.worker.common.utils.ShellUtil -import org.slf4j.LoggerFactory import java.io.File import java.io.FileNotFoundException import java.io.IOException import kotlin.system.exitProcess +import org.slf4j.LoggerFactory object Runner { @@ -117,6 +117,7 @@ object Runner { messageCode = PARAMETER_ERROR, language = AgentEnv.getLocaleLanguage() ) + ":${ignore.message}" + is FileNotFoundException, is IOException -> { MessageUtil.getMessageByLocale( messageCode = RUN_AGENT_WITHOUT_PERMISSION, @@ -124,6 +125,7 @@ object Runner { params = arrayOf("${ignore.message}") ) } + else -> MessageUtil.getMessageByLocale( messageCode = UNKNOWN_ERROR, language = AgentEnv.getLocaleLanguage() @@ -237,10 +239,9 @@ object Runner { logger.info("Start to execute the task($buildTask)") when (buildTask.status) { BuildTaskStatus.DO -> { - Preconditions.checkNotNull( - obj = buildTask.taskId, - exception = RemoteServiceException("Not valid build elementId") - ) + Preconditions.checkNotNull(buildTask.taskId) { + RemoteServiceException("Not valid build elementId") + } // 处理task和job级别的上下文 combineVariables(buildTask, buildVariables) val task = TaskFactory.create(buildTask.type ?: "empty") @@ -274,6 +275,7 @@ object Runner { waitCount = 0 } } + BuildTaskStatus.WAIT -> { var sleepStep = waitCount++ / windows if (sleepStep <= 0) { @@ -283,6 +285,7 @@ object Runner { logger.info("WAIT $sleepMills ms") Thread.sleep(sleepMills) } + BuildTaskStatus.END -> break@loop } }