Skip to content

Commit

Permalink
Merge pull request #11357 from yongyiduan/issue_11304_b
Browse files Browse the repository at this point in the history
feat:导出流水线功能优化 #11304
  • Loading branch information
bkci-bot authored Jan 3, 2025
2 parents 5285dff + d143d9a commit 468f836
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

package com.tencent.devops.common.api.check

import com.tencent.devops.common.api.exception.ParamBlankException

/**
* 前置条件校验工具类
*/
Expand All @@ -36,10 +38,27 @@ object Preconditions {
* 检查对象[obj]不为空,否则抛出指定的异常[exception]
*/
@Throws(Exception::class)
fun checkNotNull(obj: Any?, exception: Exception) {
fun <T : Any> checkNotNull(obj: T?, exception: () -> Exception): T {
if (obj == null) {
throw exception
throw exception()
}
return obj
}

/**
* 检查对象[obj]不为空,抛出默认错误内容
*/
@Throws(Exception::class)
fun <T : Any> checkNotNull(obj: T?): T {
return checkNotNull(obj) { ParamBlankException("Required value was null.") }
}

/**
* 检查对象[obj]不为空, 为空则抛出[message]内容提醒上游
*/
@Throws(Exception::class)
fun <T : Any> checkNotNull(obj: T?, message: String): T {
return checkNotNull(obj) { ParamBlankException(message) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
*
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -220,7 +218,7 @@ class DispatchVMStartupTaskAtom @Autowired constructor(
buildId = buildId,
taskId = taskId
)
)
}

// 这个任务是在构建子流程启动的,所以必须使用根流程进程ID
// 注意区分buildId和vmSeqId,BuildId是一次构建整体的ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -174,13 +175,15 @@ class PipelineVersionFacadeService @Autowired constructor(
VersionStatus.COMMITTING -> {
draftVersion?.version
}

VersionStatus.BRANCH -> {
val branchVersion = pipelineRepositoryService.getBranchVersionResource(
projectId, pipelineId, null
)
versionName = branchVersion?.versionName
branchVersion?.version
}

else -> {
draftVersion?.version
}
Expand Down Expand Up @@ -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
)
)
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -117,13 +117,15 @@ object Runner {
messageCode = PARAMETER_ERROR,
language = AgentEnv.getLocaleLanguage()
) + "${ignore.message}"

is FileNotFoundException, is IOException -> {
MessageUtil.getMessageByLocale(
messageCode = RUN_AGENT_WITHOUT_PERMISSION,
language = AgentEnv.getLocaleLanguage(),
params = arrayOf("${ignore.message}")
)
}

else -> MessageUtil.getMessageByLocale(
messageCode = UNKNOWN_ERROR,
language = AgentEnv.getLocaleLanguage()
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -274,6 +275,7 @@ object Runner {
waitCount = 0
}
}

BuildTaskStatus.WAIT -> {
var sleepStep = waitCount++ / windows
if (sleepStep <= 0) {
Expand All @@ -283,6 +285,7 @@ object Runner {
logger.info("WAIT $sleepMills ms")
Thread.sleep(sleepMills)
}

BuildTaskStatus.END -> break@loop
}
}
Expand Down

0 comments on commit 468f836

Please sign in to comment.