From 628fa96122cfe7e87900b803abd1162e29136c62 Mon Sep 17 00:00:00 2001 From: yjieliang Date: Fri, 11 Aug 2023 14:42:30 +0800 Subject: [PATCH 1/4] =?UTF-8?q?pref:=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=BB=84/=E6=A0=87=E7=AD=BE=E9=87=8D=E5=90=8D=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=8A=A5=E9=94=99=E6=8F=90=E7=A4=BA=20#9312?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/process/constant/ProcessMessageCode.kt | 1 + .../process/service/label/PipelineGroupService.kt | 14 ++++++++++---- .../i18n/process/message_en_US.properties | 1 + .../i18n/process/message_zh_CN.properties | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt b/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt index b036f747d35..21bf85e45ab 100644 --- a/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt +++ b/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt @@ -279,6 +279,7 @@ object ProcessMessageCode { const val MAXIMUM_NUMBER_CONCURRENCY_ILLEGAL = "2101178" // 最大并发数量非法 const val PIPELINE_BUILD_HAS_ENDED_CANNOT_BE_CANCELED = "2101179" // 流水线: 流水线构建已结束,不能取消 const val GET_PIPELINE_ATOM_INFO_NO_PERMISSION = "2101180" // 无权访问插件{0}的流水线信息,请联系组件管理员 + const val GROUP_IS_EXIST = "2101181" // 分组({0})已存在/group ({0}) is already exist const val BK_SUCCESSFULLY_DISTRIBUTED = "bkSuccessfullyDistributed" // 跨项目构件分发成功,共分发了{0}个文件 const val BK_SUCCESSFULLY_FAILED = "bkSuccessfullyFailed" // 跨项目构件分发失败, diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt index c210effe1d7..cf68951d737 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt @@ -153,10 +153,13 @@ class PipelineGroupService @Autowired constructor( userId = userId, id = id ) - } catch (t: DataAccessException) { + } catch (t: Throwable) { if (t.cause is SQLIntegrityConstraintViolationException) { logger.warn("Fail to create the group $pipelineGroup by userId $userId") - throw OperationException("The group is already exist") + throw ErrorCodeException( + errorCode = GROUP_IS_EXIST, + params = arrayOf("${pipelineGroup.projectId}-${pipelineGroup.name}") + ) } else throw t } return true @@ -171,10 +174,13 @@ class PipelineGroupService @Autowired constructor( name = pipelineGroup.name, userId = userId ) - } catch (t: DataAccessException) { + } catch (t: Throwable) { if (t.cause is SQLIntegrityConstraintViolationException) { logger.warn("Fail to create the group $pipelineGroup by userId $userId") - throw OperationException("The group is already exist") + throw ErrorCodeException( + errorCode = GROUP_IS_EXIST, + params = arrayOf("${pipelineGroup.projectId}-${pipelineGroup.name}") + ) } else throw t } } diff --git a/support-files/i18n/process/message_en_US.properties b/support-files/i18n/process/message_en_US.properties index 95e46f05f6e..32b5e0b04c6 100644 --- a/support-files/i18n/process/message_en_US.properties +++ b/support-files/i18n/process/message_en_US.properties @@ -178,6 +178,7 @@ 2101178=Illegal maximum number of concurrency 2101179=Pipeline: pipeline build has ended and cannot be canceled 2101180=You do not have access to the pipeline information for plugin {0}. Please contact the component administrator. +2101181=group ({0}) is already exist ATOM_POST_EXECUTE_TIP=###Tip:this is the post-action hooked by [step{0}]{1}### BK_CI_BUILD_ID=Currently build ID BK_CI_BUILD_JOB_ID=Pipelined JOB ID diff --git a/support-files/i18n/process/message_zh_CN.properties b/support-files/i18n/process/message_zh_CN.properties index edfeace52c8..e17685c783b 100644 --- a/support-files/i18n/process/message_zh_CN.properties +++ b/support-files/i18n/process/message_zh_CN.properties @@ -178,6 +178,7 @@ 2101178=最大并发数量非法 2101179=流水线: 流水线构建已结束,不能取消 2101180=无权访问插件{0}的流水线信息,请联系组件管理员 +2101181=分组({0})已存在 ATOM_POST_EXECUTE_TIP=###Tip:this is the post-action hooked by [step{0}]{1}### BK_CI_BUILD_ID=当前构建ID BK_CI_BUILD_JOB_ID=流水线JOB ID From 19946cf7edc56e30a44f3784780152c90bbf5f1b Mon Sep 17 00:00:00 2001 From: yjieliang Date: Fri, 11 Aug 2023 14:50:03 +0800 Subject: [PATCH 2/4] =?UTF-8?q?pref:=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=BB=84/=E6=A0=87=E7=AD=BE=E9=87=8D=E5=90=8D=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=8A=A5=E9=94=99=E6=8F=90=E7=A4=BA=20#9312?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../process/constant/ProcessMessageCode.kt | 1 + .../service/label/PipelineGroupService.kt | 21 ++++++++++++------- .../i18n/process/message_en_US.properties | 1 + .../i18n/process/message_zh_CN.properties | 1 + 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt b/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt index 21bf85e45ab..f4a404cb323 100644 --- a/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt +++ b/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/constant/ProcessMessageCode.kt @@ -280,6 +280,7 @@ object ProcessMessageCode { const val PIPELINE_BUILD_HAS_ENDED_CANNOT_BE_CANCELED = "2101179" // 流水线: 流水线构建已结束,不能取消 const val GET_PIPELINE_ATOM_INFO_NO_PERMISSION = "2101180" // 无权访问插件{0}的流水线信息,请联系组件管理员 const val GROUP_IS_EXIST = "2101181" // 分组({0})已存在/group ({0}) is already exist + const val GROUP_LABEL_IS_EXIST = "2101182" // 分组标签({0})已存在/group label ({0}) is already exist const val BK_SUCCESSFULLY_DISTRIBUTED = "bkSuccessfullyDistributed" // 跨项目构件分发成功,共分发了{0}个文件 const val BK_SUCCESSFULLY_FAILED = "bkSuccessfullyFailed" // 跨项目构件分发失败, diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt index cf68951d737..badca747581 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt @@ -41,6 +41,8 @@ import com.tencent.devops.model.process.tables.records.TPipelineLabelRecord import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_GROUP_COUNT_EXCEEDS_LIMIT import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_LABEL_COUNT_EXCEEDS_LIMIT import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_LABEL_NAME_TOO_LONG +import com.tencent.devops.process.constant.ProcessMessageCode.GROUP_IS_EXIST +import com.tencent.devops.process.constant.ProcessMessageCode.GROUP_LABEL_IS_EXIST import com.tencent.devops.process.dao.PipelineFavorDao import com.tencent.devops.process.dao.label.PipelineGroupDao import com.tencent.devops.process.dao.label.PipelineLabelDao @@ -55,16 +57,15 @@ import com.tencent.devops.process.pojo.classify.PipelineLabelCreate import com.tencent.devops.process.pojo.classify.PipelineLabelUpdate import com.tencent.devops.process.service.measure.MeasureEventDispatcher import com.tencent.devops.project.api.service.ServiceAllocIdResource +import java.sql.SQLIntegrityConstraintViolationException +import java.time.LocalDateTime import org.jooq.DSLContext import org.jooq.Result import org.jooq.impl.DSL import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired -import org.springframework.dao.DataAccessException import org.springframework.dao.DuplicateKeyException import org.springframework.stereotype.Service -import java.sql.SQLIntegrityConstraintViolationException -import java.time.LocalDateTime @Suppress("ALL") @Service @@ -234,9 +235,12 @@ class PipelineGroupService @Autowired constructor( userId = userId, id = id ) - } catch (t: DuplicateKeyException) { + } catch (t: Throwable) { logger.warn("Fail to add the label $pipelineLabel by userId $userId") - throw OperationException("The label is already exist") + throw ErrorCodeException( + errorCode = GROUP_LABEL_IS_EXIST, + params = arrayOf("${pipelineLabel.groupId}-${pipelineLabel.name}") + ) } return true @@ -304,9 +308,12 @@ class PipelineGroupService @Autowired constructor( logger.info("LableChangeMetricsBroadCastEvent: updateLabel $projectId|${decode(pipelineLabel.id)}") } return result - } catch (t: DuplicateKeyException) { + } catch (t: Throwable) { logger.warn("Fail to update the label $pipelineLabel by userId $userId") - throw OperationException("The label is already exist") + throw ErrorCodeException( + errorCode = GROUP_LABEL_IS_EXIST, + params = arrayOf("${pipelineLabel.groupId}-${pipelineLabel.name}") + ) } } diff --git a/support-files/i18n/process/message_en_US.properties b/support-files/i18n/process/message_en_US.properties index 32b5e0b04c6..7d5c928eea0 100644 --- a/support-files/i18n/process/message_en_US.properties +++ b/support-files/i18n/process/message_en_US.properties @@ -179,6 +179,7 @@ 2101179=Pipeline: pipeline build has ended and cannot be canceled 2101180=You do not have access to the pipeline information for plugin {0}. Please contact the component administrator. 2101181=group ({0}) is already exist +2101182=group label ({0}) is already exist ATOM_POST_EXECUTE_TIP=###Tip:this is the post-action hooked by [step{0}]{1}### BK_CI_BUILD_ID=Currently build ID BK_CI_BUILD_JOB_ID=Pipelined JOB ID diff --git a/support-files/i18n/process/message_zh_CN.properties b/support-files/i18n/process/message_zh_CN.properties index e17685c783b..ad6ca666b3b 100644 --- a/support-files/i18n/process/message_zh_CN.properties +++ b/support-files/i18n/process/message_zh_CN.properties @@ -179,6 +179,7 @@ 2101179=流水线: 流水线构建已结束,不能取消 2101180=无权访问插件{0}的流水线信息,请联系组件管理员 2101181=分组({0})已存在 +2101182=分组标签({0})已存在 ATOM_POST_EXECUTE_TIP=###Tip:this is the post-action hooked by [step{0}]{1}### BK_CI_BUILD_ID=当前构建ID BK_CI_BUILD_JOB_ID=流水线JOB ID From 2e40d169cfcc02fbf99bdfa892a430f863047d2f Mon Sep 17 00:00:00 2001 From: yjieliang Date: Fri, 11 Aug 2023 15:25:01 +0800 Subject: [PATCH 3/4] =?UTF-8?q?pref:=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=BB=84/=E6=A0=87=E7=AD=BE=E9=87=8D=E5=90=8D=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=8A=A5=E9=94=99=E6=8F=90=E7=A4=BA=20#9312?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../process/dao/label/PipelineGroupDao.kt | 15 +- .../process/dao/label/PipelineLabelDao.kt | 15 +- .../service/label/PipelineGroupService.kt | 213 +++++++++--------- 3 files changed, 137 insertions(+), 106 deletions(-) diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineGroupDao.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineGroupDao.kt index a6c1792532c..7ad7adacfce 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineGroupDao.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineGroupDao.kt @@ -29,11 +29,11 @@ package com.tencent.devops.process.dao.label import com.tencent.devops.model.process.tables.TPipelineGroup import com.tencent.devops.model.process.tables.records.TPipelineGroupRecord +import java.time.LocalDateTime import org.jooq.DSLContext import org.jooq.Result import org.slf4j.LoggerFactory import org.springframework.stereotype.Repository -import java.time.LocalDateTime /** * 用户分组管理 @@ -129,6 +129,19 @@ class PipelineGroupDao { } } + fun countByName( + dslContext: DSLContext, + projectId: String, + name: String + ): Long { + with(TPipelineGroup.T_PIPELINE_GROUP) { + return dslContext.selectCount().from(this) + .where(PROJECT_ID.eq(projectId)) + .and(NAME.eq(name)) + .fetchOne(0, Long::class.java)!! + } + } + fun get(dslContext: DSLContext, id: Long): TPipelineGroupRecord? { with(TPipelineGroup.T_PIPELINE_GROUP) { return dslContext.selectFrom(this) diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineLabelDao.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineLabelDao.kt index 36075dc1254..535c3224111 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineLabelDao.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/dao/label/PipelineLabelDao.kt @@ -29,11 +29,11 @@ package com.tencent.devops.process.dao.label import com.tencent.devops.model.process.tables.TPipelineLabel import com.tencent.devops.model.process.tables.records.TPipelineLabelRecord +import java.time.LocalDateTime import org.jooq.DSLContext import org.jooq.Result import org.slf4j.LoggerFactory import org.springframework.stereotype.Repository -import java.time.LocalDateTime /** * 流水线标签 @@ -136,6 +136,19 @@ class PipelineLabelDao { } } + fun countByGroupName( + dslContext: DSLContext, + projectId: String, + groupId: Long, + name: String + ): Long { + with(TPipelineLabel.T_PIPELINE_LABEL) { + return dslContext.selectCount().from(this) + .where(GROUP_ID.eq(groupId).and(PROJECT_ID.eq(projectId)).and(NAME.eq(name))) + .fetchOne(0, Long::class.java)!! + } + } + fun getByGroupIds( dslContext: DSLContext, projectId: String, diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt index badca747581..f111e8c91dc 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt @@ -38,8 +38,8 @@ import com.tencent.devops.common.event.pojo.measure.PipelineLabelRelateInfo import com.tencent.devops.model.process.tables.records.TPipelineFavorRecord import com.tencent.devops.model.process.tables.records.TPipelineGroupRecord import com.tencent.devops.model.process.tables.records.TPipelineLabelRecord +import com.tencent.devops.process.constant.ProcessMessageCode import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_GROUP_COUNT_EXCEEDS_LIMIT -import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_LABEL_COUNT_EXCEEDS_LIMIT import com.tencent.devops.process.constant.ProcessMessageCode.ERROR_LABEL_NAME_TOO_LONG import com.tencent.devops.process.constant.ProcessMessageCode.GROUP_IS_EXIST import com.tencent.devops.process.constant.ProcessMessageCode.GROUP_LABEL_IS_EXIST @@ -57,7 +57,6 @@ import com.tencent.devops.process.pojo.classify.PipelineLabelCreate import com.tencent.devops.process.pojo.classify.PipelineLabelUpdate import com.tencent.devops.process.service.measure.MeasureEventDispatcher import com.tencent.devops.project.api.service.ServiceAllocIdResource -import java.sql.SQLIntegrityConstraintViolationException import java.time.LocalDateTime import org.jooq.DSLContext import org.jooq.Result @@ -138,52 +137,48 @@ class PipelineGroupService @Autowired constructor( } fun addGroup(userId: String, pipelineGroup: PipelineGroupCreate): Boolean { - try { - val groupCount = pipelineGroupDao.count(dslContext = dslContext, projectId = pipelineGroup.projectId) - if (groupCount >= MAX_GROUP_UNDER_PROJECT) { - throw ErrorCodeException( - errorCode = ERROR_GROUP_COUNT_EXCEEDS_LIMIT, - defaultMessage = "At most $MAX_GROUP_UNDER_PROJECT label groups under a project" - ) - } - val id = client.get(ServiceAllocIdResource::class).generateSegmentId("PIPELINE_GROUP").data - pipelineGroupDao.create( - dslContext = dslContext, - projectId = pipelineGroup.projectId, - name = pipelineGroup.name, - userId = userId, - id = id + val count = pipelineGroupDao.countByName(dslContext, pipelineGroup.projectId, pipelineGroup.name) + if (count >= 1 ) { + logger.warn("Fail to create the group $pipelineGroup by userId $userId") + throw ErrorCodeException( + errorCode = GROUP_IS_EXIST, + params = arrayOf("${pipelineGroup.projectId}-${pipelineGroup.name}") ) - } catch (t: Throwable) { - if (t.cause is SQLIntegrityConstraintViolationException) { - logger.warn("Fail to create the group $pipelineGroup by userId $userId") - throw ErrorCodeException( - errorCode = GROUP_IS_EXIST, - params = arrayOf("${pipelineGroup.projectId}-${pipelineGroup.name}") - ) - } else throw t } + val groupCount = pipelineGroupDao.count(dslContext = dslContext, projectId = pipelineGroup.projectId) + if (groupCount >= MAX_GROUP_UNDER_PROJECT) { + throw ErrorCodeException( + errorCode = ERROR_GROUP_COUNT_EXCEEDS_LIMIT, + defaultMessage = "At most $MAX_GROUP_UNDER_PROJECT label groups under a project" + ) + } + val id = client.get(ServiceAllocIdResource::class).generateSegmentId("PIPELINE_GROUP").data + pipelineGroupDao.create( + dslContext = dslContext, + projectId = pipelineGroup.projectId, + name = pipelineGroup.name, + userId = userId, + id = id + ) return true } fun updateGroup(userId: String, pipelineGroup: PipelineGroupUpdate): Boolean { - try { - return pipelineGroupDao.update( - dslContext = dslContext, - projectId = pipelineGroup.projectId, - groupId = decode(pipelineGroup.id), - name = pipelineGroup.name, - userId = userId + val count = pipelineGroupDao.countByName(dslContext, pipelineGroup.projectId, pipelineGroup.name) + if (count >= 1 ) { + logger.warn("Fail to create the group $pipelineGroup by userId $userId") + throw ErrorCodeException( + errorCode = GROUP_IS_EXIST, + params = arrayOf("${pipelineGroup.projectId}-${pipelineGroup.name}") ) - } catch (t: Throwable) { - if (t.cause is SQLIntegrityConstraintViolationException) { - logger.warn("Fail to create the group $pipelineGroup by userId $userId") - throw ErrorCodeException( - errorCode = GROUP_IS_EXIST, - params = arrayOf("${pipelineGroup.projectId}-${pipelineGroup.name}") - ) - } else throw t } + return pipelineGroupDao.update( + dslContext = dslContext, + projectId = pipelineGroup.projectId, + groupId = decode(pipelineGroup.id), + name = pipelineGroup.name, + userId = userId + ) } fun deleteGroup(userId: String, projectId: String, groupId: String): Boolean { @@ -207,42 +202,46 @@ class PipelineGroupService @Autowired constructor( } fun addLabel(userId: String, projectId: String, pipelineLabel: PipelineLabelCreate): Boolean { - try { - val groupId = decode(pipelineLabel.groupId) - val labelCount = pipelineLabelDao.countByGroupId( - dslContext = dslContext, - projectId = projectId, - groupId = groupId - ) - if (labelCount >= MAX_LABEL_UNDER_GROUP) { - throw ErrorCodeException( - errorCode = ERROR_LABEL_COUNT_EXCEEDS_LIMIT, - defaultMessage = "No more than $MAX_LABEL_UNDER_GROUP labels under a label group" - ) - } - if (pipelineLabel.name.length > MAX_LABEL_NAME_LENGTH) { - throw ErrorCodeException( - errorCode = ERROR_LABEL_NAME_TOO_LONG, - defaultMessage = "label name cannot exceed $MAX_LABEL_NAME_LENGTH characters" - ) - } - val id = client.get(ServiceAllocIdResource::class).generateSegmentId("PIPELINE_LABEL").data - pipelineLabelDao.create( - dslContext = dslContext, - projectId = projectId, - groupId = groupId, - name = pipelineLabel.name, - userId = userId, - id = id - ) - } catch (t: Throwable) { + val groupId = decode(pipelineLabel.groupId) + val labelNameCount = pipelineLabelDao.countByGroupName( + dslContext = dslContext, + projectId = projectId, + groupId = groupId, + name = pipelineLabel.name + ) + if (labelNameCount >= 1) { logger.warn("Fail to add the label $pipelineLabel by userId $userId") throw ErrorCodeException( errorCode = GROUP_LABEL_IS_EXIST, params = arrayOf("${pipelineLabel.groupId}-${pipelineLabel.name}") ) } - + val labelCount = pipelineLabelDao.countByGroupId( + dslContext = dslContext, + projectId = projectId, + groupId = groupId + ) + if (labelCount >= MAX_LABEL_UNDER_GROUP) { + throw ErrorCodeException( + errorCode = ProcessMessageCode.ERROR_LABEL_COUNT_EXCEEDS_LIMIT, + defaultMessage = "No more than $MAX_LABEL_UNDER_GROUP labels under a label group" + ) + } + if (pipelineLabel.name.length > MAX_LABEL_NAME_LENGTH) { + throw ErrorCodeException( + errorCode = ERROR_LABEL_NAME_TOO_LONG, + defaultMessage = "label name cannot exceed $MAX_LABEL_NAME_LENGTH characters" + ) + } + val id = client.get(ServiceAllocIdResource::class).generateSegmentId("PIPELINE_LABEL").data + pipelineLabelDao.create( + dslContext = dslContext, + projectId = projectId, + groupId = groupId, + name = pipelineLabel.name, + userId = userId, + id = id + ) return true } @@ -275,46 +274,52 @@ class PipelineGroupService @Autowired constructor( } fun updateLabel(userId: String, projectId: String, pipelineLabel: PipelineLabelUpdate): Boolean { - try { - if (pipelineLabel.name.length > MAX_LABEL_NAME_LENGTH) { - throw ErrorCodeException( - errorCode = ERROR_LABEL_NAME_TOO_LONG, - defaultMessage = "label name cannot exceed $MAX_LABEL_NAME_LENGTH characters" - ) - } - val result = pipelineLabelDao.update( - dslContext = dslContext, - projectId = projectId, - labelId = decode(pipelineLabel.id), - name = pipelineLabel.name, - userId = userId + val groupId = decode(pipelineLabel.groupId) + val labelNameCount = pipelineLabelDao.countByGroupName( + dslContext = dslContext, + projectId = projectId, + groupId = groupId, + name = pipelineLabel.name + ) + if (labelNameCount >= 1) { + logger.warn("Fail to add the label $pipelineLabel by userId $userId") + throw ErrorCodeException( + errorCode = GROUP_LABEL_IS_EXIST, + params = arrayOf("${pipelineLabel.groupId}-${pipelineLabel.name}") ) - if (result) { - measureEventDispatcher.dispatch( - LabelChangeMetricsBroadCastEvent( - projectId = projectId, - userId = userId, - type = PipelineLabelChangeTypeEnum.UPDATE, - statisticsTime = LocalDateTime.now(), - pipelineLabelRelateInfos = listOf( - PipelineLabelRelateInfo( - projectId = projectId, - labelId = decode(pipelineLabel.id), - name = pipelineLabel.name - ) + } + if (pipelineLabel.name.length > MAX_LABEL_NAME_LENGTH) { + throw ErrorCodeException( + errorCode = ERROR_LABEL_NAME_TOO_LONG, + defaultMessage = "label name cannot exceed $MAX_LABEL_NAME_LENGTH characters" + ) + } + val result = pipelineLabelDao.update( + dslContext = dslContext, + projectId = projectId, + labelId = decode(pipelineLabel.id), + name = pipelineLabel.name, + userId = userId + ) + if (result) { + measureEventDispatcher.dispatch( + LabelChangeMetricsBroadCastEvent( + projectId = projectId, + userId = userId, + type = PipelineLabelChangeTypeEnum.UPDATE, + statisticsTime = LocalDateTime.now(), + pipelineLabelRelateInfos = listOf( + PipelineLabelRelateInfo( + projectId = projectId, + labelId = decode(pipelineLabel.id), + name = pipelineLabel.name ) ) ) - logger.info("LableChangeMetricsBroadCastEvent: updateLabel $projectId|${decode(pipelineLabel.id)}") - } - return result - } catch (t: Throwable) { - logger.warn("Fail to update the label $pipelineLabel by userId $userId") - throw ErrorCodeException( - errorCode = GROUP_LABEL_IS_EXIST, - params = arrayOf("${pipelineLabel.groupId}-${pipelineLabel.name}") ) + logger.info("LableChangeMetricsBroadCastEvent: updateLabel $projectId|${decode(pipelineLabel.id)}") } + return result } fun deletePipelineLabel(userId: String, projectId: String, pipelineId: String) { From 2ce1e1433414d46d6b2eb7a98301cb1a97c09fab Mon Sep 17 00:00:00 2001 From: yjieliang Date: Fri, 11 Aug 2023 15:31:54 +0800 Subject: [PATCH 4/4] =?UTF-8?q?pref:=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E7=BB=84/=E6=A0=87=E7=AD=BE=E9=87=8D=E5=90=8D=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=8A=A5=E9=94=99=E6=8F=90=E7=A4=BA=20#9312?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/process/service/label/PipelineGroupService.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt index f111e8c91dc..8e2ccccbafb 100644 --- a/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt +++ b/src/backend/ci/core/process/biz-base/src/main/kotlin/com/tencent/devops/process/service/label/PipelineGroupService.kt @@ -138,7 +138,7 @@ class PipelineGroupService @Autowired constructor( fun addGroup(userId: String, pipelineGroup: PipelineGroupCreate): Boolean { val count = pipelineGroupDao.countByName(dslContext, pipelineGroup.projectId, pipelineGroup.name) - if (count >= 1 ) { + if (count >= 1) { logger.warn("Fail to create the group $pipelineGroup by userId $userId") throw ErrorCodeException( errorCode = GROUP_IS_EXIST, @@ -165,7 +165,7 @@ class PipelineGroupService @Autowired constructor( fun updateGroup(userId: String, pipelineGroup: PipelineGroupUpdate): Boolean { val count = pipelineGroupDao.countByName(dslContext, pipelineGroup.projectId, pipelineGroup.name) - if (count >= 1 ) { + if (count >= 1) { logger.warn("Fail to create the group $pipelineGroup by userId $userId") throw ErrorCodeException( errorCode = GROUP_IS_EXIST,