-
Notifications
You must be signed in to change notification settings - Fork 124
279 lines (244 loc) · 11 KB
/
doc_review.yml
File metadata and controls
279 lines (244 loc) · 11 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
name: AI Doc Review
on:
workflow_dispatch:
issue_comment:
types:
- created
permissions:
contents: read
pull-requests: write
issues: write
jobs:
prepare:
runs-on: ubuntu-latest
if: >
github.event_name == 'workflow_dispatch' ||
(
github.event_name == 'issue_comment' &&
contains(github.event.comment.body, '/bot-review') &&
(github.event.comment.user.login == 'hfxsd' || github.event.comment.user.login == 'likidu' || github.event.comment.user.login == 'lilin90' || github.event.comment.user.login == 'Oreoxmt' || github.event.comment.user.login == 'qiancai')
)
outputs:
zh_files_count: ${{ steps.diff.outputs.zh_files_count }}
en_files_count: ${{ steps.diff.outputs.en_files_count }}
review_mode: ${{ steps.extract.outputs.REVIEW_MODE }}
commit_sha: ${{ steps.extract.outputs.COMMIT_SHA }}
base_sha: ${{ steps.extract.outputs.BASE_SHA }}
head_sha: ${{ steps.extract.outputs.HEAD_SHA }}
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Extract review parameters
id: extract
if: github.event_name == 'issue_comment'
run: |
COMMENT="${{ github.event.comment.body }}"
# Match commit range
if [[ "$COMMENT" =~ \/bot-review:[[:space:]]*([a-f0-9]{7,40})[[:space:]]*\.\.[[:space:]]*([a-f0-9]{7,40}) ]]; then
echo "BASE_SHA=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT
echo "HEAD_SHA=${BASH_REMATCH[2]}" >> $GITHUB_OUTPUT
echo "REVIEW_MODE=commit_range" >> $GITHUB_OUTPUT
echo "Detected commit range"
# Match a single commit
elif [[ "$COMMENT" =~ \/bot-review:[[:space:]]*([a-f0-9]{7,40}) ]]; then
echo "COMMIT_SHA=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT
echo "REVIEW_MODE=single_commit" >> $GITHUB_OUTPUT
printf "Detected single commit: %s\n" "${BASH_REMATCH[1]}"
# Match "/bot-review" or "/bot-review "
elif [[ "$COMMENT" =~ ^\/bot-review[[:space:]]*$ ]]; then
echo "REVIEW_MODE=latest" >> $GITHUB_OUTPUT
echo "Detected default review mode"
# Invalid format
else
echo "REVIEW_MODE=invalid" >> $GITHUB_OUTPUT
echo "Invalid bot-review command format"
fi
- name: Get PR diff
id: diff
run: |
# Initialize variables
PR_NUMBER=""
FILES=""
REVIEW_MODE="${{ steps.extract.outputs.REVIEW_MODE }}"
printf "Review mode detected: %s\n" "$REVIEW_MODE"
# Process single commit mode
if [[ "$REVIEW_MODE" == "single_commit" ]]; then
COMMIT_SHA="${{ steps.extract.outputs.COMMIT_SHA }}"
printf "Processing single commit mode with SHA: %s\n" "$COMMIT_SHA"
FILES=$(git diff-tree --no-commit-id --name-only -r "$COMMIT_SHA" 2>/dev/null || echo "")
if [[ -z "$FILES" ]]; then
printf "Fetching commit %s\n" "$COMMIT_SHA"
git fetch --depth=1 origin "$COMMIT_SHA"
FILES=$(git diff-tree --no-commit-id --name-only -r "$COMMIT_SHA" 2>/dev/null || git show --name-only --format="" "$COMMIT_SHA" 2>/dev/null || echo "")
if [[ -z "$FILES" ]]; then
printf "No files found for commit %s\n" "$COMMIT_SHA"
echo "zh_files_count=0" >> "$GITHUB_OUTPUT"
echo "en_files_count=0" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
ZH_COUNT=0
EN_COUNT=0
while IFS= read -r file; do
if [[ "$file" == zh/* ]]; then
ZH_COUNT=$((ZH_COUNT + 1))
else
# Count any non-zh file as English
EN_COUNT=$((EN_COUNT + 1))
fi
done <<< "$FILES"
printf "Files from commit %s:\n" "$COMMIT_SHA"
echo "$FILES"
echo "Chinese files: $ZH_COUNT"
echo "English files: $EN_COUNT"
# Set output variables
echo "zh_files_count=$ZH_COUNT" >> "$GITHUB_OUTPUT"
echo "en_files_count=$EN_COUNT" >> "$GITHUB_OUTPUT"
exit 0
# Process commit range mode
elif [[ "$REVIEW_MODE" == "commit_range" ]]; then
BASE_SHA="${{ steps.extract.outputs.BASE_SHA }}"
HEAD_SHA="${{ steps.extract.outputs.HEAD_SHA }}"
printf "Processing commit range mode with SHA range: %s..%s\n" "$BASE_SHA" "$HEAD_SHA"
# Try to get files for this commit range
FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" 2>/dev/null || echo "")
if [[ -z "$FILES" ]]; then
printf "Fetching commits %s and %s\n" "$BASE_SHA" "$HEAD_SHA"
git fetch --depth=1 origin "$BASE_SHA" "$HEAD_SHA"
FILES=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" 2>/dev/null || echo "")
if [[ -z "$FILES" ]]; then
printf "No files found in range %s..%s\n" "$BASE_SHA" "$HEAD_SHA"
echo "zh_files_count=0" >> "$GITHUB_OUTPUT"
echo "en_files_count=0" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
ZH_COUNT=0
EN_COUNT=0
while IFS= read -r file; do
if [[ "$file" == zh/* ]]; then
ZH_COUNT=$((ZH_COUNT + 1))
else
# Count any non-zh file as English
EN_COUNT=$((EN_COUNT + 1))
fi
done <<< "$FILES"
printf "Files from commit range %s..%s:\n" "$BASE_SHA" "$HEAD_SHA"
echo "$FILES"
echo "Chinese files: $ZH_COUNT"
echo "English files: $EN_COUNT"
# Set output variables
echo "zh_files_count=$ZH_COUNT" >> "$GITHUB_OUTPUT"
echo "en_files_count=$EN_COUNT" >> "$GITHUB_OUTPUT"
exit 0
# Process default/latest mode (entire PR)
elif [[ "$REVIEW_MODE" == "latest" ]]; then
# Get PR number
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
if [[ "${{ github.ref }}" =~ ^refs/pull/([0-9]+)/merge$ ]]; then
PR_NUMBER="${BASH_REMATCH[1]}"
else
echo "zh_files_count=0" >> "$GITHUB_OUTPUT"
echo "en_files_count=0" >> "$GITHUB_OUTPUT"
exit 0
fi
else
# For issue_comment event - get PR number from different sources
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" || jq --raw-output .issue.number "$GITHUB_EVENT_PATH")
# Extract from issue URL if null
if [[ "$PR_NUMBER" == "null" ]]; then
ISSUE_URL=$(jq --raw-output .issue.html_url "$GITHUB_EVENT_PATH")
if [[ "$ISSUE_URL" =~ /pull/([0-9]+)$ ]]; then
PR_NUMBER="${BASH_REMATCH[1]}"
fi
fi
fi
if [[ -n "$PR_NUMBER" && "$PR_NUMBER" != "null" ]]; then
# Default: get files for the entire PR
FILES=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json files --jq '.files[].path')
if [[ -z "$FILES" ]]; then
# Fallback to git
PR_HEAD=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefName --jq '.headRefName')
PR_BASE=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json baseRefName --jq '.baseRefName')
if [[ -n "$PR_HEAD" && -n "$PR_BASE" ]]; then
git fetch origin "$PR_HEAD" "$PR_BASE"
MERGE_BASE=$(git merge-base "origin/$PR_BASE" "origin/$PR_HEAD")
FILES=$(git diff --name-only "$MERGE_BASE" "origin/$PR_HEAD")
fi
fi
ZH_COUNT=0
EN_COUNT=0
while IFS= read -r file; do
if [[ "$file" == zh/* ]]; then
ZH_COUNT=$((ZH_COUNT + 1))
else
# Count any non-zh file as English
EN_COUNT=$((EN_COUNT + 1))
fi
done <<< "$FILES"
printf "Files from PR %s:\n" "$PR_NUMBER"
echo "$FILES"
echo "Chinese files: $ZH_COUNT"
echo "English files: $EN_COUNT"
echo "zh_files_count=$ZH_COUNT" >> "$GITHUB_OUTPUT"
echo "en_files_count=$EN_COUNT" >> "$GITHUB_OUTPUT"
else
echo "Could not determine PR number"
echo "zh_files_count=0" >> "$GITHUB_OUTPUT"
echo "en_files_count=0" >> "$GITHUB_OUTPUT"
fi
else
echo "Invalid review mode"
echo "zh_files_count=0" >> "$GITHUB_OUTPUT"
echo "en_files_count=0" >> "$GITHUB_OUTPUT"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
review-zh:
needs: prepare
if: needs.prepare.outputs.zh_files_count > 0
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: AI Doc Reviewer for Chinese docs
uses: qiancai/ai-codereviewer@main
continue-on-error: false
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
API_PROVIDER: "deepseek"
DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
DEEPSEEK_API_MODEL: "deepseek-chat"
exclude: "**/*.json,!zh/**" # Exclude JSON files and non-Chinese docs
REVIEW_MODE: ${{ needs.prepare.outputs.review_mode }}
COMMIT_SHA: ${{ needs.prepare.outputs.commit_sha }}
BASE_SHA: ${{ needs.prepare.outputs.base_sha }}
HEAD_SHA: ${{ needs.prepare.outputs.head_sha }}
PROMPT_PATH: "doc-review-prompt-zh.txt"
review-en:
needs: prepare
if: needs.prepare.outputs.en_files_count > 0
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: AI Doc Reviewer for English docs
uses: qiancai/ai-codereviewer@main
continue-on-error: false
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
API_PROVIDER: "openai"
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4o"
exclude: "**/*.json,zh/**" # Exclude JSON files and Chinese docs
REVIEW_MODE: ${{ needs.prepare.outputs.review_mode }}
COMMIT_SHA: ${{ needs.prepare.outputs.commit_sha }}
BASE_SHA: ${{ needs.prepare.outputs.base_sha }}
HEAD_SHA: ${{ needs.prepare.outputs.head_sha }}
PROMPT_PATH: "doc-review-prompt-en.txt"