-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
231 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
import tempfile | ||
import subprocess | ||
import shutil | ||
from typing import List, Dict | ||
from src.github_api import get_access_token | ||
|
||
|
||
def clone_repo_branch( | ||
installation_id: str, repo_full_name: str, branch: str = "main" | ||
) -> str: | ||
"""Clone specific branch of repository to temporary directory""" | ||
|
||
temp_dir = tempfile.mkdtemp() | ||
access_token = get_access_token(installation_id) | ||
clone_url = f"https://x-access-token:{access_token}@github.com/{repo_full_name}.git" | ||
try: | ||
subprocess.run( | ||
["git", "clone", "-b", branch, "--single-branch", clone_url, temp_dir], | ||
check=True, | ||
) | ||
return temp_dir | ||
except Exception as e: | ||
shutil.rmtree(temp_dir, ignore_errors=True) # Clean up on error | ||
raise e | ||
|
||
|
||
def index_code_files(temp_dir: str) -> List[Dict[str, str]]: | ||
"""Index all code files from a directory""" | ||
code_files = [] | ||
|
||
for root, _, files in os.walk(temp_dir): | ||
for file in files: | ||
if file.startswith(".") or "node_modules" in root: | ||
continue | ||
|
||
file_path = os.path.join(root, file) | ||
relative_path = os.path.relpath(file_path, temp_dir) | ||
|
||
try: | ||
with open(file_path, "r", encoding="utf-8") as f: | ||
content = f.read() | ||
code_files.append({"path": relative_path, "content": content}) | ||
except UnicodeDecodeError: | ||
continue # Skip binary files | ||
|
||
return code_files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,74 @@ | ||
import logging | ||
|
||
import ollama | ||
from typing import List, Dict | ||
|
||
from config import OLLAMA_MODEL | ||
from src.vector_db import query_similar_code | ||
from src.github_api import leave_comment | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def handle_new_pull_request( | ||
installation_id, | ||
repo_full_name, | ||
pull_request_number, | ||
pull_request_title, | ||
pull_request_body, | ||
pr_diff, | ||
): | ||
context = ( | ||
"I am programming and I plan to merge in a pull request.\ Given the title, description, and pull rquest " | ||
"code diff " | ||
"of my pull request, succinctly identify any potential issues or downsides. Remember that in the code " | ||
"diff, '+' is a code addition and '-' is code subtraction. \ End your response by asking if the PR " | ||
"author has considered these points " | ||
def generate_pr_feedback( | ||
similar_code: List[Dict], pr_title: str, pr_body: str, pr_diff: str | ||
) -> str: | ||
"""Generate feedback using Ollama""" | ||
context = "\n".join( | ||
[f"File: {code['file_path']}\n{code['content']}\n---" for code in similar_code] | ||
) | ||
|
||
prompt = f'{context} \n Title: {pull_request_title} \n Description: {pull_request_body} \n Code diff: \n """\n {pr_diff} \n """\n ' | ||
prompt = f""" | ||
I am programming and I plan to merge in a pull request. I will provide details of the pull request. You are to | ||
review it and succinctly identify in point form any potential issues, code smells, duplication, or downsides of | ||
the pull request. Consider interactions with the codebase and architectural design. Remember that in the code | ||
diff, '+' is a code addition and '-' is code subtraction. End your response by asking if the PR author has | ||
considered these points. | ||
Pull Request: | ||
Title: {pr_title} | ||
Description: {pr_body} | ||
Relevant context from codebase: | ||
{context} | ||
Changes in PR: | ||
{pr_diff} | ||
""" | ||
logging.info("generating feedback...") | ||
response = ollama.chat( | ||
model=OLLAMA_MODEL, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": prompt, | ||
}, | ||
], | ||
model=OLLAMA_MODEL, messages=[{"role": "user", "content": prompt}] | ||
) | ||
comment_text = response["message"]["content"] | ||
|
||
leave_comment(installation_id, repo_full_name, pull_request_number, comment_text) | ||
return response["message"]["content"] | ||
|
||
|
||
def handle_new_pull_request( | ||
installation_id: str, | ||
repo_id: int, | ||
repo_full_name: str, | ||
pr_number: int, | ||
pr_title: str, | ||
pr_body: str, | ||
pr_diff: str, | ||
changed_files: List[str], | ||
): | ||
"""Handle new pull request webhook""" | ||
try: | ||
# Query similar code from main branch | ||
similar_code = query_similar_code( | ||
changed_files, f"{pr_title} {pr_body} {pr_diff}", repo_id | ||
) | ||
|
||
# Generate feedback | ||
feedback = generate_pr_feedback(similar_code, pr_title, pr_body, pr_diff) | ||
|
||
# Post comment | ||
leave_comment(installation_id, repo_full_name, pr_number, feedback) | ||
|
||
logger.info(f"Posted feedback for PR #{pr_number} in {repo_full_name}") | ||
|
||
except Exception as e: | ||
logger.error(f"Error handling PR #{pr_number}: {str(e)}") | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters