Skip to content

Commit b79f032

Browse files
fix: extract JSON from mixed debug output in workflow
When DEBUG_MODE is enabled, the workflow captures both debug output (stderr) and JSON output (stdout) together. This caused JSON parsing to fail because it tried to parse the entire mixed output. Now the workflow extracts just the JSON object from the mixed output before attempting to parse it.
1 parent 6461e8d commit b79f032

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

.github/workflows/ai-code-reviewer.yml

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,29 @@ jobs:
7676
exit 1
7777
fi
7878
79+
# Extract only the JSON part (find the last valid JSON object in output)
80+
# This handles debug output being mixed with the JSON response
81+
# Try to find JSON by looking for lines starting with { and ending with }
82+
AI_JSON=$(echo "$AI_RESPONSE" | awk '/^{$/,/^}$/{print}' | tail -n +1)
83+
84+
# If that didn't work, try extracting just the last line that looks like JSON
85+
if [ -z "$AI_JSON" ] || ! echo "$AI_JSON" | jq . >/dev/null 2>&1; then
86+
# Try to extract the last complete JSON object using perl
87+
AI_JSON=$(echo "$AI_RESPONSE" | perl -0777 -ne 'print $1 if /(\{(?:[^{}]|(?R))*\})[^\{]*$/s' | tail -c 1000000)
88+
fi
89+
90+
if [ -z "$AI_JSON" ]; then
91+
echo "⚠️ Could not extract JSON from AI response."
92+
if [ "$DEBUG_MODE" = "true" ]; then
93+
echo "=== DEBUG: Full response (first 3000 chars) ==="
94+
echo "$AI_RESPONSE" | head -c 3000
95+
echo "=== END DEBUG ==="
96+
fi
97+
exit 1
98+
fi
99+
79100
# Parse JSON response
80-
if ! echo "$AI_RESPONSE" | jq . >/dev/null 2>&1; then
101+
if ! echo "$AI_JSON" | jq . >/dev/null 2>&1; then
81102
echo "⚠️ AI response is not valid JSON. Cannot process review."
82103
83104
# Log raw response for debugging (redact sensitive info)
@@ -103,9 +124,9 @@ jobs:
103124
fi
104125
105126
# Extract fields from JSON
106-
REVIEW=$(echo "$AI_RESPONSE" | jq -r '.review // "No review provided"')
107-
DECISION=$(echo "$AI_RESPONSE" | jq -r '.fail_pass_workflow // "uncertain"')
108-
LABELS=$(echo "$AI_RESPONSE" | jq -r '.labels_added[]? // empty')
127+
REVIEW=$(echo "$AI_JSON" | jq -r '.review // "No review provided"')
128+
DECISION=$(echo "$AI_JSON" | jq -r '.fail_pass_workflow // "uncertain"')
129+
LABELS=$(echo "$AI_JSON" | jq -r '.labels_added[]? // empty')
109130
110131
# Post the review as PR comment
111132
echo "$REVIEW" | gh pr comment ${{ github.event.pull_request.number }} --repo ${{ github.repository }} -F -

0 commit comments

Comments
 (0)