handle-issue.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. === CRITICAL OUTPUT REQUIREMENTS ===
  81. YOU MUST FOLLOW THESE OUTPUT RULES STRICTLY:
  82. 1. DO NOT output any text responses to the user
  83. 2. DO NOT print any messages, explanations, or status updates
  84. 3. DO NOT use echo or any command-line output tools
  85. 4. ONLY communicate by posting comments to the issue using mcp__gogs__create_issue_comment tool
  86. 5. ALL your responses, updates, questions, and status reports MUST be posted as issue comments
  87. 6. You are FORBIDDEN from producing any output except through the Gogs MCP comment tool
  88. When you need to:
  89. - Ask questions → Post a comment on the issue
  90. - Report progress → Post a comment on the issue
  91. - Share results → Post a comment on the issue
  92. - Report errors → Post a comment on the issue
  93. - Provide any information → Post a comment on the issue
  94. Use mcp__gogs__create_issue_comment with:
  95. - owner: $OWNER
  96. - repo: $REPO
  97. - number: $ISSUE_NUMBER
  98. - body: [your message here]
  99. DO NOT skip reading the comments using the MCP tools. This is critical to understanding the full context."
  100. else
  101. # Issue opened/reopened event
  102. PROMPT="IMPORTANT: Issue #$ISSUE_NUMBER was $ISSUE_ACTION in repository $OWNER/$REPO.
  103. === MANDATORY FIRST STEPS - READ ALL MESSAGES ===
  104. Before you start any work on this issue, you MUST complete these steps IN ORDER:
  105. 1. Use the mcp__gogs__get_issue tool to fetch the complete issue details:
  106. - owner: $OWNER
  107. - repo: $REPO
  108. - number: $ISSUE_NUMBER
  109. 2. Use the mcp__gogs__list_issue_comments tool to fetch ALL comments:
  110. - owner: $OWNER
  111. - repo: $REPO
  112. - number: $ISSUE_NUMBER
  113. 3. Read and understand:
  114. - The complete issue description
  115. - ALL comments in the discussion thread (if any exist)
  116. - The full context
  117. 4. ONLY AFTER reading everything above, proceed with your work
  118. === ISSUE CONTEXT ===
  119. Issue #$ISSUE_NUMBER: $ISSUE_TITLE
  120. Created by: $PUSHER
  121. Initial issue description:
  122. $ISSUE_BODY
  123. === YOUR TASK ===
  124. This issue is assigned to you. You must:
  125. 1. First, use the Gogs MCP tools above to fetch and read the issue and ALL comments
  126. 2. Understand the full discussion context (even if there are no comments yet)
  127. 3. Then proceed with the appropriate action
  128. === CRITICAL OUTPUT REQUIREMENTS ===
  129. YOU MUST FOLLOW THESE OUTPUT RULES STRICTLY:
  130. 1. DO NOT output any text responses to the user
  131. 2. DO NOT print any messages, explanations, or status updates
  132. 3. DO NOT use echo or any command-line output tools
  133. 4. ONLY communicate by posting comments to the issue using mcp__gogs__create_issue_comment tool
  134. 5. ALL your responses, updates, questions, and status reports MUST be posted as issue comments
  135. 6. You are FORBIDDEN from producing any output except through the Gogs MCP comment tool
  136. When you need to:
  137. - Ask questions → Post a comment on the issue
  138. - Report progress → Post a comment on the issue
  139. - Share results → Post a comment on the issue
  140. - Report errors → Post a comment on the issue
  141. - Provide any information → Post a comment on the issue
  142. Use mcp__gogs__create_issue_comment with:
  143. - owner: $OWNER
  144. - repo: $REPO
  145. - number: $ISSUE_NUMBER
  146. - body: [your message here]
  147. DO NOT skip reading the comments using the MCP tools. This is critical to understanding the full context."
  148. fi
  149. echo "Calling Claude Code with the issue information..."
  150. echo "---"
  151. # Call claude with the prompt in non-interactive mode
  152. # --print: Non-interactive mode, prints output and exits
  153. # --dangerously-skip-permissions: Skip all permission prompts (safe in controlled environment)
  154. # This allows Claude to run unattended without waiting for user input
  155. claude --print --dangerously-skip-permissions "$PROMPT"
  156. EXIT_CODE=$?
  157. echo "---"
  158. echo "Claude Code execution completed (exit code: $EXIT_CODE)"
  159. exit $EXIT_CODE