name: Claude Code

on:
  issue_comment:
    types: [created]
  pull_request_review_comment:
    types: [created]
  issues:
    types: [opened, assigned]
  pull_request_review:
    types: [submitted]

jobs:
  claude:
    if: |
      (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude') && github.event.comment.user.login == github.repository_owner) ||
      (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude') && github.event.comment.user.login == github.repository_owner) ||
      (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude') && github.event.review.user.login == github.repository_owner) ||
      (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')) && github.event.issue.user.login == github.repository_owner)
    runs-on: ubuntu-latest
    permissions:
      contents: write       # Allow creating branches and commits
      pull-requests: write  # Allow posting PR comments and reviews
      issues: write         # Allow posting issue comments
      id-token: write
      actions: read # Required for Claude to read CI results on PRs
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Run Claude Code
        id: claude
        uses: anthropics/claude-code-action@v1
        with:
          claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}

          # Enable sticky comments for PRs only
          use_sticky_comment: true

          # Allow Claude to post comments on issues and PRs using gh CLI
          claude_args: '--allowed-tools "Bash(gh issue comment:*),Bash(gh pr comment:*),Bash(gh issue view:*),Bash(gh pr view:*)"'

          # This is an optional setting that allows Claude to read CI results on PRs
          additional_permissions: |
            actions: read

          # Auto-invoke github-release-manager agent for bug fixes and features
          # This triggers release workflow when Claude fixes issues on GitHub
          prompt: |
            ${{ github.event.comment.body || github.event.issue.body || github.event.review.body }}

            IMPORTANT: After you complete the task, post your response using:
            - For issues: Use `gh issue comment <issue_number> --body "your response"`
            - For PRs: Use `gh pr comment <pr_number> --body "your response"`

            The issue/PR number is available in the GitHub context.

            IMPORTANT: After completing the requested task, if you:
            1. Fixed a bug (modified src/ code)
            2. Added a new feature (modified src/ code)
            3. Made any changes to pyproject.toml dependencies

            Then automatically invoke the github-release-manager agent to:
            - Determine appropriate version bump (patch for fixes, minor for features)
            - Create release branch with version updates
            - The Claude Branch Automation workflow will handle the rest

            Skip release manager if you only changed:
            - Documentation (.md files, docs/)
            - Tests only (tests/ without src/ changes)
            - CI/CD workflows (.github/)
            - Development tools (.claude/)

            Use your judgment - if unsure whether to release, ask the user.

