handle-issue-ts.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. # TypeScript-based issue handler using Claude Agent SDK
  3. # This script replaces the old bash-based Claude Code implementation
  4. # Ensure HOME is set (systemd might not set it)
  5. if [ -z "$HOME" ]; then
  6. export HOME=$(getent passwd "$(whoami)" | cut -d: -f6)
  7. fi
  8. # Add NODE to PATH
  9. export PATH="$HOME/.local/bin:$PATH"
  10. # Ensure Claude Code has writable directories for cache and config
  11. export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
  12. export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
  13. export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
  14. export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
  15. # Create these directories if they don't exist
  16. mkdir -p "$XDG_CACHE_HOME" "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_STATE_HOME"
  17. # Parse arguments
  18. OWNER="$1"
  19. REPO="$2"
  20. PUSHER="$3"
  21. ISSUE_NUMBER="$4"
  22. ISSUE_TITLE="$5"
  23. ISSUE_BODY="$6"
  24. ISSUE_ACTION="$7"
  25. COMMENT_BODY="$8"
  26. ASSIGNEE="$9"
  27. # Exit if pusher is claude (avoid loops)
  28. if [ "$PUSHER" = "claude" ]; then
  29. echo "Pusher is claude, skipping automation to avoid loops"
  30. exit 0
  31. fi
  32. # Exit if issue is not assigned to claude
  33. if [ "$ASSIGNEE" != "claude" ]; then
  34. echo "Issue not assigned to claude (assignee: $ASSIGNEE), skipping"
  35. exit 0
  36. fi
  37. # Exit if issue action is 'closed' (but allow 'reopened')
  38. if [ "$ISSUE_ACTION" = "closed" ]; then
  39. echo "Issue action is 'closed', skipping automation"
  40. exit 0
  41. fi
  42. # Determine repo path
  43. REPO_PATH="$HOME/$REPO"
  44. # Check if repo exists, if not clone it
  45. if [ ! -d "$REPO_PATH" ]; then
  46. echo "Repository not found at $REPO_PATH, cloning..."
  47. mkdir -p "$HOME"
  48. GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git"
  49. git clone "$GIT_URL" "$REPO_PATH"
  50. if [ $? -ne 0 ]; then
  51. echo "Failed to clone repository"
  52. exit 1
  53. fi
  54. echo "Repository cloned successfully"
  55. else
  56. echo "Repository found at $REPO_PATH"
  57. # Pull latest changes
  58. cd "$REPO_PATH" || exit 1
  59. echo "Pulling latest changes..."
  60. # Try to pull, but don't fail if it errors (might be file system issues)
  61. if ! git pull 2>&1; then
  62. echo "Warning: git pull failed, continuing with existing repository state"
  63. fi
  64. fi
  65. # Change to repo directory
  66. cd "$REPO_PATH" || exit 1
  67. echo "Working directory: $(pwd)"
  68. # Load AGENT_MANAGER_PATH from project .env file
  69. # Try multiple possible locations for the agent-manager .env file
  70. AGENT_MANAGER_ENV_PATHS=(
  71. "/home/claude/agent-manager/.env"
  72. "/data/agent-manager/.env"
  73. "$HOME/agent-manager/.env"
  74. )
  75. AGENT_MANAGER_PATH=""
  76. for ENV_PATH in "${AGENT_MANAGER_ENV_PATHS[@]}"; do
  77. if [ -f "$ENV_PATH" ]; then
  78. # Load AGENT_MANAGER_PATH from .env if it exists
  79. AGENT_MANAGER_PATH=$(grep -E "^AGENT_MANAGER_PATH=" "$ENV_PATH" | cut -d '=' -f2-)
  80. if [ -n "$AGENT_MANAGER_PATH" ]; then
  81. echo "Loaded AGENT_MANAGER_PATH from: $ENV_PATH"
  82. break
  83. fi
  84. fi
  85. done
  86. # Fallback: try to detect from script location
  87. if [ -z "$AGENT_MANAGER_PATH" ]; then
  88. # Get the directory where this script is located
  89. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  90. # Go up one level from scripts/ to get agent-manager root
  91. AGENT_MANAGER_PATH="$(dirname "$SCRIPT_DIR")"
  92. echo "Using detected path from script location: $AGENT_MANAGER_PATH"
  93. fi
  94. echo "Calling Claude Agent SDK TypeScript client..."
  95. echo "Agent Manager Path: $AGENT_MANAGER_PATH"
  96. echo "---"
  97. # Call the TypeScript client
  98. node "$AGENT_MANAGER_PATH/client/dist/index.js" \
  99. "$OWNER" \
  100. "$REPO" \
  101. "$PUSHER" \
  102. "$ISSUE_NUMBER" \
  103. "$ISSUE_TITLE" \
  104. "$ISSUE_BODY" \
  105. "$ISSUE_ACTION" \
  106. "${COMMENT_BODY:-}" \
  107. "${ASSIGNEE:-}"
  108. EXIT_CODE=$?
  109. echo "---"
  110. echo "Claude Agent SDK execution completed (exit code: $EXIT_CODE)"
  111. exit $EXIT_CODE