From 6314e8d7eb58cd52e2a7bd3b5ffb9112b0b69577 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Tue, 3 Sep 2024 11:42:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E6=B5=81=E6=B0=B4=E7=BA=BF=E5=87=BA=E7=8E=B0=E7=9A=84=E5=90=84?= =?UTF-8?q?=E7=A7=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/certd/pipeline/crud.tsx | 67 ++++++++++++++++--- .../views/certd/pipeline/pipeline/index.vue | 3 +- .../pipeline/service/pipeline-service.ts | 30 ++++++--- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/packages/ui/certd-client/src/views/certd/pipeline/crud.tsx b/packages/ui/certd-client/src/views/certd/pipeline/crud.tsx index 6488839e4..bfd1df90b 100644 --- a/packages/ui/certd-client/src/views/certd/pipeline/crud.tsx +++ b/packages/ui/certd-client/src/views/certd/pipeline/crud.tsx @@ -10,11 +10,52 @@ import { env } from "/@/utils/util.env"; import { useUserStore } from "/@/store/modules/user"; import dayjs from "dayjs"; import { useSettingStore } from "/@/store/modules/settings"; +import _ from "lodash-es"; export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOptionsProps): CreateCrudOptionsRet { const router = useRouter(); const { t } = useI18n(); const lastResRef = ref(); + + function setRunnableIds(pipeline: any) { + const idMap: any = {}; + function createId(oldId: any) { + if (oldId == null) { + return nanoid(); + } + const newId = nanoid(); + idMap[oldId] = newId; + return newId; + } + if (pipeline.stages) { + for (const stage of pipeline.stages) { + stage.id = createId(stage.id); + if (stage.tasks) { + for (const task of stage.tasks) { + task.id = createId(task.id); + if (task.steps) { + for (const step of task.steps) { + step.id = createId(step.id); + } + } + } + } + } + } + + for (const trigger of pipeline.triggers) { + trigger.id = nanoid(); + } + for (const notification of pipeline.notifications) { + notification.id = nanoid(); + } + + let content = JSON.stringify(pipeline); + for (const key in idMap) { + content = content.replaceAll(key, idMap[key]); + } + return JSON.parse(content); + } const pageRequest = async (query: UserPageQuery): Promise => { return await api.GetList(query); }; @@ -34,9 +75,16 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp title: form.title }); } else { - const content = JSON.parse(form.content); - content.title = form.title; - form.content = JSON.stringify(content); + //复制的流水线 + delete form.status; + delete form.lastHistoryTime; + delete form.lastVars; + delete form.createTime; + delete form.id; + let pipeline = JSON.parse(form.content); + pipeline.title = form.title; + pipeline = setRunnableIds(pipeline); + form.content = JSON.stringify(pipeline); } const res = await api.AddObj(form); @@ -48,12 +96,11 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp // 添加certd pipeline const triggers = []; if (form.triggerCron) { - triggers.push({ id: nanoid(), title: "定时触发", type: "timer", props: { cron: form.triggerCron } }); + triggers.push({ title: "定时触发", type: "timer", props: { cron: form.triggerCron } }); } const notifications = []; if (form.emailNotify) { notifications.push({ - id: nanoid(), type: "email", when: ["error", "turnToSuccess"], options: { @@ -61,19 +108,16 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp } }); } - const pipeline = { + let pipeline = { title: form.domains[0] + "证书自动化", stages: [ { - id: nanoid(), title: "证书申请阶段", tasks: [ { - id: nanoid(), title: "证书申请任务", steps: [ { - id: nanoid(), title: "申请证书", input: { renewDays: 20, @@ -92,8 +136,10 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp triggers, notifications }; + pipeline = setRunnableIds(pipeline); const id = await api.Save({ + title: pipeline.title, content: JSON.stringify(pipeline), keepHistoryCount: 30 }); @@ -147,7 +193,8 @@ export default function ({ crudExpose, context: { certdFormRef } }: CreateCrudOp click: async (context) => { const { ui } = useUi(); // @ts-ignore - const row = context[ui.tableColumn.row]; + let row = context[ui.tableColumn.row]; + row = _.cloneDeep(row); row.title = row.title + "_copy"; await crudExpose.openCopy({ row: row, diff --git a/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue b/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue index 127b83cd1..ef52ee981 100644 --- a/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue +++ b/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue @@ -31,7 +31,7 @@
- + 手动触发 @@ -231,7 +231,6 @@ import PiTaskView from "./component/task-view/index.vue"; import PiStatusShow from "./component/status-show.vue"; import _ from "lodash-es"; import { message, Modal, notification } from "ant-design-vue"; -import { pluginManager } from "/@/views/certd/pipeline/pipeline/plugin"; import { nanoid } from "nanoid"; import { PipelineDetail, PipelineOptions, PluginGroups, RunHistory } from "./type"; import type { Runnable } from "@certd/pipeline"; diff --git a/packages/ui/certd-server/src/modules/pipeline/service/pipeline-service.ts b/packages/ui/certd-server/src/modules/pipeline/service/pipeline-service.ts index 850f7fc07..422ebf957 100644 --- a/packages/ui/certd-server/src/modules/pipeline/service/pipeline-service.ts +++ b/packages/ui/certd-server/src/modules/pipeline/service/pipeline-service.ts @@ -49,13 +49,7 @@ export class PipelineService extends BaseService { } async add(bean: PipelineEntity) { - if (!isPlus()) { - const count = await this.repository.count(); - if (count >= freeCount) { - throw new NeedVIPException('免费版最多只能创建10个pipeline'); - } - } - await super.add(bean); + await this.save(bean); return bean; } @@ -105,19 +99,37 @@ export class PipelineService extends BaseService { } async save(bean: PipelineEntity) { + let old = null; + if (bean.id > 0) { + //修改 + old = await this.info(bean.id); + } + const isUpdate = bean.id > 0 && old != null; if (!isPlus()) { - const count = await this.repository.count(); + let count = await this.repository.count(); + if (isUpdate) { + count -= 1; + } if (count >= freeCount) { throw new NeedVIPException('免费版最多只能创建10个pipeline'); } } + if (!isUpdate) { + //如果是添加,先保存一下,获取到id,更新pipeline.id + await this.addOrUpdate(bean); + } await this.clearTriggers(bean.id); if (bean.content) { const pipeline = JSON.parse(bean.content); - bean.title = pipeline.title; + if (pipeline.title) { + bean.title = pipeline.title; + } + pipeline.id = bean.id; + bean.content = JSON.stringify(pipeline); } await this.addOrUpdate(bean); await this.registerTriggerById(bean.id); + return bean; } async foreachPipeline(callback: (pipeline: PipelineEntity) => void) {