Skip to content

Commit 2232cdf

Browse files
committed
Enhance Gemini PR review script with guidelines download and integration
1 parent c6de3d6 commit 2232cdf

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

.github/workflows/gemini-pr-review.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ jobs:
2020
python-version: "3.9"
2121

2222
- name: Install dependencies
23-
run: pip install google-generativeai requests PyGithub
24-
25-
- name: Install gcloud CLI
26-
uses: google-github-actions/setup-gcloud@v2
27-
28-
- name: List files using curl
29-
run: |
30-
curl -X GET -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
31-
"https://storage.googleapis.com/storage/v1/b/hackathon-sme-code-review-train/o"
23+
run: pip install google-generativeai requests PyGithub google-cloud-storage
3224

3325
- name: Run Gemini review script
3426
env:

hack/gemini_review.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import google.generativeai as genai
22
import os
33
from github import Github
4+
import re
5+
from google.cloud import storage
46

57
def get_pr_latest_commit_diff_files(repo_name, pr_number, github_token):
68
"""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):
2527
print(f"Error getting diff files from latest commit: {e}")
2628
return None
2729

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."""
3049
genai.configure(api_key=api_key)
3150
model = genai.GenerativeModel('gemini-pro')
3251

@@ -37,6 +56,10 @@ def generate_gemini_review_with_annotations(diff_file, api_key):
3756
diff += "\n... (truncated due to length limit) ..."
3857

3958
prompt = f"""
59+
The following are the API review guidelines:
60+
61+
{guidelines}
62+
4063
Review the following code diff from file `{diff_file.filename}` and provide feedback.
4164
Point out potential issues, suggest improvements, and highlight good practices.
4265
For each issue or suggestion, specify the line numbers from the diff where the change occurs.
@@ -94,14 +117,19 @@ def main():
94117
repo_name = os.environ.get('GITHUB_REPOSITORY')
95118
github_token = os.environ.get('GITHUB_TOKEN')
96119

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+
97125
diff_files = get_pr_latest_commit_diff_files(repo_name, pr_number, github_token)
98126

99127
if diff_files is None:
100128
print("Failed to retrieve PR diff files from latest commit. Exiting.")
101129
return
102130

103131
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)
105133
post_github_review_comments(repo_name, pr_number, diff_file, review_comment, github_token)
106134

107135
if __name__ == "__main__":

0 commit comments

Comments
 (0)