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_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude'))) ||
(github.event_name == 'issues' && github.event.action == 'assigned' && github.event.assignee.login == 'claude[bot]')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
issues: write
id-token: write
actions: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Set up global Claude config
env:
GH_TOKEN: ${{ secrets.CROSS_REPO_PAT }}
run: |
mkdir -p ~/.claude
gh api repos/drewster99/dev-ops/contents/claude-rules/base.md \
--jq '.content' | base64 -d > ~/.claude/CLAUDE.md
echo "" >> ~/.claude/CLAUDE.md
gh api repos/drewster99/dev-ops/contents/claude-rules/ci.md \
--jq '.content' | base64 -d >> ~/.claude/CLAUDE.md
- name: Run Claude Code
id: claude
uses: anthropics/claude-code-action@v1
with:
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
additional_permissions: |
actions: read
- name: Auto-create PR if Claude pushed a branch
if: github.event_name == 'issues' || (github.event_name == 'issue_comment' && !github.event.issue.pull_request)
env:
GH_TOKEN: ${{ github.token }}
run: |
# Find branches created by this workflow run
ISSUE_NUMBER=${{ github.event.issue.number }}
BRANCH=$(git branch -r --list "origin/claude/*${ISSUE_NUMBER}*" | head -1 | xargs)
if [ -z "$BRANCH" ]; then
echo "No Claude branch found for issue #${ISSUE_NUMBER}, skipping PR creation."
exit 0
fi
LOCAL_BRANCH="${BRANCH#origin/}"
# Check if a PR already exists for this branch
EXISTING_PR=$(gh pr list --head "$LOCAL_BRANCH" --json number --jq '.[0].number' 2>/dev/null)
if [ -n "$EXISTING_PR" ]; then
echo "PR #${EXISTING_PR} already exists for branch ${LOCAL_BRANCH}."
exit 0
fi
echo "Creating PR from ${LOCAL_BRANCH} for issue #${ISSUE_NUMBER}..."
gh pr create \
--head "$LOCAL_BRANCH" \
--title "$(gh issue view $ISSUE_NUMBER --json title --jq .title)" \
--body "Resolves #${ISSUE_NUMBER}
Auto-generated by Claude Code from issue #${ISSUE_NUMBER}." \
|| echo "PR creation failed -- Claude may not have pushed commits."