Skip to content

Commit 4e4db68

Browse files
liuxiaotongliuxiaotong
andauthored
fix: auto-review JSON 解析误判 bug (#10)
* 升级 auto-review workflow:异步API + 审查闭环 * fix: auto-review JSON 解析改用逐位置尝试,修复 approved 误判 bug --------- Co-authored-by: liuxiaotong <liuxiaotong@knowlyr.com>
1 parent 2fd742a commit 4e4db68

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

.github/workflows/auto-review.yml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,32 @@ jobs:
176176
RESULT='{"approved": false, "summary": "Review task timed out", "comments": []}'
177177
else
178178
# 解析返回结果:result.output 是林锐的文本输出,从中提取 JSON
179+
# 逐位置尝试解析,找包含 approved key 的 JSON 对象(避免贪婪正则被文本中花括号干扰)
179180
RESULT=$(echo "$RESPONSE" | python3 -c "
180-
import json, sys, re
181+
import json, sys
182+
181183
data = json.load(sys.stdin)
182184
result = data.get('result', {})
183185
text = result.get('output', '') if isinstance(result, dict) else str(result)
184-
# 尝试从文本中提取 JSON
185-
match = re.search(r'\{.*\}', text, re.DOTALL)
186-
if match:
187-
try:
188-
parsed = json.loads(match.group())
189-
print(json.dumps(parsed))
190-
except:
191-
print(json.dumps({'approved': False, 'summary': text[:2000], 'comments': []}))
186+
187+
# 逐位置尝试解析 JSON,找包含 approved 的对象
188+
parsed = None
189+
for i, ch in enumerate(text):
190+
if ch == '{':
191+
for j in range(len(text), i, -1):
192+
if text[j-1] == '}':
193+
try:
194+
candidate = json.loads(text[i:j])
195+
if isinstance(candidate, dict) and 'approved' in candidate:
196+
parsed = candidate
197+
break
198+
except (json.JSONDecodeError, ValueError):
199+
continue
200+
if parsed:
201+
break
202+
203+
if parsed:
204+
print(json.dumps(parsed))
192205
else:
193206
print(json.dumps({'approved': False, 'summary': text[:2000], 'comments': []}))
194207
" 2>/dev/null || echo '{"approved": false, "summary": "Failed to parse review response", "comments": []}')

0 commit comments

Comments
 (0)