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: 创建项目时,增加是否创建本地权限参数 #879 #882

Merged
merged 4 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -36,7 +36,8 @@ import com.tencent.bkrepo.common.artifact.event.base.EventType
*/
data class ProjectCreatedEvent(
override val projectId: String,
override val userId: String
override val userId: String,
val createPermission: Boolean
) : ArtifactEvent(
type = EventType.PROJECT_CREATED,
projectId = projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,13 @@ class EdgePullReplicaExecutor(
localProjectId: String,
localRepoName: String,
): RepositoryDetail {
val projectCreateRequest = ProjectCreateRequest(localProjectId, localProjectId, null, centerRepo.createdBy)
val projectCreateRequest = ProjectCreateRequest(
name = localProjectId,
displayName = localProjectId,
description = null,
createPermission = true,
operator = centerRepo.createdBy
)
try {
projectClient.createProject(projectCreateRequest)
} catch (e: RemoteErrorCodeException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ data class ProjectCreateRequest(
val displayName: String,
@ApiModelProperty("描述", required = true)
val description: String? = null,
@ApiModelProperty("创建权限", required = false)
val createPermission: Boolean = true,
@ApiModelProperty("操作用户", required = false)
val operator: String = SYSTEM_USER
) : ProjectRequest
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class ResourcePermissionListener(
*/
fun handle(event: ProjectCreatedEvent) {
with(event) {
if (!createPermission) {
return
}
if (isAuthedNormalUser(userId) && isNeedLocalPermission(projectId)) {
val projectManagerRoleId = roleResource.createProjectManage(projectId).data!!
userResource.addUserRole(userId, projectManagerRoleId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ object ProjectEventFactory {
with(request) {
return ProjectCreatedEvent(
projectId = name,
userId = operator
userId = operator,
createPermission = createPermission
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,63 +70,63 @@ class ProjectServiceTest @Autowired constructor(
@Test
@DisplayName("测试创建项目")
fun `test create project`() {
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
}

@Test
@DisplayName("测试创建同名项目")
fun `should throw exception when project exists`() {
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
val request = ProjectCreateRequest(UT_PROJECT_ID, UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
assertThrows<ErrorCodeException> { projectService.createProject(request) }
}

@Test
@DisplayName("测试非法项目名称")
fun `should throw exception with illegal name`() {
var request = ProjectCreateRequest("1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
var request = ProjectCreateRequest("1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest("11", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("11", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest("a".repeat(33), UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("a".repeat(33), UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest("test_1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("test_1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("test-1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("test-1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("_prebuild", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("_prebuild", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

request = ProjectCreateRequest("CODECC_a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest("CODECC_a1", UT_PROJECT_DISPLAY, UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
}

@Test
@DisplayName("测试非法项目显示名")
fun `should throw exception with illegal display name`() {
var request = ProjectCreateRequest(UT_PROJECT_ID, "", UT_PROJECT_DESC, UT_USER)
var request = ProjectCreateRequest(UT_PROJECT_ID, "", UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(33), UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(33), UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(1), UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(1), UT_PROJECT_DESC, true, UT_USER)
assertThrows<ErrorCodeException> { projectService.createProject(request) }

request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(32), UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "1".repeat(32), UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)

removeAllProject()
request = ProjectCreateRequest(UT_PROJECT_ID, "123-abc", UT_PROJECT_DESC, UT_USER)
request = ProjectCreateRequest(UT_PROJECT_ID, "123-abc", UT_PROJECT_DESC, true, UT_USER)
projectService.createProject(request)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ class RepositoryServiceTest @Autowired constructor(
fun beforeAll() {
initMock()
if (!projectService.checkExist(UT_PROJECT_ID)) {
val projectCreateRequest = ProjectCreateRequest(UT_PROJECT_ID, UT_REPO_NAME, UT_REPO_DISPLAY, UT_USER)
val projectCreateRequest = ProjectCreateRequest(
name = UT_PROJECT_ID,
displayName = UT_REPO_NAME,
description = UT_REPO_DISPLAY,
createPermission = true,
operator = UT_USER
)
projectService.createProject(projectCreateRequest)
}
val request = StorageCredentialsCreateRequest(UT_STORAGE_CREDENTIALS_KEY, storageCredentials, UT_REGION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,13 @@ open class ServiceBaseTest {
credentialsKey: String? = null
) {
if (!projectService.checkExist(UT_PROJECT_ID)) {
val projectCreateRequest = ProjectCreateRequest(UT_PROJECT_ID, UT_REPO_NAME, UT_REPO_DISPLAY, UT_USER)
val projectCreateRequest = ProjectCreateRequest(
name = UT_PROJECT_ID,
displayName = UT_REPO_NAME,
description = UT_REPO_DISPLAY,
createPermission = true,
operator = UT_USER
)
projectService.createProject(projectCreateRequest)
}
if (!repositoryService.checkExist(UT_PROJECT_ID, UT_REPO_NAME)) {
Expand All @@ -180,7 +186,7 @@ open class ServiceBaseTest {
projectService: ProjectService,
projectId: String = UT_PROJECT_ID
): ProjectInfo {
val projectCreateRequest = ProjectCreateRequest(projectId, UT_REPO_NAME, UT_REPO_DISPLAY, UT_USER)
val projectCreateRequest = ProjectCreateRequest(projectId, UT_REPO_NAME, UT_REPO_DISPLAY, true, UT_USER)
return projectService.createProject(projectCreateRequest)
}

Expand Down
Loading