From 99559621c5aabc5861c3ef5d1487e5a3ec43d45e Mon Sep 17 00:00:00 2001 From: Ke Sun Date: Mon, 13 Apr 2026 19:24:50 +0800 Subject: [PATCH] ci(wechat-notify): inline payload building logic into workflow - Remove build_wechat_payload.py script and consolidate payload construction directly in workflow - Eliminate intermediate environment variables and file I/O operations for cleaner execution - Inline AI summary payload generation into curl request - Inline WeChat notification payload generation into curl request - Remove unnecessary checkout step since script is no longer needed - Simplify workflow by reducing file dependencies and improving readability --- .github/scripts/build_wechat_payload.py | 24 -------- .github/workflows/release-notify-wechat.yml | 62 +++++++-------------- 2 files changed, 21 insertions(+), 65 deletions(-) delete mode 100644 .github/scripts/build_wechat_payload.py diff --git a/.github/scripts/build_wechat_payload.py b/.github/scripts/build_wechat_payload.py deleted file mode 100644 index 5e292ee9..00000000 --- a/.github/scripts/build_wechat_payload.py +++ /dev/null @@ -1,24 +0,0 @@ -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 b6c8a04d..4264570f 100644 --- a/.github/workflows/release-notify-wechat.yml +++ b/.github/workflows/release-notify-wechat.yml @@ -13,31 +13,23 @@ jobs: steps: # 防止 GitHub HEAD 未同步 - - name: Wait for ref sync - run: sleep 3 + - run: sleep 3 # 1️⃣ 获取分支 HEAD - name: Get HEAD id: head - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - REPO: ${{ github.repository }} - BASE_REF: ${{ github.event.pull_request.base.ref }} run: | HEAD_SHA=$(curl -s \ - -H "Authorization: Bearer $GH_TOKEN" \ - "https://api.github.com/repos/$REPO/git/ref/heads/$BASE_REF" \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + https://api.github.com/repos/${{ github.repository }}/git/ref/heads/${{ github.event.pull_request.base.ref }} \ | jq -r '.object.sha') echo "head_sha=$HEAD_SHA" >> $GITHUB_OUTPUT # 2️⃣ 判断是否最终PR - name: Check Latest id: check - env: - MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha }} - HEAD_SHA: ${{ steps.head.outputs.head_sha }} run: | - if [ "$MERGE_SHA" = "$HEAD_SHA" ]; then + if [ "${{ github.event.pull_request.merge_commit_sha }}" = "${{ steps.head.outputs.head_sha }}" ]; then echo "ok=true" >> $GITHUB_OUTPUT else echo "ok=false" >> $GITHUB_OUTPUT @@ -47,54 +39,42 @@ jobs: - name: Get Commits if: steps.check.outputs.ok == 'true' id: commits - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COMMITS_URL: ${{ github.event.pull_request.commits_url }} run: | curl -s \ - -H "Authorization: Bearer $GH_TOKEN" \ - "$COMMITS_URL" \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + ${{ github.event.pull_request.commits_url }} \ | jq -r '.[].commit.message' | head -n 20 > commits.txt # 4️⃣ 阿里 AI 总结(通义千问) - name: AI Summary (Qwen) if: steps.check.outputs.ok == 'true' id: ai - env: - DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }} run: | - COMMIT_MESSAGES=$(cat commits.txt) - - jq -n --arg msgs "请用中文总结以下代码提交,输出3-5条,面向测试人员: - $COMMIT_MESSAGES" \ - '{"model": "qwen-plus", "input": {"prompt": $msgs}}' > ai_payload.json + CONTENT=$(cat commits.txt | sed ':a;N;$!ba;s/\n/\\n/g') SUMMARY=$(curl -s https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \ - -H "Authorization: Bearer $DASHSCOPE_API_KEY" \ + -H "Authorization: Bearer ${{ secrets.DASHSCOPE_API_KEY }}" \ -H "Content-Type: application/json" \ - -d @ai_payload.json | jq -r '.output.text') + -d "{ + \"model\": \"qwen-plus\", + \"input\": { + \"prompt\": \"请用中文总结以下代码提交,输出3-5条,面向测试人员:\\n$CONTENT\" + } + }" | jq -r '.output.text') echo "summary<> $GITHUB_OUTPUT echo "$SUMMARY" >> $GITHUB_OUTPUT 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: - WECHAT_WEBHOOK: ${{ secrets.WECHAT_WEBHOOK }} - BRANCH: ${{ github.event.pull_request.base.ref }} - AUTHOR: ${{ github.event.pull_request.user.login }} - PR_TITLE: ${{ github.event.pull_request.title }} - PR_URL: ${{ github.event.pull_request.html_url }} - AI_SUMMARY: ${{ steps.ai.outputs.summary }} run: | - python3 .github/scripts/build_wechat_payload.py - - curl -s "$WECHAT_WEBHOOK" \ + curl '${{ secrets.WECHAT_WEBHOOK }}' \ -H 'Content-Type: application/json' \ - -d @wechat_payload.json + -d "{ + \"msgtype\": \"markdown\", + \"markdown\": { + \"content\": \"## 🚀 Release 发布通知\n> 📦 **分支**: ${{ github.event.pull_request.base.ref }}\n> 👤 **提交人**: ${{ github.event.pull_request.user.login }}\n> 📝 **标题**: ${{ github.event.pull_request.title }}\n\n### 🧠 AI变更摘要\n${{ steps.ai.outputs.summary }}\n\n---\n🔗 [查看PR详情](${{ github.event.pull_request.html_url }})\" + } + }"