ソースを参照

fix: correct Claude Code headless mode invocation in issue handler

The previous implementation had incorrect flags that caused Claude Code
to run but exit without producing any output or comments on issues.

Changes:
- Fixed flag usage: use -p with --output-format json (not stream-json)
- Capture output properly using OUTPUT=$(...) instead of direct execution
- Added JSON output parsing to log session details for debugging
- Added proper error handling and logging for both success and failure cases
- Include stderr in output capture (2>&1) for better error visibility

According to Claude Code headless documentation:
- --print/-p is for non-interactive mode
- --output-format json provides structured output for parsing
- stream-json is for multi-turn streaming, not single-turn execution

Tested with test-claude-simple.sh which confirms Claude Code now
executes successfully and returns proper JSON responses.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Claude 9 ヶ月 前
コミット
34019beb0e
1 ファイル変更18 行追加3 行削除
  1. 18 3
      scripts/handle-issue.sh

+ 18 - 3
scripts/handle-issue.sh

@@ -215,14 +215,29 @@ echo "Calling Claude Code with the issue information..."
 echo "---"
 
 # Call claude with the prompt in non-interactive mode
-# --print: Non-interactive mode, prints output and exits
+# -p / --print: Non-interactive mode, prints output and exits
+# --output-format json: Get structured JSON output for programmatic parsing
 # --dangerously-skip-permissions: Skip all permission prompts (safe in controlled environment)
 # This allows Claude to run unattended without waiting for user input
-claude --print --dangerously-skip-permissions "$PROMPT"
-
+OUTPUT=$(claude -p "$PROMPT" --output-format json --dangerously-skip-permissions 2>&1)
 EXIT_CODE=$?
 
 echo "---"
 echo "Claude Code execution completed (exit code: $EXIT_CODE)"
 
+# Log the output for debugging
+if [ $EXIT_CODE -eq 0 ]; then
+    echo "Claude response received successfully"
+    # Parse JSON output to check for errors or important info
+    if command -v jq &> /dev/null; then
+        echo "Session details:"
+        echo "$OUTPUT" | jq -r '.sessionId, .cost, .duration' 2>/dev/null || echo "$OUTPUT"
+    else
+        echo "Output (first 500 chars): ${OUTPUT:0:500}"
+    fi
+else
+    echo "Error: Claude Code failed with exit code $EXIT_CODE"
+    echo "Output: $OUTPUT"
+fi
+
 exit $EXIT_CODE