Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加项目元数据字段 #1055 #1239

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectMetadata> = emptyList(),
) : ProjectRequest
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ data class ProjectInfo(
@ApiModelProperty("上次修改者")
val lastModifiedBy: String,
@ApiModelProperty("上次修改日期")
val lastModifiedDate: String
val lastModifiedDate: String,
@ApiModelProperty("项目元数据")
val metadata: List<ProjectMetadata> = emptyList(),
)
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectMetadata> = emptyList(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ data class UserProjectCreateRequest(
@ApiModelProperty("描述")
val description: String,
@ApiModelProperty("创建本地权限")
val createPermission: Boolean = true
val createPermission: Boolean = true,
@ApiModelProperty("项目元数据")
val metadata: List<ProjectMetadata> = emptyList(),
) : ProjectRequest
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class UserProjectController(
displayName = displayName,
description = description,
operator = userId,
createPermission = createPermission
createPermission = createPermission,
metadata = metadata
)
}
projectService.createProject(createRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
* 项目模型
Expand All @@ -52,5 +53,6 @@ data class TProject(

var name: String,
var displayName: String,
var description: String
var description: String,
var metadata: List<ProjectMetadata> = emptyList(),
cnlkl marked this conversation as resolved.
Show resolved Hide resolved
)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`() {
Expand Down
Loading