|
| 1 | +# AI-Powered Pull Request Review Bot |
| 2 | + |
| 3 | +This example demonstrates how to use Codegen to create an intelligent PR review bot that analyzes code changes and their dependencies to provide comprehensive code reviews. The bot uses GPT-4 to generate contextual feedback based on modified code and its relationships. |
| 4 | + |
| 5 | +> [!NOTE] |
| 6 | +> This codemod helps development teams by providing automated, context-aware code reviews that consider both direct and indirect code dependencies. |
| 7 | +
|
| 8 | +## How the PR Review Bot Works |
| 9 | + |
| 10 | +The script analyzes pull requests in several key steps: |
| 11 | + |
| 12 | +1. **Symbol Analysis** |
| 13 | + ```python |
| 14 | + modified_symbols = codebase.get_modified_symbols_in_pr(pr_number) |
| 15 | + for symbol in modified_symbols: |
| 16 | + deps = codebase.get_symbol_dependencies(symbol, max_depth=2) |
| 17 | + rev_deps = codebase.get_symbol_dependents(symbol, max_depth=2) |
| 18 | + ``` |
| 19 | + - Identifies modified symbols in the PR |
| 20 | + - Analyzes dependencies up to 2 levels deep |
| 21 | + - Tracks reverse dependencies (symbols that depend on changes) |
| 22 | + |
| 23 | +2. **Context Building** |
| 24 | + ```python |
| 25 | + context = { |
| 26 | + "pr_title": pr.title, |
| 27 | + "pr_body": pr.body, |
| 28 | + "modified_symbols": [...], |
| 29 | + "context_symbols": [...] |
| 30 | + } |
| 31 | + ``` |
| 32 | + - Gathers PR metadata |
| 33 | + - Collects modified code content |
| 34 | + - Includes relevant dependency context |
| 35 | + |
| 36 | +3. **AI Review Generation** |
| 37 | + ```python |
| 38 | + review = codebase.ai_client.llm_query_with_retry( |
| 39 | + messages=[...], |
| 40 | + model="gpt-4", |
| 41 | + max_tokens=2000 |
| 42 | + ) |
| 43 | + ``` |
| 44 | + - Uses GPT-4 for analysis |
| 45 | + - Generates comprehensive review feedback |
| 46 | + - Considers full context of changes |
| 47 | + |
| 48 | +## Why This Makes Code Review Better |
| 49 | + |
| 50 | +1. **Context-Aware Analysis** |
| 51 | + - Understands code dependencies |
| 52 | + - Considers impact of changes |
| 53 | + - Reviews code in proper context |
| 54 | + |
| 55 | +2. **Comprehensive Review** |
| 56 | + - Analyzes direct modifications |
| 57 | + - Evaluates dependency impact |
| 58 | + - Suggests improvements |
| 59 | + |
| 60 | +3. **Consistent Feedback** |
| 61 | + - Structured review format |
| 62 | + - Thorough analysis every time |
| 63 | + - Scalable review process |
| 64 | + |
| 65 | +## Review Output Format |
| 66 | + |
| 67 | +The bot provides structured feedback including: |
| 68 | + |
| 69 | +``` |
| 70 | +1. Overall Assessment |
| 71 | + - High-level review of changes |
| 72 | + - Impact analysis |
| 73 | +
|
| 74 | +2. Specific Code Feedback |
| 75 | + - Detailed code comments |
| 76 | + - Style suggestions |
| 77 | + - Best practices |
| 78 | +
|
| 79 | +3. Potential Issues |
| 80 | + - Security concerns |
| 81 | + - Performance impacts |
| 82 | + - Edge cases |
| 83 | +
|
| 84 | +4. Dependency Analysis |
| 85 | + - Impact on dependent code |
| 86 | + - Breaking changes |
| 87 | + - Integration considerations |
| 88 | +
|
| 89 | +``` |
| 90 | + |
| 91 | +## Key Benefits to Note |
| 92 | + |
| 93 | +1. **Better Code Quality** |
| 94 | + - Thorough code analysis |
| 95 | + - Consistent review standards |
| 96 | + - Early issue detection |
| 97 | + |
| 98 | +2. **Time Savings** |
| 99 | + - Automated initial review |
| 100 | + - Quick feedback loop |
| 101 | + - Reduced review burden |
| 102 | + |
| 103 | +3. **Knowledge Sharing** |
| 104 | + - Educational feedback |
| 105 | + - Best practice suggestions |
| 106 | + - Team learning |
| 107 | + |
| 108 | + |
| 109 | +## Configuration Options |
| 110 | + |
| 111 | +You can customize the review by: |
| 112 | +- Adjusting dependency depth |
| 113 | +- Modifying the AI prompt |
| 114 | +- Changing the review focus areas |
| 115 | +- Tuning the GPT-4 parameters |
| 116 | + |
| 117 | +## Learn More |
| 118 | + |
| 119 | +- [Codegen Documentation](https://docs.codegen.com) |
| 120 | +- [OpenAI API Documentation](https://platform.openai.com/docs/api-reference) |
| 121 | +- [GitHub API Documentation](https://docs.github.com/en/rest) |
| 122 | +- [Codegen llm integration](https://docs.codegen.com/building-with-codegen/calling-out-to-llms) |
| 123 | + |
| 124 | +## Contributing |
| 125 | + |
| 126 | +Feel free to submit issues and enhancement requests! Contributions to improve the review bot's capabilities are welcome. |
0 commit comments