review.yml 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. name: Guidelines Check
  2. on:
  3. pull_request_target:
  4. types: [opened]
  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. PR_DATA=$(gh api /repos/${{ github.repository }}/pulls/${{ steps.pr-number.outputs.number }})
  48. echo "title=$(echo "$PR_DATA" | jq -r .title)" >> $GITHUB_OUTPUT
  49. echo "body=$(echo "$PR_DATA" | jq -r .body)" >> $GITHUB_OUTPUT
  50. echo "sha=$(echo "$PR_DATA" | jq -r .head.sha)" >> $GITHUB_OUTPUT
  51. env:
  52. GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  53. - name: Check PR guidelines compliance
  54. env:
  55. ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
  56. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  57. OPENCODE_PERMISSION: '{ "bash": { "gh*": "allow", "gh pr review*": "deny", "*": "deny" } }'
  58. run: |
  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. ${{ steps.pr-details.outputs.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. 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.
  68. Command MUST be like this.
  69. ```
  70. gh api \
  71. --method POST \
  72. -H "Accept: application/vnd.github+json" \
  73. -H "X-GitHub-Api-Version: 2022-11-28" \
  74. /repos/${{ github.repository }}/pulls/${{ steps.pr-number.outputs.number }}/comments \
  75. -f 'body=[summary of issue]' -f 'commit_id=${{ steps.pr-details.outputs.sha }}' -f 'path=[path-to-file]' -F "line=[line]" -f 'side=RIGHT'
  76. ```
  77. Only create comments for actual violations. If the code follows all guidelines, don't run any gh commands."