handle-issue-ts.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # Determine repo path
  38. REPO_PATH="$HOME/$REPO"
  39. # Check if repo exists, if not clone it
  40. if [ ! -d "$REPO_PATH" ]; then
  41. echo "Repository not found at $REPO_PATH, cloning..."
  42. mkdir -p "$HOME"
  43. GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git"
  44. git clone "$GIT_URL" "$REPO_PATH"
  45. if [ $? -ne 0 ]; then
  46. echo "Failed to clone repository"
  47. exit 1
  48. fi
  49. echo "Repository cloned successfully"
  50. else
  51. echo "Repository found at $REPO_PATH"
  52. # Pull latest changes
  53. cd "$REPO_PATH" || exit 1
  54. echo "Pulling latest changes..."
  55. # Try to pull, but don't fail if it errors (might be file system issues)
  56. if ! git pull 2>&1; then
  57. echo "Warning: git pull failed, continuing with existing repository state"
  58. fi
  59. fi
  60. # Change to repo directory
  61. cd "$REPO_PATH" || exit 1
  62. echo "Working directory: $(pwd)"
  63. echo "Calling Claude Agent SDK TypeScript client..."
  64. echo "---"
  65. # Get the path to the agent-manager directory
  66. AGENT_MANAGER_DIR="/data/agent-manager"
  67. # Call the TypeScript client
  68. node "$AGENT_MANAGER_DIR/client/dist/index.js" \
  69. "$OWNER" \
  70. "$REPO" \
  71. "$PUSHER" \
  72. "$ISSUE_NUMBER" \
  73. "$ISSUE_TITLE" \
  74. "$ISSUE_BODY" \
  75. "$ISSUE_ACTION" \
  76. "${COMMENT_BODY:-}" \
  77. "${ASSIGNEE:-}"
  78. EXIT_CODE=$?
  79. echo "---"
  80. echo "Claude Agent SDK execution completed (exit code: $EXIT_CODE)"
  81. exit $EXIT_CODE