feat(service): ✨ http错误提示 #60
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: PR Review Bot | |
on: | |
pull_request: | |
types: [opened, synchronize] | |
permissions: | |
contents: read | |
pull-requests: write | |
jobs: | |
review: | |
# 跳过 Renovate PR | |
if: | | |
github.actor != 'renovate[bot]' && | |
github.actor != 'renovate-preview[bot]' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Get PR diff | |
id: diff | |
run: | | |
git fetch origin ${{ github.event.pull_request.base.sha }} | |
# 排除配置文件,只分析源代码文件 | |
DIFF=$(git diff ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- \ | |
'src/**/*.vue' \ | |
'src/**/*.ts' \ | |
'src/**/*.tsx' \ | |
'src-tauri/**/*.rs' \ | |
':!:**/*.json' \ | |
':!:**/*.yaml' \ | |
':!:**/*.yml' \ | |
':!:**/*.config.*' \ | |
':!:**/*.lock' \ | |
':!:**/*.toml' \ | |
':!:.env*' \ | |
':!:.eslintrc*' \ | |
':!:.prettierrc*') | |
# 如果没有相关文件变更,设置一个提示信息 | |
if [ -z "$DIFF" ]; then | |
echo "NO_CHANGES=true" >> $GITHUB_ENV | |
echo "DIFF=没有检测到相关文件的变更。" >> $GITHUB_ENV | |
else | |
echo "NO_CHANGES=false" >> $GITHUB_ENV | |
echo "DIFF<<EOF" >> $GITHUB_ENV | |
echo "$DIFF" >> $GITHUB_ENV | |
echo "EOF" >> $GITHUB_ENV | |
fi | |
# 首先安装 pnpm | |
- name: Install pnpm | |
uses: pnpm/action-setup@v4 | |
with: | |
version: 9 | |
run_install: false | |
# 然后设置 Node.js | |
- name: Setup Node | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '22' | |
cache: 'pnpm' | |
- name: Install dependencies | |
run: | | |
pnpm config set registry https://registry.npmmirror.com/ | |
pnpm install | |
pnpm add openai | |
- name: Analyze PR | |
id: analyze | |
if: env.NO_CHANGES != 'true' | |
uses: actions/github-script@v7 | |
env: | |
DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }} | |
with: | |
script: | | |
const OpenAI = require('openai'); | |
const openai = new OpenAI({ | |
apiKey: process.env.DASHSCOPE_API_KEY, | |
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1" | |
}); | |
const diff = process.env.DIFF; | |
try { | |
const completion = await openai.chat.completions.create({ | |
model: "qwen-plus", | |
messages: [{ | |
role: "system", | |
content: "你是一个代码审查助手。请用中文分析以下代码变更,重点关注:\n" + | |
"1. 代码逻辑的改动\n" + | |
"2. 潜在的问题或优化空间\n" + | |
"3. TypeScript 类型定义的准确性\n" + | |
"4. Vue 组件的性能影响\n" + | |
"5. Rust 代码的安全性和性能\n" + | |
"请用中文简明扼要地总结。" | |
}, { | |
role: "user", | |
content: `请分析以下代码变更并总结主要改动:\n\n${diff}` | |
}], | |
temperature: 0.7, | |
max_tokens: 1000 | |
}); | |
const analysis = completion.choices[0].message.content; | |
core.setOutput('analysis', analysis); | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: ` | |
PR 代码分析 | |
${analysis} | |
*这是由通义千问 AI 自动生成的 PR 分析,仅供参考。*` | |
}); | |
} catch (error) { | |
core.setFailed(`分析失败: ${error.message}`); | |
} | |
- name: Skip Analysis Comment | |
if: env.NO_CHANGES == 'true' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
await github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `## PR 代码分析 | |
本次变更不包含需要分析的代码文件(src 目录下的 .vue/.ts/.tsx 文件或 src-tauri 目录下的 .rs 文件)。 | |
--- | |
*这是自动生成的通知。*` | |
}); |