ci(wechat-notify): extract payload building logic to Python script

- Create new `.github/scripts/build_wechat_payload.py` to handle WeChat payload generation
- Replace inline Python string concatenation with dedicated script for better maintainability
- Add checkout step to access the script during workflow execution
- Simplify workflow by delegating payload construction to external script
- Improve code readability and reusability for future notification enhancements
This commit is contained in:
Ke Sun
2026-04-13 19:20:53 +08:00
parent 8495aa5dde
commit b20971dc95
2 changed files with 29 additions and 16 deletions

24
.github/scripts/build_wechat_payload.py vendored Normal file
View File

@@ -0,0 +1,24 @@
import json
import os
branch = os.environ.get("BRANCH", "")
author = os.environ.get("AUTHOR", "")
pr_title = os.environ.get("PR_TITLE", "")
pr_url = os.environ.get("PR_URL", "")
ai_summary = os.environ.get("AI_SUMMARY", "")
content = (
"## 🚀 Release 发布通知\n"
f"> 📦 **分支**: {branch}\n"
f"> 👤 **提交人**: {author}\n"
f"> 📝 **标题**: {pr_title}\n\n"
"### 🧠 AI变更摘要\n"
f"{ai_summary}\n\n"
"---\n"
f"🔗 [查看PR详情]({pr_url})"
)
payload = {"msgtype": "markdown", "markdown": {"content": content}}
with open("wechat_payload.json", "w", encoding="utf-8") as f:
json.dump(payload, f, ensure_ascii=False)

View File

@@ -79,6 +79,10 @@ jobs:
echo "EOF" >> $GITHUB_OUTPUT
# 5⃣ 企业微信通知Markdown
- name: Checkout for script
if: steps.check.outputs.ok == 'true'
uses: actions/checkout@v4
- name: Notify WeChat
if: steps.check.outputs.ok == 'true'
env:
@@ -89,22 +93,7 @@ jobs:
PR_URL: ${{ github.event.pull_request.html_url }}
AI_SUMMARY: ${{ steps.ai.outputs.summary }}
run: |
python3 -c "
import json, os
content = (
'## 🚀 Release 发布通知\n'
'> 📦 **分支**: ' + os.environ['BRANCH'] + '\n'
'> 👤 **提交人**: ' + os.environ['AUTHOR'] + '\n'
'> 📝 **标题**: ' + os.environ['PR_TITLE'] + '\n\n'
'### 🧠 AI变更摘要\n'
+ os.environ['AI_SUMMARY'] + '\n\n'
'---\n'
'🔗 [查看PR详情](' + os.environ['PR_URL'] + ')'
)
payload = {'msgtype': 'markdown', 'markdown': {'content': content}}
with open('wechat_payload.json', 'w') as f:
json.dump(payload, f, ensure_ascii=False)
"
python3 .github/scripts/build_wechat_payload.py
curl -s "$WECHAT_WEBHOOK" \
-H 'Content-Type: application/json' \