Skip to content

Commit a5918f8

Browse files
committed
Claude PR react to review workflow
1 parent 7d0851d commit a5918f8

File tree

1 file changed

+352
-0
lines changed

1 file changed

+352
-0
lines changed
Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
name: "Claude PR Review"
2+
3+
on:
4+
workflow_dispatch:
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+
workflow_call:
15+
inputs:
16+
pr_number:
17+
description: "Pull request number"
18+
required: true
19+
type: number
20+
review_id:
21+
description: "Pull request review ID"
22+
required: true
23+
type: number
24+
25+
concurrency:
26+
group: claude-pr-review-${{ inputs.pr_number }}-${{ inputs.review_id }}
27+
cancel-in-progress: false
28+
29+
permissions:
30+
contents: read
31+
32+
jobs:
33+
respond:
34+
runs-on: ubuntu-latest
35+
timeout-minutes: 30
36+
permissions:
37+
contents: write
38+
pull-requests: write
39+
steps:
40+
- name: Harden the runner (Audit all outbound calls)
41+
uses: step-security/harden-runner@5ef0c079ce82195b2a36a210272d6b661572d83e # v2.14.2
42+
with:
43+
egress-policy: audit
44+
45+
- name: Fetch PR and review details
46+
id: review
47+
env:
48+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_PR_TOKEN }}
49+
run: |
50+
PR_NUM="${{ inputs.pr_number }}"
51+
REPO="phpstan/phpstan"
52+
53+
# Core PR and review data
54+
gh api "repos/$REPO/pulls/$PR_NUM" > /tmp/pr.json
55+
gh api "repos/$REPO/pulls/$PR_NUM/reviews/${{ inputs.review_id }}" > /tmp/review.json
56+
gh api "repos/$REPO/pulls/$PR_NUM/reviews/${{ inputs.review_id }}/comments" --paginate > /tmp/review-comments.json
57+
58+
# Additional context: all comments, all reviews, and PR diff
59+
gh api "repos/$REPO/issues/$PR_NUM/comments" --paginate > /tmp/pr-comments.json
60+
gh api "repos/$REPO/pulls/$PR_NUM/reviews" --paginate > /tmp/all-reviews.json
61+
gh api "repos/$REPO/pulls/$PR_NUM" -H "Accept: application/vnd.github.diff" > /tmp/pr.diff
62+
63+
# Fetch linked issues referenced in the PR body
64+
pr_body=$(jq -r '.body // ""' /tmp/pr.json)
65+
echo '[]' > /tmp/linked-issues.json
66+
if [ -n "$pr_body" ]; then
67+
# Match patterns like #1234, phpstan/phpstan#1234, phpstan/phpstan-src#1234,
68+
# and full GitHub issue/PR URLs
69+
issue_refs=$(echo "$pr_body" | grep -oE '(phpstan/(phpstan-src|phpstan))?#[0-9]+|https://github\.com/phpstan/(phpstan|phpstan-src)/issues/[0-9]+' | sort -u || true)
70+
issues_array="[]"
71+
for ref in $issue_refs; do
72+
# Normalize to owner/repo and number
73+
if echo "$ref" | grep -qE '^https://'; then
74+
issue_repo=$(echo "$ref" | grep -oE 'phpstan/(phpstan|phpstan-src)')
75+
issue_num=$(echo "$ref" | grep -oE '[0-9]+$')
76+
elif echo "$ref" | grep -qE '^phpstan/'; then
77+
issue_repo=$(echo "$ref" | cut -d'#' -f1)
78+
issue_num=$(echo "$ref" | cut -d'#' -f2)
79+
else
80+
# Plain #1234 - default to phpstan/phpstan (the issue tracker)
81+
issue_repo="phpstan/phpstan"
82+
issue_num=$(echo "$ref" | tr -d '#')
83+
fi
84+
85+
issue_json=$(gh api "repos/$issue_repo/issues/$issue_num" 2>/dev/null || echo "")
86+
if [ -n "$issue_json" ]; then
87+
issues_array=$(echo "$issues_array" | jq --argjson item "$issue_json" '. + [$item]')
88+
fi
89+
done
90+
echo "$issues_array" > /tmp/linked-issues.json
91+
fi
92+
93+
echo "pr_head_ref=$(jq -r '.head.ref' /tmp/pr.json)" >> "$GITHUB_OUTPUT"
94+
95+
- name: "Checkout"
96+
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
97+
with:
98+
ref: ${{ steps.review.outputs.pr_head_ref }}
99+
token: ${{ secrets.PHPSTAN_BOT_FORK_TOKEN }}
100+
101+
- name: "Install PHP"
102+
uses: "shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1" # v2
103+
with:
104+
coverage: "none"
105+
php-version: "8.4"
106+
extensions: mbstring
107+
ini-file: development
108+
ini-values: memory_limit=-1
109+
110+
- name: "Install dependencies"
111+
uses: "ramsey/composer-install@3cf229dc2919194e9e36783941438d17239e8520" # v3
112+
113+
- name: "Install test dependencies"
114+
run: composer install --working-dir tests
115+
116+
- name: Build prompt
117+
id: prompt
118+
shell: bash
119+
run: |
120+
review_body=$(jq -r '.body // ""' /tmp/review.json)
121+
review_state=$(jq -r '.state' /tmp/review.json)
122+
reviewer=$(jq -r '.user.login' /tmp/review.json)
123+
num_comments=$(jq 'length' /tmp/review-comments.json)
124+
125+
prompt="A reviewer ($reviewer) submitted a review on PR #${{ inputs.pr_number }} with state: $review_state."
126+
prompt+=$'\n'
127+
128+
# --- PR description ---
129+
pr_title=$(jq -r '.title // ""' /tmp/pr.json)
130+
pr_body=$(jq -r '.body // ""' /tmp/pr.json)
131+
pr_author=$(jq -r '.user.login' /tmp/pr.json)
132+
133+
prompt+=$'\n'
134+
prompt+="## Pull request"
135+
prompt+=$'\n\n'
136+
prompt+="**#${{ inputs.pr_number }}: $pr_title** (by $pr_author)"
137+
prompt+=$'\n\n'
138+
139+
if [ -n "$pr_body" ] && [ "$pr_body" != "null" ]; then
140+
prompt+="$pr_body"
141+
prompt+=$'\n'
142+
fi
143+
144+
# --- Linked issues ---
145+
num_issues=$(jq 'length' /tmp/linked-issues.json)
146+
if [ "$num_issues" -gt 0 ]; then
147+
prompt+=$'\n'
148+
prompt+="## Linked issues"
149+
prompt+=$'\n'
150+
151+
for i in $(seq 0 $((num_issues - 1))); do
152+
issue_title=$(jq -r ".[$i].title" /tmp/linked-issues.json)
153+
issue_number=$(jq -r ".[$i].number" /tmp/linked-issues.json)
154+
issue_body=$(jq -r ".[$i].body // \"\"" /tmp/linked-issues.json)
155+
issue_repo=$(jq -r ".[$i].repository_url" /tmp/linked-issues.json | grep -oE '[^/]+/[^/]+$')
156+
issue_state=$(jq -r ".[$i].state" /tmp/linked-issues.json)
157+
158+
prompt+=$'\n'
159+
prompt+="### $issue_repo#$issue_number: $issue_title ($issue_state)"
160+
prompt+=$'\n\n'
161+
162+
if [ -n "$issue_body" ] && [ "$issue_body" != "null" ]; then
163+
# Truncate very long issue bodies
164+
truncated=$(echo "$issue_body" | head -c 3000)
165+
prompt+="$truncated"
166+
if [ ${#issue_body} -gt 3000 ]; then
167+
prompt+=$'\n[...truncated]'
168+
fi
169+
prompt+=$'\n'
170+
fi
171+
done
172+
fi
173+
174+
# --- PR diff ---
175+
prompt+=$'\n'
176+
prompt+="## PR diff"
177+
prompt+=$'\n\n'
178+
prompt+='```diff'
179+
prompt+=$'\n'
180+
# Truncate very large diffs
181+
diff_content=$(head -c 50000 /tmp/pr.diff)
182+
prompt+="$diff_content"
183+
diff_size=$(wc -c < /tmp/pr.diff | tr -d ' ')
184+
if [ "$diff_size" -gt 50000 ]; then
185+
prompt+=$'\n[...diff truncated, read the full files for complete context]'
186+
fi
187+
prompt+=$'\n'
188+
prompt+='```'
189+
prompt+=$'\n'
190+
191+
# --- Previous PR comments (conversation) ---
192+
num_pr_comments=$(jq 'length' /tmp/pr-comments.json)
193+
if [ "$num_pr_comments" -gt 0 ]; then
194+
prompt+=$'\n'
195+
prompt+="## Previous PR comments"
196+
prompt+=$'\n'
197+
198+
for i in $(seq 0 $((num_pr_comments - 1))); do
199+
commenter=$(jq -r ".[$i].user.login" /tmp/pr-comments.json)
200+
comment_body=$(jq -r ".[$i].body" /tmp/pr-comments.json)
201+
comment_date=$(jq -r ".[$i].created_at" /tmp/pr-comments.json)
202+
203+
prompt+=$'\n'
204+
prompt+="**$commenter** ($comment_date):"
205+
prompt+=$'\n\n'
206+
prompt+="$comment_body"
207+
prompt+=$'\n'
208+
done
209+
fi
210+
211+
# --- Previous reviews ---
212+
num_all_reviews=$(jq 'length' /tmp/all-reviews.json)
213+
if [ "$num_all_reviews" -gt 0 ]; then
214+
prompt+=$'\n'
215+
prompt+="## Previous reviews"
216+
prompt+=$'\n'
217+
218+
for i in $(seq 0 $((num_all_reviews - 1))); do
219+
rid=$(jq -r ".[$i].id" /tmp/all-reviews.json)
220+
# Skip the current review - it's shown separately below
221+
if [ "$rid" = "${{ inputs.review_id }}" ]; then
222+
continue
223+
fi
224+
225+
rev_reviewer=$(jq -r ".[$i].user.login" /tmp/all-reviews.json)
226+
rev_state=$(jq -r ".[$i].state" /tmp/all-reviews.json)
227+
rev_body=$(jq -r ".[$i].body // \"\"" /tmp/all-reviews.json)
228+
rev_date=$(jq -r ".[$i].submitted_at" /tmp/all-reviews.json)
229+
230+
prompt+=$'\n'
231+
prompt+="### Review by $rev_reviewer ($rev_state, $rev_date)"
232+
prompt+=$'\n\n'
233+
234+
if [ -n "$rev_body" ] && [ "$rev_body" != "null" ] && [ "$rev_body" != "" ]; then
235+
prompt+="$rev_body"
236+
prompt+=$'\n'
237+
fi
238+
done
239+
fi
240+
241+
# --- Current review (the one to address) ---
242+
prompt+=$'\n'
243+
prompt+="## Current review to address"
244+
prompt+=$'\n'
245+
246+
if [ -n "$review_body" ] && [ "$review_body" != "null" ]; then
247+
prompt+=$'\n'
248+
prompt+="### Review body"
249+
prompt+=$'\n\n'
250+
prompt+="$review_body"
251+
prompt+=$'\n'
252+
fi
253+
254+
if [ "$num_comments" -gt 0 ]; then
255+
prompt+=$'\n'
256+
prompt+="### Review comments"
257+
prompt+=$'\n'
258+
259+
for i in $(seq 0 $((num_comments - 1))); do
260+
file_path=$(jq -r ".[$i].path" /tmp/review-comments.json)
261+
line=$(jq -r ".[$i].line // .[$i].original_line // \"?\"" /tmp/review-comments.json)
262+
body=$(jq -r ".[$i].body" /tmp/review-comments.json)
263+
diff_hunk=$(jq -r ".[$i].diff_hunk // \"\"" /tmp/review-comments.json)
264+
265+
prompt+=$'\n'
266+
prompt+="#### $file_path (line $line)"
267+
prompt+=$'\n\n'
268+
269+
if [ -n "$diff_hunk" ] && [ "$diff_hunk" != "null" ]; then
270+
prompt+='```diff'
271+
prompt+=$'\n'
272+
prompt+="$diff_hunk"
273+
prompt+=$'\n'
274+
prompt+='```'
275+
prompt+=$'\n\n'
276+
fi
277+
278+
prompt+="$body"
279+
prompt+=$'\n'
280+
done
281+
fi
282+
283+
prompt+=$'\n'
284+
prompt+="## Instructions"
285+
prompt+=$'\n\n'
286+
prompt+="Please address this review. Make the requested code changes, then run tests with \`make tests\` and static analysis with \`make phpstan\` to verify."
287+
prompt+=$'\n'
288+
prompt+="Commit each logical change separately with a descriptive message."
289+
290+
{
291+
echo 'PROMPT<<PROMPT_DELIMITER'
292+
echo "$prompt"
293+
echo 'PROMPT_DELIMITER'
294+
} >> "$GITHUB_OUTPUT"
295+
296+
- name: Install Claude Code
297+
run: npm install -g @anthropic-ai/claude-code
298+
299+
- name: Run Claude
300+
env:
301+
CLAUDE_CODE_OAUTH_TOKEN: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
302+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_FORK_TOKEN }}
303+
PROMPT: ${{ steps.prompt.outputs.PROMPT }}
304+
run: |
305+
claude --model claude-opus-4-6 \
306+
--dangerously-skip-permissions \
307+
--output-format text \
308+
-p "$PROMPT" \
309+
> /tmp/claude-response.txt
310+
311+
- name: Commit and push changes
312+
id: push
313+
run: |
314+
if [ -z "$(git status --porcelain)" ]; then
315+
echo "pushed=false" >> "$GITHUB_OUTPUT"
316+
exit 0
317+
fi
318+
319+
git config user.name "phpstan-bot"
320+
git config user.email "ondrej+phpstanbot@mirtes.cz"
321+
git add -A
322+
git commit -m "Address review feedback on PR #${{ inputs.pr_number }}"
323+
git push
324+
echo "pushed=true" >> "$GITHUB_OUTPUT"
325+
326+
- name: Reply to review
327+
if: always()
328+
env:
329+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_PR_TOKEN }}
330+
PUSHED: ${{ steps.push.outputs.pushed }}
331+
run: |
332+
body=""
333+
if [ -f /tmp/claude-response.txt ]; then
334+
body=$(cat /tmp/claude-response.txt)
335+
fi
336+
337+
if [ "$PUSHED" = "true" ]; then
338+
body+=$'\n\n---\n_I pushed a commit addressing this review._'
339+
fi
340+
341+
if [ -z "$body" ]; then
342+
body="I processed this review but have nothing to report."
343+
fi
344+
345+
gh api \
346+
"repos/phpstan/phpstan/pulls/${{ inputs.pr_number }}/comments" \
347+
-f body="$body" \
348+
-F in_reply_to="$(jq -r '.[0].id // empty' /tmp/review-comments.json)" \
349+
2>/dev/null \
350+
|| gh pr comment "${{ inputs.pr_number }}" \
351+
--repo "phpstan/phpstan" \
352+
--body "$body"

0 commit comments

Comments
 (0)