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 71967ec commit bfc50d9
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,11 @@ class CodeWebhookService @Autowired constructor(
commitId = commitId,
context = context,
targetBranch = targetBranch
) ?: return
)
if (record == null) {
logger.info("Code web hook add git check record not exist, skip")
return
}
updateCommitCheck(
buildNum = buildNum,
record = record,
Expand Down Expand Up @@ -445,15 +449,11 @@ class CodeWebhookService @Autowired constructor(
logger.info("Consume github pr event($event)")

try {
val startedAt = if (event.startedAt != null) {
LocalDateTime.ofInstant(Instant.ofEpochSecond(event.startedAt!!), ZoneId.systemDefault())
} else {
null
val startedAt = event.startedAt?.let {
LocalDateTime.ofInstant(Instant.ofEpochSecond(it), ZoneId.systemDefault())
}
val completedAt = if (event.completedAt != null) {
LocalDateTime.ofInstant(Instant.ofEpochSecond(event.completedAt!!), ZoneId.systemDefault())
} else {
null
val completedAt = event.completedAt?.let {
LocalDateTime.ofInstant(Instant.ofEpochSecond(it), ZoneId.systemDefault())
}

event.retryTime--
Expand Down Expand Up @@ -499,7 +499,11 @@ class CodeWebhookService @Autowired constructor(
"repo=$repositoryConfig, commitId=$commitId, status=$status]"
)
// 不存在检查记录直接跳过
val record = pluginGithubCheckDao.getOrNull(dslContext, pipelineId, repositoryConfig, commitId) ?: return
val record = pluginGithubCheckDao.getOrNull(dslContext, pipelineId, repositoryConfig, commitId)
if (record == null) {
logger.info("Code web hook add pr check record not exist, skip")
return
}
val buildHistoryResult = client.get(ServiceBuildResource::class).getBuildVars(
userId = userId,
projectId = projectId,
Expand Down Expand Up @@ -544,8 +548,6 @@ 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,
Expand All @@ -554,15 +556,15 @@ class CodeWebhookService @Autowired constructor(
status = status,
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId
)
result.id
} else {
scmCheckService.updateGithubCheckRuns(
checkRunId = record.checkRunId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
repositoryConfig = repositoryConfig,
// 兼容历史数据
name = record.checkRunName ?: "$pipelineName #$buildNum",
Expand All @@ -573,6 +575,8 @@ class CodeWebhookService @Autowired constructor(
startedAt = startedAt,
conclusion = conclusion,
completedAt = completedAt,
pipelineId = pipelineId,
buildId = buildId,
pipelineName = pipelineName
)
record.checkRunId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import com.tencent.devops.repository.pojo.CodeGitlabRepository
import com.tencent.devops.repository.pojo.CodeP4Repository
import com.tencent.devops.repository.pojo.CodeSvnRepository
import com.tencent.devops.repository.pojo.CodeTGitRepository
import com.tencent.devops.repository.pojo.GithubCheckRuns
import com.tencent.devops.repository.pojo.github.GithubCheckRuns
import com.tencent.devops.repository.pojo.GithubCheckRunsResponse
import com.tencent.devops.repository.pojo.GithubRepository
import com.tencent.devops.repository.pojo.Repository
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 All @@ -36,7 +37,6 @@ import com.tencent.devops.common.web.utils.I18nUtil
import com.tencent.devops.plugin.codecc.CodeccUtils
import com.tencent.devops.plugin.constant.PluginMessageCode
import com.tencent.devops.process.api.service.ServiceVarResource
import com.tencent.devops.process.pojo.mq.commit.check.TGitCommitCheckEvent
import com.tencent.devops.process.service.PipelineListFacadeService
import com.tencent.devops.quality.api.v2.ServiceQualityIndicatorResource
import com.tencent.devops.quality.api.v2.ServiceQualityInterceptResource
Expand All @@ -57,32 +57,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 +108,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 +153,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 +185,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 bfc50d9

Please sign in to comment.