Skip to content

Commit

Permalink
feat:推荐版本号优化 TencentBlueKing#10958 推荐版本号在编排中维护初始值,在db中维护当前值
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed Sep 19, 2024
1 parent 6a7594d commit 30453cb
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,15 @@ class VariableTransfer {

fun makeRecommendedVersion(model: Model): RecommendedVersion? {
val triggerContainer = model.stages[0].containers[0] as TriggerContainer
val res = if (triggerContainer.buildNo != null) {
with(triggerContainer.buildNo) {
RecommendedVersion(
enabled = true, allowModifyAtStartup = this!!.required, buildNo = RecommendedVersion.BuildNo(
this.buildNo,
RecommendedVersion.Strategy.parse(this.buildNoType).alis
)
val res = triggerContainer.buildNo?.let {
RecommendedVersion(
enabled = true,
allowModifyAtStartup = it.required,
buildNo = RecommendedVersion.BuildNo(
it.buildNo, RecommendedVersion.Strategy.parse(it.buildNoType).alis
)
}
} else return null
)
} ?: return null

(model.stages[0].containers[0] as TriggerContainer).params.forEach {
if (it.id == MAJORVERSION || it.id == "MajorVersion") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
package com.tencent.devops.common.pipeline.pojo

data class BuildNo(
var buildNo: Int,
var buildNo: Int, // 用于保存编排中定义的构建号初始值
val buildNoType: BuildNoType,
var required: Boolean? = false
var required: Boolean? = false,
var currentBuildNo: Int? = null // 用于替换当前的最新值,如果是创建流水线则不传值
)

enum class BuildNoType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* 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.devops.common.pipeline.pojo

import io.swagger.v3.oas.annotations.media.Schema

@Schema(title = "流水线构建推荐版本号更新")
data class BuildNoUpdateReq(
@get:Schema(title = "推荐版本号buildNo当前值", required = true)
val currentBuildNo: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.tencent.devops.common.api.pojo.Page
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.pipeline.PipelineVersionWithModel
import com.tencent.devops.common.pipeline.PipelineVersionWithModelRequest
import com.tencent.devops.common.pipeline.pojo.BuildNoUpdateReq
import com.tencent.devops.common.pipeline.pojo.TemplateInstanceCreateRequest
import com.tencent.devops.common.pipeline.pojo.transfer.PreviewResponse
import com.tencent.devops.process.pojo.PipelineDetail
Expand Down Expand Up @@ -334,4 +335,21 @@ interface UserPipelineVersionResource {
@QueryParam("storageType")
storageType: String?
): Response

@Operation(summary = "重置流水线推荐版本号")
@POST
@Path("/projects/{projectId}/pipelines/{pipelineId}/updateBuildNo")
fun updateBuildNo(
@Parameter(description = "用户ID", required = true, example = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@Parameter(description = "项目ID", required = true)
@PathParam("projectId")
projectId: String,
@Parameter(description = "流水线ID", required = true)
@PathParam("pipelineId")
pipelineId: String,
@Parameter(description = "流水线构建推荐版本号更新", required = true)
buildNo: BuildNoUpdateReq
): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum class OperationLogType(val description: String) {
ADD_PIPELINE_TO_GROUP("将流水线添加到流水线组时:「添加到流水线组 a」"),
MOVE_PIPELINE_OUT_OF_GROUP("将流水线移出流水线组时:「从流水线组 a 中移出」"),
UPDATE_PIPELINE_SETTING("修改流水线设置:「修改配置新增版本 P2.T1.2」"),
RESET_RECOMMENDED_VERSION_BUILD_NO("重置流水线推荐版本号:「重置流水线推荐版本号为 xxx」"),
NORMAL_SAVE_OPERATION("普通保存操作");

fun getI18n(language: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ class PipelineBuildSummaryDao {
fun resetDebugInfo(
dslContext: DSLContext,
projectId: String,
pipelineId: String
pipelineId: String,
debugBuildNo: Int
) {
with(T_PIPELINE_BUILD_SUMMARY) {
dslContext.update(this)
.set(DEBUG_BUILD_NO, 0)
.set(DEBUG_BUILD_NO, debugBuildNo)
.set(DEBUG_BUILD_NUM, 0)
.where(PIPELINE_ID.eq(pipelineId).and(PROJECT_ID.eq(projectId))).execute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ class PipelineBuildDetailService @Autowired constructor(
}

val triggerContainer = model.stages[0].containers[0] as TriggerContainer
val buildNo = triggerContainer.buildNo
if (buildNo != null) {
buildNo.buildNo = buildSummaryRecord?.buildNo ?: buildNo.buildNo
triggerContainer.buildNo?.apply {
currentBuildNo = buildSummaryRecord?.buildNo ?: buildNo
}
val params = triggerContainer.params
val newParams = ArrayList<BuildFormProperty>(params.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,11 @@ class PipelineRepositoryService constructor(
channelCode = channelCode,
yamlInfo = yamlInfo
)

val buildNo = (model.stages[0].containers[0] as TriggerContainer).buildNo
val triggerContainer = model.stages[0].containers[0] as TriggerContainer
val buildNo = triggerContainer.buildNo?.apply {
// #10958 每次存储model都需要忽略当前的推荐版本号值,在返回前端时重查
currentBuildNo = null
}
var canManualStartup = false
var canElementSkip = false
run lit@{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@ class PipelineBuildRecordService @Autowired constructor(
}
watcher.start("fixModel")
val triggerContainer = model.stages[0].containers[0] as TriggerContainer
triggerContainer.buildNo?.let {
it.buildNo = if (buildInfo.debug) {
triggerContainer.buildNo?.apply {
currentBuildNo = if (buildInfo.debug) {
buildSummaryRecord?.debugBuildNo
} else {
buildSummaryRecord?.buildNo
} ?: it.buildNo
} ?: buildNo
}
val params = triggerContainer.params
val newParams = ArrayList<BuildFormProperty>(params.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.tencent.devops.common.auth.api.AuthResourceType
import com.tencent.devops.common.pipeline.PipelineVersionWithModel
import com.tencent.devops.common.pipeline.PipelineVersionWithModelRequest
import com.tencent.devops.common.pipeline.enums.PipelineStorageType
import com.tencent.devops.common.pipeline.pojo.BuildNoUpdateReq
import com.tencent.devops.common.pipeline.pojo.TemplateInstanceCreateRequest
import com.tencent.devops.common.pipeline.pojo.transfer.PreviewResponse
import com.tencent.devops.common.web.RestResource
Expand Down Expand Up @@ -486,6 +487,21 @@ class UserPipelineVersionResourceImpl @Autowired constructor(
)
}

override fun updateBuildNo(
userId: String,
projectId: String,
pipelineId: String,
buildNo: BuildNoUpdateReq
): Result<Boolean> {
pipelineInfoFacadeService.updateBuildNo(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildNo = buildNo
)
return Result(true)
}

private fun checkParam(userId: String, projectId: String) {
if (userId.isBlank()) {
throw ParamBlankException("Invalid userId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class MQPipelineUpdateListener @Autowired constructor(
override fun run(event: PipelineUpdateEvent) {
val watcher = Watcher(id = "${event.traceId}|UpdatePipeline#${event.pipelineId}|${event.userId}")

if (event.buildNo != null) {
event.buildNo?.apply {
watcher.safeAround("updateBuildNo") {
pipelineRuntimeService.updateBuildNo(
projectId = event.projectId, pipelineId = event.pipelineId,
buildNo = event.buildNo!!.buildNo, debug = false
buildNo = currentBuildNo ?: buildNo, debug = false
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import com.tencent.devops.common.pipeline.enums.VersionStatus
import com.tencent.devops.common.pipeline.extend.ModelCheckPlugin
import com.tencent.devops.common.pipeline.pojo.BuildFormProperty
import com.tencent.devops.common.pipeline.pojo.BuildNo
import com.tencent.devops.common.pipeline.pojo.BuildNoUpdateReq
import com.tencent.devops.common.pipeline.pojo.PipelineModelAndSetting
import com.tencent.devops.common.pipeline.pojo.element.atom.BeforeDeleteParam
import com.tencent.devops.common.pipeline.pojo.setting.PipelineSetting
Expand All @@ -78,6 +79,7 @@ import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_NO_PERMISSIO
import com.tencent.devops.process.constant.ProcessMessageCode.ILLEGAL_PIPELINE_MODEL_JSON
import com.tencent.devops.process.constant.ProcessMessageCode.USER_NEED_PIPELINE_X_PERMISSION
import com.tencent.devops.process.engine.compatibility.BuildPropertyCompatibilityTools
import com.tencent.devops.process.engine.dao.PipelineBuildSummaryDao
import com.tencent.devops.process.engine.dao.PipelineInfoDao
import com.tencent.devops.process.engine.dao.template.TemplateDao
import com.tencent.devops.process.engine.pojo.PipelineInfo
Expand Down Expand Up @@ -135,6 +137,7 @@ class PipelineInfoFacadeService @Autowired constructor(
private val processJmxApi: ProcessJmxApi,
private val client: Client,
private val pipelineInfoDao: PipelineInfoDao,
private val pipelineBuildSummaryDao: PipelineBuildSummaryDao,
private val transferService: PipelineTransferYamlService,
private val yamlFacadeService: PipelineYamlFacadeService,
private val operationLogService: PipelineOperationLogService,
Expand Down Expand Up @@ -1188,6 +1191,30 @@ class PipelineInfoFacadeService @Autowired constructor(
return result
}

fun updateBuildNo(
userId: String,
projectId: String,
pipelineId: String,
buildNo: BuildNoUpdateReq
) {
operationLogService.addOperationLog(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
version = 0,
operationLogType = OperationLogType.RESET_RECOMMENDED_VERSION_BUILD_NO,
params = buildNo.currentBuildNo.toString(),
description = null
)
pipelineBuildSummaryDao.updateBuildNo(
dslContext = dslContext,
projectId = projectId,
pipelineId = pipelineId,
buildNo = buildNo.currentBuildNo,
debug = false
)
}

fun saveAll(
userId: String,
projectId: String,
Expand Down Expand Up @@ -1304,10 +1331,10 @@ class PipelineInfoFacadeService @Autowired constructor(
): Model {
try {
val triggerContainer = model.stages[0].containers[0] as TriggerContainer
val buildNo = triggerContainer.buildNo
if (buildNo != null) {
buildNo.buildNo = pipelineRepositoryService.getBuildNo(projectId = projectId, pipelineId = pipelineId)
?: buildNo.buildNo
// #10958 每次存储model都需要忽略当前的推荐版本号值,在返回前端时重查
triggerContainer.buildNo?.apply {
currentBuildNo = pipelineRepositoryService.getBuildNo(projectId = projectId, pipelineId = pipelineId)
?: buildNo
}
// 兼容性处理
BuildPropertyCompatibilityTools.fix(triggerContainer.params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ class PipelineVersionFacadeService @Autowired constructor(
)
}
}
pipelineBuildSummaryDao.resetDebugInfo(dslContext, projectId, pipelineId)
// 查询编排中的基准值,并把调试的版本号刷为基准值
val debugBuildNo = (draftVersion.model.stages[0].containers[0] as TriggerContainer)
.buildNo?.buildNo ?: 0
pipelineBuildSummaryDao.resetDebugInfo(dslContext, projectId, pipelineId, debugBuildNo)
pipelineBuildDao.clearDebugHistory(dslContext, projectId, pipelineId)

var targetUrl: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,11 @@ class PipelineBuildFacadeService(

BuildPropertyCompatibilityTools.fix(params)

val currentBuildNo = triggerContainer.buildNo
if (currentBuildNo != null) {
currentBuildNo.buildNo = pipelineRepositoryService.getBuildNo(
val currentBuildNo = triggerContainer.buildNo?.apply {
currentBuildNo = pipelineRepositoryService.getBuildNo(
projectId = projectId,
pipelineId = pipelineId
) ?: currentBuildNo.buildNo
) ?: buildNo
}

return BuildManualStartupInfo(
Expand Down Expand Up @@ -664,7 +663,7 @@ class PipelineBuildFacadeService(

if (buildNo != null) {
pipelineRuntimeService.updateBuildNo(
projectId, pipelineId, buildNo, version != null
projectId, pipelineId, buildNo, debug
)
logger.info("[$pipelineId] buildNo was changed to [$buildNo]")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1501,14 +1501,12 @@ class TemplateFacadeService @Autowired constructor(
)
logger.info("[$userId|$projectId|$templateId|$version] Get the param ($instanceParams)")

val buildNo = templateTriggerContainer.buildNo
var instanceBuildNoObj: BuildNo? = null
// 模板中的buildNo存在才需要回显
if (buildNo != null) {
instanceBuildNoObj = BuildNo(
buildNoType = buildNo.buildNoType,
required = buildNo.required ?: instanceTriggerContainer.buildNo?.required,
buildNo = buildNos[pipelineId] ?: buildNo.buildNo
val instanceBuildNoObj = templateTriggerContainer.buildNo?.let { no ->
BuildNo(
buildNoType = no.buildNoType,
required = no.required ?: instanceTriggerContainer.buildNo?.required,
buildNo = buildNos[pipelineId] ?: no.buildNo
)
}

Expand Down
3 changes: 2 additions & 1 deletion support-files/i18n/process/message_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ operationLogType.ADD_PIPELINE_OWNER=Add pipeline executors
operationLogType.ADD_PIPELINE_TO_GROUP=Add to pipeline group
operationLogType.MOVE_PIPELINE_OUT_OF_GROUP=Move out of pipeline group
operationLogType.UPDATE_PIPELINE_BY_OPENAPI=Modify setting and add new version
operationLogType.RESET_RECOMMENDED_VERSION_BUILD_NO=Reset recommended version to

trigger.build.param.basic=Basic parameters of pipeline
trigger.build.param.codeGitWebHookTrigger.ci.repo_type.desc=Repository type
Expand Down Expand Up @@ -780,4 +781,4 @@ bkPipelineRunConditionResult=The calculation result of custom condition [{0}] is
bkPipelineRunConditionNotMatch=, which is not met and will be skipped.
bkPipelineRunConditionWithError=The calculation of custom condition has error: {0}
transferErrorCheckAgentIdFailed=The current private build machine data is incorrect or does not exist
transferErrorCheckEnvIdFailed=The current private build machine cluster data is incorrect or does not exist
transferErrorCheckEnvIdFailed=The current private build machine cluster data is incorrect or does not exist
1 change: 1 addition & 0 deletions support-files/i18n/process/message_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ operationLogType.ADD_PIPELINE_OWNER=添加执行者
operationLogType.ADD_PIPELINE_TO_GROUP=添加到流水线组
operationLogType.MOVE_PIPELINE_OUT_OF_GROUP=移出流水线组
operationLogType.UPDATE_PIPELINE_BY_OPENAPI=修改配置新增版本
operationLogType.RESET_RECOMMENDED_VERSION_BUILD_NO=重置推荐版本号为
trigger.build.param.basic=流水线内置变量
trigger.build.param.codeGitWebHookTrigger.ci.repo_type.desc=触发的代码库类型
Expand Down

0 comments on commit 30453cb

Please sign in to comment.