- 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
120 lines
4.3 KiB
YAML
120 lines
4.3 KiB
YAML
name: Release Notify Workflow
|
||
|
||
on:
|
||
pull_request:
|
||
types: [closed]
|
||
|
||
jobs:
|
||
notify:
|
||
if: >
|
||
github.event.pull_request.merged == true &&
|
||
startsWith(github.event.pull_request.base.ref, 'release')
|
||
runs-on: ubuntu-latest
|
||
|
||
steps:
|
||
# 防止 GitHub HEAD 未同步
|
||
- run: sleep 3
|
||
|
||
# 1️⃣ 获取分支 HEAD
|
||
- name: Get HEAD
|
||
id: head
|
||
run: |
|
||
HEAD_SHA=$(curl -s \
|
||
-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
|
||
run: |
|
||
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
|
||
fi
|
||
|
||
# 3️⃣ 获取 commits
|
||
- name: Get Commits
|
||
if: steps.check.outputs.ok == 'true'
|
||
id: commits
|
||
run: |
|
||
curl -s \
|
||
-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: |
|
||
python3 << 'PYEOF'
|
||
import json, os, urllib.request
|
||
|
||
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<<EOF" >> $GITHUB_OUTPUT
|
||
echo "$SUMMARY" >> $GITHUB_OUTPUT
|
||
echo "EOF" >> $GITHUB_OUTPUT
|
||
|
||
# 5️⃣ 企业微信通知(Markdown)
|
||
- 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 << 'PYEOF'
|
||
import json, os, urllib.request
|
||
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}}
|
||
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||
req = urllib.request.Request(
|
||
os.environ["WECHAT_WEBHOOK"],
|
||
data=data,
|
||
headers={"Content-Type": "application/json"}
|
||
)
|
||
resp = urllib.request.urlopen(req)
|
||
print(resp.read().decode())
|
||
PYEOF
|