1
0

claude-pr-review.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. name: Claude PR Review
  2. on:
  3. issue_comment:
  4. types: [created]
  5. pull_request_target:
  6. types: [opened, ready_for_review]
  7. permissions:
  8. contents: read
  9. issues: read
  10. pull-requests: write
  11. id-token: write
  12. jobs:
  13. review:
  14. if: >-
  15. (github.event_name == 'pull_request_target'
  16. && github.event.pull_request.user.type != 'Bot'
  17. && !github.event.pull_request.draft)
  18. || (github.event_name == 'issue_comment'
  19. && github.event.issue.pull_request
  20. && github.event.issue.state == 'open'
  21. && startsWith(github.event.comment.body, '@claude review')
  22. && contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.comment.author_association))
  23. runs-on: ubuntu-latest
  24. timeout-minutes: 45
  25. concurrency:
  26. group: claude-review-${{ github.event.pull_request.number || github.event.issue.number }}
  27. cancel-in-progress: false
  28. permissions:
  29. contents: read
  30. pull-requests: write
  31. issues: read
  32. id-token: write
  33. steps:
  34. - name: Record when this run started
  35. id: started
  36. run: echo "at=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT"
  37. # A custom prompt puts the action in agent mode, which never reacts on its
  38. # own, so the requester gets no sign the run started.
  39. - name: Acknowledge the request
  40. if: github.event_name == 'issue_comment'
  41. continue-on-error: true
  42. env:
  43. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  44. REPO: ${{ github.repository }}
  45. COMMENT_ID: ${{ github.event.comment.id }}
  46. run: gh api "repos/${REPO}/issues/comments/${COMMENT_ID}/reactions" -f content=eyes
  47. - uses: actions/checkout@v7
  48. with:
  49. persist-credentials: false
  50. # An `@claude review` vouches for the head that existed when it was typed;
  51. # a push after it would swap the code out from under that approval.
  52. - name: Pin the head this run reviews
  53. id: pinned-sha
  54. env:
  55. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  56. REPO: ${{ github.repository }}
  57. PR: ${{ github.event.pull_request.number || github.event.issue.number }}
  58. PAYLOAD_SHA: ${{ github.event.pull_request.head.sha }}
  59. COMMENT_AT: ${{ github.event.comment.created_at }}
  60. run: |
  61. set -euo pipefail
  62. if [ -n "$PAYLOAD_SHA" ]; then
  63. echo "sha=${PAYLOAD_SHA}" >> "$GITHUB_OUTPUT"
  64. exit 0
  65. fi
  66. head=$(gh api "repos/${REPO}/pulls/${PR}" --jq '"\(.head.sha) \(.head.repo.pushed_at // "")"')
  67. HEAD_SHA=${head%% *}
  68. HEAD_PUSHED_AT=${head#* }
  69. if [ -z "$HEAD_PUSHED_AT" ]; then
  70. gh pr comment "$PR" --repo "$REPO" --body "The head repository of this pull request is gone, so the code to review cannot be verified. Nothing was reviewed."
  71. echo "::error::The head repository is unavailable; refusing to check it out."
  72. exit 1
  73. fi
  74. if [ "$(date -d "$HEAD_PUSHED_AT" +%s)" -gt "$(date -d "$COMMENT_AT" +%s)" ]; then
  75. gh pr comment "$PR" --repo "$REPO" --body "The head branch was pushed to at ${HEAD_PUSHED_AT}, after this review was requested at ${COMMENT_AT}, so the code that would be checked out here is not the code the request vouched for. Nothing was reviewed. Ask again to review the current head."
  76. echo "::error::The head moved after the request; refusing to check it out."
  77. exit 1
  78. fi
  79. echo "sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT"
  80. # An automatic re-review of a head that already has one spends a whole run
  81. # to reach the same conclusion, so settle it here rather than in the model.
  82. - name: Skip a head that already has a review
  83. id: reviewed
  84. if: github.event_name == 'pull_request_target'
  85. env:
  86. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  87. REPO: ${{ github.repository }}
  88. PR: ${{ github.event.pull_request.number }}
  89. HEAD_SHA: ${{ steps.pinned-sha.outputs.sha }}
  90. run: |
  91. set -euo pipefail
  92. posted=$(gh api "repos/${REPO}/issues/${PR}/comments" --paginate \
  93. --jq "[.[] | select(.user.login == \"github-actions[bot]\") | select((.body | contains(\"Reviewed head:\")) and (.body | contains(\"${HEAD_SHA}\")))] | length")
  94. if [ "$posted" != "0" ]; then
  95. echo "done=true" >> "$GITHUB_OUTPUT"
  96. echo "::notice::#${PR} already carries a review of ${HEAD_SHA}; nothing to review."
  97. fi
  98. # Read-only, and pinned to one immutable commit: this job holds a
  99. # write-scoped token, so running anything out of pr-head/ would be a pwn-request.
  100. - uses: actions/checkout@v7
  101. if: steps.reviewed.outputs.done != 'true'
  102. with:
  103. ref: ${{ steps.pinned-sha.outputs.sha }}
  104. path: pr-head
  105. persist-credentials: false
  106. allow-unsafe-pr-checkout: true
  107. # The skill reads CLAUDE.md on its own but not REVIEW.md, and knows nothing
  108. # of pr-head/ or this run's head: the brief is the only way both reach it.
  109. - name: Brief the reviewer
  110. if: steps.reviewed.outputs.done != 'true'
  111. env:
  112. REPO: ${{ github.repository }}
  113. PR: ${{ github.event.pull_request.number || github.event.issue.number }}
  114. HEAD_SHA: ${{ steps.pinned-sha.outputs.sha }}
  115. TRIGGER: ${{ github.event_name }} / ${{ github.event.action }}
  116. run: |
  117. set -euo pipefail
  118. {
  119. cat .github/claude/review-job.md
  120. printf '\n## This run\n\n'
  121. printf -- '- Repository: %s\n' "$REPO"
  122. printf -- '- Pull request: #%s\n' "$PR"
  123. printf -- '- Head under review, checked out read-only in pr-head/: %s\n' "$HEAD_SHA"
  124. printf -- '- Trigger: %s\n' "$TRIGGER"
  125. printf -- '- CI on that head: gh api repos/%s/commits/%s/check-runs\n' "$REPO" "$HEAD_SHA"
  126. } > "$RUNNER_TEMP/review-brief.md"
  127. - uses: anthropics/claude-code-action@v1
  128. id: review
  129. if: steps.reviewed.outputs.done != 'true'
  130. # A refused run fails this step exactly like a real defect would, so the
  131. # job classifies the failure below instead of going red on both alike.
  132. continue-on-error: true
  133. with:
  134. github_token: ${{ secrets.GITHUB_TOKEN }}
  135. claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
  136. allowed_non_write_users: "*"
  137. plugin_marketplaces: "https://github.com/anthropics/claude-code.git"
  138. plugins: "code-review@claude-code-plugins"
  139. prompt: "/code-review:code-review max --comment ${{ github.repository }}/pull/${{ github.event.pull_request.number || github.event.issue.number }}"
  140. # allowedTools only pre-approves; it denies nothing. Only the deny
  141. # list stops the review executing what it just checked out.
  142. claude_args: |
  143. --model claude-opus-5
  144. --effort xhigh
  145. --max-turns 100
  146. --allowedTools "mcp__github_inline_comment__create_inline_comment,Bash(gh api:*),Bash(gh pr diff:*),Bash(grep:*),Bash(rg:*),Bash(ls:*),Bash(find:*),Bash(sed:*),Bash(git log:*),Bash(git show:*),Bash(git diff:*),Bash(go doc:*),Bash(go env:*),Read,Glob,Grep,WebFetch,WebSearch"
  147. --disallowedTools "Bash(go build:*),Bash(go run:*),Bash(go test:*),Bash(go generate:*),Bash(go install:*),Bash(make:*),Bash(npm:*),Bash(npx:*),Bash(pnpm:*),Bash(yarn:*),Bash(node:*),Bash(bash:*),Bash(sh:*),Bash(docker:*),Bash(chmod:*),Edit,Write,NotebookEdit"
  148. --append-system-prompt-file ${{ runner.temp }}/review-brief.md
  149. - name: Upload the run transcript
  150. if: always()
  151. env:
  152. NODE_OPTIONS: ""
  153. uses: actions/upload-artifact@v7
  154. with:
  155. name: claude-review-${{ github.event.pull_request.number || github.event.issue.number }}-${{ github.run_id }}-${{ github.run_attempt }}
  156. path: ${{ runner.temp }}/claude-execution-output.json
  157. if-no-files-found: ignore
  158. retention-days: 7
  159. # An exhausted usage window or an overloaded API is not a broken workflow.
  160. # Say so where the maintainer will see it, and leave the job green.
  161. - name: Report a review the API refused to run
  162. id: throttled
  163. if: ${{ !cancelled() && steps.review.outcome == 'failure' }}
  164. env:
  165. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  166. REPO: ${{ github.repository }}
  167. PR: ${{ github.event.pull_request.number || github.event.issue.number }}
  168. TRANSCRIPT: ${{ runner.temp }}/claude-execution-output.json
  169. run: |
  170. set -euo pipefail
  171. [ -f "$TRANSCRIPT" ] || exit 0
  172. if jq -e 'any(.[]; .type == "rate_limit_event" and .rate_limit_info.status == "rejected")' "$TRANSCRIPT" >/dev/null 2>&1; then
  173. reason="the account's usage limit was already spent when this run started"
  174. elif jq -e 'any(.[]; .subtype == "api_retry" and .error_status == 529)' "$TRANSCRIPT" >/dev/null 2>&1; then
  175. reason="the API stayed overloaded through every retry"
  176. else
  177. exit 0
  178. fi
  179. echo "skipped=true" >> "$GITHUB_OUTPUT"
  180. echo "::notice::No review of #${PR}: ${reason}."
  181. gh pr comment "$PR" --repo "$REPO" --body "No review ran on this head: ${reason}. Nothing in this pull request was examined. A maintainer can ask for one with \`@claude review\`."
  182. - name: Fail if the review posted nothing
  183. if: ${{ !cancelled() && steps.pinned-sha.outcome == 'success' && steps.reviewed.outputs.done != 'true' && steps.throttled.outputs.skipped != 'true' }}
  184. env:
  185. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  186. REPO: ${{ github.repository }}
  187. PR: ${{ github.event.pull_request.number || github.event.issue.number }}
  188. STARTED_AT: ${{ steps.started.outputs.at }}
  189. run: |
  190. set -euo pipefail
  191. head=$(gh api "repos/${REPO}/pulls/${PR}" --jq '.head.sha')
  192. # updated_at, not created_at: the skill may update its existing sticky comment.
  193. # "Reviewed head:" as well as the SHA — the bot's other comments quote SHAs too.
  194. posted=$(gh api "repos/${REPO}/issues/${PR}/comments" --paginate \
  195. --jq "[.[] | select(.user.login == \"github-actions[bot]\") | select((.updated_at >= \"${STARTED_AT}\") or ((.body | contains(\"Reviewed head:\")) and (.body | contains(\"${head}\"))))] | length")
  196. inline=$(gh api "repos/${REPO}/pulls/${PR}/comments" --paginate \
  197. --jq "[.[] | select(.user.login == \"github-actions[bot]\") | select(.updated_at >= \"${STARTED_AT}\")] | length")
  198. if [ "$posted" = "0" ] && [ "$inline" = "0" ]; then
  199. echo "::error::The review run ended without posting a review of ${head} on #${PR}. Read the uploaded transcript before re-running."
  200. exit 1
  201. fi