Skip to content

Commit b2217f2

Browse files
committed
Incorporates PR comment history into Gemini review
1 parent 0c0a937 commit b2217f2

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

hack/gemini_review.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,24 @@ def download_and_combine_guidelines(bucket_name, prefix):
4343
print(f"Error downloading or combining guidelines: {e}")
4444
return ""
4545

46-
def generate_gemini_review_with_annotations(diff_file, api_key, guidelines):
46+
def download_and_combine_pr_comments(bucket_name, prefix):
47+
"""Downloads text files from GCS using the google-cloud-storage library."""
48+
try:
49+
storage_client = storage.Client()
50+
bucket = storage_client.bucket(bucket_name)
51+
blobs = bucket.list_blobs(prefix=prefix) # Use prefix for efficiency
52+
53+
pr_comments_content = ""
54+
for blob in blobs:
55+
if blob.name.endswith(".txt"):
56+
pr_comments_content += blob.download_as_text() + "\n\n"
57+
return pr_comments_content
58+
59+
except Exception as e:
60+
print(f"Error downloading or combining PR comments: {e}")
61+
return ""
62+
63+
def generate_gemini_review_with_annotations(diff_file, api_key, guidelines, pr_comments):
4764
"""Generates a code review with annotations, incorporating guidelines."""
4865
genai.configure(api_key=api_key)
4966
model = genai.GenerativeModel('gemini-2.0-flash')
@@ -59,6 +76,10 @@ def generate_gemini_review_with_annotations(diff_file, api_key, guidelines):
5976
6077
{guidelines}
6178
79+
The following are the previous PR comments history:
80+
81+
{pr_comments}
82+
6283
Review the following code diff from file `{diff_file.filename}` and provide feedback.
6384
Point out potential issues, suggest improvements, and highlight good practices.
6485
For each issue or suggestion, specify the line numbers from the diff where the change occurs.
@@ -129,8 +150,12 @@ def main():
129150
print("Failed to retrieve PR diff files from latest commit. Exiting.")
130151
return
131152

153+
pr_comments = download_and_combine_pr_comments("hackathon-sme-code-review-train", "pr_comments/")
154+
if not pr_comments:
155+
print("Warning: No PR comments loaded. Review will proceed without PR comments history.")
156+
132157
for diff_file in diff_files:
133-
review_comment = generate_gemini_review_with_annotations(diff_file, api_key, guidelines)
158+
review_comment = generate_gemini_review_with_annotations(diff_file, api_key, guidelines, pr_comments)
134159
post_github_review_comments(repo_name, pr_number, diff_file, review_comment, github_token)
135160

136161
if __name__ == "__main__":

0 commit comments

Comments
 (0)