#!/bin/bash # Script to handle issue automation with Claude Code # This script is called when an issue is opened or commented on # Add claude binary to PATH export PATH="$HOME/.local/bin:$PATH" # Parse arguments OWNER="$1" REPO="$2" PUSHER="$3" ISSUE_NUMBER="$4" ISSUE_TITLE="$5" ISSUE_BODY="$6" ISSUE_ACTION="$7" COMMENT_BODY="$8" ASSIGNEE="$9" # Exit if pusher is claude (avoid loops) if [ "$PUSHER" = "claude" ]; then echo "Pusher is claude, skipping automation to avoid loops" exit 0 fi # Exit if issue is not assigned to claude if [ "$ASSIGNEE" != "claude" ]; then echo "Issue not assigned to claude (assignee: $ASSIGNEE), skipping" exit 0 fi # Determine repo path REPO_PATH="$HOME/$REPO" # Check if repo exists, if not clone it if [ ! -d "$REPO_PATH" ]; then echo "Repository not found at $REPO_PATH, cloning..." mkdir -p "$HOME" GIT_URL="ssh://git@207.154.220.231:10022/$OWNER/$REPO.git" git clone "$GIT_URL" "$REPO_PATH" if [ $? -ne 0 ]; then echo "Failed to clone repository" exit 1 fi echo "Repository cloned successfully" else echo "Repository found at $REPO_PATH" # Pull latest changes cd "$REPO_PATH" || exit 1 echo "Pulling latest changes..." git pull fi # Change to repo directory cd "$REPO_PATH" || exit 1 echo "Working directory: $(pwd)" # Build the prompt for Claude with instructions to use Gogs MCP tool if [ -n "$COMMENT_BODY" ]; then # Issue comment event - new comment was added PROMPT="IMPORTANT: A new comment has been added to issue #$ISSUE_NUMBER in repository $OWNER/$REPO. === MANDATORY FIRST STEPS - READ ALL MESSAGES === Before you start any work on this issue, you MUST complete these steps IN ORDER: 1. Use the mcp__gogs__get_issue tool to fetch the complete issue details: - owner: $OWNER - repo: $REPO - number: $ISSUE_NUMBER 2. Use the mcp__gogs__list_issue_comments tool to fetch ALL comments: - owner: $OWNER - repo: $REPO - number: $ISSUE_NUMBER 3. Read and understand: - The original issue description - ALL comments in the discussion thread (chronologically) - The full context and conversation history 4. ONLY AFTER reading everything above, proceed with your work === ISSUE CONTEXT === Issue #$ISSUE_NUMBER: $ISSUE_TITLE A new comment was just added by: $PUSHER Initial issue description: $ISSUE_BODY Most recent comment: $COMMENT_BODY === YOUR TASK === This issue is assigned to you. You must: 1. First, use the Gogs MCP tools above to fetch and read ALL issue comments 2. Understand the full discussion context 3. Then proceed with the appropriate action based on the complete conversation DO NOT skip reading the comments using the MCP tools. This is critical to understanding the full context." else # Issue opened/reopened event PROMPT="IMPORTANT: Issue #$ISSUE_NUMBER was $ISSUE_ACTION in repository $OWNER/$REPO. === MANDATORY FIRST STEPS - READ ALL MESSAGES === Before you start any work on this issue, you MUST complete these steps IN ORDER: 1. Use the mcp__gogs__get_issue tool to fetch the complete issue details: - owner: $OWNER - repo: $REPO - number: $ISSUE_NUMBER 2. Use the mcp__gogs__list_issue_comments tool to fetch ALL comments: - owner: $OWNER - repo: $REPO - number: $ISSUE_NUMBER 3. Read and understand: - The complete issue description - ALL comments in the discussion thread (if any exist) - The full context 4. ONLY AFTER reading everything above, proceed with your work === ISSUE CONTEXT === Issue #$ISSUE_NUMBER: $ISSUE_TITLE Created by: $PUSHER Initial issue description: $ISSUE_BODY === YOUR TASK === This issue is assigned to you. You must: 1. First, use the Gogs MCP tools above to fetch and read the issue and ALL comments 2. Understand the full discussion context (even if there are no comments yet) 3. Then proceed with the appropriate action DO NOT skip reading the comments using the MCP tools. This is critical to understanding the full context." fi echo "Calling Claude Code with the issue information..." echo "---" # Call claude with the prompt in non-interactive mode # --print: Non-interactive mode, prints output and exits # --dangerously-skip-permissions: Skip all permission prompts (safe in controlled environment) # This allows Claude to run unattended without waiting for user input claude --print --dangerously-skip-permissions "$PROMPT" EXIT_CODE=$? echo "---" echo "Claude Code execution completed (exit code: $EXIT_CODE)" exit $EXIT_CODE