3
3
from github import Github
4
4
5
5
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 ."""
7
7
g = Github (github_token )
8
8
repo = g .get_repo (repo_name )
9
9
pr = repo .get_pull (pr_number )
@@ -15,8 +15,10 @@ def get_pr_latest_commit_diff(repo_name, pr_number, github_token):
15
15
files = latest_commit .files
16
16
combined_diff = ""
17
17
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 "
20
22
return combined_diff
21
23
else :
22
24
return None # No commits in the PR
@@ -29,6 +31,11 @@ def generate_gemini_review(diff, api_key):
29
31
genai .configure (api_key = api_key )
30
32
model = genai .GenerativeModel ('gemini-pro' )
31
33
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
+
32
39
prompt = f"""
33
40
Review the following code diff and provide feedback. Point out potential issues,
34
41
suggest improvements, and highlight good practices. Keep the review concise.
@@ -55,7 +62,7 @@ def main():
55
62
repo_name = os .environ .get ('GITHUB_REPOSITORY' )
56
63
github_token = os .environ .get ('GITHUB_TOKEN' )
57
64
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 )
59
66
60
67
if diff is None :
61
68
print ("Failed to retrieve PR diff from latest commit. Exiting." )
0 commit comments