Skip to content

Commit 2934a37

Browse files
committed
Claude PR react on review
1 parent 824e74c commit 2934a37

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
name: "Claude PR Review"
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
pr_number:
7+
description: "Pull request number"
8+
required: true
9+
type: number
10+
review_id:
11+
description: "Pull request review ID"
12+
required: true
13+
type: number
14+
15+
concurrency:
16+
group: claude-pr-review-${{ inputs.pr_number }}-${{ inputs.review_id }}
17+
cancel-in-progress: false
18+
19+
permissions:
20+
contents: read
21+
22+
jobs:
23+
respond:
24+
runs-on: ubuntu-latest
25+
timeout-minutes: 30
26+
permissions:
27+
contents: write
28+
pull-requests: write
29+
steps:
30+
- name: Harden the runner (Audit all outbound calls)
31+
uses: step-security/harden-runner@5ef0c079ce82195b2a36a210272d6b661572d83e # v2.14.2
32+
with:
33+
egress-policy: audit
34+
35+
- name: Fetch review details
36+
id: review
37+
env:
38+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_PR_TOKEN }}
39+
run: |
40+
gh api "repos/${{ github.repository }}/pulls/${{ inputs.pr_number }}" > /tmp/pr.json
41+
gh api "repos/${{ github.repository }}/pulls/${{ inputs.pr_number }}/reviews/${{ inputs.review_id }}" > /tmp/review.json
42+
gh api "repos/${{ github.repository }}/pulls/${{ inputs.pr_number }}/reviews/${{ inputs.review_id }}/comments" --paginate > /tmp/review-comments.json
43+
44+
echo "pr_head_ref=$(jq -r '.head.ref' /tmp/pr.json)" >> "$GITHUB_OUTPUT"
45+
46+
- name: "Checkout"
47+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
48+
with:
49+
ref: ${{ steps.review.outputs.pr_head_ref }}
50+
token: ${{ secrets.PHPSTAN_BOT_FORK_TOKEN }}
51+
fetch-depth: 0
52+
53+
- name: "Install PHP"
54+
uses: "shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1" # v2
55+
with:
56+
coverage: "none"
57+
php-version: "8.4"
58+
extensions: mbstring
59+
ini-file: development
60+
ini-values: memory_limit=-1
61+
62+
- name: "Install dependencies"
63+
uses: "ramsey/composer-install@3cf229dc2919194e9e36783941438d17239e8520" # v3
64+
65+
- name: "Install test dependencies"
66+
run: composer install --working-dir tests
67+
68+
- name: Build prompt
69+
id: prompt
70+
shell: bash
71+
run: |
72+
review_body=$(jq -r '.body // ""' /tmp/review.json)
73+
review_state=$(jq -r '.state' /tmp/review.json)
74+
reviewer=$(jq -r '.user.login' /tmp/review.json)
75+
num_comments=$(jq 'length' /tmp/review-comments.json)
76+
77+
prompt="A reviewer ($reviewer) submitted a review on PR #${{ inputs.pr_number }} with state: $review_state."
78+
prompt+=$'\n'
79+
80+
if [ -n "$review_body" ] && [ "$review_body" != "null" ]; then
81+
prompt+=$'\n'
82+
prompt+="## Review body"
83+
prompt+=$'\n\n'
84+
prompt+="$review_body"
85+
prompt+=$'\n'
86+
fi
87+
88+
if [ "$num_comments" -gt 0 ]; then
89+
prompt+=$'\n'
90+
prompt+="## Review comments"
91+
prompt+=$'\n'
92+
93+
for i in $(seq 0 $((num_comments - 1))); do
94+
file_path=$(jq -r ".[$i].path" /tmp/review-comments.json)
95+
line=$(jq -r ".[$i].line // .[$i].original_line // \"?\"" /tmp/review-comments.json)
96+
body=$(jq -r ".[$i].body" /tmp/review-comments.json)
97+
diff_hunk=$(jq -r ".[$i].diff_hunk // \"\"" /tmp/review-comments.json)
98+
99+
prompt+=$'\n'
100+
prompt+="### $file_path (line $line)"
101+
prompt+=$'\n\n'
102+
103+
if [ -n "$diff_hunk" ] && [ "$diff_hunk" != "null" ]; then
104+
prompt+='```diff'
105+
prompt+=$'\n'
106+
prompt+="$diff_hunk"
107+
prompt+=$'\n'
108+
prompt+='```'
109+
prompt+=$'\n\n'
110+
fi
111+
112+
prompt+="$body"
113+
prompt+=$'\n'
114+
done
115+
fi
116+
117+
prompt+=$'\n'
118+
prompt+="## Instructions"
119+
prompt+=$'\n\n'
120+
prompt+="Please address this review. Make the requested code changes, then run tests with \`make tests\` and static analysis with \`make phpstan\` to verify."
121+
prompt+=$'\n'
122+
prompt+="Commit each logical change separately with a descriptive message."
123+
124+
{
125+
echo 'PROMPT<<PROMPT_DELIMITER'
126+
echo "$prompt"
127+
echo 'PROMPT_DELIMITER'
128+
} >> "$GITHUB_OUTPUT"
129+
130+
- name: Install Claude Code
131+
run: npm install -g @anthropic-ai/claude-code
132+
133+
- name: Run Claude
134+
env:
135+
CLAUDE_CODE_OAUTH_TOKEN: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
136+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_FORK_TOKEN }}
137+
PROMPT: ${{ steps.prompt.outputs.PROMPT }}
138+
run: |
139+
claude -p "$PROMPT" \
140+
--output-format text \
141+
> /tmp/claude-response.txt
142+
143+
- name: Commit and push changes
144+
id: push
145+
run: |
146+
if [ -z "$(git status --porcelain)" ]; then
147+
echo "pushed=false" >> "$GITHUB_OUTPUT"
148+
exit 0
149+
fi
150+
151+
git config user.name "phpstan-bot"
152+
git config user.email "ondrej+phpstanbot@mirtes.cz"
153+
git add -A
154+
git commit -m "Address review feedback on PR #${{ inputs.pr_number }}"
155+
git push
156+
echo "pushed=true" >> "$GITHUB_OUTPUT"
157+
158+
- name: Reply to review
159+
if: always() && steps.claude.outcome != 'skipped'
160+
env:
161+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_PR_TOKEN }}
162+
PUSHED: ${{ steps.push.outputs.pushed }}
163+
run: |
164+
body=""
165+
if [ -f /tmp/claude-response.txt ]; then
166+
body=$(cat /tmp/claude-response.txt)
167+
fi
168+
169+
if [ "$PUSHED" = "true" ]; then
170+
body+=$'\n\n---\n_I pushed a commit addressing this review._'
171+
fi
172+
173+
if [ -z "$body" ]; then
174+
body="I processed this review but have nothing to report."
175+
fi
176+
177+
gh api \
178+
"repos/${{ github.repository }}/pulls/${{ inputs.pr_number }}/comments" \
179+
-f body="$body" \
180+
-F in_reply_to="$(jq -r '.[0].id // empty' /tmp/review-comments.json)" \
181+
2>/dev/null \
182+
|| gh pr comment "${{ inputs.pr_number }}" \
183+
--repo "${{ github.repository }}" \
184+
--body "$body"

0 commit comments

Comments
 (0)