-
Notifications
You must be signed in to change notification settings - Fork 2
299 lines (278 loc) · 10.8 KB
/
pr-review.yml
File metadata and controls
299 lines (278 loc) · 10.8 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
name: PR Review
on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened]
paths:
- 'backend/**/*.py'
- 'backend/requirements.txt'
- 'backend/static/**'
- 'Dockerfile'
- 'docker-compose.yml'
- 'docs/**'
- '.github/workflows/**'
- '.github/scripts/**'
pull_request_target:
branches: [main]
types: [opened, synchronize, reopened]
paths:
- 'backend/**/*.py'
- 'backend/requirements.txt'
- 'backend/static/**'
- 'Dockerfile'
- 'docker-compose.yml'
- 'docs/**'
- '.github/workflows/**'
- '.github/scripts/**'
workflow_dispatch:
permissions:
contents: read
pull-requests: write
issues: write
env:
BASE_REF: ${{ github.event.pull_request.base.ref || github.ref_name }}
jobs:
guard:
name: Guard
runs-on: ubuntu-latest
if: github.event.pull_request == null
steps:
- run: echo "No PR context, skipping PR Review"
security-check:
name: Security Check
runs-on: ubuntu-latest
if: github.event.pull_request != null
outputs:
safe_to_run: ${{ steps.check.outputs.safe_to_run }}
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
fetch-depth: 0
- name: Fetch base branch
run: git fetch origin ${{ env.BASE_REF }}
- name: Check sensitive files
id: check
run: |
CHANGED=$(git diff --name-only origin/${{ env.BASE_REF }}...HEAD -- '.github/workflows/*.yml' '.github/scripts/*.py' 2>/dev/null || true)
if [ -n "$CHANGED" ]; then
echo "safe_to_run=false" >> $GITHUB_OUTPUT
echo "⚠️ 敏感文件被修改 (.github/workflows 或 .github/scripts),跳过 AI 审查"
else
echo "safe_to_run=true" >> $GITHUB_OUTPUT
fi
auto-check:
name: Auto Check
runs-on: ubuntu-latest
needs: security-check
if: needs.security-check.outputs.safe_to_run == 'true'
outputs:
has_reviewable_changes: ${{ steps.check.outputs.has_reviewable_changes }}
syntax_ok: ${{ steps.check.outputs.syntax_ok }}
steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Install flake8
run: pip install flake8
- name: Static check (syntax + Flake8)
id: check
run: |
git fetch origin ${{ env.BASE_REF }}
# 检测可审查的代码变更:Python、JavaScript、CSS
ALL_FILES=$(git diff --name-only origin/${{ env.BASE_REF }}...HEAD -- '*.py' '*.js' '*.css' 'backend/**/*.py' 'backend/**/*.js' 'backend/**/*.css' 2>/dev/null | tr '\n' ' ')
PY_FILES=$(git diff --name-only origin/${{ env.BASE_REF }}...HEAD -- '*.py' 'backend/**/*.py' 2>/dev/null | tr '\n' ' ')
if [ -z "$ALL_FILES" ]; then
echo "has_reviewable_changes=false" >> $GITHUB_OUTPUT
echo "syntax_ok=true" >> $GITHUB_OUTPUT
echo "## Static Check: 无 Python/JS/CSS 变更" >> $GITHUB_STEP_SUMMARY
exit 0
fi
echo "has_reviewable_changes=true" >> $GITHUB_OUTPUT
SYNTAX_OK=true
if [ -n "$PY_FILES" ]; then
for f in $PY_FILES; do
if [ -f "$f" ]; then
python -m py_compile "$f" 2>/dev/null || SYNTAX_OK=false
fi
done
flake8 --select=E9,F63,F7,F82 $PY_FILES 2>/dev/null || SYNTAX_OK=false
echo "## Static Check: 已检查 $(echo $PY_FILES | wc -w) 个 Python 文件" >> $GITHUB_STEP_SUMMARY
else
echo "## Static Check: 仅 JS/CSS 变更,跳过 Python 检查" >> $GITHUB_STEP_SUMMARY
fi
echo "syntax_ok=$SYNTAX_OK" >> $GITHUB_OUTPUT
ai-review:
name: AI Review
runs-on: ubuntu-latest
needs: [security-check, auto-check]
if: |
always() &&
needs.security-check.outputs.safe_to_run == 'true' &&
needs.auto-check.outputs.has_reviewable_changes == 'true' &&
needs.auto-check.result == 'success'
steps:
- name: Checkout scripts from default branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.repository.default_branch }}
sparse-checkout: |
.github/scripts
path: main-scripts
- name: Checkout PR code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
path: pr-code
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install AI dependencies
run: pip install google-genai openai httpx
- name: Run AI review
working-directory: pr-code
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_BASE_REF: ${{ env.BASE_REF }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GEMINI_MODEL_FALLBACK: ${{ vars.GEMINI_MODEL_FALLBACK || 'gemini-2.5-flash' }}
OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL }}
OPENAI_MODEL: ${{ vars.OPENAI_MODEL }}
run: |
python ../main-scripts/.github/scripts/ai_review.py || echo "⚠️ AI 审查未执行:请配置 GEMINI_API_KEY 或 OPENAI_API_KEY" > ai_review_result.txt
- name: Upload AI review result
uses: actions/upload-artifact@v4
with:
name: ai-review-result
path: pr-code/ai_review_result.txt
labeler:
name: Labeler
runs-on: ubuntu-latest
if: github.event.pull_request != null
steps:
- name: Apply labels
uses: actions/github-script@v7
with:
script: |
try {
const pr = context.payload.pull_request;
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
const labels = new Set();
for (const f of files) {
const fn = f.filename;
if (fn.includes('backend/') && !fn.includes('backend/static/')) labels.add('backend');
if (fn.includes('Plugins/')) labels.add('plugins');
if (fn.includes('backend/static/')) labels.add('frontend');
if (fn.includes('docs/')) labels.add('documentation');
if (fn.includes('.github/')) labels.add('ci/cd');
if (fn.includes('scripts/')) labels.add('scripts');
}
const additions = files.reduce((a, f) => a + (f.additions || 0), 0);
const deletions = files.reduce((a, f) => a + (f.deletions || 0), 0);
const total = additions + deletions;
if (total < 50) labels.add('size/S');
else if (total < 200) labels.add('size/M');
else if (total < 500) labels.add('size/L');
else labels.add('size/XL');
const allLabels = [...labels];
if (allLabels.length > 0) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: allLabels,
});
}
} catch (e) {
console.log('Labeler skipped or failed:', e.message);
}
comment:
name: Comment Report
runs-on: ubuntu-latest
needs: [security-check, auto-check, ai-review]
if: always() && github.event.pull_request != null
steps:
- name: Download AI review artifact
uses: actions/download-artifact@v4
with:
name: ai-review-result
continue-on-error: true
- name: Create or update comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const safeToRun = '${{ needs.security-check.outputs.safe_to_run }}' === 'true';
let aiResult = 'AI 审查未执行或未产生结果。';
if (fs.existsSync('ai_review_result.txt')) {
aiResult = fs.readFileSync('ai_review_result.txt', 'utf8');
}
const prNum = context.payload.pull_request?.number || context.issue?.number;
if (!prNum) return;
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNum,
});
const additions = files.reduce((a, f) => a + (f.additions || 0), 0);
const deletions = files.reduce((a, f) => a + (f.deletions || 0), 0);
const autoOk = '${{ needs.auto-check.outputs.syntax_ok }}' === 'true';
if (!safeToRun) {
aiResult = '⚠️ 本 PR 修改了 `.github/workflows` 或 `.github/scripts`,出于安全考虑已跳过 AI 审查。合并后,后续仅修改业务代码的 PR 将获得完整审查。';
}
const staticStatus = !safeToRun ? '⏭️ 已跳过(敏感文件变更)' : (autoOk ? '✅ 通过' : '❌ 未通过');
const body = [
'## 🤖 自动审查报告',
'',
'### 变更统计',
'- 变更文件:' + files.length,
'- 新增行:+' + additions,
'- 删除行:-' + deletions,
'',
'### 静态检查',
staticStatus,
'',
'### AI 代码审查',
aiResult,
'',
'\u002D\u002D\u002D',
'*本报告由 CI 自动生成*',
].join('\n');
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
});
const bot = comments.find(c => c.user.type === 'Bot' && c.body.includes('## 🤖 自动审查报告'));
if (bot) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: bot.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNum,
body,
});
}