Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:推荐版本号优化 #10958 #10993

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -534,4 +534,8 @@ object PipelineVarUtil {

return "$majorVersion.$minorVersion.$fixVersion"
}

fun recommendVersionKey(key: String): Boolean {
return key == MAJORVERSION || key == MINORVERSION || key == FIXVERSION
}
}
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 @@ -27,15 +27,13 @@

package com.tencent.devops.process.engine.service

import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.util.PageUtil
import com.tencent.devops.common.api.util.timestampmilli
import com.tencent.devops.common.auth.api.pojo.ProjectConditionDTO
import com.tencent.devops.common.auth.enums.AuthSystemType
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.pipeline.enums.VersionStatus
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.process.constant.ProcessMessageCode
import com.tencent.devops.process.engine.control.lock.PipelineModelLock
import com.tencent.devops.process.engine.control.lock.PipelineVersionLock
import com.tencent.devops.process.engine.dao.PipelineBuildDao
Expand All @@ -48,7 +46,6 @@ import com.tencent.devops.process.pojo.setting.PipelineVersionSimple
import com.tencent.devops.process.utils.PipelineVersionUtils
import com.tencent.devops.project.api.service.ServiceProjectResource
import org.jooq.DSLContext
import org.jooq.impl.DSL
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.concurrent.Executors
Expand Down Expand Up @@ -107,23 +104,8 @@ class PipelineRepositoryVersionService(
val pipelineVersionLock = PipelineVersionLock(redisOperation, pipelineId, version)
try {
pipelineVersionLock.lock()
val count = pipelineBuildDao.countBuildNumByVersion(
dslContext = dslContext,
projectId = projectId,
pipelineId = pipelineId,
version = version
)
if (count > 0) {
throw ErrorCodeException(
errorCode = ProcessMessageCode.ERROR_PIPELINE_CAN_NOT_DELETE_WHEN_HAVE_BUILD_RECORD
)
}
dslContext.transaction { t ->
val transactionContext = DSL.using(t)
// #8161 软删除数据,前端无法查询到该版本
pipelineResourceVersionDao.deleteByVersion(transactionContext, projectId, pipelineId, version)
// pipelineSettingVersionDao.deleteByVer(transactionContext, projectId, pipelineId, version)
}
// #8161 软删除数据,前端无法查询到该版本
pipelineResourceVersionDao.deleteByVersion(dslContext, projectId, pipelineId, version)
} finally {
pipelineVersionLock.unlock()
}
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
Loading
Loading