Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#1171 from yaoxuwan/issue_1160
Browse files Browse the repository at this point in the history
feat: 允许用户新增系统元数据 TencentBlueKing#1169
  • Loading branch information
owenlxu authored Sep 15, 2023
2 parents 8910261 + 8ca8e68 commit 1041319
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ data class RepositoryProperties(
@NestedConfigurationProperty
var job: RepoJobProperties = RepoJobProperties(),
@NestedConfigurationProperty
var repoStorageMapping: RepoStorageMapping = RepoStorageMapping()
var repoStorageMapping: RepoStorageMapping = RepoStorageMapping(),
var allowUserAddSystemMetadata: List<String> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@

package com.tencent.bkrepo.repository.service.metadata.impl

import com.tencent.bkrepo.common.api.constant.StringPool
import com.tencent.bkrepo.common.api.exception.ErrorCodeException
import com.tencent.bkrepo.common.api.message.CommonMessageCode
import com.tencent.bkrepo.common.artifact.exception.NodeNotFoundException
import com.tencent.bkrepo.common.artifact.message.ArtifactMessageCode
import com.tencent.bkrepo.common.artifact.path.PathUtils.normalizeFullPath
import com.tencent.bkrepo.common.artifact.util.ClusterUtils
import com.tencent.bkrepo.common.security.exception.PermissionException
import com.tencent.bkrepo.common.service.cluster.DefaultCondition
import com.tencent.bkrepo.common.service.util.SpringContextUtils.Companion.publishEvent
import com.tencent.bkrepo.repository.config.RepositoryProperties
import com.tencent.bkrepo.repository.dao.NodeDao
import com.tencent.bkrepo.repository.model.TMetadata
import com.tencent.bkrepo.repository.model.TNode
Expand All @@ -65,6 +68,7 @@ import org.springframework.transaction.annotation.Transactional
@Conditional(DefaultCondition::class)
class MetadataServiceImpl(
private val nodeDao: NodeDao,
private val repositoryProperties: RepositoryProperties
) : MetadataService {

override fun listMetadata(projectId: String, repoName: String, fullPath: String): Map<String, Any> {
Expand All @@ -83,7 +87,11 @@ class MetadataServiceImpl(
?: throw ErrorCodeException(ArtifactMessageCode.NODE_NOT_FOUND, fullPath)
ClusterUtils.checkContainsSrcCluster(node.clusterNames)
val oldMetadata = node.metadata ?: ArrayList()
val newMetadata = MetadataUtils.compatibleConvertAndCheck(metadata, nodeMetadata)
val newMetadata = MetadataUtils.compatibleConvertAndCheck(
metadata,
MetadataUtils.changeSystem(nodeMetadata, repositoryProperties.allowUserAddSystemMetadata)
)
checkIfUpdateSystemMetadata(oldMetadata, newMetadata)
node.metadata = MetadataUtils.merge(oldMetadata, newMetadata)

nodeDao.save(node)
Expand Down Expand Up @@ -133,6 +141,30 @@ class MetadataServiceImpl(
}
}

/**
* 检查是否有更新允许用户添加的系统元数据
*/
private fun checkIfUpdateSystemMetadata(
oldMetadata: MutableList<TMetadata>,
newMetadata: MutableList<TMetadata>
) {
val oldAllowUserAddSystemMetadata =
oldMetadata.map { it.key }.intersectIgnoreCase(repositoryProperties.allowUserAddSystemMetadata)
val newAllowUserAddSystemMetadata =
newMetadata.map { it.key }.intersectIgnoreCase(repositoryProperties.allowUserAddSystemMetadata)
val updateSystemMetadata = oldAllowUserAddSystemMetadata.intersect(newAllowUserAddSystemMetadata)
if (updateSystemMetadata.isNotEmpty()) {
throw ErrorCodeException(
CommonMessageCode.PARAMETER_INVALID,
updateSystemMetadata.joinToString(StringPool.COMMA)
)
}
}

private fun List<String>.intersectIgnoreCase(list: List<String>): List<String> {
return this.filter { k -> list.any { it.equals(k, true) } }
}

companion object {
private val logger = LoggerFactory.getLogger(MetadataServiceImpl::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package com.tencent.bkrepo.repository.service.metadata.impl.center

import com.tencent.bkrepo.common.service.cluster.CommitEdgeCenterCondition
import com.tencent.bkrepo.repository.config.RepositoryProperties
import com.tencent.bkrepo.repository.dao.NodeDao
import com.tencent.bkrepo.repository.service.metadata.impl.MetadataServiceImpl
import org.springframework.context.annotation.Conditional
Expand All @@ -36,7 +37,9 @@ import org.springframework.stereotype.Service
@Service
@Conditional(CommitEdgeCenterCondition::class)
class CommitEdgeCenterMetadataServiceImpl(
nodeDao: NodeDao
nodeDao: NodeDao,
repositoryProperties: RepositoryProperties
) : MetadataServiceImpl(
nodeDao
nodeDao,
repositoryProperties
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.tencent.bkrepo.common.service.cluster.ClusterProperties
import com.tencent.bkrepo.common.service.cluster.CommitEdgeEdgeCondition
import com.tencent.bkrepo.common.service.feign.FeignClientFactory
import com.tencent.bkrepo.repository.api.cluster.ClusterMetadataClient
import com.tencent.bkrepo.repository.config.RepositoryProperties
import com.tencent.bkrepo.repository.dao.NodeDao
import com.tencent.bkrepo.repository.pojo.metadata.MetadataDeleteRequest
import com.tencent.bkrepo.repository.pojo.metadata.MetadataSaveRequest
Expand All @@ -42,9 +43,11 @@ import org.springframework.stereotype.Service
@Conditional(CommitEdgeEdgeCondition::class)
class EdgeMetadataServiceImpl(
nodeDao: NodeDao,
repositoryProperties: RepositoryProperties,
clusterProperties: ClusterProperties
) : MetadataServiceImpl(
nodeDao
nodeDao,
repositoryProperties
) {

private val centerMetadataClient: ClusterMetadataClient by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ abstract class NodeBaseService(
sha256 = if (folder) null else sha256,
md5 = if (folder) null else md5,
nodeNum = null,
metadata = MetadataUtils.compatibleConvertAndCheck(metadata, nodeMetadata),
metadata = MetadataUtils.compatibleConvertAndCheck(
metadata,
MetadataUtils.changeSystem(nodeMetadata, repositoryProperties.allowUserAddSystemMetadata)
),
createdBy = createdBy ?: operator,
createdDate = createdDate ?: LocalDateTime.now(),
lastModifiedBy = createdBy ?: operator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ object MetadataUtils {
}
}

/**
* 将允许用户新增为系统元数据的元数据设置为System=true
*/
fun changeSystem(nodeMetadata: List<MetadataModel>?, allowUserAddSystemMetadata: List<String>) =
nodeMetadata?.map { m ->
if (allowUserAddSystemMetadata.any { it.equals(m.key, true) }) {
m.copy(system = true)
} else {
m
}
}?.toMutableList()

private fun convertAndCheck(metadataMap: Map<String, Any>?): MutableList<TMetadata> {
return metadataMap
?.filter { it.key.isNotBlank() }
Expand Down

0 comments on commit 1041319

Please sign in to comment.