From 0d16e168e75ebe611ec6baab938ea4f7ef222881 Mon Sep 17 00:00:00 2001 From: Ke Sun Date: Mon, 13 Apr 2026 19:36:27 +0800 Subject: [PATCH] ci(wechat-notify): refactor AI summary generation to Python - Replace curl with urllib.request for API calls to improve portability - Move API key to environment variable for better security practices - Inline Python script using heredoc for cleaner workflow definition - Add intermediate file (ai_summary.txt) to separate concerns between API call and output handling - Simplify JSON payload construction using Python's json module - Improve error handling with fallback message for failed AI generation --- .github/workflows/release-notify-wechat.yml | 38 +++++++++++++++------ 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release-notify-wechat.yml b/.github/workflows/release-notify-wechat.yml index 6894db15..d8f4e5aa 100644 --- a/.github/workflows/release-notify-wechat.yml +++ b/.github/workflows/release-notify-wechat.yml @@ -49,19 +49,37 @@ jobs: - name: AI Summary (Qwen) if: steps.check.outputs.ok == 'true' id: ai + env: + DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }} run: | - CONTENT=$(cat commits.txt | sed ':a;N;$!ba;s/\n/\\n/g') + python3 << 'PYEOF' + import json, os, urllib.request - SUMMARY=$(curl -s https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \ - -H "Authorization: Bearer ${{ secrets.DASHSCOPE_API_KEY }}" \ - -H "Content-Type: application/json" \ - -d "{ - \"model\": \"qwen-plus\", - \"input\": { - \"prompt\": \"请用中文总结以下代码提交,输出3-5条要点,面向测试人员。直接输出编号列表,不要输出标题或前言:\\n$CONTENT\" - } - }" | jq -r '.output.text') + with open("commits.txt", "r") as f: + commits = f.read().strip() + prompt = "请用中文总结以下代码提交,输出3-5条要点,面向测试人员。直接输出编号列表,不要输出标题或前言:\n" + commits + payload = {"model": "qwen-plus", "input": {"prompt": prompt}} + data = json.dumps(payload, ensure_ascii=False).encode("utf-8") + + req = urllib.request.Request( + "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation", + data=data, + headers={ + "Authorization": "Bearer " + os.environ["DASHSCOPE_API_KEY"], + "Content-Type": "application/json" + } + ) + resp = urllib.request.urlopen(req) + result = json.loads(resp.read().decode()) + summary = result.get("output", {}).get("text", "AI 摘要生成失败") + print(summary) + + with open("ai_summary.txt", "w", encoding="utf-8") as f: + f.write(summary) + PYEOF + + SUMMARY=$(cat ai_summary.txt) echo "summary<> $GITHUB_OUTPUT echo "$SUMMARY" >> $GITHUB_OUTPUT echo "EOF" >> $GITHUB_OUTPUT