#!/bin/bash # Script to handle issue automation with Claude Code # This script is called when an issue is opened or commented on # Ensure HOME is set (systemd might not set it) if [ -z "$HOME" ]; then export HOME=$(getent passwd "$(whoami)" | cut -d: -f6) fi # Add claude binary to PATH export PATH="$HOME/.local/bin:$PATH" # Ensure Claude Code has writable directories for cache and config export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}" export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}" # Create these directories if they don't exist mkdir -p "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME" # Parse arguments OWNER="$1" REPO="$2" PUSHER="$3" ISSUE_NUMBER="$4" ISSUE_TITLE="$5" ISSUE_BODY="$6" ISSUE_ACTION="$7" COMMENT_BODY="$8" ASSIGNEE="$9" # Exit if pusher is claude (avoid loops) if [ "$PUSHER" = "claude" ]; then echo "Pusher is claude, skipping automation to avoid loops" exit 0 fi # Exit if issue is not assigned to claude if [ "$ASSIGNEE" != "claude" ]; then echo "Issue not assigned to claude (assignee: $ASSIGNEE), skipping" exit 0 fi # Determine repo path REPO_PATH="$HOME/$REPO" # Check if repo exists, if not clone it if [ ! -d "$REPO_PATH" ]; then echo "Repository not found at $REPO_PATH, cloning..." mkdir -p "$HOME" GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git" git clone "$GIT_URL" "$REPO_PATH" if [ $? -ne 0 ]; then echo "Failed to clone repository" exit 1 fi echo "Repository cloned successfully" else echo "Repository found at $REPO_PATH" # Pull latest changes cd "$REPO_PATH" || exit 1 echo "Pulling latest changes..." # Try to pull, but don't fail if it errors (might be file system issues) if ! git pull 2>&1; then echo "Warning: git pull failed, continuing with existing repository state" fi fi # Change to repo directory cd "$REPO_PATH" || exit 1 echo "Working directory: $(pwd)" # Load .env file to get GOGS_MCP_PATH if [ -f "$REPO_PATH/.env" ]; then export $(grep -v '^#' "$REPO_PATH/.env" | grep GOGS_MCP_PATH | xargs) fi # Set default GOGS_MCP_PATH if not set GOGS_MCP_PATH="${GOGS_MCP_PATH:-/data/gogs-mcp/dist/index.js}" # Generate MCP config file dynamically with environment-specific path cat > "$REPO_PATH/.mcp-gogs.json" <&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