-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: package自定义搜索支持版本名、元数据、checksum搜索 #1717
(cherry picked from commit f44cef4faca90b83052243c709d735104154de3e)
- Loading branch information
Showing
13 changed files
with
379 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...in/kotlin/com/tencent/bkrepo/repository/search/packages/VersionChecksumRuleInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. | ||
* | ||
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. | ||
* | ||
* A copy of the MIT License is included in this file. | ||
* | ||
* | ||
* Terms of the MIT License: | ||
* --------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.tencent.bkrepo.repository.search.packages | ||
|
||
import com.tencent.bkrepo.common.api.exception.ErrorCodeException | ||
import com.tencent.bkrepo.common.api.message.CommonMessageCode | ||
import com.tencent.bkrepo.common.artifact.pojo.RepositoryType | ||
import com.tencent.bkrepo.common.query.enums.OperationType | ||
import com.tencent.bkrepo.common.query.model.Rule | ||
import com.tencent.bkrepo.common.security.util.SecurityUtils | ||
import com.tencent.bkrepo.repository.dao.NodeDao | ||
import com.tencent.bkrepo.repository.dao.PackageVersionDao | ||
import com.tencent.bkrepo.repository.model.TNode | ||
import com.tencent.bkrepo.repository.model.TPackageVersion | ||
import com.tencent.bkrepo.repository.pojo.repo.RepoListOption | ||
import com.tencent.bkrepo.repository.service.repo.RepositoryService | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.data.mongodb.core.query.Query | ||
import org.springframework.data.mongodb.core.query.inValues | ||
import org.springframework.data.mongodb.core.query.isEqualTo | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* 版本Checksum规则拦截器, 查询包版本Checksum需要在嵌套查询规则列表中指定projectId和repoType条件,且均为EQ操作 | ||
*/ | ||
@Component | ||
class VersionChecksumRuleInterceptor( | ||
override val packageVersionDao: PackageVersionDao, | ||
private val nodeDao: NodeDao, | ||
private val repositoryService: RepositoryService | ||
) : VersionRuleInterceptor(packageVersionDao) { | ||
|
||
override fun match(rule: Rule): Boolean { | ||
return rule is Rule.QueryRule && rule.field in CHECKSUM_FIELDS | ||
} | ||
|
||
override fun getVersionCriteria(rule: Rule, context: PackageQueryContext): Criteria { | ||
with(rule as Rule.QueryRule) { | ||
if (operation != OperationType.EQ) { | ||
throw ErrorCodeException(CommonMessageCode.METHOD_NOT_ALLOWED, "$field only support EQ operation type.") | ||
} | ||
val projectId = context.findProjectId() | ||
val repoType = context.findRepoType().toUpperCase() | ||
val userId = SecurityUtils.getUserId() | ||
// 筛选有权限的仓库列表, docker和oci仓库互相兼容 | ||
val repoList = if (repoType in listOf(RepositoryType.DOCKER.name, RepositoryType.OCI.name)) { | ||
val dockerRepoList = repositoryService.listPermissionRepo( | ||
userId, projectId, RepoListOption(type = RepositoryType.DOCKER.name) | ||
) | ||
val ociRepoList = repositoryService.listPermissionRepo( | ||
userId, projectId, RepoListOption(type = RepositoryType.OCI.name) | ||
) | ||
dockerRepoList + ociRepoList | ||
} else | ||
repositoryService.listPermissionRepo(userId, projectId, RepoListOption(type = repoType)) | ||
// 从有权限的仓库列表查询符合checksum规则的节点 | ||
val nodeQuery = Query( | ||
Criteria.where(TNode::projectId.name).isEqualTo(projectId) | ||
.and(TNode::repoName.name).inValues(repoList.map { it.name }) | ||
.and(field).isEqualTo(value.toString()) | ||
) | ||
val fullPaths = queryRecords(nodeQuery) { q -> nodeDao.find(q) }.map { it.fullPath } | ||
// 转换为package_version查询条件 | ||
return if (repoType == RepositoryType.DOCKER.name || repoType == RepositoryType.OCI.name) { | ||
Criteria.where(TPackageVersion::manifestPath.name).inValues(fullPaths) | ||
} else { | ||
Criteria.where(TPackageVersion::artifactPath.name).inValues(fullPaths) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private val CHECKSUM_FIELDS = arrayOf( | ||
TNode::sha256.name, | ||
TNode::md5.name | ||
) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...in/kotlin/com/tencent/bkrepo/repository/search/packages/VersionMetadataRuleInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. | ||
* | ||
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. | ||
* | ||
* A copy of the MIT License is included in this file. | ||
* | ||
* | ||
* Terms of the MIT License: | ||
* --------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.tencent.bkrepo.repository.search.packages | ||
|
||
import com.tencent.bkrepo.common.query.model.Rule | ||
import com.tencent.bkrepo.repository.dao.PackageVersionDao | ||
import com.tencent.bkrepo.repository.search.common.MetadataRuleInterceptor | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* 版本元数据规则拦截器 | ||
*/ | ||
@Component | ||
class VersionMetadataRuleInterceptor( | ||
override val packageVersionDao: PackageVersionDao, | ||
private val metadataRuleInterceptor: MetadataRuleInterceptor | ||
) : VersionRuleInterceptor(packageVersionDao) { | ||
|
||
override fun match(rule: Rule) = metadataRuleInterceptor.match(rule) | ||
override fun getVersionCriteria(rule: Rule, context: PackageQueryContext) = | ||
metadataRuleInterceptor.intercept(rule, context) | ||
} |
79 changes: 79 additions & 0 deletions
79
...c/main/kotlin/com/tencent/bkrepo/repository/search/packages/VersionNameRuleInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. | ||
* | ||
* Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
* | ||
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. | ||
* | ||
* A copy of the MIT License is included in this file. | ||
* | ||
* | ||
* Terms of the MIT License: | ||
* --------------------------------------------------- | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.tencent.bkrepo.repository.search.packages | ||
|
||
import com.tencent.bkrepo.common.api.exception.ErrorCodeException | ||
import com.tencent.bkrepo.common.api.message.CommonMessageCode | ||
import com.tencent.bkrepo.common.query.enums.OperationType | ||
import com.tencent.bkrepo.common.query.model.Rule | ||
import com.tencent.bkrepo.repository.dao.PackageVersionDao | ||
import com.tencent.bkrepo.repository.model.TPackageVersion | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* 版本号规则拦截器 | ||
*/ | ||
@Component | ||
class VersionNameRuleInterceptor( | ||
override val packageVersionDao: PackageVersionDao | ||
) : VersionRuleInterceptor(packageVersionDao) { | ||
|
||
override fun match(rule: Rule): Boolean { | ||
return rule is Rule.QueryRule && rule.field == "version" | ||
} | ||
|
||
override fun getVersionCriteria(rule: Rule, context: PackageQueryContext): Criteria { | ||
with(rule as Rule.QueryRule) { | ||
val versionQueryRule = when (operation) { | ||
OperationType.IN -> | ||
rule.copy(field = TPackageVersion::name.name, value = (value as List<*>).map { it.toString() }) | ||
|
||
in SUPPORT_OPERATIONS -> rule.copy(field = TPackageVersion::name.name, value = value.toString()) | ||
else -> throw ErrorCodeException( | ||
CommonMessageCode.METHOD_NOT_ALLOWED, | ||
"$field only support ${SUPPORT_OPERATIONS.map { it.name }} operation type." | ||
) | ||
}.toFixed() | ||
return context.interpreter.resolveRule(versionQueryRule, context) | ||
} | ||
} | ||
|
||
companion object { | ||
private val SUPPORT_OPERATIONS = arrayOf( | ||
OperationType.EQ, | ||
OperationType.IN, | ||
OperationType.MATCH, | ||
OperationType.MATCH_I | ||
) | ||
} | ||
} |
Oops, something went wrong.