From 47c780303c175cb12de6c615c29e03d745594111 Mon Sep 17 00:00:00 2001 From: ZC-A <1483681501@qq.com> Date: Tue, 14 Jan 2025 15:19:51 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=86=85?= =?UTF-8?q?=E7=BD=AE=E8=B5=8B=E5=80=BC=E8=8A=82=E7=82=B9=20#82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collections/value_assign/__init__.py | 19 ++++ .../collections/value_assign/v1_0_0.py | 101 ++++++++++++++++++ .../static/components/value_assign/v1_0_0.js | 32 ++++++ 3 files changed, 152 insertions(+) create mode 100644 bkflow/pipeline_plugins/components/collections/value_assign/__init__.py create mode 100644 bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py create mode 100644 bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js diff --git a/bkflow/pipeline_plugins/components/collections/value_assign/__init__.py b/bkflow/pipeline_plugins/components/collections/value_assign/__init__.py new file mode 100644 index 0000000..c67ae4e --- /dev/null +++ b/bkflow/pipeline_plugins/components/collections/value_assign/__init__.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making +蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available. +Copyright (C) 2024 THL A29 Limited, +a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at http://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +either express or implied. See the License for the +specific language governing permissions and limitations under the License. + +We undertake not to change the open source license (MIT license) applicable + +to the current version of the project delivered to anyone in the future. +""" diff --git a/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py b/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py new file mode 100644 index 0000000..9344609 --- /dev/null +++ b/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +""" +TencentBlueKing is pleased to support the open source community by making +蓝鲸流程引擎服务 (BlueKing Flow Engine Service) available. +Copyright (C) 2024 THL A29 Limited, +a Tencent company. All rights reserved. +Licensed under the MIT License (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at http://opensource.org/licenses/MIT +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, +either express or implied. See the License for the +specific language governing permissions and limitations under the License. + +We undertake not to change the open source license (MIT license) applicable + +to the current version of the project delivered to anyone in the future. +""" + +from django.conf import settings +from django.utils.translation import ugettext_lazy as _ +from pipeline.component_framework.component import Component +from pipeline.core.flow.io import ArrayItemSchema, ObjectItemSchema, StringItemSchema +from pipeline.eri.runtime import BambooDjangoRuntime + +from bkflow.pipeline_plugins.components.collections.base import BKFlowBaseService + +__group_name__ = _("蓝鲸服务(BK)") + + +class ValueAssignService(BKFlowBaseService): + def inputs_format(self): + return [ + self.InputItem( + name=_("赋值变量或常量与被赋值变量列表"), + key="bk_assignment_list", + type="array", + schema=ArrayItemSchema( + description=_("赋值变量或常量与被赋值变量的映射列表"), + item_schema=ObjectItemSchema( + description=_("单个赋值关系"), + property_schemas={ + "bk_assign_source_var": StringItemSchema(description=_("赋值变量或常量")), + "bk_assgin_target_var": StringItemSchema(description=_("被赋值变量")), + }, + ), + ), + ) + ] + + def outputs_format(self): + # 插件返回内容 + return [] + + def plugin_execute(self, data, parent_data): + runtime = BambooDjangoRuntime() + upsert_dict = {} + pipeline_id = self._runtime_attrs["root_pipeline_id"] + assign_list = data.get_one_of_inputs("bk_assignment_list") + for assign in assign_list: + # 循环处理表格中的内容 检查是否存在/类型问题后再统一事务批量执行 + input_var = assign["bk_assign_source_var"] + target_var = assign["bk_assgin_target_var"] + target_var_str = "${{{}}}".format(target_var) + context = runtime.get_context_values(pipeline_id=pipeline_id, keys={target_var_str}) + if not context: + # 如果没有找到对应的全局变量则直接返回错误 + err_msg = "target variable {} is not exist".format(target_var) + self.logger.exception(err_msg) + data.outputs.ex_data = err_msg + return False + context = context[0] + + # 处理输入变量与目标变量的类型转换(输入常量时) 如果是相同类型则必然成功 + try: + # 尝试将输入转换为目标变量类型 + input_var = type(context.value)(input_var) + except (ValueError, TypeError): + err_msg = "input variable '{}' cannot be converted to the type of target variable '{}': {}".format( + input_var, target_var, type(context.value).__name__ + ) + self.logger.exception(err_msg) + data.outputs.ex_data = err_msg + return False + + # 更新上下文并放入字典 + context.value = input_var + upsert_dict[target_var_str] = context + runtime.upsert_plain_context_values(pipeline_id=pipeline_id, update=upsert_dict) + # 批量更新内容 事务原子操作 + return True + + +class ValueAssignComponent(Component): + name = _("赋值节点") + code = "value_assign" + bound_service = ValueAssignService + form = settings.STATIC_URL + "components/value_assign/v1_0_0.js" + version = "v1.0.0" + desc = "提供内置赋值功能" diff --git a/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js b/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js new file mode 100644 index 0000000..29a4668 --- /dev/null +++ b/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js @@ -0,0 +1,32 @@ + +(function () { + $.atoms.value_assign = [ + { + tag_code: "bk_assignment_list", + type: "datatable", + attrs: { + pagination: true, + name: gettext("赋值变量或常量与被赋值变量列表"), + hookable: true, + add_btn: true, + empty_text: gettext("赋值变量或常量与被赋值变量的映射列表,一行填写一个映射"), + columns: [ + { + tag_code: "bk_assign_source_var", + type: "textarea", + attrs: { + name: gettext("赋值变量或常量"), + } + }, + { + tag_code: "bk_assgin_target_var", + type: "textarea", + attrs: { + name: gettext("被赋值变量"), + } + }, + ] + } + } + ] +})(); \ No newline at end of file From 7ebf1df9d830ed40ebf19137a96c859926b14f48 Mon Sep 17 00:00:00 2001 From: ZC-A <1483681501@qq.com> Date: Tue, 14 Jan 2025 17:16:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E6=9B=B4=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9F=A5=E8=AF=A2=20=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=90=8D=E7=A7=B0=20#82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collections/value_assign/v1_0_0.py | 28 +++++++++++++------ .../static/components/value_assign/v1_0_0.js | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py b/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py index 9344609..711c95e 100644 --- a/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py +++ b/bkflow/pipeline_plugins/components/collections/value_assign/v1_0_0.py @@ -41,7 +41,7 @@ def inputs_format(self): item_schema=ObjectItemSchema( description=_("单个赋值关系"), property_schemas={ - "bk_assign_source_var": StringItemSchema(description=_("赋值变量或常量")), + "bk_assign_source_val": StringItemSchema(description=_("赋值变量或常量")), "bk_assgin_target_var": StringItemSchema(description=_("被赋值变量")), }, ), @@ -58,34 +58,46 @@ def plugin_execute(self, data, parent_data): upsert_dict = {} pipeline_id = self._runtime_attrs["root_pipeline_id"] assign_list = data.get_one_of_inputs("bk_assignment_list") + # 构建目标变量集合 + target_var_set = {"${{{}}}".format(assign["bk_assgin_target_var"]) for assign in assign_list} + + try: + # 批量查询目标变量的上下文值 + context_values = runtime.get_context_values(pipeline_id=pipeline_id, keys=target_var_set) + except ValueError as e: + self.logger.exception(str(e)) + data.outputs.ex_data = str(e) + return False + + # 构建一个字典以快速访问 + context_dict = {cv.key: cv for cv in context_values} + for assign in assign_list: # 循环处理表格中的内容 检查是否存在/类型问题后再统一事务批量执行 - input_var = assign["bk_assign_source_var"] + input_val = assign["bk_assign_source_val"] target_var = assign["bk_assgin_target_var"] target_var_str = "${{{}}}".format(target_var) - context = runtime.get_context_values(pipeline_id=pipeline_id, keys={target_var_str}) + context = context_dict.get(target_var_str) if not context: - # 如果没有找到对应的全局变量则直接返回错误 err_msg = "target variable {} is not exist".format(target_var) self.logger.exception(err_msg) data.outputs.ex_data = err_msg return False - context = context[0] # 处理输入变量与目标变量的类型转换(输入常量时) 如果是相同类型则必然成功 try: # 尝试将输入转换为目标变量类型 - input_var = type(context.value)(input_var) + input_val = type(context.value)(input_val) except (ValueError, TypeError): err_msg = "input variable '{}' cannot be converted to the type of target variable '{}': {}".format( - input_var, target_var, type(context.value).__name__ + input_val, target_var, type(context.value).__name__ ) self.logger.exception(err_msg) data.outputs.ex_data = err_msg return False # 更新上下文并放入字典 - context.value = input_var + context.value = input_val upsert_dict[target_var_str] = context runtime.upsert_plain_context_values(pipeline_id=pipeline_id, update=upsert_dict) # 批量更新内容 事务原子操作 diff --git a/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js b/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js index 29a4668..1dfce12 100644 --- a/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js +++ b/bkflow/pipeline_plugins/static/components/value_assign/v1_0_0.js @@ -12,7 +12,7 @@ empty_text: gettext("赋值变量或常量与被赋值变量的映射列表,一行填写一个映射"), columns: [ { - tag_code: "bk_assign_source_var", + tag_code: "bk_assign_source_val", type: "textarea", attrs: { name: gettext("赋值变量或常量"),