-
-
Notifications
You must be signed in to change notification settings - Fork 170
145 lines (126 loc) · 4.56 KB
/
pr-chatbot-review.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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 文件)。
---
*这是自动生成的通知。*`
});