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] =?UTF-8?q?feat:=20=E6=9B=B4=E6=94=B9=E4=B8=BA=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E6=9F=A5=E8=AF=A2=20=E6=9B=B4=E6=94=B9=E8=BE=93?= =?UTF-8?q?=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("赋值变量或常量"),