Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#1292 from SunnyShen434/issue_1208
Browse files Browse the repository at this point in the history
bug: 扫描质量规则元数据异常 TencentBlueKing#1208
  • Loading branch information
owenlxu authored Oct 26, 2023
2 parents cdf299b + b561e37 commit 782538e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ abstract class AbsSubScanTaskDao<E : SubScanTaskDefinition> : ScannerSimpleMongo
criteria.and(SubScanTaskDefinition::createdDate.name).gte(startDateTime!!).lte(endDateTime!!)
}
qualityRedLine?.let { criteria.and(SubScanTaskDefinition::qualityRedLine.name).isEqualTo(qualityRedLine) }
unQuality?.let { criteria.and(SubScanTaskDefinition::qualityRedLine.name).nin(listOf(true, false)) }

val pageRequest = Pages.ofRequest(pageNumber, pageSize)
val query = Query(criteria)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import com.tencent.bkrepo.repository.pojo.metadata.packages.PackageMetadataSaveR
import com.tencent.bkrepo.analyst.event.SubtaskStatusChangedEvent
import com.tencent.bkrepo.analyst.model.SubScanTaskDefinition
import com.tencent.bkrepo.analyst.model.TPlanArtifactLatestSubScanTask
import com.tencent.bkrepo.analyst.utils.ScanPlanConverter
import com.tencent.bkrepo.analyst.pojo.request.ArtifactPlanRelationRequest
import com.tencent.bkrepo.analyst.service.ScanPlanService
import org.slf4j.LoggerFactory
import org.springframework.context.event.EventListener
import org.springframework.scheduling.annotation.Async
Expand All @@ -51,7 +52,8 @@ import org.springframework.stereotype.Component
@Component
class SubtaskStatusChangedEventListener(
private val metadataClient: MetadataClient,
private val packageMetadataClient: PackageMetadataClient
private val packageMetadataClient: PackageMetadataClient,
private val scanPlanService: ScanPlanService
) {
@Async
@EventListener(SubtaskStatusChangedEvent::class)
Expand All @@ -65,13 +67,7 @@ class SubtaskStatusChangedEventListener(

// 更新扫描状态元数据
val metadata = ArrayList<MetadataModel>(4)
metadata.add(
MetadataModel(
key = SCAN_STATUS,
value = ScanPlanConverter.convertToScanStatus(status).name,
system = true
)
)
addScanStatus(this, metadata)
// 更新质量规则元数据
qualityRedLine?.let {
// 未通过质量规则,判断是否触发禁用
Expand All @@ -80,24 +76,7 @@ class SubtaskStatusChangedEventListener(
}
metadata.add(MetadataModel(key = SubScanTaskDefinition::qualityRedLine.name, value = it, system = true))
}
if (repoType == RepositoryType.GENERIC.name) {
val request = MetadataSaveRequest(
projectId = projectId,
repoName = repoName,
fullPath = fullPath,
nodeMetadata = metadata
)
metadataClient.saveMetadata(request)
} else {
val request = PackageMetadataSaveRequest(
projectId = projectId,
repoName = repoName,
packageKey = packageKey!!,
version = version!!,
versionMetadata = metadata
)
packageMetadataClient.saveMetadata(request)
}
saveMetadata(this, metadata)
logger.info("update project[$projectId] repo[$repoName] fullPath[$fullPath] metadata[$metadata] success")
}
}
Expand Down Expand Up @@ -138,6 +117,52 @@ class SubtaskStatusChangedEventListener(
}
}

fun addScanStatus(subtask: TPlanArtifactLatestSubScanTask, metadata: ArrayList<MetadataModel>) {
with(subtask) {
//状态转换, 存到元数据中
val artifactPlanStatus = scanPlanService.artifactPlanStatus(
ArtifactPlanRelationRequest(
projectId = projectId,
repoName = repoName,
repoType = repoType,
packageKey = packageKey,
version = version,
fullPath = fullPath
)
) ?: return
metadata.add(
MetadataModel(
key = SCAN_STATUS,
value = artifactPlanStatus,
system = true
)
)
}
}

