Skip to content

Commit

Permalink
Refactor local diff review
Browse files Browse the repository at this point in the history
  • Loading branch information
rbx committed Oct 15, 2024
1 parent 1f5857b commit c4ad691
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 66 deletions.
71 changes: 5 additions & 66 deletions src/pearbot.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,16 @@
import argparse
import json
import re
import sys

from colorama import Fore, Style
from dotenv import load_dotenv

load_dotenv()

from storage import get_or_create_session
from agents import Agent
from review_github import GitHubReviewer
from review_local import analyze_diff
from ollama_utils import is_model_available, get_available_models

def extract_commit_info(diff_content):
commit_range = None
commit_messages = []

commit_hashes = re.findall(r'index ([0-9a-f]{7,40})\.\.([0-9a-f]{7,40})', diff_content)
if commit_hashes:
first_commit, last_commit = commit_hashes[0][0], commit_hashes[-1][1]
commit_range = f"{first_commit}..{last_commit}"
print(f"Extracted commit range: {commit_range}")
else:
print("No commit range found in the diff content.")

message_blocks = re.findall(r'From [0-9a-f]+ .*\nFrom: .*\nDate: .*\nSubject: \[PATCH\] (.*)', diff_content)
commit_messages.extend(message_blocks)

if commit_messages:
print(f"Extracted {len(commit_messages)} commit message(s) from the diff content.")
else:
print("No commit messages found in the diff content.")

return commit_messages

def analyze_diff(diff_content, initial_review_models, final_review_model):
extracted_messages = extract_commit_info(diff_content)

if not extracted_messages:
comments_str = "No commit information found."
else:
comments_str = "\n ".join(extracted_messages)

pr_data = {
"title": "Local Diff Analysis",
"description": f" commits:\n\n{comments_str}",
"changes": diff_content,
"context": ""
}

print(json.dumps(pr_data, indent=4))

code_review_agent = Agent(role="code_reviewer", use_post_request=True)
feedback_improver_agent = Agent(role="feedback_improver", use_post_request=True)

initial_reviews = []
for model in initial_review_models:
print(Fore.GREEN + f"\n\n >>> Requesting initial review with {model}...")
print(Style.RESET_ALL)
_, initial_review = code_review_agent.analyze(pr_data, model)
initial_reviews.append(initial_review)

improvement_data = {
"pr_data": pr_data,
"initial_reviews": initial_reviews
}

print(Fore.GREEN + f"\n\n >>> Requesting improved review (with {final_review_model})...\n\n")
print(Style.RESET_ALL)
_, improved_feedback = feedback_improver_agent.analyze(improvement_data, final_review_model)

print(f"\n\nImproved feedback:\n{improved_feedback}\n\n")

def validate_models(initial_review_models, final_review_model):
all_models = initial_review_models + [final_review_model]
unavailable_models = [model for model in all_models if not is_model_available(model)]
Expand Down Expand Up @@ -101,10 +39,11 @@ def main():
if not validate_models(initial_review_models, final_review_model):
sys.exit(1)

code_review_agent = Agent(role="code_reviewer", use_post_request=True)
feedback_improver_agent = Agent(role="feedback_improver", use_post_request=True)

if args.server:
print("Running as a server...")
code_review_agent = Agent(role="code_reviewer", use_post_request=True)
feedback_improver_agent = Agent(role="feedback_improver", use_post_request=True)
github_reviewer = GitHubReviewer(code_review_agent, feedback_improver_agent)
github_reviewer.app.config['INITIAL_REVIEW_MODELS'] = initial_review_models
github_reviewer.app.config['FINAL_REVIEW_MODEL'] = final_review_model
Expand All @@ -121,7 +60,7 @@ def main():
parser.print_help()
return

analyze_diff(diff_content, initial_review_models, final_review_model)
analyze_diff(diff_content, code_review_agent, feedback_improver_agent, initial_review_models, final_review_model)
else:
parser.print_help()

Expand Down
61 changes: 61 additions & 0 deletions src/review_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json
import re

from colorama import Fore, Style

def extract_commit_info(diff_content):
commit_range = None
commit_messages = []

commit_hashes = re.findall(r'index ([0-9a-f]{7,40})\.\.([0-9a-f]{7,40})', diff_content)
if commit_hashes:
first_commit, last_commit = commit_hashes[0][0], commit_hashes[-1][1]
commit_range = f"{first_commit}..{last_commit}"
print(f"Extracted commit range: {commit_range}")
else:
print("No commit range found in the diff content.")

message_blocks = re.findall(r'From [0-9a-f]+ .*\nFrom: .*\nDate: .*\nSubject: \[PATCH\] (.*)', diff_content)
commit_messages.extend(message_blocks)

if commit_messages:
print(f"Extracted {len(commit_messages)} commit message(s) from the diff content.")
else:
print("No commit messages found in the diff content.")

return commit_messages

def analyze_diff(diff_content, code_review_agent, feedback_improver_agent, initial_review_models, final_review_model):
extracted_messages = extract_commit_info(diff_content)

if not extracted_messages:
comments_str = "No commit information found."
else:
comments_str = "\n ".join(extracted_messages)

pr_data = {
"title": "Local Diff Analysis",
"description": f" commits:\n\n{comments_str}",
"changes": diff_content,
"context": ""
}

print(json.dumps(pr_data, indent=4))

initial_reviews = []
for model in initial_review_models:
print(Fore.GREEN + f"\n\n >>> Requesting initial review with {model}...")
print(Style.RESET_ALL)
_, initial_review = code_review_agent.analyze(pr_data, model)
initial_reviews.append(initial_review)

improvement_data = {
"pr_data": pr_data,
"initial_reviews": initial_reviews
}

print(Fore.GREEN + f"\n\n >>> Requesting improved review (with {final_review_model})...\n\n")
print(Style.RESET_ALL)
_, improved_feedback = feedback_improver_agent.analyze(improvement_data, final_review_model)

print(f"\n\nImproved feedback:\n{improved_feedback}\n\n")

0 comments on commit c4ad691

Please sign in to comment.