Skip to content

Commit e33a0ae

Browse files
authored
Update gemini_review.py
1 parent 8a7c61e commit e33a0ae

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

hack/gemini_review.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from github import Github
44

55
def get_pr_latest_commit_diff(repo_name, pr_number, github_token):
6-
"""Retrieves and cleans the diff from the latest commit of a PR."""
6+
"""Retrieves and cleans the diff from the latest commit of a PR, excluding test files."""
77
g = Github(github_token)
88
repo = g.get_repo(repo_name)
99
pr = repo.get_pull(pr_number)
@@ -15,8 +15,10 @@ def get_pr_latest_commit_diff(repo_name, pr_number, github_token):
1515
files = latest_commit.files
1616
combined_diff = ""
1717
for file in files:
18-
if file.patch:
19-
combined_diff += file.patch + "\n"
18+
# Exclude test files (adjust the condition as needed)
19+
if not file.filename.endswith("_test.go") and not file.filename.endswith("_test.py") and not "/test/" in file.filename:
20+
if file.patch:
21+
combined_diff += file.patch + "\n"
2022
return combined_diff
2123
else:
2224
return None # No commits in the PR
@@ -29,6 +31,11 @@ def generate_gemini_review(diff, api_key):
2931
genai.configure(api_key=api_key)
3032
model = genai.GenerativeModel('gemini-pro')
3133

34+
max_diff_length = 20000 # Example limit (adjust based on token count)
35+
if len(diff) > max_diff_length:
36+
diff = diff[:max_diff_length]
37+
diff += "\n... (truncated due to length limit) ..."
38+
3239
prompt = f"""
3340
Review the following code diff and provide feedback. Point out potential issues,
3441
suggest improvements, and highlight good practices. Keep the review concise.
@@ -55,7 +62,7 @@ def main():
5562
repo_name = os.environ.get('GITHUB_REPOSITORY')
5663
github_token = os.environ.get('GITHUB_TOKEN')
5764

58-
diff = get_pr_latest_commit_diff(repo_name, pr_number, github_token) # get diff from latest commit
65+
diff = get_pr_latest_commit_diff(repo_name, pr_number, github_token)
5966

6067
if diff is None:
6168
print("Failed to retrieve PR diff from latest commit. Exiting.")

0 commit comments

Comments
 (0)