From 63d41399b89bc29752dc61b3a0f80f59cf395fca Mon Sep 17 00:00:00 2001 From: "yen.nguyen" Date: Mon, 30 Mar 2026 12:21:18 +0200 Subject: [PATCH] add more claude-interaction --- .gitea/workflows/claude.yml | 264 ++++++++++++++++++++++++++++++------ 1 file changed, 223 insertions(+), 41 deletions(-) diff --git a/.gitea/workflows/claude.yml b/.gitea/workflows/claude.yml index e3f4c9f..7cf6a0c 100644 --- a/.gitea/workflows/claude.yml +++ b/.gitea/workflows/claude.yml @@ -1,62 +1,244 @@ -name: Claude Issue Agent +name: Claude Code Issue Handler on: - issues: - types: [labeled] + issue_comment: + types: [created] + +env: + CLAUDE_MAX_TURNS: "30" jobs: handle-issue: - if: ${{ contains(github.event.issue.labels.*.name, 'claude-ready') }} + # Only run on issue comments, not PR comments + if: ${{ !github.event.issue.pull_request }} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Classify issue by labels + id: classify + run: | + LABELS='${{ toJson(github.event.issue.labels) }}' + echo "Raw labels: $LABELS" + IS_BUG=$(echo "$LABELS" | jq '[.[].name | ascii_downcase] | any(. == "bug")' 2>/dev/null || echo "false") + IS_IDEATION=$(echo "$LABELS" | jq '[.[].name | ascii_downcase] | any(. == "help wanted")' 2>/dev/null || echo "false") + echo "is_bug=$IS_BUG" >> $GITHUB_OUTPUT + echo "is_ideation=$IS_IDEATION" >> $GITHUB_OUTPUT + echo "is_bug=$IS_BUG | is_ideation=$IS_IDEATION" - - name: Install Claude Code + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITEA_TOKEN }} + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: "20" + + - name: Install Claude Code CLI run: npm install -g @anthropic-ai/claude-code - - name: Create branch - run: | - git config user.name "Claude Agent" - git config user.email "claude@agent.local" - git checkout -b fix/issue-${{ github.event.issue.number }} - - - name: Claude works on issue + - name: Authenticate Claude Code CLI env: ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} run: | - useradd -m claude-runner - chown -R claude-runner:claude-runner /workspace - claude --output-format stream-json --print --verbose " - Work on issue #${{ github.event.issue.number }}: ${{ github.event.issue.title }} + claude config set -g apiKey "$ANTHROPIC_API_KEY" + claude config set -g autoUpdaterStatus disabled - Beschreibung: - ${{ github.event.issue.body }} - - Vorgehensweise: - 1. Read /docs/PRD.md to understand the code structure and understand the issue - 2. Create a detailed plan - 3. Implement the solution - 4. Check if all tests still pass, update them or create new tests if there are failing or missing tests. - " - - - name: Commit changes + - name: Configure Git run: | - git add . - git commit -m "${{ github.event.issue.title }}" + git config --global user.email "claude@vibentec-it.io" + git config --global user.name "Claude Code Bot" + git config --global url."https://oauth2:${{ secrets.GITEA_TOKEN }}@gitea.vibentec-it.io/".insteadOf "https://gitea.vibentec-it.io/" - - name: Push branch + # ───────────────────────────────────────────── + # BUG FLOW: research + fix + PR + # ───────────────────────────────────────────── + - name: "[Bug] Checkout fix branch" + if: steps.classify.outputs.is_bug == 'true' run: | - git push origin fix/issue-${{ github.event.issue.number }} + BRANCH_NAME="fix/issue-${{ github.event.issue.number }}" + git checkout -b "$BRANCH_NAME" + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV - - name: Create PR + - name: "[Bug] Run Claude Code to research and fix" + if: steps.classify.outputs.is_bug == 'true' + id: claude-bug + env: + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + ISSUE_TITLE: ${{ github.event.issue.title }} + ISSUE_BODY: ${{ github.event.issue.body }} + ISSUE_COMMENT: ${{ github.event.comment.body }} run: | - curl -X POST "${{ secrets.INSTANCE_URL }}/api/v1/repos/${{ github.repository }}/pulls" \ - -H "Authorization: token ${{ secrets.API_TOKEN }}" \ + PROMPT=$(printf '%s\n' \ + "You are a software engineer fixing a bug reported in issue #${ISSUE_NUMBER}: \"${ISSUE_TITLE}\"." \ + "Issue description: ${ISSUE_BODY}" \ + "Comment that triggered this action: ${ISSUE_COMMENT}" \ + "Your tasks:" \ + "1. Read and understand the codebase relevant to this bug." \ + "2. Identify the root cause and which files need to change." \ + "3. Implement minimal, focused fixes — do not refactor unrelated code." \ + "4. After making code changes, run \"cd homepage && npm run lint\" via the Bash tool" \ + " and immediately fix every lint error or syntax warning it reports." \ + "5. Do NOT run Jenkinsfile or any Jenkins-related checks." \ + "6. Do NOT run git commands yourself; the CI pipeline will commit your changes." \ + "After completing your investigation, fixes, and lint pass, output a brief plain-text" \ + "summary of the root cause, what you changed, and the lint result. Keep it under 200 words." \ + ) + claude \ + --allowedTools "Read,Write,Edit,Bash,Glob,Grep,WebSearch,WebFetch" \ + --max-turns "$CLAUDE_MAX_TURNS" \ + --output-format text \ + -p "$PROMPT" > /tmp/claude_bug_output.txt 2>&1 || true + echo "--- Claude output ---" + cat /tmp/claude_bug_output.txt + + - name: "[Bug] Check for file changes" + if: steps.classify.outputs.is_bug == 'true' + id: git-status + run: | + if git diff --quiet && git diff --cached --quiet; then + echo "has_changes=false" >> $GITHUB_OUTPUT + echo "No file changes detected." + else + echo "has_changes=true" >> $GITHUB_OUTPUT + git diff --stat + fi + + - name: "[Bug] Commit changes" + if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'true' + run: | + git add -A + git commit -m "fix: resolve issue #${{ github.event.issue.number }} - ${{ github.event.issue.title }}" + + - name: "[Bug] Push branch" + if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'true' + run: git push origin "$BRANCH_NAME" + + - name: "[Bug] Create Pull Request" + if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'true' + id: create-pr + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + REPO: ${{ github.repository }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + ISSUE_TITLE: ${{ github.event.issue.title }} + BASE_URL: ${{ github.server_url }} + run: | + CLAUDE_SUMMARY=$(cat /tmp/claude_bug_output.txt | tail -n 50) + PR_TITLE="fix: resolve issue #${ISSUE_NUMBER} - ${ISSUE_TITLE}" + PR_BODY=$(printf '## Summary\n\nThis PR addresses the bug reported in issue #%s.\n\n### Claude Code Analysis\n%s\n\n---\nCloses #%s' \ + "$ISSUE_NUMBER" "$CLAUDE_SUMMARY" "$ISSUE_NUMBER") + PAYLOAD=$(jq -n \ + --arg title "$PR_TITLE" \ + --arg body "$PR_BODY" \ + --arg head "$BRANCH_NAME" \ + --arg base "main" \ + '{title: $title, body: $body, head: $head, base: $base}') + PR_RESPONSE=$(curl -s -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ - -d "{ - \"title\": \"Fix #${{ github.event.issue.number }}: ${{ github.event.issue.title }}\", - \"head\": \"fix/issue-${{ github.event.issue.number }}\", - \"base\": \"main\", - \"body\": \"Automatically resolved by Claude.\n\nCloses #${{ github.event.issue.number }}\" - }" \ No newline at end of file + "${BASE_URL}/api/v1/repos/${REPO}/pulls" \ + -d "$PAYLOAD") + PR_NUMBER=$(echo "$PR_RESPONSE" | jq -r '.number // empty') + PR_URL=$(echo "$PR_RESPONSE" | jq -r '.html_url // empty') + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT + echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT + echo "Created PR #$PR_NUMBER: $PR_URL" + + - name: "[Bug] Comment on issue — PR created" + if: >- + steps.classify.outputs.is_bug == 'true' && + steps.git-status.outputs.has_changes == 'true' && + steps.create-pr.outputs.pr_url != '' + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + REPO: ${{ github.repository }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + BASE_URL: ${{ github.server_url }} + PR_NUMBER: ${{ steps.create-pr.outputs.pr_number }} + PR_URL: ${{ steps.create-pr.outputs.pr_url }} + run: | + COMMENT=$(printf "I've analyzed this bug and implemented a fix.\n\n**Pull Request:** [PR #%s](%s)\n\nPlease review the changes and merge when ready. If the fix doesn't fully address the issue, feel free to leave a comment and I'll take another look." \ + "$PR_NUMBER" "$PR_URL") + PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}') + curl -s -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + "${BASE_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUMBER}/comments" \ + -d "$PAYLOAD" + + - name: "[Bug] Comment on issue — no code changes needed" + if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'false' + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + REPO: ${{ github.repository }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + BASE_URL: ${{ github.server_url }} + run: | + ANALYSIS=$(cat /tmp/claude_bug_output.txt 2>/dev/null || echo "No analysis output available.") + COMMENT=$(printf "After researching this issue, **no code changes appear to be necessary** at this time.\n\n### Analysis\n%s\n\nIf you believe something was missed, please provide more details and I'll investigate further." \ + "$ANALYSIS") + PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}') + curl -s -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + "${BASE_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUMBER}/comments" \ + -d "$PAYLOAD" + + # ───────────────────────────────────────────── + # IDEATION FLOW: research + comment only + # ───────────────────────────────────────────── + - name: "[Ideation] Run Claude Code to research" + if: steps.classify.outputs.is_ideation == 'true' + id: claude-ideation + env: + ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + ISSUE_TITLE: ${{ github.event.issue.title }} + ISSUE_BODY: ${{ github.event.issue.body }} + ISSUE_COMMENT: ${{ github.event.comment.body }} + run: | + PROMPT=$(printf '%s\n' \ + "You are a software architect reviewing a feature idea from issue #${ISSUE_NUMBER}: \"${ISSUE_TITLE}\"." \ + "Idea description: ${ISSUE_BODY}" \ + "Comment that triggered this action: ${ISSUE_COMMENT}" \ + "Your tasks:" \ + "1. Explore the existing codebase to understand what already exists related to this idea." \ + "2. Research best practices and possible implementation approaches." \ + "3. Identify technical challenges, risks, or dependencies." \ + "4. Provide a structured Markdown response suitable for posting as an issue comment." \ + "Format your response with these sections:" \ + "- **Current State** (what already exists)" \ + "- **Proposed Approach** (recommended implementation path)" \ + "- **Considerations** (risks, trade-offs, dependencies)" \ + "- **Next Steps** (actionable recommendations)" \ + "Keep the total response under 500 words." \ + ) + claude \ + --allowedTools "Read,Glob,Grep,WebSearch,WebFetch" \ + --max-turns 15 \ + --output-format text \ + -p "$PROMPT" > /tmp/claude_ideation_output.txt 2>&1 || true + echo "--- Claude output ---" + cat /tmp/claude_ideation_output.txt + + - name: "[Ideation] Comment research findings on issue" + if: steps.classify.outputs.is_ideation == 'true' + env: + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + REPO: ${{ github.repository }} + ISSUE_NUMBER: ${{ github.event.issue.number }} + BASE_URL: ${{ github.server_url }} + run: | + RESEARCH=$(cat /tmp/claude_ideation_output.txt 2>/dev/null || echo "Research output unavailable.") + COMMENT=$(printf "## Research Findings\n\nI've explored the codebase and researched this idea. Here's what I found:\n\n%s\n\n---\n*This research was performed automatically by Claude Code. Feel free to discuss further in the comments.*" \ + "$RESEARCH") + PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}') + curl -s -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + "${BASE_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUMBER}/comments" \ + -d "$PAYLOAD"