Skip to content

Commit

Permalink
feat: 脚本/作业模版标签设置优化 TencentBlueKing#2781
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Feb 22, 2024
1 parent 93b7064 commit 2da5b6c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public interface TagService {
boolean updateTagById(String username, TagDTO tag);

/**
* 批量创建不存在的标签
* 移除非法的标签
*/
List<TagDTO> createNewTagIfNotExist(List<TagDTO> tags, Long appId, String username);
List<TagDTO> removeInvalidTags(List<TagDTO> tags, Long appId, String username);

/**
* 标签通用查询
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private void saveScriptTags(Long appId, ScriptDTO script) {
private void saveScriptTags(String operator, Long appId, String scriptId, List<TagDTO> tags) {
List<TagDTO> newTags = tags;
if (tags != null && !tags.isEmpty()) {
newTags = tagService.createNewTagIfNotExist(tags, appId, operator);
newTags = tagService.removeInvalidTags(tags, appId, operator);
}

Integer resourceType = appId == (JobConstants.PUBLIC_APP_ID) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,18 @@ private void checkRequiredParam(TagDTO tag) {
}

@Override
public List<TagDTO> createNewTagIfNotExist(List<TagDTO> tags, Long appId, String username) {
List<Long> tagIdList = new ArrayList<>();
public List<TagDTO> removeInvalidTags(List<TagDTO> tags, Long appId, String username) {
Iterator<TagDTO> tagIterator = tags.iterator();
while (tagIterator.hasNext()) {
TagDTO tag = tagIterator.next();
if (tag.getId() == null || tag.getId() == 0) {
String tagName = tag.getName();
TagDTO tagDTO = new TagDTO();
tagDTO.setAppId(appId);
tagDTO.setName(tagName);
tagDTO.setCreator(username);
tagDTO.setLastModifyUser(username);
Long tagId = tagDAO.insertTag(tagDTO);

tag.setId(tagId);
// 移除没有 id 的标签
tagIterator.remove();
}
if (tagIdList.contains(tag.getId())) {
TagDTO existTag = getTagInfoById(appId, tag.getId());
if (existTag == null) {
// 移除不存在的标签
tagIterator.remove();
} else {
TagDTO existTag = getTagInfoById(appId, tag.getId());
if (existTag == null) {
throw new InvalidParamException(ErrorCode.ILLEGAL_PARAM_WITH_PARAM_NAME_AND_REASON,
new String[]{"tagId", String.format("tag (id=%s, app_id=%s) not exist", tag.getId(), appId)});
}
tagIdList.add(tag.getId());
}
}
return tags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ Long saveTaskTemplateForMigration(
);

/**
* 为模板创建标签
* 检查模板标签
*
* @param taskTemplateInfo 模版信息
*/
void createNewTagForTemplateIfNotExist(TaskTemplateInfoDTO taskTemplateInfo);
void checkTags(TaskTemplateInfoDTO taskTemplateInfo);

/**
* 更新模版步骤信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ private TaskTemplateInfoDTO saveOrUpdateTaskTemplate(TaskTemplateInfoDTO taskTem
if (!LockUtils.tryGetDistributedLock(lockKey, JobContextUtil.getRequestId(), 60_000)) {
throw new AbortedException(ErrorCode.TEMPLATE_LOCK_ACQUIRE_FAILED);
}
// 保存新增的标签并获取tagId
createNewTagForTemplateIfNotExist(taskTemplateInfo);

checkTags(taskTemplateInfo);

// 获取引用的非线上脚本
Map<String, Long> outdatedScriptMap = getOutdatedScriptMap(taskTemplateInfo.getStepList());
Expand Down Expand Up @@ -564,10 +564,10 @@ private boolean templateHasChange(TaskTemplateInfoDTO taskTemplateInfo) {
}

@Override
public void createNewTagForTemplateIfNotExist(TaskTemplateInfoDTO taskTemplateInfo) {
public void checkTags(TaskTemplateInfoDTO taskTemplateInfo) {
List<TagDTO> tags = taskTemplateInfo.getTags();
if (tags != null && !tags.isEmpty()) {
List<TagDTO> newTags = tagService.createNewTagIfNotExist(tags, taskTemplateInfo.getAppId(),
List<TagDTO> newTags = tagService.removeInvalidTags(tags, taskTemplateInfo.getAppId(),
taskTemplateInfo.getLastModifyUser());
taskTemplateInfo.setTags(newTags);
}
Expand Down Expand Up @@ -654,7 +654,7 @@ public TaskTemplateInfoDTO saveTaskTemplateBasicInfo(String username, TaskTempla
throw new NotFoundException(ErrorCode.TEMPLATE_NOT_EXIST);
}

createNewTagForTemplateIfNotExist(taskTemplateInfo);
checkTags(taskTemplateInfo);
updateTemplateTags(taskTemplateInfo);
if (!taskTemplateDAO.updateTaskTemplateById(taskTemplateInfo, false)) {
throw new InternalException(ErrorCode.UPDATE_TEMPLATE_FAILED);
Expand Down Expand Up @@ -749,7 +749,7 @@ public Long saveTaskTemplateForMigration(
if (taskTemplateByName != null) {
throw new AlreadyExistsException(ErrorCode.TEMPLATE_NAME_EXIST);
}
createNewTagForTemplateIfNotExist(taskTemplateInfo);
checkTags(taskTemplateInfo);

if (createTime != null && createTime > 0) {
taskTemplateInfo.setCreateTime(createTime);
Expand Down

0 comments on commit 2da5b6c

Please sign in to comment.