-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrun.py
More file actions
92 lines (76 loc) · 2.79 KB
/
run.py
File metadata and controls
92 lines (76 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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.")