From 7ef8931b94ce3a8801e00413b35a37691456e800 Mon Sep 17 00:00:00 2001
From: rabbitali <wenxin32@foxmail.com>
Date: Wed, 28 Aug 2024 09:34:49 +0800
Subject: [PATCH 1/1] fix issue with language display in task generation api

---
 apollo/database/proxy/task/cve_fix.py      | 18 ++++++++++++++++--
 apollo/database/proxy/task/cve_rollback.py | 20 +++++++++++++++++---
 apollo/function/schema/task.py             |  6 +++---
 database/aops-apollo.sql                   |  2 +-
 4 files changed, 37 insertions(+), 9 deletions(-)

diff --git a/apollo/database/proxy/task/cve_fix.py b/apollo/database/proxy/task/cve_fix.py
index 1aa5546..fc98633 100644
--- a/apollo/database/proxy/task/cve_fix.py
+++ b/apollo/database/proxy/task/cve_fix.py
@@ -23,7 +23,7 @@ from typing import Dict, Tuple
 
 import sqlalchemy.orm
 from elasticsearch import ElasticsearchException
-from flask import g
+from flask import request
 from sqlalchemy import func, case
 from sqlalchemy.exc import SQLAlchemyError
 from sqlalchemy.sql import or_
@@ -462,6 +462,11 @@ class CveFixTaskProxy(TaskProxy):
                 }
 
         """
+        lang_info = request.headers.get("Accept-Language")
+        if lang_info:
+            lang = lang_info.split(',')[0].split(';')[0]
+        else:
+            lang = "en"
         task_id = str(uuid.uuid1()).replace('-', '')
         task_info = copy.deepcopy(data)
         task_info['task_id'] = task_id
@@ -470,8 +475,17 @@ class CveFixTaskProxy(TaskProxy):
         task_info["check_items"] = ",".join(task_info["check_items"])
         task_info["host_num"] = len(wait_fix_rpms.keys())
         task_info["fix_type"] = fix_way
+
+        prefix_map = {
+            "zh": {"hotpatch": "热补丁修复", "coldpatch": "冷补丁修复"},
+            "en": {"hotpatch": "Livepatch Upgrade", "coldpatch": "Normal Upgrade"},
+        }
         if subtask:
-            task_prefix = "冷补丁修复：" if fix_way == "coldpatch" else "热补丁修复："
+            task_prefix = (
+                f"{prefix_map['en'].get(fix_way,'coldpatch')}："
+                if lang.startswith("en")
+                else f"{prefix_map['zh'].get(fix_way,'coldpatch')}："
+            )
             task_info["description"] = task_prefix + task_info["description"]
             task_info["task_name"] = task_prefix + task_info["task_name"]
             task_info["takeover"] = False if fix_way == "coldpatch" else task_info["takeover"]
diff --git a/apollo/database/proxy/task/cve_rollback.py b/apollo/database/proxy/task/cve_rollback.py
index e93be15..0525602 100644
--- a/apollo/database/proxy/task/cve_rollback.py
+++ b/apollo/database/proxy/task/cve_rollback.py
@@ -14,7 +14,7 @@ from typing import Tuple, Optional
 
 import sqlalchemy.orm
 from elasticsearch import ElasticsearchException
-from flask import g
+from flask import request
 from sqlalchemy import or_, func, case
 from sqlalchemy.exc import SQLAlchemyError
 
@@ -106,16 +106,30 @@ class CveRollbackTaskProxy(TaskProxy):
 
     @staticmethod
     def _gen_task_row(data: dict, cve_fix_task_info: sqlalchemy.orm.Query) -> dict:
+        lang_info = request.headers.get("Accept-Language")
+        if lang_info:
+            lang = lang_info.split(',')[0].split(';')[0]
+        else:
+            lang = "en"
+
         fix_task_description = cve_fix_task_info.description
         fix_task_name = cve_fix_task_info.task_name
         host_num = cve_fix_task_info.host_num
+
+        if lang.startswith("en"):
+            task_name = "ROLLBACK_TASK: %s" % fix_task_name
+            description = "ORIGIN_TASK_DESCRIPTION: %s" % fix_task_description
+        else:
+            task_name = "回滚: %s" % fix_task_name
+            description = "原CVE修复任务描述: %s" % fix_task_description
+
         task_data = {
             "cluster_id": data["cluster_id"],
             "task_id": data["task_id"],
             "task_type": data["task_type"],
             "create_time": data["create_time"],
-            "task_name": "回滚: %s" % fix_task_name,
-            "description": "原CVE修复任务描述: %s" % fix_task_description,
+            "task_name": task_name,
+            "description": description,
             "host_num": host_num,
             "username": data.get("username"),
         }
diff --git a/apollo/function/schema/task.py b/apollo/function/schema/task.py
index a494703..f306cea 100644
--- a/apollo/function/schema/task.py
+++ b/apollo/function/schema/task.py
@@ -91,7 +91,7 @@ class GenerateCveTaskSchema(Schema):
     """
 
     task_name = fields.String(required=True, validate=lambda s: 0 < len(s) <= 20)
-    description = fields.String(required=True, validate=lambda s: 0 < len(s) <= 50)
+    description = fields.String(required=True, validate=lambda s: 0 < len(s) <= 100)
     accepted = fields.Boolean(required=True, validate=validate.OneOf([True, False]))
     check_items = fields.List(fields.String(required=True, validate=lambda s: 0 < len(s) <= 32), required=False)
     takeover = fields.Boolean(required=True, validate=validate.OneOf([True, False]))
@@ -178,7 +178,7 @@ class GenerateRepoTaskSchema(Schema):
     """
 
     task_name = fields.String(required=True, validate=lambda s: 0 < len(s) <= 20)
-    description = fields.String(required=True, validate=lambda s: 0 < len(s) <= 50)
+    description = fields.String(required=True, validate=lambda s: 0 < len(s) <= 100)
     repo_id = fields.String(required=True, validate=lambda s: 0 < len(s) <= 36)
     host_list = fields.List(fields.String(required=True, validate=lambda s: 0 < len(s) <= 36), required=True)
 
@@ -367,7 +367,7 @@ class HotpatchRemoveInfoSchema(Schema):
 
 class GenerateHotpatchRemoveTaskSchema(Schema):
     task_name = fields.String(required=True, validate=lambda s: 0 < len(s) <= 20)
-    description = fields.String(required=True, validate=lambda s: 0 < len(s) <= 50)
+    description = fields.String(required=True, validate=lambda s: 0 < len(s) <= 100)
     info = fields.List(fields.Nested(HotpatchRemoveInfoSchema), required=True, validate=lambda s: len(s) > 0)
 
 
diff --git a/database/aops-apollo.sql b/database/aops-apollo.sql
index 4e99d7a..b78bb3a 100644
--- a/database/aops-apollo.sql
+++ b/database/aops-apollo.sql
@@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS `cve`  (
 CREATE TABLE IF NOT EXISTS `vul_task` (
   `task_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
   `task_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
-  `description` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
+  `description` varchar(150) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
   `task_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
   `latest_execute_time` int(11) NULL DEFAULT NULL,
   `create_time` int(11) NULL DEFAULT NULL,
-- 
2.33.0

