Skip to content

Commit

Permalink
feat: Stage准入支持审核流 TencentBlueKing#4531 在暂停时做变量替换,每步审核都做通知
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed Aug 13, 2021
1 parent 83f43cf commit 8444b9e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.devops.common.pipeline.pojo

import com.tencent.devops.common.api.util.EnvUtils
import com.tencent.devops.common.api.util.UUIDUtil
import com.tencent.devops.common.api.util.timestampmilli
import com.tencent.devops.common.pipeline.enums.BuildStatus
Expand Down Expand Up @@ -153,6 +154,21 @@ data class StagePauseCheck(
}
}

/**
* 进入审核流程前完成所有审核人变量替换
*/
fun parseReviewVariables(variables: Map<String, String>) {
reviewGroups?.forEach { group ->
if (group.status == null) {
val reviewers = group.reviewers.joinToString(",")
val realReviewers = EnvUtils.parseEnv(reviewers, variables)
.split(",").toList()
group.reviewers = realReviewers
}
}
reviewDesc = EnvUtils.parseEnv(reviewDesc, variables)
}

companion object {
fun convertControlOption(stageControlOption: StageControlOption): StagePauseCheck {
return StagePauseCheck(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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

import com.tencent.devops.common.api.util.DateTimeUtil
import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher
import com.tencent.devops.common.event.enums.ActionType
import com.tencent.devops.common.pipeline.enums.BuildStatus
Expand All @@ -39,14 +40,20 @@ import com.tencent.devops.process.engine.dao.PipelineBuildDao
import com.tencent.devops.process.engine.dao.PipelineBuildStageDao
import com.tencent.devops.process.engine.dao.PipelineBuildSummaryDao
import com.tencent.devops.process.engine.pojo.PipelineBuildStage
import com.tencent.devops.process.engine.pojo.event.PipelineBuildNotifyEvent
import com.tencent.devops.process.engine.pojo.event.PipelineBuildStageEvent
import com.tencent.devops.process.engine.pojo.event.PipelineBuildWebSocketPushEvent
import com.tencent.devops.process.engine.service.detail.StageBuildDetailService
import com.tencent.devops.process.pojo.PipelineNotifyTemplateEnum
import com.tencent.devops.process.service.BuildVariableService
import com.tencent.devops.process.utils.PIPELINE_BUILD_NUM
import com.tencent.devops.process.utils.PIPELINE_NAME
import org.jooq.DSLContext
import org.jooq.impl.DSL
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*

/**
* 流水线Stage相关的服务
Expand All @@ -59,6 +66,7 @@ class PipelineStageService @Autowired constructor(
private val pipelineBuildDao: PipelineBuildDao,
private val pipelineBuildSummaryDao: PipelineBuildSummaryDao,
private val pipelineBuildStageDao: PipelineBuildStageDao,
private val buildVariableService: BuildVariableService,
private val stageBuildDetailService: StageBuildDetailService
) {
companion object {
Expand Down Expand Up @@ -188,7 +196,15 @@ class PipelineStageService @Autowired constructor(
controlOption = controlOption, checkIn = checkIn, checkOut = checkOut
)

// 如果还有待审核的审核组,则直接通知并返回
if (checkIn?.groupToReview() != null) {
val variables = buildVariableService.getAllVariable(buildId)
pauseStageNotify(
userId = userId,
stage = buildStage,
pipelineName = variables[PIPELINE_NAME] ?: pipelineId,
buildNum = variables[PIPELINE_BUILD_NUM] ?: "1"
)
return true
}
val allStageStatus = stageBuildDetailService.stageStart(
Expand Down Expand Up @@ -295,4 +311,35 @@ class PipelineStageService @Autowired constructor(
}
return pendingStage
}

fun pauseStageNotify(
userId: String,
stage: PipelineBuildStage,
pipelineName: String,
buildNum: String
) {
val checkIn = stage.checkIn ?: return
val group = stage.checkIn?.groupToReview() ?: return

pipelineEventDispatcher.dispatch(
PipelineBuildNotifyEvent(
notifyTemplateEnum = PipelineNotifyTemplateEnum.PIPELINE_MANUAL_REVIEW_STAGE_NOTIFY_TEMPLATE.name,
source = "s(${stage.stageId}) waiting for REVIEW",
projectId = stage.projectId, pipelineId = stage.pipelineId,
userId = userId, buildId = stage.buildId,
receivers = group.reviewers,
titleParams = mutableMapOf(
"projectName" to "need to add in notifyListener",
"pipelineName" to pipelineName,
"buildNum" to buildNum
),
bodyParams = mutableMapOf(
"projectName" to "need to add in notifyListener",
"pipelineName" to pipelineName,
"dataTime" to DateTimeUtil.formatDate(Date(), "yyyy-MM-dd HH:mm:ss"),
"reviewDesc" to (checkIn.reviewDesc ?: "")
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,27 @@

package com.tencent.devops.process.engine.control.command.stage.impl

import com.tencent.devops.common.api.util.DateTimeUtil
import com.tencent.devops.common.api.util.EnvUtils
import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher
import com.tencent.devops.common.pipeline.enums.BuildStatus
import com.tencent.devops.process.engine.common.BS_MANUAL_START_STAGE
import com.tencent.devops.process.engine.control.command.CmdFlowState
import com.tencent.devops.process.engine.control.command.stage.StageCmd
import com.tencent.devops.process.engine.control.command.stage.StageContext
import com.tencent.devops.process.engine.pojo.PipelineBuildStage
import com.tencent.devops.process.engine.pojo.event.PipelineBuildNotifyEvent
import com.tencent.devops.process.engine.pojo.event.PipelineBuildStageEvent
import com.tencent.devops.process.pojo.PipelineNotifyTemplateEnum
import com.tencent.devops.process.engine.service.PipelineStageService
import com.tencent.devops.process.service.BuildVariableService
import com.tencent.devops.process.utils.PIPELINE_BUILD_NUM
import com.tencent.devops.process.utils.PIPELINE_NAME
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.Date

/**
* Stage暂停及审核事件的命令处理
*/
@Service
class CheckPauseReviewStageCmd(
private val buildVariableService: BuildVariableService,
private val pipelineEventDispatcher: PipelineEventDispatcher
private val pipelineStageService: PipelineStageService
) : StageCmd {

override fun canExecute(commandContext: StageContext): Boolean {
Expand All @@ -76,7 +71,13 @@ class CheckPauseReviewStageCmd(
if (needPause(event, stage)) {
// #3742 进入暂停状态则刷新完状态后直接返回,等待手动触发
LOG.info("ENGINE|${event.buildId}|${event.source}|STAGE_PAUSE|${event.stageId}")
pauseStageNotify(commandContext)
stage.checkIn?.parseReviewVariables(commandContext.variables)
pipelineStageService.pauseStageNotify(
userId = event.userId,
stage = stage,
pipelineName = commandContext.variables[PIPELINE_NAME] ?: stage.pipelineId,
buildNum = commandContext.variables[PIPELINE_BUILD_NUM] ?: "1"
)
commandContext.buildStatus = BuildStatus.STAGE_SUCCESS
commandContext.latestSummary = "s(${stage.stageId}) waiting for REVIEW"
commandContext.cmdFlowState = CmdFlowState.FINALLY
Expand Down Expand Up @@ -119,50 +120,6 @@ class CheckPauseReviewStageCmd(
stage.controlOption?.stageControlOption?.triggered != true
}

/**
* 发送通知
*/
private fun pauseStageNotify(commandContext: StageContext) {
val stage = commandContext.stage
val checkIn = stage.checkIn ?: return
val group = stage.checkIn?.groupToReview() ?: return
val notifyUsers = mutableListOf<String>()

if (group.status == null) {
val reviewers = group.reviewers.joinToString(",")
val realReviewers = EnvUtils.parseEnv(reviewers, commandContext.variables)
.split(",").toList()
group.reviewers = realReviewers
notifyUsers.addAll(realReviewers)
}

val pipelineName = commandContext.variables[PIPELINE_NAME] ?: stage.pipelineId
val buildNum = commandContext.variables[PIPELINE_BUILD_NUM] ?: "1"
var reviewDesc = checkIn.reviewDesc
reviewDesc = EnvUtils.parseEnv(reviewDesc, commandContext.variables)
checkIn.reviewDesc = reviewDesc // 替换变量 #3395

pipelineEventDispatcher.dispatch(
PipelineBuildNotifyEvent(
notifyTemplateEnum = PipelineNotifyTemplateEnum.PIPELINE_MANUAL_REVIEW_STAGE_NOTIFY_TEMPLATE.name,
source = commandContext.latestSummary, projectId = stage.projectId, pipelineId = stage.pipelineId,
userId = commandContext.event.userId, buildId = stage.buildId,
receivers = notifyUsers,
titleParams = mutableMapOf(
"projectName" to "need to add in notifyListener",
"pipelineName" to pipelineName,
"buildNum" to buildNum
),
bodyParams = mutableMapOf(
"projectName" to "need to add in notifyListener",
"pipelineName" to pipelineName,
"dataTime" to DateTimeUtil.formatDate(Date(), "yyyy-MM-dd HH:mm:ss"),
"reviewDesc" to reviewDesc
)
)
)
}

companion object {
private val LOG = LoggerFactory.getLogger(CheckPauseReviewStageCmd::class.java)
}
Expand Down

0 comments on commit 8444b9e

Please sign in to comment.