From 0a6775ae1245d441bc9886b041a7c12ae6f7a193 Mon Sep 17 00:00:00 2001 From: LearningCircuit <185559241+LearningCircuit@users.noreply.github.com> Date: Thu, 13 Nov 2025 22:08:36 +0100 Subject: [PATCH 1/2] fix: capture and display ai-reviewer.sh error messages in workflow Previously, when ai-reviewer.sh failed with exit code 1, the workflow would immediately stop due to shell errexit (-e flag) without displaying the actual error message. This made debugging failures very difficult. Changes: - Temporarily disable errexit with 'set +e' before running ai-reviewer.sh - Capture both stdout and stderr with '2>&1' redirection - Store the exit code separately for inspection - Re-enable errexit with 'set -e' after capture - Check exit code and display full error output if non-zero - Include exit code in debug output for better troubleshooting This fix ensures that when the AI reviewer fails, users will see the actual error message (e.g., "Missing OPENROUTER_API_KEY", "Diff too large", "API call failed") instead of just "Error: Process completed with exit code 1". --- .github/workflows/ai-code-reviewer.yml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ai-code-reviewer.yml b/.github/workflows/ai-code-reviewer.yml index db53033..44c5c54 100644 --- a/.github/workflows/ai-code-reviewer.yml +++ b/.github/workflows/ai-code-reviewer.yml @@ -46,15 +46,30 @@ jobs: run: | # Run the AI reviewer and save response to file echo "Running AI code review..." - AI_RESPONSE=$(cat diff.txt | bash ai-reviewer.sh) + + # Temporarily disable errexit to capture errors properly + set +e + AI_RESPONSE=$(cat diff.txt | bash ai-reviewer.sh 2>&1) + EXIT_CODE=$? + set -e # Output raw response for debugging (only in debug mode) if [ "$DEBUG_MODE" = "true" ]; then echo "=== RAW AI RESPONSE FOR DEBUG ===" + echo "Exit code: $EXIT_CODE" echo "$AI_RESPONSE" echo "=== END RAW AI RESPONSE ===" fi + # Check if the script failed + if [ $EXIT_CODE -ne 0 ]; then + echo "❌ AI reviewer script failed with exit code $EXIT_CODE" + echo "" + echo "Error output:" + echo "$AI_RESPONSE" + exit 1 + fi + # Check if AI_RESPONSE is empty if [ -z "$AI_RESPONSE" ]; then echo "❌ AI response is empty" From 6485056d6ecac354472374399468a7001dc20e0b Mon Sep 17 00:00:00 2001 From: LearningCircuit <185559241+LearningCircuit@users.noreply.github.com> Date: Thu, 13 Nov 2025 22:52:33 +0100 Subject: [PATCH 2/2] fix: only output debug messages when DEBUG_MODE=true The workflow's stderr capture (2>&1) was picking up progress messages from ai-reviewer.sh, contaminating the JSON response. Now these messages only appear in debug mode, keeping stdout clean for JSON parsing. --- ai-reviewer.sh | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/ai-reviewer.sh b/ai-reviewer.sh index 917b717..cb7ab1d 100644 --- a/ai-reviewer.sh +++ b/ai-reviewer.sh @@ -95,26 +95,32 @@ fi AVAILABLE_LABELS="" if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then # Fetch all labels from the repository - echo "🔍 Fetching available labels from repository..." >&2 + if [ "$DEBUG_MODE" = "true" ]; then + echo "🔍 Fetching available labels from repository..." >&2 + fi AVAILABLE_LABELS=$(gh api "repos/$REPO_FULL_NAME/labels" --paginate 2>/dev/null \ --jq '.[] | "- **\(.name)**: \(.description // "No description") (color: #\(.color))"' || echo "") - if [ -n "$AVAILABLE_LABELS" ]; then - LABEL_COUNT=$(echo "$AVAILABLE_LABELS" | wc -l) - echo "✅ Successfully fetched $LABEL_COUNT labels from repository" >&2 - else - echo "â„šī¸ No existing labels found in repository or API call failed" >&2 + if [ "$DEBUG_MODE" = "true" ]; then + if [ -n "$AVAILABLE_LABELS" ]; then + LABEL_COUNT=$(echo "$AVAILABLE_LABELS" | wc -l) + echo "✅ Successfully fetched $LABEL_COUNT labels from repository" >&2 + else + echo "â„šī¸ No existing labels found in repository or API call failed" >&2 + fi fi fi # Fetch PR title and description PR_DESCRIPTION="" if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then - echo "🔍 Fetching PR title and description..." >&2 + if [ "$DEBUG_MODE" = "true" ]; then + echo "🔍 Fetching PR title and description..." >&2 + fi PR_DESCRIPTION=$(gh api "repos/$REPO_FULL_NAME/pulls/$PR_NUMBER" \ --jq '"**PR Title**: " + .title + "\n\n**Description**:\n" + (.body // "No description provided")' 2>/dev/null | head -c 2000 || echo "") - if [ -n "$PR_DESCRIPTION" ]; then + if [ "$DEBUG_MODE" = "true" ] && [ -n "$PR_DESCRIPTION" ]; then echo "✅ Successfully fetched PR description" >&2 fi fi @@ -122,11 +128,13 @@ fi # Fetch commit messages (limit to 15 most recent, exclude merges) COMMIT_MESSAGES="" if [ -n "$PR_NUMBER" ] && [ -n "$REPO_FULL_NAME" ] && [ -n "$GITHUB_TOKEN" ]; then - echo "🔍 Fetching commit messages..." >&2 + if [ "$DEBUG_MODE" = "true" ]; then + echo "🔍 Fetching commit messages..." >&2 + fi COMMIT_MESSAGES=$(gh api "repos/$REPO_FULL_NAME/pulls/$PR_NUMBER/commits" --paginate \ --jq '[.[] | select(.commit.message | startswith("Merge") | not)] | .[-15:] | .[] | "- " + (.commit.message | split("\n")[0]) + (if (.commit.message | split("\n\n")[1]) then "\n " + (.commit.message | split("\n\n")[1]) else "" end)' 2>/dev/null | head -c 2500 || echo "") - if [ -n "$COMMIT_MESSAGES" ]; then + if [ "$DEBUG_MODE" = "true" ] && [ -n "$COMMIT_MESSAGES" ]; then COMMIT_COUNT=$(echo "$COMMIT_MESSAGES" | grep -c "^- " || echo "0") echo "✅ Successfully fetched $COMMIT_COUNT commit messages" >&2 fi