Skip to content

Commit 5d8a040

Browse files
liuxiaotongliuxiaotong
andauthored
升级 auto-review workflow:异步API + 审查闭环 (#8)
Co-authored-by: liuxiaotong <liuxiaotong@knowlyr.com>
1 parent 64f2d22 commit 5d8a040

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

.github/workflows/auto-review.yml

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ jobs:
119119
- summary: 审查总结
120120
- comments: 具体意见数组(每条包含 file, line, comment)"
121121
122-
# 调用林锐审查
123-
RESPONSE=$(curl -s -X POST "https://crew.knowlyr.com/api/run/employee/code-reviewer" \
122+
# 调用林锐审查(异步 API:POST 返回 task_id,轮询等结果)
123+
SUBMIT=$(curl -s -X POST "https://crew.knowlyr.com/run/employee/code-reviewer" \
124124
-H "Authorization: Bearer ${CREW_API_TOKEN}" \
125125
-H "Content-Type: application/json" \
126126
-d "$(python3 -c "
@@ -129,24 +129,60 @@ jobs:
129129
print(json.dumps({'task': task, 'format': 'json'}))
130130
" <<< "$TASK")")
131131
132-
echo "Review response received"
133-
134-
# 解析返回结果,提取 JSON
135-
RESULT=$(echo "$RESPONSE" | python3 -c "
132+
TASK_ID=$(echo "$SUBMIT" | python3 -c "import json,sys; print(json.load(sys.stdin).get('task_id',''))" 2>/dev/null)
133+
if [ -z "$TASK_ID" ]; then
134+
echo "Failed to submit review task: $SUBMIT"
135+
RESULT='{"approved": false, "summary": "Failed to submit review task", "comments": []}'
136+
else
137+
echo "Review task submitted: $TASK_ID — polling for result..."
138+
139+
# 轮询等待完成(最多 5 分钟)
140+
POLL_TIMEOUT=300
141+
POLL_INTERVAL=10
142+
POLL_ELAPSED=0
143+
RESPONSE=""
144+
145+
while [ $POLL_ELAPSED -lt $POLL_TIMEOUT ]; do
146+
sleep $POLL_INTERVAL
147+
POLL_ELAPSED=$((POLL_ELAPSED + POLL_INTERVAL))
148+
POLL_RESP=$(curl -s "https://crew.knowlyr.com/tasks/${TASK_ID}" \
149+
-H "Authorization: Bearer ${CREW_API_TOKEN}")
150+
STATUS=$(echo "$POLL_RESP" | python3 -c "import json,sys; print(json.load(sys.stdin).get('status','unknown'))" 2>/dev/null)
151+
152+
if [ "$STATUS" = "completed" ]; then
153+
RESPONSE="$POLL_RESP"
154+
echo "Review completed (${POLL_ELAPSED}s)"
155+
break
156+
elif [ "$STATUS" = "failed" ]; then
157+
echo "Review task failed"
158+
break
159+
fi
160+
echo " Still running... (${POLL_ELAPSED}s / ${POLL_TIMEOUT}s)"
161+
done
162+
163+
if [ -z "$RESPONSE" ]; then
164+
echo "Review task timed out or failed"
165+
RESULT='{"approved": false, "summary": "Review task timed out", "comments": []}'
166+
else
167+
# 解析返回结果:result.output 是林锐的文本输出,从中提取 JSON
168+
RESULT=$(echo "$RESPONSE" | python3 -c "
136169
import json, sys, re
137170
data = json.load(sys.stdin)
138-
text = data.get('result', data.get('output', ''))
171+
result = data.get('result', {})
172+
text = result.get('output', '') if isinstance(result, dict) else str(result)
139173
# 尝试从文本中提取 JSON
140174
match = re.search(r'\{.*\}', text, re.DOTALL)
141175
if match:
142176
try:
143177
parsed = json.loads(match.group())
144178
print(json.dumps(parsed))
145179
except:
146-
print(json.dumps({'approved': False, 'summary': text, 'comments': []}))
180+
print(json.dumps({'approved': False, 'summary': text[:2000], 'comments': []}))
147181
else:
148-
print(json.dumps({'approved': False, 'summary': text, 'comments': []}))
182+
print(json.dumps({'approved': False, 'summary': text[:2000], 'comments': []}))
149183
" 2>/dev/null || echo '{"approved": false, "summary": "Failed to parse review response", "comments": []}')
184+
fi
185+
fi
150186
151187
APPROVED=$(echo "$RESULT" | python3 -c "import json,sys; print(json.load(sys.stdin).get('approved', False))")
152188
SUMMARY=$(echo "$RESULT" | python3 -c "import json,sys; print(json.load(sys.stdin).get('summary', 'No summary'))")
@@ -198,7 +234,7 @@ jobs:
198234
)"
199235
else
200236
echo "PR not approved — leaving review comments"
201-
gh pr comment "$PR_NUMBER" --body "$(cat <<EOF
237+
gh pr review "$PR_NUMBER" --request-changes --body "$(cat <<EOF
202238
**Auto Review (Round ${ROUND}) — Changes Requested**
203239
204240
${SUMMARY}
@@ -214,7 +250,7 @@ jobs:
214250
gh pr edit "$PR_NUMBER" --add-label "needs-human-review"
215251
216252
# 通知 Kai
217-
curl -s -X POST "https://crew.knowlyr.com/api/run/employee/ceo-assistant" \
253+
curl -s -X POST "https://crew.knowlyr.com/run/employee/ceo-assistant" \
218254
-H "Authorization: Bearer ${CREW_API_TOKEN}" \
219255
-H "Content-Type: application/json" \
220256
-d "$(python3 -c "

0 commit comments

Comments
 (0)