Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions examples/pr_review_bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# AI-Powered Pull Request Review Bot

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.

> [!NOTE]
> This codemod helps development teams by providing automated, context-aware code reviews that consider both direct and indirect code dependencies.

## How the PR Review Bot Works

The script analyzes pull requests in several key steps:

1. **Symbol Analysis**
```python
modified_symbols = codebase.get_modified_symbols_in_pr(pr_number)
for symbol in modified_symbols:
deps = codebase.get_symbol_dependencies(symbol, max_depth=2)
rev_deps = codebase.get_symbol_dependents(symbol, max_depth=2)
```
- Identifies modified symbols in the PR
- Analyzes dependencies up to 2 levels deep
- Tracks reverse dependencies (symbols that depend on changes)

2. **Context Building**
```python
context = {
"pr_title": pr.title,
"pr_body": pr.body,
"modified_symbols": [...],
"context_symbols": [...]
}
```
- Gathers PR metadata
- Collects modified code content
- Includes relevant dependency context

3. **AI Review Generation**
```python
review = codebase.ai_client.llm_query_with_retry(
messages=[...],
model="gpt-4",
max_tokens=2000
)
```
- Uses GPT-4 for analysis
- Generates comprehensive review feedback
- Considers full context of changes

## Why This Makes Code Review Better

1. **Context-Aware Analysis**
- Understands code dependencies
- Considers impact of changes
- Reviews code in proper context

2. **Comprehensive Review**
- Analyzes direct modifications
- Evaluates dependency impact
- Suggests improvements

3. **Consistent Feedback**
- Structured review format
- Thorough analysis every time
- Scalable review process

## Review Output Format

The bot provides structured feedback including:

```
1. Overall Assessment
- High-level review of changes
- Impact analysis

2. Specific Code Feedback
- Detailed code comments
- Style suggestions
- Best practices

3. Potential Issues
- Security concerns
- Performance impacts
- Edge cases

4. Dependency Analysis
- Impact on dependent code
- Breaking changes
- Integration considerations

```

## Key Benefits to Note

1. **Better Code Quality**
- Thorough code analysis
- Consistent review standards
- Early issue detection

2. **Time Savings**
- Automated initial review
- Quick feedback loop
- Reduced review burden

3. **Knowledge Sharing**
- Educational feedback
- Best practice suggestions
- Team learning


## Configuration Options

You can customize the review by:
- Adjusting dependency depth
- Modifying the AI prompt
- Changing the review focus areas
- Tuning the GPT-4 parameters

## Learn More

- [Codegen Documentation](https://docs.codegen.com)
- [OpenAI API Documentation](https://platform.openai.com/docs/api-reference)
- [GitHub API Documentation](https://docs.github.com/en/rest)
- [Codegen llm integration](https://docs.codegen.com/building-with-codegen/calling-out-to-llms)

## Contributing

Feel free to submit issues and enhancement requests! Contributions to improve the review bot's capabilities are welcome.
92 changes: 92 additions & 0 deletions examples/pr_review_bot/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import codegen
from codegen import Codebase
from codegen.sdk.enums import ProgrammingLanguage
from codegen.sdk.codebase.config import CodebaseConfig, GSFeatureFlags, Secrets
import json

github_token = "Your github token"
open_ai_key = "your open ai key"
pr_number = 0 # Your PR number must be an integer

codegen.function("pr-review-bot")


def run(codebase: Codebase):
context_symbols = set()

modified_symbols = codebase.get_modified_symbols_in_pr(pr_number)
for symbol in modified_symbols:
# Get direct dependencies
deps = codebase.get_symbol_dependencies(symbol, max_depth=2)
context_symbols.update(deps)

# Get reverse dependencies (symbols that depend on this one)
rev_deps = codebase.get_symbol_dependents(symbol, max_depth=2)
context_symbols.update(rev_deps)

# Prepare context for LLM
context = {
"modified_symbols": [
{
"name": symbol.name,
"type": symbol.symbol_type.value,
"filepath": symbol.filepath,
"content": symbol.content,
}
for symbol in modified_symbols
],
"context_symbols": [
{
"name": symbol.name,
"type": symbol.symbol_type.value,
"filepath": symbol.filepath,
"content": symbol.content,
}
for symbol in context_symbols
],
}

system_prompt = """
You are a helpful assistant that reviews pull requests and provides feedback on the code.
"""
# Generate review using AI
prompt = f"""Please review this pull request based on the following context:

Title: {context["pr_title"]}
Description: {context["pr_body"]}

Modified Symbols:
{json.dumps(context["modified_symbols"], indent=2)}

Related Context (Dependencies):
{json.dumps(context["context_symbols"], indent=2)}

Please provide a thorough code review that includes:
1. Overall assessment
2. Specific feedback on modified code
3. Potential issues or improvements
4. Impact on dependencies
5. Suggestions for testing
"""

review = codebase.ai_client.llm_query_with_retry(messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": prompt}], model="gpt-4", max_tokens=2000, temperature=0.7)
return review


if __name__ == "__main__":
print("Starting codebase analysis...")
codebase = Codebase.from_repo(
"getsentry/sentry",
shallow=False,
programming_language=ProgrammingLanguage.PYTHON,
config=CodebaseConfig(
secrets=Secrets(openai_key=open_ai_key, github_api_key=github_token),
feature_flags=GSFeatureFlags(
sync_enabled=True,
),
),
)
review = run(codebase)
print(review)

print("Codebase analysis complete.")