From c101cc3682e8591854be1c5b28020a8e16de4902 Mon Sep 17 00:00:00 2001 From: luofann Date: Tue, 10 Jun 2025 16:53:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=88=9B=E5=BB=BAbkflow=E7=A9=BA=E9=97=B4=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=85=BC=E5=AE=B9bkflow=E7=BD=91=E5=85=B3?= =?UTF-8?q?=E6=9C=AA=E6=8E=88=E6=9D=83=E5=9C=BA=E6=99=AF=20#=20Reviewed,?= =?UTF-8?q?=20transaction=20id:=2045595?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/server/controller/flow.js | 18 ++++++++++-------- lib/server/service/business/flow.js | 12 +++++++++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/server/controller/flow.js b/lib/server/controller/flow.js index a9aa0342e..d76988b5d 100644 --- a/lib/server/controller/flow.js +++ b/lib/server/controller/flow.js @@ -33,7 +33,7 @@ import { LCDataService, TABLE_FILE_NAME } from '../service/common/data-service' import httpConf from '../conf/http' import v3Conf from '../conf/v3' import { transFlowTplToBkFlowPipelineTree } from '../utils/flow' -import { getTaskList } from '../service/business/flow' +import { getLesscodeBkFlowSpaceId, getTaskList } from '../service/business/flow' const bkflowApiGatewayName = global.BKFLOW_API_GATEWAY_NAME const authorization = { @@ -539,7 +539,7 @@ export default class FlowController { const taskName = `${flowDetail.name}_${dayjs().format('YYYYMMDDHHmmss')}` const notifyConfig = JSON.parse(flowDetail.notifyConfig) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() const bkFlowTaskDetail = await execApiGateWay({ apiName: bkflowApiGatewayName, @@ -624,7 +624,7 @@ export default class FlowController { const taskIds = list.map(item => item.bkFlowTaskId) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() // 调用BkFlow接口查询任务的执行状态 const tasksStatusRes = await execApiGateWay({ @@ -688,7 +688,7 @@ export default class FlowController { const taskIds = list.map(item => item.bkFlowTaskId) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() // 调用BkFlow接口查询任务的执行状态 const tasksStatusRes = await execApiGateWay({ @@ -742,7 +742,9 @@ export default class FlowController { console.log('准备请求 =====') const pipelineTree = transFlowTplToBkFlowPipelineTree(projectId, nodes, edges) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() + try { const res = await execApiGateWay({ apiName: bkflowApiGatewayName, @@ -786,7 +788,7 @@ export default class FlowController { ) { const runningNodeIds = [] const taskItem = await LCDataService.findOne(TABLE_FILE_NAME.FLOW_TASK, { id: taskId }) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() // 任务执行详情数据 const taskExecDetail = await execApiGateWay({ @@ -863,7 +865,7 @@ export default class FlowController { @PathParams({ name: 'taskId', require: true }) taskId, ) { const taskItem = await LCDataService.findOne(TABLE_FILE_NAME.FLOW_TASK, { id: taskId }) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() // 任务执行详情数据 const taskExecDetail = await execApiGateWay({ @@ -956,7 +958,7 @@ export default class FlowController { }) const taskItem = await LCDataService.findOne(TABLE_FILE_NAME.FLOW_TASK, { id: taskId }) - const { bkFlowSpaceId } = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + const bkFlowSpaceId = await getLesscodeBkFlowSpaceId() const taskTplDetail = await execApiGateWay({ apiName: bkflowApiGatewayName, diff --git a/lib/server/service/business/flow.js b/lib/server/service/business/flow.js index 76064de29..435935dc2 100644 --- a/lib/server/service/business/flow.js +++ b/lib/server/service/business/flow.js @@ -55,6 +55,7 @@ export const createBkFlowSpace = async () => { projectId: 0, bkFlowSpaceId: response.data.space.id }) + return response.data.space.id } } catch (err) { console.error('create bkflow space error:\n', err) @@ -62,7 +63,16 @@ export const createBkFlowSpace = async () => { } } - return spaceData + return spaceData.bkFlowSpaceId +} + +// 获取lesscode平台在bkflow的空间id,如果不存在则创建一个 +export const getLesscodeBkFlowSpaceId = async () => { + const spaceData = await LCDataService.findOne(TABLE_FILE_NAME.PROJECT_BKFLOW_SPACE, { projectId: 0 }) + if (spaceData) { + return spaceData.bkFlowSpaceId + } + return await createBkFlowSpace() } export const getTaskList = (parmas) => {