handle-issue-ts.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 AND assignee is NOT claude (avoid loops)
  28. # Allow Claude to work on issues it created and assigned to itself
  29. if [ "$PUSHER" = "claude" ] && [ "$ASSIGNEE" != "claude" ]; then
  30. echo "Pusher is claude and issue not assigned to claude, skipping automation to avoid loops"
  31. exit 0
  32. fi
  33. # Exit if issue is not assigned to claude
  34. if [ "$ASSIGNEE" != "claude" ]; then
  35. echo "Issue not assigned to claude (assignee: $ASSIGNEE), skipping"
  36. exit 0
  37. fi
  38. # Exit if issue action is 'closed' (but allow 'reopened')
  39. if [ "$ISSUE_ACTION" = "closed" ]; then
  40. echo "Issue action is 'closed', skipping automation"
  41. exit 0
  42. fi
  43. # Determine repo path
  44. REPO_PATH="$HOME/$REPO"
  45. # Check if repo exists, if not clone it
  46. if [ ! -d "$REPO_PATH" ]; then
  47. echo "Repository not found at $REPO_PATH, cloning..."
  48. mkdir -p "$HOME"
  49. GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git"
  50. git clone "$GIT_URL" "$REPO_PATH"
  51. if [ $? -ne 0 ]; then
  52. echo "Failed to clone repository"
  53. exit 1
  54. fi
  55. echo "Repository cloned successfully"
  56. else
  57. echo "Repository found at $REPO_PATH"
  58. # Pull latest changes
  59. cd "$REPO_PATH" || exit 1
  60. echo "Pulling latest changes..."
  61. # Try to pull, but don't fail if it errors (might be file system issues)
  62. if ! git pull 2>&1; then
  63. echo "Warning: git pull failed, continuing with existing repository state"
  64. fi
  65. fi
  66. # Change to repo directory
  67. cd "$REPO_PATH" || exit 1
  68. echo "Working directory: $(pwd)"
  69. # Load AGENT_MANAGER_PATH from project .env file
  70. # Try multiple possible locations for the agent-manager .env file
  71. AGENT_MANAGER_ENV_PATHS=(
  72. "/home/claude/agent-manager/.env"
  73. "/data/agent-manager/.env"
  74. "$HOME/agent-manager/.env"
  75. )
  76. AGENT_MANAGER_PATH=""
  77. for ENV_PATH in "${AGENT_MANAGER_ENV_PATHS[@]}"; do
  78. if [ -f "$ENV_PATH" ]; then
  79. # Load AGENT_MANAGER_PATH from .env if it exists
  80. AGENT_MANAGER_PATH=$(grep -E "^AGENT_MANAGER_PATH=" "$ENV_PATH" | cut -d '=' -f2-)
  81. if [ -n "$AGENT_MANAGER_PATH" ]; then
  82. echo "Loaded AGENT_MANAGER_PATH from: $ENV_PATH"
  83. break
  84. fi
  85. fi
  86. done
  87. # Fallback: try to detect from script location
  88. if [ -z "$AGENT_MANAGER_PATH" ]; then
  89. # Get the directory where this script is located
  90. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  91. # Go up one level from scripts/ to get agent-manager root
  92. AGENT_MANAGER_PATH="$(dirname "$SCRIPT_DIR")"
  93. echo "Using detected path from script location: $AGENT_MANAGER_PATH"
  94. fi
  95. echo "Calling Claude Agent SDK TypeScript client..."
  96. echo "Agent Manager Path: $AGENT_MANAGER_PATH"
  97. echo "---"
  98. # Call the TypeScript client
  99. node "$AGENT_MANAGER_PATH/client/dist/index.js" \
  100. "$OWNER" \
  101. "$REPO" \
  102. "$PUSHER" \
  103. "$ISSUE_NUMBER" \
  104. "$ISSUE_TITLE" \
  105. "$ISSUE_BODY" \
  106. "$ISSUE_ACTION" \
  107. "${COMMENT_BODY:-}" \
  108. "${ASSIGNEE:-}"
  109. EXIT_CODE=$?
  110. echo "---"
  111. echo "Claude Agent SDK execution completed (exit code: $EXIT_CODE)"
  112. exit $EXIT_CODE