Skip to content

Commit

Permalink
feat:保存流水线时,校验是否有子流水线循环依赖 TencentBlueKing#10479
Browse files Browse the repository at this point in the history
  • Loading branch information
hejieehe committed Jul 16, 2024
1 parent 3969312 commit 44451c3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

package com.tencent.devops.process.dao

import com.tencent.devops.model.process.tables.TSubPipelineRef
import com.tencent.devops.model.process.tables.records.TSubPipelineRefRecord
import com.tencent.devops.model.process.tables.TPipelineSubRef
import com.tencent.devops.model.process.tables.records.TPipelineSubRefRecord
import com.tencent.devops.process.pojo.pipeline.SubPipelineRef
import org.jooq.DSLContext
import org.jooq.Result
Expand All @@ -45,7 +45,7 @@ class SubPipelineRefDao {
return
}

with(TSubPipelineRef.T_SUB_PIPELINE_REF) {
with(TPipelineSubRef.T_PIPELINE_SUB_REF) {
dslContext.batch(
subPipelineRefList.map {
dslContext.insertInto(
Expand Down Expand Up @@ -90,8 +90,8 @@ class SubPipelineRefDao {
dslContext: DSLContext,
projectId: String,
pipelineId: String
): Result<TSubPipelineRefRecord> {
return with(TSubPipelineRef.T_SUB_PIPELINE_REF) {
): Result<TPipelineSubRefRecord> {
return with(TPipelineSubRef.T_PIPELINE_SUB_REF) {
dslContext.selectFrom(this).where(
PROJECT_ID.eq(projectId).and(
PIPELINE_ID.eq(pipelineId)
Expand All @@ -105,7 +105,7 @@ class SubPipelineRefDao {
projectId: String,
pipelineId: String
): Int {
return with(TSubPipelineRef.T_SUB_PIPELINE_REF) {
return with(TPipelineSubRef.T_PIPELINE_SUB_REF) {
dslContext.deleteFrom(this).where(
PROJECT_ID.eq(projectId).and(
PIPELINE_ID.eq(pipelineId)
Expand All @@ -114,14 +114,31 @@ class SubPipelineRefDao {
}
}

fun delete(
dslContext: DSLContext,
projectId: String,
pipelineId: String,
taskId: String
): Int {
return with(TPipelineSubRef.T_PIPELINE_SUB_REF) {
dslContext.deleteFrom(this).where(
listOf(
PROJECT_ID.eq(projectId),
PIPELINE_ID.eq(pipelineId),
TASK_ID.eq(taskId)
)
).execute()
}
}

fun batchDelete(
dslContext: DSLContext,
ids: List<Long>
) {
if (ids.isEmpty()) {
return
}
with(TSubPipelineRef.T_SUB_PIPELINE_REF) {
with(TPipelineSubRef.T_PIPELINE_SUB_REF) {
dslContext.deleteFrom(this).where(
ID.`in`(ids)
).execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import com.tencent.devops.process.engine.service.AgentPipelineRefService
import com.tencent.devops.process.engine.service.PipelineAtomStatisticsService
import com.tencent.devops.process.engine.service.RepoPipelineRefService
import com.tencent.devops.process.engine.service.PipelineWebhookService
import com.tencent.devops.process.service.pipeline.SubPipelineRefService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

Expand All @@ -54,7 +53,6 @@ class MQPipelineCreateListener @Autowired constructor(
private val callBackControl: CallBackControl,
private val agentPipelineRefService: AgentPipelineRefService,
private val repoPipelineRefService: RepoPipelineRefService,
private val subPipelineRefService: SubPipelineRefService,
pipelineEventDispatcher: PipelineEventDispatcher
) : BaseListener<PipelineCreateEvent>(pipelineEventDispatcher) {

Expand Down Expand Up @@ -94,12 +92,6 @@ class MQPipelineCreateListener @Autowired constructor(
}
}

watcher.safeAround("saveSubPipelineRef") {
with(event) {
subPipelineRefService.updateSubPipelineRef(userId, projectId, pipelineId)
}
}

LogUtils.printCostTimeWE(watcher = watcher)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import com.tencent.devops.process.engine.pojo.event.PipelineRestoreEvent
import com.tencent.devops.process.engine.service.AgentPipelineRefService
import com.tencent.devops.process.engine.service.PipelineAtomStatisticsService
import com.tencent.devops.process.engine.service.RepoPipelineRefService
import com.tencent.devops.process.service.pipeline.SubPipelineRefService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

Expand All @@ -51,7 +50,6 @@ class MQPipelineRestoreListener @Autowired constructor(
private val pipelineAtomStatisticsService: PipelineAtomStatisticsService,
private val callBackControl: CallBackControl,
private val repoPipelineRefService: RepoPipelineRefService,
private val subPipelineRefService: SubPipelineRefService,
pipelineEventDispatcher: PipelineEventDispatcher
) : BaseListener<PipelineRestoreEvent>(pipelineEventDispatcher) {

Expand All @@ -76,8 +74,6 @@ class MQPipelineRestoreListener @Autowired constructor(
callBackControl.pipelineRestoreEvent(projectId = event.projectId, pipelineId = event.pipelineId)
with(event) {
repoPipelineRefService.updateRepoPipelineRef(userId, "restore_pipeline", projectId, pipelineId)
// 更新子流水线关联关系
subPipelineRefService.updateSubPipelineRef(userId, projectId, pipelineId)
}
} finally {
watcher.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import com.tencent.devops.process.engine.service.PipelineAtomStatisticsService
import com.tencent.devops.process.engine.service.RepoPipelineRefService
import com.tencent.devops.process.engine.service.PipelineRuntimeService
import com.tencent.devops.process.engine.service.PipelineWebhookService
import com.tencent.devops.process.service.pipeline.SubPipelineRefService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

Expand All @@ -55,7 +54,6 @@ class MQPipelineUpdateListener @Autowired constructor(
private val agentPipelineRefService: AgentPipelineRefService,
private val pipelineWebhookService: PipelineWebhookService,
private val repoPipelineRefService: RepoPipelineRefService,
private val subPipelineRefService: SubPipelineRefService,
pipelineEventDispatcher: PipelineEventDispatcher
) : BaseListener<PipelineUpdateEvent>(pipelineEventDispatcher) {

Expand Down Expand Up @@ -100,13 +98,6 @@ class MQPipelineUpdateListener @Autowired constructor(
}
}

watcher.safeAround("updateSubPipelineRef") {
with(event) {
// 更新子流水线关联关系
subPipelineRefService.updateSubPipelineRef(userId, projectId, pipelineId)
}
}

LogUtils.printCostTimeWE(watcher)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.process.service.pipeline

import com.fasterxml.jackson.databind.ObjectMapper
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.common.api.exception.OperationException
import com.tencent.devops.common.api.util.EnvUtils
import com.tencent.devops.common.pipeline.Model
import com.tencent.devops.common.pipeline.container.Stage
Expand All @@ -41,6 +42,9 @@ import com.tencent.devops.common.pipeline.pojo.element.atom.SubPipelineType
import com.tencent.devops.common.pipeline.pojo.element.market.MarketBuildAtomElement
import com.tencent.devops.common.pipeline.pojo.element.market.MarketBuildLessAtomElement
import com.tencent.devops.common.util.ThreadPoolUtil
import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.model.process.tables.TPipelineSubRef
import com.tencent.devops.model.process.tables.records.TPipelineSubRefRecord
import com.tencent.devops.process.constant.ProcessMessageCode
import com.tencent.devops.process.dao.SubPipelineRefDao
import com.tencent.devops.process.engine.dao.PipelineInfoDao
Expand Down Expand Up @@ -295,6 +299,15 @@ class SubPipelineRefService @Autowired constructor(
return PipelineVarUtil.fillVariableMap(paramsMap.mapValues { it.value.defaultValue.toString() })
}

// fun getContextMap(projectId: String,pipelineId: String): Map<String /* 流水线变量名 */, String> {
// val triggerContainer = (triggerStage.containers.getOrNull(0) ?: throw ErrorCodeException(
// errorCode = ProcessMessageCode.ERROR_PIPELINE_MODEL_NEED_JOB
// )) as TriggerContainer
// // 检查触发容器
// val paramsMap = PipelineUtils.checkPipelineParams(triggerContainer.params)
// return PipelineVarUtil.fillVariableMap(paramsMap.mapValues { it.value.defaultValue.toString() })
// }

private fun resolveSubPipelineCall(
projectId: String,
element: SubPipelineCallElement,
Expand Down Expand Up @@ -395,17 +408,21 @@ class SubPipelineRefService @Autowired constructor(
fun checkCircularDependency(
projectId: String,
pipelineId: String,
existsPipeline: List<String>
existsPipeline: MutableMap<String, TPipelineSubRefRecord>
): ElementCheckResult {
// :TODO 递归校验
// val subPipelineList = subPipelineRefDao.list(dslContext, projectId, pipelineId)
// val intersect = existsPipeline.intersect(subPipelineList)
// if (intersect.isNotEmpty()) {
// return ElementCheckResult(
// result = false,
// errorTitle = "",
// errorMessage = ""
// )
// subPipelineList.forEach {
// val pipelineItem = "${it.projectId}_${it.pipelineId}"
// if (existsPipeline.contains(pipelineItem)){
// throw OperationException(
// I18nUtil.getCodeLanMessage(
// messageCode = ProcessMessageCode.ERROR_SUB_PIPELINE_NOT_ALLOWED_CIRCULAR_CALL,
// params = arrayOf(projectId, pipelineId)
// )
// )
// }
// checkCircularDependency(it.projectId,it.pipelineId,existsPipeline)
// }
return ElementCheckResult(result = true)
}
Expand Down

0 comments on commit 44451c3

Please sign in to comment.