Skip to content

Commit

Permalink
feat: commit check功能优化 #10448
Browse files Browse the repository at this point in the history
  • Loading branch information
hejieehe committed Aug 14, 2024
1 parent 71403d3 commit 204141f
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -544,44 +544,36 @@ class CodeWebhookService @Autowired constructor(
val checkRunId = if (conclusion == null) {
val result = scmCheckService.addGithubCheckRuns(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
repositoryConfig = repositoryConfig,
name = record.checkRunName ?: "$pipelineName #$buildNum",
commitId = commitId,
detailUrl = detailUrl,
externalId = "${userId}_${projectId}_${pipelineId}_$buildId",
status = status,
startedAt = startedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
),
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
)
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId
)
result.id
} else {
scmCheckService.updateGithubCheckRuns(
checkRunId = record.checkRunId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
pipelineName = pipelineName,
repositoryConfig = repositoryConfig,
// 兼容历史数据
name = record.checkRunName ?: "$pipelineName #$buildNum",
commitId = commitId,
detailUrl = detailUrl,
externalId = "${userId}_${projectId}_${pipelineId}_$buildId",
status = status,
startedAt = startedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
),
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
)
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId,
pipelineName = pipelineName
)
record.checkRunId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package com.tencent.devops.process.service.commit.check
import com.tencent.devops.common.api.enums.RepositoryConfig
import com.tencent.devops.common.api.enums.RepositoryType
import com.tencent.devops.common.api.exception.RemoteServiceException
import com.tencent.devops.common.api.util.DateTimeUtil
import com.tencent.devops.common.api.util.timestamp
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.event.dispatcher.pipeline.PipelineEventDispatcher
Expand Down Expand Up @@ -81,7 +82,6 @@ import org.springframework.stereotype.Service
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter

