Commit 3bf3863c authored by Auto Backup's avatar Auto Backup

refactor: 统一Git信息获取函数,修复PowerShell中%转义问题

- 新增 getGitCommitInfo() 统一函数获取作者、提交信息、邮箱
- 新增 runCommandWithOutput() 跨平台命令执行函数
- 修复 Windows PowerShell 中 %B/%an/%ae 被解释为变量的问题
- 使用 sh -c 包装 git 命令避免 PowerShell 转义问题
- 简化 post 阶段代码,复用统一函数
parent 14641928
......@@ -19,6 +19,74 @@ def runPowerShellWithOutput(script) {
}
}
/**
* 获取 Git 提交信息(统一函数,修复 PowerShell 中 % 转义问题)
* @param commitHash 可选,指定 commit hash,默认使用最新提交
* @return Map [author, message, email, bugId]
*/
def getGitCommitInfo(commitHash = '') {
def result = [
author: '',
message: '',
email: '',
bugId: ''
]
// 确定使用哪个 commit
def commitArg = commitHash ? "${commitHash}" : "-1"
def gitCmdPrefix = "git log ${commitArg} --pretty="
// Windows PowerShell 中 % 是特殊字符,需要用单引号避免变量展开
// 使用 sh -c 在 Windows 上也能工作
def cmdAuthor = isUnix() ? "${gitCmdPrefix}%an" : "sh -c '${gitCmdPrefix}%an'"
def cmdMessage = isUnix() ? "${gitCmdPrefix}%B" : "sh -c '${gitCmdPrefix}%B'"
def cmdEmail = isUnix() ? "${gitCmdPrefix}%ae" : "sh -c '${gitCmdPrefix}%ae'"
try {
// 获取作者
def authorResult = runCommandWithOutput(cmdAuthor)
if (authorResult.success) {
result.author = authorResult.stdout
}
// 获取提交信息
def msgResult = runCommandWithOutput(cmdMessage)
if (msgResult.success) {
result.message = msgResult.stdout
// 提取 Bug ID
def matcher = (result.message =~ /#[0-9]+/)
result.bugId = matcher ? matcher[0] : "未关联Bug"
}
// 获取邮箱
def emailResult = runCommandWithOutput(cmdEmail)
if (emailResult.success) {
result.email = emailResult.stdout
}
} catch (Exception e) {
echo "获取 Git 提交信息失败: ${e.message}"
result.author = "未知提交人"
result.message = "无法获取提交信息"
result.email = "unknown@example.com"
result.bugId = "未关联Bug"
}
return result
}
/**
* 跨平台命令执行函数(统一)
*/
def runCommandWithOutput(command) {
try {
def result = runPowerShellWithOutput(command)
return [success: true, stdout: result, stderr: '']
} catch (Exception e) {
return [success: false, stdout: '', stderr: e.message]
}
}
// properties([
// pipelineTriggers([
// // 使用 Cron 表达式,实现极其精准的控制
......@@ -80,17 +148,12 @@ pipeline {
// --- 优先级 2: 检查 Commit Message ---
else {
// 2. 抓取 Commit 信息(使用跨平台方式)
def commitMsg = ""
// 2. 抓取 Commit 信息(使用统一函数)
def branch = env.BRANCH_NAME ?: '' // 若 env.BRANCH_NAME 为 null,则转为空字符串
echo "当前分支: ${branch}"
try {
commitMsg = runPowerShellWithOutput('git log -1 --pretty=%B')
} catch (Exception e) {
echo "AI 自动决策中心发生错误: ${e.message}"
// 如果 PowerShell 失败,尝试直接使用 git 命令
// commitMsg = runPowerShellWithOutput("git log -1 --pretty=%B")
}
def gitInfo = getGitCommitInfo()
def commitMsg = gitInfo.message
if (commitMsg.contains("[deploy]") ||commitMsg.contains("[部署]") || commitMsg.contains("[发布]") ) {
echo "🚀 [高优先级] 检测到 [deploy] 关键字,执行强制构建..."
......@@ -141,7 +204,7 @@ pipeline {
buildParams += " --webapps=${params.webapps} "
echo "执行构建参数: ${buildParams}"
echo "执行构建参数:node ./BuildScripts/ai-build.js ${buildParams}"
// 执行 AI 构建脚本
def buildResult = runPowerShellWithOutput("cd \"${params.NODE_WORKING_DIR}\"; node ./BuildScripts/ai-build.js ${buildParams}")
......@@ -186,61 +249,19 @@ pipeline {
always {
script {
try {
// 优先使用 env.GIT_COMMIT 获取最新的 Commit ID 和相关信息
// 优先使用 env.GIT_COMMIT 获取最新的 Commit ID
def commitId = env.GIT_COMMIT ?: "未知Commit"
// 基于 commitId 获取详细的 Git 信息
def author = ""
def msg = ""
def lastAuthorEmail = ""
try {
if (commitId != "未知Commit") {
// 使用 commitId 精确获取信息,更加可靠
author = runPowerShellWithOutput("git log -1 ${commitId} --pretty=%an")
msg = runPowerShellWithOutput("git log -1 ${commitId} --pretty=%B")
lastAuthorEmail = runPowerShellWithOutput("git log -1 ${commitId} --pretty=%ae")
} else {
// fallback 方案
author = runPowerShellWithOutput("git log -1 --pretty=%an")
msg = runPowerShellWithOutput("git log -1 --pretty=%B")
lastAuthorEmail = runPowerShellWithOutput("git log -1 --pretty=%ae")
}
} catch (Exception e) {
// 如果 PowerShell 方式失败,使用基础 git 命令
echo "PowerShell 方式获取 Git 信息失败,尝试基础命令: ${e.message}"
try {
if (commitId != "未知Commit") {
author = runPowerShellWithOutput("git log -1 ${commitId} --pretty=%an")
msg = runPowerShellWithOutput("git log -1 ${commitId} --pretty=%B")
lastAuthorEmail = runPowerShellWithOutput("git log -1 ${commitId} --pretty=%ae")
} else {
author = runPowerShellWithOutput("git log -1 --pretty=%an")
msg = runPowerShellWithOutput("git log -1 --pretty=%B")
lastAuthorEmail = runPowerShellWithOutput("git log -1 --pretty=%ae")
}
} catch (Exception e2) {
echo "基础 Git 命令也失败: ${e2.message}"
author = "未知提交人"
msg = "无法获取提交信息"
lastAuthorEmail = "unknown@example.com"
}
}
// 使用 matcher 对象进行匹配
def matcher = (msg =~ /#[0-9]+/)
// 安全提取:只有匹配到了才取 [0],否则赋默认值
def bugId = matcher ? matcher[0] : "未关联Bug"
// 使用统一函数获取 Git 提交信息
def gitInfo = getGitCommitInfo(commitId)
echo "--- 自动决策中心报告 ---"
echo "提交信息: ${msg}"
echo "关联任务: ${bugId}"
echo "修复人员: ${author} (${lastAuthorEmail})"
echo "提交信息: ${gitInfo.message}"
echo "关联任务: ${gitInfo.bugId}"
echo "修复人员: ${gitInfo.author} (${gitInfo.email})"
// 智能化逻辑:如果是修复 Bug,可以触发特定的通知
if (bugId != "未关联Bug") {
if (gitInfo.bugId != "未关联Bug") {
echo "检测到 Bug 修复任务,准备执行回归测试..."
}
......@@ -252,12 +273,12 @@ pipeline {
text: [
"### 构建报告: ${env.JOB_NAME}",
"---",
"🎉 修复喜报!", "修复人: ${lastAuthorEmail}", "关联Bug: ${bugId}",
"🎉 修复喜报!", "修复人: ${gitInfo.email}", "关联Bug: ${gitInfo.bugId}",
"- **当前分支**: ${env.BRANCH_NAME}",
"- **Commit ID**: ${commitId}",
"- **结果**: ${currentBuild.currentResult}",
"- **提交人**: ${author}",
"- **提交信息**: ${msg}",
"- **提交人**: ${gitInfo.author}",
"- **提交信息**: ${gitInfo.message}",
"- **查看详情**: [点击跳转](${env.BUILD_URL})"
]
// 移除 @ 特定测试人员的逻辑
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment