Skip to content

Commit 5eb5034

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

1 file changed

Lines changed: 349 additions & 0 deletions

File tree

Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
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: Build prompt
114+
id: prompt
115+
shell: bash
116+
run: |
117+
review_body=$(jq -r '.body // ""' /tmp/review.json)
118+
review_state=$(jq -r '.state' /tmp/review.json)
119+
reviewer=$(jq -r '.user.login' /tmp/review.json)
120+
num_comments=$(jq 'length' /tmp/review-comments.json)
121+
122+
prompt="A reviewer ($reviewer) submitted a review on PR #${{ inputs.pr_number }} with state: $review_state."
123+
prompt+=$'\n'
124+
125+
# --- PR description ---
126+
pr_title=$(jq -r '.title // ""' /tmp/pr.json)
127+
pr_body=$(jq -r '.body // ""' /tmp/pr.json)
128+
pr_author=$(jq -r '.user.login' /tmp/pr.json)
129+
130+
prompt+=$'\n'
131+
prompt+="## Pull request"
132+
prompt+=$'\n\n'
133+
prompt+="**#${{ inputs.pr_number }}: $pr_title** (by $pr_author)"
134+
prompt+=$'\n\n'
135+
136+
if [ -n "$pr_body" ] && [ "$pr_body" != "null" ]; then
137+
prompt+="$pr_body"
138+
prompt+=$'\n'
139+
fi
140+
141+
# --- Linked issues ---
142+
num_issues=$(jq 'length' /tmp/linked-issues.json)
143+
if [ "$num_issues" -gt 0 ]; then
144+
prompt+=$'\n'
145+
prompt+="## Linked issues"
146+
prompt+=$'\n'
147+
148+
for i in $(seq 0 $((num_issues - 1))); do
149+
issue_title=$(jq -r ".[$i].title" /tmp/linked-issues.json)
150+
issue_number=$(jq -r ".[$i].number" /tmp/linked-issues.json)
151+
issue_body=$(jq -r ".[$i].body // \"\"" /tmp/linked-issues.json)
152+
issue_repo=$(jq -r ".[$i].repository_url" /tmp/linked-issues.json | grep -oE '[^/]+/[^/]+$')
153+
issue_state=$(jq -r ".[$i].state" /tmp/linked-issues.json)
154+
155+
prompt+=$'\n'
156+
prompt+="### $issue_repo#$issue_number: $issue_title ($issue_state)"
157+
prompt+=$'\n\n'
158+
159+
if [ -n "$issue_body" ] && [ "$issue_body" != "null" ]; then
160+
# Truncate very long issue bodies
161+
truncated=$(echo "$issue_body" | head -c 3000)
162+
prompt+="$truncated"
163+
if [ ${#issue_body} -gt 3000 ]; then
164+
prompt+=$'\n[...truncated]'
165+
fi
166+
prompt+=$'\n'
167+
fi
168+
done
169+
fi
170+
171+
# --- PR diff ---
172+
prompt+=$'\n'
173+
prompt+="## PR diff"
174+
prompt+=$'\n\n'
175+
prompt+='```diff'
176+
prompt+=$'\n'
177+
# Truncate very large diffs
178+
diff_content=$(head -c 50000 /tmp/pr.diff)
179+
prompt+="$diff_content"
180+
diff_size=$(wc -c < /tmp/pr.diff | tr -d ' ')
181+
if [ "$diff_size" -gt 50000 ]; then
182+
prompt+=$'\n[...diff truncated, read the full files for complete context]'
183+
fi
184+
prompt+=$'\n'
185+
prompt+='```'
186+
prompt+=$'\n'
187+
188+
# --- Previous PR comments (conversation) ---
189+
num_pr_comments=$(jq 'length' /tmp/pr-comments.json)
190+
if [ "$num_pr_comments" -gt 0 ]; then
191+
prompt+=$'\n'
192+
prompt+="## Previous PR comments"
193+
prompt+=$'\n'
194+
195+
for i in $(seq 0 $((num_pr_comments - 1))); do
196+
commenter=$(jq -r ".[$i].user.login" /tmp/pr-comments.json)
197+
comment_body=$(jq -r ".[$i].body" /tmp/pr-comments.json)
198+
comment_date=$(jq -r ".[$i].created_at" /tmp/pr-comments.json)
199+
200+
prompt+=$'\n'
201+
prompt+="**$commenter** ($comment_date):"
202+
prompt+=$'\n\n'
203+
prompt+="$comment_body"
204+
prompt+=$'\n'
205+
done
206+
fi
207+
208+
# --- Previous reviews ---
209+
num_all_reviews=$(jq 'length' /tmp/all-reviews.json)
210+
if [ "$num_all_reviews" -gt 0 ]; then
211+
prompt+=$'\n'
212+
prompt+="## Previous reviews"
213+
prompt+=$'\n'
214+
215+
for i in $(seq 0 $((num_all_reviews - 1))); do
216+
rid=$(jq -r ".[$i].id" /tmp/all-reviews.json)
217+
# Skip the current review - it's shown separately below
218+
if [ "$rid" = "${{ inputs.review_id }}" ]; then
219+
continue
220+
fi
221+
222+
rev_reviewer=$(jq -r ".[$i].user.login" /tmp/all-reviews.json)
223+
rev_state=$(jq -r ".[$i].state" /tmp/all-reviews.json)
224+
rev_body=$(jq -r ".[$i].body // \"\"" /tmp/all-reviews.json)
225+
rev_date=$(jq -r ".[$i].submitted_at" /tmp/all-reviews.json)
226+
227+
prompt+=$'\n'
228+
prompt+="### Review by $rev_reviewer ($rev_state, $rev_date)"
229+
prompt+=$'\n\n'
230+
231+
if [ -n "$rev_body" ] && [ "$rev_body" != "null" ] && [ "$rev_body" != "" ]; then
232+
prompt+="$rev_body"
233+
prompt+=$'\n'
234+
fi
235+
done
236+
fi
237+
238+
# --- Current review (the one to address) ---
239+
prompt+=$'\n'
240+
prompt+="## Current review to address"
241+
prompt+=$'\n'
242+
243+
if [ -n "$review_body" ] && [ "$review_body" != "null" ]; then
244+
prompt+=$'\n'
245+
prompt+="### Review body"
246+
prompt+=$'\n\n'
247+
prompt+="$review_body"
248+
prompt+=$'\n'
249+
fi
250+
251+
if [ "$num_comments" -gt 0 ]; then
252+
prompt+=$'\n'
253+
prompt+="### Review comments"
254+
prompt+=$'\n'
255+
256+
for i in $(seq 0 $((num_comments - 1))); do
257+
file_path=$(jq -r ".[$i].path" /tmp/review-comments.json)
258+
line=$(jq -r ".[$i].line // .[$i].original_line // \"?\"" /tmp/review-comments.json)
259+
body=$(jq -r ".[$i].body" /tmp/review-comments.json)
260+
diff_hunk=$(jq -r ".[$i].diff_hunk // \"\"" /tmp/review-comments.json)
261+
262+
prompt+=$'\n'
263+
prompt+="#### $file_path (line $line)"
264+
prompt+=$'\n\n'
265+
266+
if [ -n "$diff_hunk" ] && [ "$diff_hunk" != "null" ]; then
267+
prompt+='```diff'
268+
prompt+=$'\n'
269+
prompt+="$diff_hunk"
270+
prompt+=$'\n'
271+
prompt+='```'
272+
prompt+=$'\n\n'
273+
fi
274+
275+
prompt+="$body"
276+
prompt+=$'\n'
277+
done
278+
fi
279+
280+
prompt+=$'\n'
281+
prompt+="## Instructions"
282+
prompt+=$'\n\n'
283+
prompt+="Please address this review. Make the requested code changes, then run tests with \`make tests\` and static analysis with \`make phpstan\` to verify."
284+
prompt+=$'\n'
285+
prompt+="Commit each logical change separately with a descriptive message."
286+
287+
{
288+
echo 'PROMPT<<PROMPT_DELIMITER'
289+
echo "$prompt"
290+
echo 'PROMPT_DELIMITER'
291+
} >> "$GITHUB_OUTPUT"
292+
293+
- name: Install Claude Code
294+
run: npm install -g @anthropic-ai/claude-code
295+
296+
- name: Run Claude
297+
env:
298+
CLAUDE_CODE_OAUTH_TOKEN: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
299+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_FORK_TOKEN }}
300+
PROMPT: ${{ steps.prompt.outputs.PROMPT }}
301+
run: |
302+
claude --model claude-opus-4-6 \
303+
--dangerously-skip-permissions \
304+
--output-format text \
305+
-p "$PROMPT" \
306+
> /tmp/claude-response.txt
307+
308+
- name: Commit and push changes
309+
id: push
310+
run: |
311+
if [ -z "$(git status --porcelain)" ]; then
312+
echo "pushed=false" >> "$GITHUB_OUTPUT"
313+
exit 0
314+
fi
315+
316+
git config user.name "phpstan-bot"
317+
git config user.email "ondrej+phpstanbot@mirtes.cz"
318+
git add -A
319+
git commit -m "Address review feedback on PR #${{ inputs.pr_number }}"
320+
git push
321+
echo "pushed=true" >> "$GITHUB_OUTPUT"
322+
323+
- name: Reply to review
324+
if: always()
325+
env:
326+
GH_TOKEN: ${{ secrets.PHPSTAN_BOT_PR_TOKEN }}
327+
PUSHED: ${{ steps.push.outputs.pushed }}
328+
run: |
329+
body=""
330+
if [ -f /tmp/claude-response.txt ]; then
331+
body=$(cat /tmp/claude-response.txt)
332+
fi
333+
334+
if [ "$PUSHED" = "true" ]; then
335+
body+=$'\n\n---\n_I pushed a commit addressing this review._'
336+
fi
337+
338+
if [ -z "$body" ]; then
339+
body="I processed this review but have nothing to report."
340+
fi
341+
342+
gh api \
343+
"repos/phpstan/phpstan/pulls/${{ inputs.pr_number }}/comments" \
344+
-f body="$body" \
345+
-F in_reply_to="$(jq -r '.[0].id // empty' /tmp/review-comments.json)" \
346+
2>/dev/null \
347+
|| gh pr comment "${{ inputs.pr_number }}" \
348+
--repo "phpstan/phpstan" \
349+
--body "$body"

0 commit comments

Comments
 (0)