review.yml 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. name: Guidelines Check
  2. on:
  3. pull_request_target:
  4. types: [opened, ready_for_review]
  5. issue_comment:
  6. types: [created]
  7. jobs:
  8. check-guidelines:
  9. if: |
  10. (github.event_name == 'pull_request_target' &&
  11. github.event.pull_request.draft == false) ||
  12. (github.event_name == 'issue_comment' &&
  13. github.event.issue.pull_request &&
  14. startsWith(github.event.comment.body, '/review'))
  15. runs-on: ubuntu-latest
  16. permissions:
  17. contents: read
  18. pull-requests: write
  19. steps:
  20. - name: Check if user has write permission
  21. if: github.event_name == 'issue_comment'
  22. run: |
  23. PERMISSION=$(gh api /repos/${{ github.repository }}/collaborators/${{ github.event.comment.user.login }}/permission --jq '.permission')
  24. if [[ "$PERMISSION" != "write" && "$PERMISSION" != "admin" ]]; then
  25. echo "User does not have write permission"
  26. exit 1
  27. fi
  28. env:
  29. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  30. - name: Get PR number
  31. id: pr-number
  32. run: |
  33. if [ "${{ github.event_name }}" = "pull_request_target" ]; then
  34. echo "number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
  35. else
  36. echo "number=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
  37. fi
  38. - name: Checkout repository
  39. uses: actions/checkout@v4
  40. with:
  41. fetch-depth: 1
  42. - name: Install opencode
  43. run: curl -fsSL https://opencode.ai/install | bash
  44. - name: Get PR details
  45. id: pr-details
  46. run: |
  47. gh api /repos/${{ github.repository }}/pulls/${{ steps.pr-number.outputs.number }} > pr_data.json
  48. echo "title=$(jq -r .title pr_data.json)" >> $GITHUB_OUTPUT
  49. echo "sha=$(jq -r .head.sha pr_data.json)" >> $GITHUB_OUTPUT
  50. env:
  51. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  52. - name: Check PR guidelines compliance
  53. env:
  54. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  55. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  56. OPENCODE_PERMISSION: '{ "bash": { "gh*": "allow", "gh pr review*": "deny", "*": "deny" } }'
  57. run: |
  58. PR_BODY=$(jq -r .body pr_data.json)
  59. opencode run -m anthropic/claude-sonnet-4-5 "A new pull request has been created: '${{ steps.pr-details.outputs.title }}'
  60. <pr-number>
  61. ${{ steps.pr-number.outputs.number }}
  62. </pr-number>
  63. <pr-description>
  64. $PR_BODY
  65. </pr-description>
  66. Please check all the code changes in this pull request against the style guide, also look for any bugs if they exist. Diffs are important but make sure you read the entire file to get proper context. Make it clear the suggestions are merely suggestions and the human can decide what to do
  67. When critiquing code against the style guide, be sure that the code is ACTUALLY in violation, don't complain about else statements if they already use early returns there. You may complain about excessive nesting though, regardless of else statement usage.
  68. Use the gh cli to create comments on the files for the violations. Try to leave the comment on the exact line number. If you have a suggested fix include it in a suggestion code block.
  69. Command MUST be like this.
  70. \`\`\`
  71. gh api \
  72. --method POST \
  73. -H \"Accept: application/vnd.github+json\" \
  74. -H \"X-GitHub-Api-Version: 2022-11-28\" \
  75. /repos/${{ github.repository }}/pulls/${{ steps.pr-number.outputs.number }}/comments \
  76. -f 'body=[summary of issue]' -f 'commit_id=${{ steps.pr-details.outputs.sha }}' -f 'path=[path-to-file]' -F \"line=[line]\" -f 'side=RIGHT'
  77. \`\`\`
  78. Only create comments for actual violations. If the code follows all guidelines, don't run any gh commands."