handle-issue.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. # Script to handle issue automation with Claude Code
  3. # This script is called when an issue is opened or commented on
  4. # Add claude binary to PATH
  5. export PATH="$HOME/.local/bin:$PATH"
  6. # Parse arguments
  7. OWNER="$1"
  8. REPO="$2"
  9. PUSHER="$3"
  10. ISSUE_NUMBER="$4"
  11. ISSUE_TITLE="$5"
  12. ISSUE_BODY="$6"
  13. ISSUE_ACTION="$7"
  14. COMMENT_BODY="$8"
  15. ASSIGNEE="$9"
  16. # Exit if pusher is claude (avoid loops)
  17. if [ "$PUSHER" = "claude" ]; then
  18. echo "Pusher is claude, skipping automation to avoid loops"
  19. exit 0
  20. fi
  21. # Exit if issue is not assigned to claude
  22. if [ "$ASSIGNEE" != "claude" ]; then
  23. echo "Issue not assigned to claude (assignee: $ASSIGNEE), skipping"
  24. exit 0
  25. fi
  26. # Determine repo path
  27. REPO_PATH="$HOME/$REPO"
  28. # Check if repo exists, if not clone it
  29. if [ ! -d "$REPO_PATH" ]; then
  30. echo "Repository not found at $REPO_PATH, cloning..."
  31. mkdir -p "$HOME"
  32. GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git"
  33. git clone "$GIT_URL" "$REPO_PATH"
  34. if [ $? -ne 0 ]; then
  35. echo "Failed to clone repository"
  36. exit 1
  37. fi
  38. echo "Repository cloned successfully"
  39. else
  40. echo "Repository found at $REPO_PATH"
  41. # Pull latest changes
  42. cd "$REPO_PATH" || exit 1
  43. echo "Pulling latest changes..."
  44. git pull
  45. fi
  46. # Change to repo directory
  47. cd "$REPO_PATH" || exit 1
  48. echo "Working directory: $(pwd)"
  49. # Build the prompt for Claude with instructions to use Gogs MCP tool
  50. if [ -n "$COMMENT_BODY" ]; then
  51. # Issue comment event - new comment was added
  52. PROMPT="IMPORTANT: A new comment has been added to issue #$ISSUE_NUMBER in repository $OWNER/$REPO.
  53. === MANDATORY FIRST STEPS - READ ALL MESSAGES ===
  54. Before you start any work on this issue, you MUST complete these steps IN ORDER:
  55. 1. Use the mcp__gogs__get_issue tool to fetch the complete issue details:
  56. - owner: $OWNER
  57. - repo: $REPO
  58. - number: $ISSUE_NUMBER
  59. 2. Use the mcp__gogs__list_issue_comments tool to fetch ALL comments:
  60. - owner: $OWNER
  61. - repo: $REPO
  62. - number: $ISSUE_NUMBER
  63. 3. Read and understand:
  64. - The original issue description
  65. - ALL comments in the discussion thread (chronologically)
  66. - The full context and conversation history
  67. 4. ONLY AFTER reading everything above, proceed with your work
  68. === ISSUE CONTEXT ===
  69. Issue #$ISSUE_NUMBER: $ISSUE_TITLE
  70. A new comment was just added by: $PUSHER
  71. Initial issue description:
  72. $ISSUE_BODY
  73. Most recent comment:
  74. $COMMENT_BODY
  75. === YOUR TASK ===
  76. This issue is assigned to you. You must:
  77. 1. First, use the Gogs MCP tools above to fetch and read ALL issue comments
  78. 2. Understand the full discussion context
  79. 3. Then proceed with the appropriate action based on the complete conversation
  80. DO NOT skip reading the comments using the MCP tools. This is critical to understanding the full context."
  81. else
  82. # Issue opened/reopened event
  83. PROMPT="IMPORTANT: Issue #$ISSUE_NUMBER was $ISSUE_ACTION in repository $OWNER/$REPO.
  84. === MANDATORY FIRST STEPS - READ ALL MESSAGES ===
  85. Before you start any work on this issue, you MUST complete these steps IN ORDER:
  86. 1. Use the mcp__gogs__get_issue tool to fetch the complete issue details:
  87. - owner: $OWNER
  88. - repo: $REPO
  89. - number: $ISSUE_NUMBER
  90. 2. Use the mcp__gogs__list_issue_comments tool to fetch ALL comments:
  91. - owner: $OWNER
  92. - repo: $REPO
  93. - number: $ISSUE_NUMBER
  94. 3. Read and understand:
  95. - The complete issue description
  96. - ALL comments in the discussion thread (if any exist)
  97. - The full context
  98. 4. ONLY AFTER reading everything above, proceed with your work
  99. === ISSUE CONTEXT ===
  100. Issue #$ISSUE_NUMBER: $ISSUE_TITLE
  101. Created by: $PUSHER
  102. Initial issue description:
  103. $ISSUE_BODY
  104. === YOUR TASK ===
  105. This issue is assigned to you. You must:
  106. 1. First, use the Gogs MCP tools above to fetch and read the issue and ALL comments
  107. 2. Understand the full discussion context (even if there are no comments yet)
  108. 3. Then proceed with the appropriate action
  109. DO NOT skip reading the comments using the MCP tools. This is critical to understanding the full context."
  110. fi
  111. echo "Calling Claude Code with the issue information..."
  112. echo "---"
  113. # Call claude with the prompt in non-interactive mode
  114. # --print: Non-interactive mode, prints output and exits
  115. # --dangerously-skip-permissions: Skip all permission prompts (safe in controlled environment)
  116. # This allows Claude to run unattended without waiting for user input
  117. claude --print --dangerously-skip-permissions "$PROMPT"
  118. EXIT_CODE=$?
  119. echo "---"
  120. echo "Claude Code execution completed (exit code: $EXIT_CODE)"
  121. exit $EXIT_CODE