1
1
import google .generativeai as genai
2
2
import os
3
3
from github import Github
4
+ import re
5
+ from google .cloud import storage
4
6
5
7
def get_pr_latest_commit_diff_files (repo_name , pr_number , github_token ):
6
8
"""Retrieves diff information for each file in the latest commit of a PR, excluding test files."""
@@ -25,8 +27,25 @@ def get_pr_latest_commit_diff_files(repo_name, pr_number, github_token):
25
27
print (f"Error getting diff files from latest commit: { e } " )
26
28
return None
27
29
28
- def generate_gemini_review_with_annotations (diff_file , api_key ):
29
- """Generates a code review with annotations for a single file using the Gemini API."""
30
+ def download_and_combine_guidelines (bucket_name , prefix ):
31
+ """Downloads markdown files from GCS using the google-cloud-storage library."""
32
+ try :
33
+ storage_client = storage .Client ()
34
+ bucket = storage_client .bucket (bucket_name )
35
+ blobs = bucket .list_blobs (prefix = prefix ) # Use prefix for efficiency
36
+
37
+ guidelines_content = ""
38
+ for blob in blobs :
39
+ if blob .name .endswith (".md" ):
40
+ guidelines_content += blob .download_as_text () + "\n \n "
41
+ return guidelines_content
42
+
43
+ except Exception as e :
44
+ print (f"Error downloading or combining guidelines: { e } " )
45
+ return ""
46
+
47
+ def generate_gemini_review_with_annotations (diff_file , api_key , guidelines ):
48
+ """Generates a code review with annotations, incorporating guidelines."""
30
49
genai .configure (api_key = api_key )
31
50
model = genai .GenerativeModel ('gemini-pro' )
32
51
@@ -37,6 +56,10 @@ def generate_gemini_review_with_annotations(diff_file, api_key):
37
56
diff += "\n ... (truncated due to length limit) ..."
38
57
39
58
prompt = f"""
59
+ The following are the API review guidelines:
60
+
61
+ { guidelines }
62
+
40
63
Review the following code diff from file `{ diff_file .filename } ` and provide feedback.
41
64
Point out potential issues, suggest improvements, and highlight good practices.
42
65
For each issue or suggestion, specify the line numbers from the diff where the change occurs.
@@ -94,14 +117,19 @@ def main():
94
117
repo_name = os .environ .get ('GITHUB_REPOSITORY' )
95
118
github_token = os .environ .get ('GITHUB_TOKEN' )
96
119
120
+ # Use the GCS client library
121
+ guidelines = download_and_combine_guidelines ("hackathon-sme-code-review-train" , "guidelines/" )
122
+ if not guidelines :
123
+ print ("Warning: No guidelines loaded. Review will proceed without guidelines." )
124
+
97
125
diff_files = get_pr_latest_commit_diff_files (repo_name , pr_number , github_token )
98
126
99
127
if diff_files is None :
100
128
print ("Failed to retrieve PR diff files from latest commit. Exiting." )
101
129
return
102
130
103
131
for diff_file in diff_files :
104
- review_comment = generate_gemini_review_with_annotations (diff_file , api_key )
132
+ review_comment = generate_gemini_review_with_annotations (diff_file , api_key , guidelines )
105
133
post_github_review_comments (repo_name , pr_number , diff_file , review_comment , github_token )
106
134
107
135
if __name__ == "__main__" :
0 commit comments