Pārlūkot izejas kodu

将构建日志文件的内容格式化为 html 后返回到前端

reghao 2 dienas atpakaļ
vecāks
revīzija
c70c127865

+ 24 - 1
mgr/src/main/java/cn/reghao/devops/mgr/ops/app/db/query/impl/AppBuildQueryImpl.java

@@ -197,6 +197,29 @@ public class AppBuildQueryImpl implements AppBuildQuery {
         BuildLog buildLog = buildLogRepository.findByBuildLogId(buildLogId);
         String logPath = buildLog.getLogPath();
         String logContent = textFile.readFile(logPath);
-        return logContent;
+        return ansiToHtml(logContent);
+    }
+
+    /**
+     * 将包含 ANSI 颜色代码的原始日志转换为带有 HTML span 标签的文本
+     */
+    private String ansiToHtml(String rawLog) {
+        if (rawLog == null) return "";
+
+        // 简单的 HTML 转义,防止 XSS
+        String html = rawLog.replace("&", "&")
+                .replace("<", "&lt;")
+                .replace(">", "&gt;");
+
+        // 替换常见的 ANSI 颜色代码
+        // [31m -> Red, [32m -> Green, [33m -> Yellow, [36m -> Cyan, [0m -> Reset
+        html = html.replaceAll("\\u001b\\[31m", "<span style='color: #F56C6C;'>") // Red
+                .replaceAll("\\u001b\\[32m", "<span style='color: #67C23A;'>") // Green
+                .replaceAll("\\u001b\\[33m", "<span style='color: #E6A23C;'>") // Yellow
+                .replaceAll("\\u001b\\[36m", "<span style='color: #409EFF;'>") // Cyan
+                .replaceAll("\\u001b\\[0m", "</span>"); // Reset
+
+        // 处理换行符
+        return html.replace("\n", "<br/>");
     }
 }