From b20971dc9565fb91925dbc4f461f607fb112f3ca Mon Sep 17 00:00:00 2001 From: Ke Sun Date: Mon, 13 Apr 2026 19:20:53 +0800 Subject: [PATCH] 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 --- .github/scripts/build_wechat_payload.py | 24 +++++++++++++++++++++ .github/workflows/release-notify-wechat.yml | 21 +++++------------- 2 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 .github/scripts/build_wechat_payload.py diff --git a/.github/scripts/build_wechat_payload.py b/.github/scripts/build_wechat_payload.py new file mode 100644 index 00000000..5e292ee9 --- /dev/null +++ b/.github/scripts/build_wechat_payload.py @@ -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) diff --git a/.github/workflows/release-notify-wechat.yml b/.github/workflows/release-notify-wechat.yml index ae5f3f6e..b6c8a04d 100644 --- a/.github/workflows/release-notify-wechat.yml +++ b/.github/workflows/release-notify-wechat.yml @@ -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' \