@Service
@Suppress("ALL")
Expand Down Expand Up @@ -235,7 +235,7 @@ class CodeWebhookService @Autowired constructor(
repositoryConfig = repositoryConfig,
commitId = commitId,
status = GITHUB_CHECK_RUNS_STATUS_COMPLETED,
startedAt = null,
startedAt = (event.startTime ?: 0L) / 1000, // 毫秒数 -> 秒数
conclusion = if (buildStatus == BuildStatus.SUCCEED) {
GITHUB_CHECK_RUNS_CONCLUSION_SUCCESS
} else {
Expand Down Expand Up @@ -336,6 +336,7 @@ class CodeWebhookService @Autowired constructor(
}

else -> {
logger.warn("fail to consume git event", ignored)
throw ignored
}
}
Expand Down Expand Up @@ -493,6 +494,10 @@ class CodeWebhookService @Autowired constructor(
val completedAt = event.completedAt?.let {
LocalDateTime.ofInstant(Instant.ofEpochSecond(it), ZoneId.systemDefault())
}
logger.info(
"consume github pr event|startAt[${DateTimeUtil.toDateTime(startedAt)}]|" +
"completedAt[${DateTimeUtil.toDateTime(completedAt)}]"
)
addGithubPullRequestCheck(
userId = event.userId,
projectId = event.projectId,
Expand Down Expand Up @@ -521,6 +526,10 @@ class CodeWebhookService @Autowired constructor(
)
)
}
else -> {
logger.warn("fail to consume github pr event", ignored)
throw ignored
}
}
}
}
Expand Down Expand Up @@ -590,6 +599,7 @@ class CodeWebhookService @Autowired constructor(
context = name,
targetBranch = targetBranch
)
logger.info("get github commit check record[$record]")
if (record == null) {
val result = scmCheckService.addGithubCheckRuns(
projectId = projectId,
Expand All @@ -599,10 +609,13 @@ class CodeWebhookService @Autowired constructor(
detailUrl = detailUrl,
externalId = "${userId}_${projectId}_${pipelineId}_$buildId",
status = status,
startedAt = startedAt?.atZone(ZoneId.systemDefault())?.format(DateTimeFormatter.ISO_INSTANT),
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt?.atZone(ZoneId.systemDefault())?.format(DateTimeFormatter.ISO_INSTANT)
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId
)
logger.info("get github commit check run info[$result]")
addGitCheckCommitRecord(
pipelineId = pipelineId,
buildNum = buildNum.toInt(),
Expand All @@ -624,13 +637,11 @@ class CodeWebhookService @Autowired constructor(
detailUrl = detailUrl,
externalId = "${userId}_${projectId}_${pipelineId}_$buildId",
status = status,
startedAt = startedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
),
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
)
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId
)
result.id
} else {
Expand All @@ -644,13 +655,12 @@ class CodeWebhookService @Autowired constructor(
detailUrl = detailUrl,
externalId = "${userId}_${projectId}_${pipelineId}_$buildId",
status = status,
startedAt = startedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
),
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt?.atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
)
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId,
pipelineName = pipelineName
)
record.checkRunId
}
Expand Down Expand Up @@ -854,9 +864,9 @@ class CodeWebhookService @Autowired constructor(
status = GITHUB_CHECK_RUNS_STATUS_COMPLETED,
startedAt = null,
conclusion = GITHUB_CHECK_RUNS_CONCLUSION_SUCCESS,
completedAt = LocalDateTime.now().atZone(ZoneId.systemDefault())?.format(
DateTimeFormatter.ISO_INSTANT
)
completedAt = LocalDateTime.now(),
buildId = buildId,
pipelineId = pipelineId
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.devops.process.service.commit.check

import com.tencent.devops.common.api.enums.ScmType
import com.tencent.devops.common.api.util.DateTimeUtil
import com.tencent.devops.common.client.Client
import com.tencent.devops.common.pipeline.enums.StartType
Expand Down Expand Up @@ -57,32 +58,48 @@ class QualityService @Autowired constructor(
}

fun getQualityGitMrResult(
event: TGitCommitCheckEvent
projectId: String,
pipelineId: String,
buildId: String,
eventStatus: String,
startTime: Long,
triggerType: String,
scmType: ScmType
): Pair<List<String>, MutableMap<String, MutableList<List<String>>>> {
val projectId = event.projectId
val pipelineId = event.pipelineId
val buildId = event.buildId
val pipelineName = pipelineListFacadeService.getPipelineNameByIds(
projectId = projectId,
pipelineIds = setOf(pipelineId),
filterDelete = true
)[pipelineId] ?: ""

// github 不需要插入链接, 仅在插件名处插入链接,链接地址用codecc插件输出变量
val githubRepo = scmType == ScmType.GITHUB
val titleData = mutableListOf(
event.status,
DateTimeUtil.formatMilliTime(System.currentTimeMillis() - event.startTime),
eventStatus,
DateTimeUtil.formatMilliTime(System.currentTimeMillis() - startTime),
StartType.toReadableString(
event.triggerType,
triggerType,
null,
I18nUtil.getLanguage(I18nUtil.getRequestUserId())
),
pipelineName,
"${HomeHostUtil.innerServerHost()}/console/pipeline/$projectId/$pipelineId/detail/$buildId",
if (githubRepo) {
""
} else {
"${HomeHostUtil.innerServerHost()}/console/pipeline/$projectId/$pipelineId/detail/$buildId"
},
I18nUtil.getCodeLanMessage(PluginMessageCode.BK_CI_PIPELINE)
)

logger.info("get quality git mr result|title[$titleData]")
val ruleName = mutableSetOf<String>()

// 插件输出变量
val reportUrl = getBuildVar(
client = client,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
varName = CodeccUtils.BK_CI_CODECC_REPORT_URL
)
// key:质量红线产出插件
// value:指标、预期、结果、状态
val resultMap = mutableMapOf<String, MutableList<List<String>>>()
Expand All @@ -92,19 +109,25 @@ class QualityService @Autowired constructor(
val indicator = client.get(ServiceQualityIndicatorResource::class)
.get(projectId, interceptItem.indicatorId).data
val indicatorElementName = indicator?.elementType ?: ""
val elementCnName = getElementCnName(indicatorElementName, projectId)
val elementCnName = getElementCnName(indicatorElementName, projectId).let {
if (githubRepo) {
"<a target='_blank' href='$reportUrl'>$it</a>"
} else {
it
}
}
val resultList = resultMap[elementCnName] ?: mutableListOf()
val actualValue = if (CodeccUtils.isCodeccAtom(indicatorElementName)) {
getActualValue(
val actualValue = when {
githubRepo -> interceptItem.actualValue ?: "null"
CodeccUtils.isCodeccAtom(indicatorElementName) -> getActualValue(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
detail = indicator?.elementDetail,
value = interceptItem.actualValue ?: "null",
client = client
)
} else {
interceptItem.actualValue ?: "null"
else -> interceptItem.actualValue ?: "null"
}
resultList.add(
listOf(
Expand All @@ -131,13 +154,13 @@ class QualityService @Autowired constructor(
value: String,
client: Client
): String {
val variable = client.get(ServiceVarResource::class).getBuildVar(
val taskId = getBuildVar(
client = client,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
varName = CodeccUtils.BK_CI_CODECC_TASK_ID
).data
val taskId = variable?.get(CodeccUtils.BK_CI_CODECC_TASK_ID)
)
return if (detail.isNullOrBlank() || detail.split(",").size > 1) {
"<a target='_blank' href='${HomeHostUtil.innerServerHost()}/" +
"console/codecc/$projectId/task/$taskId/detail?buildId=$buildId'>$value</a>"
Expand All @@ -163,4 +186,21 @@ class QualityService @Autowired constructor(
}
return map[classType] ?: ""
}

private fun getBuildVar(
client: Client,
projectId: String,
pipelineId: String,
buildId: String,
varName: String
): String? {
val variable = client.get(ServiceVarResource::class).getBuildVar(
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
varName = varName
).data
val taskId = variable?.get(varName)
return taskId
}
}
Loading

0 comments on commit 204141f

Please sign in to comment.