github-pr-review.mdx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ---
  2. title: "GitHub PR Review"
  3. description: "Automatically review Pull Requests with AI using Cline CLI in GitHub Actions."
  4. ---
  5. Automate code review for every Pull Request. Detailed analysis, security checks, and code suggestions provided by Cline running autonomously in GitHub Actions.
  6. ## The Workflow
  7. When a PR is opened or marked ready for review, this workflow:
  8. 1. **Checks out** the code.
  9. 2. **Installs** Node.js and Cline CLI.
  10. 3. **Configures** authentication (e.g., Anthropic, OpenAI).
  11. 4. **Runs Cline** with a comprehensive system prompt to analyze the diff, context, and related issues using GitHub CLI (`gh`).
  12. 5. **Posts** a detailed review comment with inline code suggestions.
  13. ## Prerequisites
  14. - **GitHub repository** with Actions enabled.
  15. - **AI Provider API Key** (e.g., Anthropic, OpenRouter) added as a repository secret (e.g., `ANTHROPIC_API_KEY`).
  16. - **GitHub Token** (automatically provided by Actions as `GITHUB_TOKEN`).
  17. ## Setup
  18. ### 1. Create the Workflow File
  19. Create a file named `.github/workflows/cline-pr-review.yml` in your repository:
  20. ```yaml
  21. name: Cline PR Code Review
  22. on:
  23. pull_request:
  24. types: [opened, ready_for_review]
  25. workflow_dispatch:
  26. inputs:
  27. pr_number:
  28. description: "PR number to review"
  29. required: true
  30. type: string
  31. concurrency:
  32. group: pr-review-${{ github.event.pull_request.number || inputs.pr_number }}
  33. cancel-in-progress: true
  34. jobs:
  35. cline-pr-review:
  36. if: |
  37. (github.event_name == 'pull_request' && github.event.pull_request.draft == false) ||
  38. github.event_name == 'workflow_dispatch'
  39. runs-on: ubuntu-latest
  40. timeout-minutes: 60
  41. permissions:
  42. contents: read
  43. pull-requests: write
  44. issues: read
  45. steps:
  46. - name: Checkout repository
  47. uses: actions/checkout@v4
  48. with:
  49. fetch-depth: 0
  50. - name: Setup Node.js
  51. uses: actions/setup-node@v4
  52. with:
  53. node-version: 22
  54. cache: "npm"
  55. - name: Install Cline CLI
  56. run: npm install -g cline
  57. - name: Configure Cline Authentication
  58. # Replace 'anthropic' with your provider of choice (openai, openrouter, etc.)
  59. # and ensure the corresponding secret is set in your repo settings.
  60. run: |
  61. cline auth --provider anthropic \
  62. --apikey "${{ secrets.ANTHROPIC_API_KEY }}" \
  63. --modelid claude-opus-4-5-20251101
  64. - name: Get PR number
  65. id: pr
  66. run: |
  67. if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
  68. echo "number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT
  69. else
  70. echo "number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
  71. fi
  72. - name: Review PR with Cline
  73. env:
  74. PR_NUMBER: ${{ steps.pr.outputs.number }}
  75. GITHUB_REPO: ${{ github.repository }}
  76. GH_TOKEN: ${{ github.token }}
  77. # Restrict Cline to only safe, read-only GitHub CLI commands
  78. CLINE_COMMAND_PERMISSIONS: |
  79. {
  80. "allow": [
  81. "gh pr diff *",
  82. "gh pr view *",
  83. "gh pr checks *",
  84. "gh pr list *",
  85. "gh issue list *",
  86. "gh issue view *",
  87. "git log *",
  88. "gh pr comment ${{ steps.pr.outputs.number }} *",
  89. "gh api repos/${{ github.repository }}/pulls/${{ steps.pr.outputs.number }}/comments *",
  90. "gh api repos/${{ github.repository }}/pulls/${{ steps.pr.outputs.number }}/reviews *"
  91. ]
  92. }
  93. run: |
  94. cline --yolo 'You are a GitHub PR reviewer for this repository. Your goal is to give the PR author helpful feedback and give maintainers the context they need to review efficiently.
  95. PR: #'"${PR_NUMBER}"'
  96. ## Gather context
  97. Use `gh` commands to fetch the PR diff, details, and checks.
  98. ```bash
  99. # Get full PR details
  100. gh pr view '"${PR_NUMBER}"' --json number,title,body,author,createdAt,updatedAt,isDraft,labels,commits,files,additions,deletions,changedFiles,baseRefName,headRefName,mergeable,reviewDecision
  101. # Get the diff
  102. gh pr diff '"${PR_NUMBER}"'
  103. # Check CI status
  104. gh pr checks '"${PR_NUMBER}"'
  105. ```
  106. ## Deep code review
  107. Analyze the code changes. Look for:
  108. - Logic errors and edge cases
  109. - Security vulnerabilities
  110. - Performance issues
  111. - adherence to patterns in the codebase
  112. ## Submit Review
  113. Post a single comprehensive comment summarizing your review.
  114. If you have specific code suggestions, use the GitHub API to post inline comments:
  115. ```bash
  116. gh api repos/'"${GITHUB_REPO}"'/pulls/'"${PR_NUMBER}"'/reviews \
  117. -X POST \
  118. -f event="COMMENT" \
  119. -f body="" \
  120. -F comments='[{"path": "src/file.ts", "line": 10, "body": "Suggestion: ..."}]'
  121. ```
  122. Start your main comment with "Reviewed by Cline".'
  123. ```
  124. ### 2. Configure Secrets
  125. 1. Go to your repository settings -> **Secrets and variables** -> **Actions**.
  126. 2. Add a **New repository secret**.
  127. 3. Name: `ANTHROPIC_API_KEY` (or match the key used in your workflow).
  128. 4. Value: Your actual API key.
  129. ## Key Components Explained
  130. ### Permissions
  131. ```yaml
  132. permissions:
  133. contents: read
  134. pull-requests: write
  135. issues: read
  136. ```
  137. We grant `pull-requests: write` so Cline can post comments and inline reviews. `contents: read` ensures it can analyze the code but **cannot push changes directly**, providing a security boundary.
  138. ### Authentication
  139. ```bash
  140. cline auth --provider anthropic --apikey "..."
  141. ```
  142. The `auth` command configures Cline in the CI environment without interactive prompts. You can switch providers (e.g., `openai`, `openrouter`) by changing the flags.
  143. ### Autonomous Mode (`--yolo`)
  144. ```bash
  145. cline --yolo '...'
  146. ```
  147. The `--yolo` flag tells Cline to run autonomously, executing commands without waiting for user approval. This is essential for CI/CD workflows.
  148. ### Command Permissions
  149. We explicitly restrict what commands Cline can run using `CLINE_COMMAND_PERMISSIONS`. This ensures Cline can only use `gh` and `git` commands relevant to reviewing, preventing any accidental or malicious system modifications.
  150. ## Customizing the Reviewer
  151. The "System Prompt" passed to Cline in the final step is fully customizable. You can modify it to:
  152. - Enforce specific style guides.
  153. - Focus on security vs. performance.
  154. - Ask for specific types of feedback (e.g., "Roast my code" vs. "Be gentle").