fun saveMetadata(subtask: TPlanArtifactLatestSubScanTask, metadata: ArrayList<MetadataModel>) {
with(subtask) {
if (repoType == RepositoryType.GENERIC.name) {
val request = MetadataSaveRequest(
projectId = projectId,
repoName = repoName,
fullPath = fullPath,
nodeMetadata = metadata
)
metadataClient.saveMetadata(request)
} else {
val request = PackageMetadataSaveRequest(
projectId = projectId,
repoName = repoName,
packageKey = packageKey!!,
version = version!!,
versionMetadata = metadata
)
packageMetadataClient.saveMetadata(request)
}
}
}

companion object {
private val logger = LoggerFactory.getLogger(SubtaskStatusChangedEventListener::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ interface ScanPlanService {
*/
fun artifactPlanList(request: ArtifactPlanRelationRequest): ArtifactPlanRelations

/**
* 获取制品扫描状态
*
* @param request 制品信息
*
* @return 制品扫描状态
*/
fun artifactPlanStatus(request: ArtifactPlanRelationRequest): String?

/**
* 获取扫描方案(license)最新一次扫描详情
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ class ScanPlanServiceImpl(
}
}

private fun artifactPlanRelation(request: ArtifactPlanRelationRequest): List<ArtifactPlanRelation> {
private fun artifactPlanRelation(
request: ArtifactPlanRelationRequest,
checkPermission: Boolean = true
): List<ArtifactPlanRelation> {
with(request) {
ScanParamUtil.checkParam(
repoType = RepositoryType.valueOf(repoType),
Expand All @@ -288,6 +291,8 @@ class ScanPlanServiceImpl(
fullPath = packageVersion?.contentPath ?: packageVersion?.manifestPath
fullPath ?: throw NotFoundException(CommonMessageCode.RESOURCE_NOT_FOUND, packageKey!!, version!!)
}
if (checkPermission)
permissionCheckHandler.checkNodePermission(projectId, repoName, fullPath!!, PermissionAction.READ)
val subtasks = planArtifactLatestSubScanTaskDao.findAll(projectId, repoName, fullPath!!)
val planIds = subtasks.mapNotNull { it.planId }
val scanPlanMap = scanPlanDao.findByIds(planIds, true).associateBy { it.id!! }
Expand All @@ -301,6 +306,16 @@ class ScanPlanServiceImpl(
}
}

override fun artifactPlanStatus(request: ArtifactPlanRelationRequest): String? {
val relations = artifactPlanRelation(request, false)

if (relations.isEmpty()) {
return null
}

return ScanPlanConverter.artifactStatus(relations.map { it.status })
}

private fun checkRunning(planId: String) {
if (scanTaskDao.existsByPlanIdAndStatus(planId, runningStatus)) {
throw ErrorCodeException(ScannerMessageCode.SCAN_PLAN_DELETE_FAILED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,11 @@ object ScanPlanConverter {
ScanStatus.INIT -> listOf(SubScanTaskStatus.CREATED, SubScanTaskStatus.PULLED)
ScanStatus.RUNNING -> listOf(SubScanTaskStatus.EXECUTING)
ScanStatus.STOP -> listOf(SubScanTaskStatus.STOPPED)
ScanStatus.FAILED -> listOf(SubScanTaskStatus.FAILED)
ScanStatus.FAILED -> listOf(
SubScanTaskStatus.FAILED,
SubScanTaskStatus.BLOCK_TIMEOUT,
SubScanTaskStatus.TIMEOUT
)
ScanStatus.SUCCESS,
ScanStatus.UN_QUALITY,
ScanStatus.QUALITY_PASS,
Expand Down

0 comments on commit 782538e

Please sign in to comment.