claude.yml 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. name: Claude Code
  2. on:
  3. issue_comment:
  4. types: [created]
  5. pull_request_review_comment:
  6. types: [created]
  7. issues:
  8. types: [opened, assigned]
  9. pull_request_review:
  10. types: [submitted]
  11. jobs:
  12. claude:
  13. if: |
  14. (github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
  15. (github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
  16. (github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
  17. (github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
  18. runs-on: ubuntu-latest
  19. permissions:
  20. contents: read
  21. pull-requests: read
  22. issues: read
  23. id-token: write
  24. actions: read # Required for Claude to read CI results on PRs
  25. steps:
  26. - name: Checkout repository
  27. uses: actions/checkout@v4
  28. with:
  29. fetch-depth: 1
  30. # Workaround for claude-code-action bug with fork PRs: The action fetches by branch name
  31. # (git fetch origin --depth=N <branch>), but fork PR branches don't exist on origin.
  32. # Fix: redirect origin to the fork's URL so the action can fetch the branch directly.
  33. - name: Configure git remote for fork PRs
  34. env:
  35. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  36. run: |
  37. # Determine PR number based on event type
  38. if [ "${{ github.event_name }}" = "issue_comment" ]; then
  39. PR_NUMBER="${{ github.event.issue.number }}"
  40. elif [ "${{ github.event_name }}" = "pull_request_review_comment" ] || [ "${{ github.event_name }}" = "pull_request_review" ]; then
  41. PR_NUMBER="${{ github.event.pull_request.number }}"
  42. else
  43. exit 0 # issues event — no PR branch to worry about
  44. fi
  45. # Fetch fork info in one API call; silently skip if this is not a PR
  46. PR_INFO=$(gh pr view "${PR_NUMBER}" --json isCrossRepository,headRepositoryOwner,headRepository 2>/dev/null || echo "")
  47. if [ -z "$PR_INFO" ]; then
  48. exit 0
  49. fi
  50. IS_FORK=$(echo "$PR_INFO" | jq -r '.isCrossRepository')
  51. if [ "$IS_FORK" = "true" ]; then
  52. FORK_OWNER=$(echo "$PR_INFO" | jq -r '.headRepositoryOwner.login')
  53. FORK_REPO=$(echo "$PR_INFO" | jq -r '.headRepository.name')
  54. echo "Fork PR detected from ${FORK_OWNER}/${FORK_REPO}: updating origin to fork URL"
  55. git remote set-url origin "https://github.com/${FORK_OWNER}/${FORK_REPO}.git"
  56. fi
  57. - name: Run Claude Code
  58. id: claude
  59. uses: anthropics/claude-code-action@e763fe78de2db7389e04818a00b5ff8ba13d1360 # v1
  60. with:
  61. claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
  62. # This is an optional setting that allows Claude to read CI results on PRs
  63. additional_permissions: |
  64. actions: read
  65. # Optional: Give a custom prompt to Claude. If this is not specified, Claude will perform the instructions specified in the comment that tagged it.
  66. # prompt: 'Update the pull request description to include a summary of changes.'
  67. # Optional: Add claude_args to customize behavior and configuration
  68. # See https://github.com/anthropics/claude-code-action/blob/main/docs/usage.md
  69. # or https://code.claude.com/docs/en/cli-reference for available options
  70. # claude_args: '--allowed-tools Bash(gh pr:*)'