From 3e51c06406c64cf58369a31a7fe3ffc07135649b Mon Sep 17 00:00:00 2001 From: kunlongli Date: Tue, 10 Oct 2023 14:32:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E5=85=83=E6=95=B0=E6=8D=AE=E5=AD=97=E6=AE=B5=20#1055?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pojo/project/ProjectCreateRequest.kt | 4 +- .../repository/pojo/project/ProjectInfo.kt | 4 +- .../pojo/project/ProjectMetadata.kt | 42 +++++++++++++++++++ .../pojo/project/ProjectUpdateRequest.kt | 4 +- .../pojo/project/UserProjectCreateRequest.kt | 4 +- .../controller/user/UserProjectController.kt | 3 +- .../bkrepo/repository/model/TProject.kt | 6 ++- .../service/repo/impl/ProjectServiceImpl.kt | 10 ++++- .../repository/service/ProjectServiceTest.kt | 40 ++++++++++++++++++ 9 files changed, 108 insertions(+), 9 deletions(-) create mode 100644 src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetadata.kt diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectCreateRequest.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectCreateRequest.kt index 17634e047d..16a0428fe1 100644 --- a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectCreateRequest.kt +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectCreateRequest.kt @@ -46,5 +46,7 @@ data class ProjectCreateRequest( @ApiModelProperty("创建权限", required = false) val createPermission: Boolean = true, @ApiModelProperty("操作用户", required = false) - val operator: String = SYSTEM_USER + val operator: String = SYSTEM_USER, + @ApiModelProperty("项目元数据", required = false) + val metadata: List = emptyList(), ) : ProjectRequest diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectInfo.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectInfo.kt index 37a891acce..d17fa1c5b1 100644 --- a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectInfo.kt +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectInfo.kt @@ -49,5 +49,7 @@ data class ProjectInfo( @ApiModelProperty("上次修改者") val lastModifiedBy: String, @ApiModelProperty("上次修改日期") - val lastModifiedDate: String + val lastModifiedDate: String, + @ApiModelProperty("项目元数据") + val metadata: List = emptyList(), ) diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetadata.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetadata.kt new file mode 100644 index 0000000000..50fba832a0 --- /dev/null +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectMetadata.kt @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. + * + * Copyright (C) 2023 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.pojo.project + +/** + * 项目元数据 + */ +data class ProjectMetadata( + /** + * 元数据键 + */ + val key: String, + /** + * 元数据值 + */ + var value: Any, +) diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectUpdateRequest.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectUpdateRequest.kt index cb39b36041..c8aec7b737 100644 --- a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectUpdateRequest.kt +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/ProjectUpdateRequest.kt @@ -39,5 +39,7 @@ data class ProjectUpdateRequest( @ApiModelProperty("显示名", required = false) val displayName: String? = null, @ApiModelProperty("描述", required = false) - val description: String? = null + val description: String? = null, + @ApiModelProperty("项目元数据", required = false) + val metadata: List = emptyList(), ) diff --git a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/UserProjectCreateRequest.kt b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/UserProjectCreateRequest.kt index 14ce100c09..6f6b3da1c3 100644 --- a/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/UserProjectCreateRequest.kt +++ b/src/backend/repository/api-repository/src/main/kotlin/com/tencent/bkrepo/repository/pojo/project/UserProjectCreateRequest.kt @@ -43,5 +43,7 @@ data class UserProjectCreateRequest( @ApiModelProperty("描述") val description: String, @ApiModelProperty("创建本地权限") - val createPermission: Boolean = true + val createPermission: Boolean = true, + @ApiModelProperty("项目元数据") + val metadata: List = emptyList(), ) : ProjectRequest diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt index 1268c90856..ad0dfb2b9c 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/controller/user/UserProjectController.kt @@ -75,7 +75,8 @@ class UserProjectController( displayName = displayName, description = description, operator = userId, - createPermission = createPermission + createPermission = createPermission, + metadata = metadata ) } projectService.createProject(createRequest) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProject.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProject.kt index 38b428f45d..658a7516ff 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProject.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/model/TProject.kt @@ -31,10 +31,11 @@ package com.tencent.bkrepo.repository.model -import java.time.LocalDateTime +import com.tencent.bkrepo.repository.pojo.project.ProjectMetadata import org.springframework.data.mongodb.core.index.CompoundIndex import org.springframework.data.mongodb.core.index.CompoundIndexes import org.springframework.data.mongodb.core.mapping.Document +import java.time.LocalDateTime /** * 项目模型 @@ -52,5 +53,6 @@ data class TProject( var name: String, var displayName: String, - var description: String + var description: String, + var metadata: List = emptyList(), ) diff --git a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt index 9352c7d953..4619c88035 100644 --- a/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt +++ b/src/backend/repository/biz-repository/src/main/kotlin/com/tencent/bkrepo/repository/service/repo/impl/ProjectServiceImpl.kt @@ -182,7 +182,8 @@ class ProjectServiceImpl( createdBy = operator, createdDate = LocalDateTime.now(), lastModifiedBy = operator, - lastModifiedDate = LocalDateTime.now() + lastModifiedDate = LocalDateTime.now(), + metadata = metadata, ) return try { projectDao.insert(project) @@ -223,6 +224,10 @@ class ProjectServiceImpl( request.displayName?.let { this.set(TProject::displayName.name, it) } request.description?.let { this.set(TProject::description.name, it) } } + if (request.metadata.isNotEmpty()) { + // 直接使用request的metadata,不存在于request的metadata会被删除,存在的会被覆盖 + update.set(TProject::metadata.name, request.metadata) + } val updateResult = projectDao.updateFirst(query, update) return if (updateResult.modifiedCount == 1L) { logger.info("Update project [$name] success.") @@ -262,7 +267,8 @@ class ProjectServiceImpl( createdBy = it.createdBy, createdDate = it.createdDate.format(DateTimeFormatter.ISO_DATE_TIME), lastModifiedBy = it.lastModifiedBy, - lastModifiedDate = it.lastModifiedDate.format(DateTimeFormatter.ISO_DATE_TIME) + lastModifiedDate = it.lastModifiedDate.format(DateTimeFormatter.ISO_DATE_TIME), + metadata = it.metadata ) } } diff --git a/src/backend/repository/biz-repository/src/test/kotlin/com/tencent/bkrepo/repository/service/ProjectServiceTest.kt b/src/backend/repository/biz-repository/src/test/kotlin/com/tencent/bkrepo/repository/service/ProjectServiceTest.kt index 2950218312..97123e6551 100644 --- a/src/backend/repository/biz-repository/src/test/kotlin/com/tencent/bkrepo/repository/service/ProjectServiceTest.kt +++ b/src/backend/repository/biz-repository/src/test/kotlin/com/tencent/bkrepo/repository/service/ProjectServiceTest.kt @@ -38,7 +38,10 @@ import com.tencent.bkrepo.repository.UT_PROJECT_ID import com.tencent.bkrepo.repository.UT_USER import com.tencent.bkrepo.repository.dao.ProjectDao import com.tencent.bkrepo.repository.pojo.project.ProjectCreateRequest +import com.tencent.bkrepo.repository.pojo.project.ProjectMetadata +import com.tencent.bkrepo.repository.pojo.project.ProjectUpdateRequest import com.tencent.bkrepo.repository.service.repo.ProjectService +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName @@ -74,6 +77,43 @@ class ProjectServiceTest @Autowired constructor( projectService.createProject(request) } + @Test + @DisplayName("测试更新项目") + fun `test update project`() { + // create project + val metadata = ProjectMetadata(key = "k", value = "v") + val request = ProjectCreateRequest( + UT_PROJECT_ID, + UT_PROJECT_DISPLAY, + UT_PROJECT_DESC, + true, + UT_USER, + listOf(metadata) + ) + val projectInfo = projectService.createProject(request) + Assertions.assertEquals(UT_PROJECT_ID, projectInfo.name) + Assertions.assertEquals(1, projectInfo.metadata.size) + + // update project + val newDisplayName = "$UT_PROJECT_DISPLAY new" + val newDes = "$UT_PROJECT_DESC new" + val newMetadata = listOf(ProjectMetadata(key = "newKey", value = "newVal")) + val updateRequest = ProjectUpdateRequest( + displayName = newDisplayName, + description = newDes, + metadata = newMetadata, + ) + projectService.updateProject(UT_PROJECT_ID, updateRequest) + val newProject = projectService.getProjectInfo(UT_PROJECT_ID) + Assertions.assertNotNull(newProject) + Assertions.assertEquals(newDisplayName, newProject!!.displayName) + Assertions.assertEquals(newDes, newProject.description) + Assertions.assertEquals(newMetadata.size, newProject.metadata.size) + for(i in newMetadata.indices) { + Assertions.assertEquals(newMetadata[i], newProject.metadata[i]) + } + } + @Test @DisplayName("测试创建同名项目") fun `should throw exception when project exists`() {