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:源材料展示优化 #10733 #10914

Merged
merged 9 commits into from
Oct 18, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.devops.process.api.builds

import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_BUILD_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_CI_TASK_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_PIPELINE_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_PROJECT_ID
import com.tencent.devops.common.api.pojo.Result
Expand Down Expand Up @@ -61,6 +62,9 @@ interface BuildRepositoryMaterialResource {
@HeaderParam(AUTH_HEADER_DEVOPS_PIPELINE_ID)
pipelineId: String,
@Parameter(description = "物料列表", required = true)
pipelineBuildMaterials: List<PipelineBuildMaterial>
pipelineBuildMaterials: List<PipelineBuildMaterial>,
@Parameter(description = "插件ID", required = true)
@HeaderParam(AUTH_HEADER_DEVOPS_CI_TASK_ID)
taskId: String?
): Result<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@ data class PipelineBuildMaterial(
@get:Schema(title = "当前提交备注信息", required = false)
val newCommitComment: String?,
@get:Schema(title = "提交次数", required = false)
val commitTimes: Int?
val commitTimes: Int?,
@get:Schema(title = "是否为源材料主仓库", required = false)
val mainRepo: Boolean? = false,
@get:Schema(title = "提交时间", required = false)
val createTime: Long? = System.currentTimeMillis() / 1000,
@get:Schema(title = "插件ID", required = false)
val taskId: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ class PipelineRuntimeService @Autowired constructor(
status = status.name,
stageStatus = stageStatus,
currentTimestamp = currentTimestamp,
material = material?.sortedBy { it.aliasName },
material = material,
queueTime = queueTime,
artifactList = artifactList,
remark = remark,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,54 @@ class PipelineBuildMaterialService @Autowired constructor(
) {
private val logger = LoggerFactory.getLogger(PipelineBuildMaterialService::class.java)

@SuppressWarnings("CyclomaticComplexMethod", "ComplexMethod")
fun saveBuildMaterial(
buildId: String,
projectId: String,
pipelineBuildMaterials: List<PipelineBuildMaterial>
pipelineBuildMaterials: List<PipelineBuildMaterial>,
taskId: String?
): Int {
var newPipelineBuildMaterials = pipelineBuildMaterials
val pipelineBuildHistoryRecord = pipelineBuildDao.getBuildInfo(dslContext, projectId, buildId)
val materialList = mutableListOf<PipelineBuildMaterial>()
val pipelineBuildHistoryRecord = pipelineBuildDao.getBuildInfo(dslContext, projectId, buildId) ?: return 0
// 如果找不到构建历史或重试时,不做原材料写入
if (pipelineBuildHistoryRecord == null ||
pipelineBuildHistoryRecord.executeCount?.let { it > 1 } == true
) {
logger.info("save build material|buildId=$buildId|taskId=$taskId|${pipelineBuildMaterials.size}")
val material = pipelineBuildHistoryRecord.material
// 重试场景下,只要有空taskId,就直接返回
val containsEmptyTaskId = material?.find { it.taskId.isNullOrBlank() } != null || taskId.isNullOrBlank()
if (pipelineBuildHistoryRecord.executeCount?.let { it > 1 } == true && containsEmptyTaskId) {
logger.info("skip save build material")
return 0
}
val material = pipelineBuildHistoryRecord.material
val existTaskIds = material?.mapNotNull { it.taskId } ?: listOf()
if (!material.isNullOrEmpty()) {
newPipelineBuildMaterials = newPipelineBuildMaterials.plus(material)
materialList.addAll(material)
// 存在空TaskId,直接添加
// 不存在空TaskId,旧数据不存在当前TaskId上报的源材料时则添加
if ((!containsEmptyTaskId && !existTaskIds.contains(taskId)) || containsEmptyTaskId) {
materialList.addAll(pipelineBuildMaterials.map { it.copy(taskId = taskId) })
}
} else {
materialList.addAll(pipelineBuildMaterials.map { it.copy(taskId = taskId) })
}
// 按顺序保存
val materials = JsonUtil.toJson(
bean = materialList.let { list ->
list.sortedWith { o1, o2 ->
val (mainRepo1, createTime1) = (o1.mainRepo ?: false) to (o1.createTime ?: 0)
val (mainRepo2, createTime2) = (o2.mainRepo ?: false) to (o2.createTime ?: 0)
when {
mainRepo1 == mainRepo2 -> if (mainRepo1) {
createTime2.compareTo(createTime1)
} else {
(o1.aliasName ?: "").compareTo(o2.aliasName ?: "")
}

val materials = JsonUtil.toJson(newPipelineBuildMaterials, formatted = false)
logger.info("BuildId: $buildId save material size: ${newPipelineBuildMaterials.size}")
else -> mainRepo2.compareTo(mainRepo1)
}
}
},
formatted = false
)
pipelineBuildDao.updateBuildMaterial(
dslContext = dslContext,
projectId = projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ class BuildRepositoryMaterialResourceImpl @Autowired constructor(
buildId: String,
projectId: String,
pipelineId: String,
pipelineBuildMaterials: List<PipelineBuildMaterial>
pipelineBuildMaterials: List<PipelineBuildMaterial>,
taskId: String?
): Result<Int> {
checkParam(buildId = buildId, projectId = projectId, pipelineId = pipelineId)
return Result(pipelineBuildMaterialService.saveBuildMaterial(
buildId = buildId,
projectId = projectId,
pipelineBuildMaterials = pipelineBuildMaterials
pipelineBuildMaterials = pipelineBuildMaterials,
taskId = taskId
))
}

Expand Down
Loading