Skip to content

Commit 601924c

Browse files
committed
Add Gemini API-review workflow and script
1 parent ea50bae commit 601924c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Gemini API Review
2+
3+
on:
4+
pull_request:
5+
types: [labeled]
6+
branches: [ "gke-ai-hackathon-api-review-bot" ]
7+
8+
jobs:
9+
pr-review:
10+
if: ${{ github.event.label.name == 'api-review' }}
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Python 3.9
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: '3.9'
20+
21+
- name: Install dependencies
22+
run: |
23+
pip install -q -U google-genai requests
24+
25+
- name: Run Gemini API review script
26+
env:
27+
PR_NUMBER: ${{ github.event.pull_request.number }}
28+
PR_TITLE: ${{ github.event.pull_request.title }}
29+
PR_BODY: ${{ github.event.pull_request.body }}
30+
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
GITHUB_REPOSITORY: ${{ github.repository }}
33+
run: python hack/gemini_api_review.py

hack/gemini_api_review.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import json
4+
import requests
5+
from google import genai
6+
7+
8+
def gather_markdown_files(file_paths):
9+
combined = ""
10+
for file in file_paths:
11+
if os.path.exists(file):
12+
try:
13+
with open(file, 'r') as f:
14+
content = f.read()
15+
combined += f"\n\n---\nContent of {file}:\n{content}\n"
16+
except Exception as e:
17+
print(f"Error reading {file}: {e}")
18+
else:
19+
print(f"File {file} not found, skipping.")
20+
return combined
21+
22+
23+
def post_comment_to_pr(repository, pr_number, comment_body, github_token):
24+
url = f"https://api.github.com/repos/{repository}/issues/{pr_number}/comments"
25+
headers = {
26+
"Authorization": f"token {github_token}",
27+
"Content-Type": "application/json",
28+
"Accept": "application/vnd.github.v3+json"
29+
}
30+
payload = {"body": comment_body}
31+
response = requests.post(url, headers=headers, json=payload)
32+
if response.status_code in (200, 201):
33+
print("Successfully posted comment to PR.")
34+
else:
35+
print(f"Failed to post comment to PR (status code {response.status_code}): {response.text}")
36+
37+
38+
def main():
39+
# Define the markdown files to be gathered
40+
# file_paths = ["README.md", "docs/api-guidelines.md", "docs/api-changes.md"]
41+
file_paths = ["README.md"]
42+
docs = gather_markdown_files(file_paths)
43+
44+
# Retrieve environment variables for PR data and Gemini API credentials
45+
pr_number = os.environ.get("PR_NUMBER")
46+
pr_title = os.environ.get("PR_TITLE")
47+
pr_body = os.environ.get("PR_BODY")
48+
gemini_api_key = os.environ.get("GEMINI_API_KEY") # Now used with google-genai
49+
50+
# Retrieve GitHub info for posting a comment
51+
github_token = os.environ.get("GITHUB_TOKEN")
52+
repository = os.environ.get("GITHUB_REPOSITORY")
53+
54+
if not all([pr_number, pr_title, pr_body, gemini_api_key]):
55+
print("Error: One or more required environment variables (PR_NUMBER, PR_TITLE, PR_BODY, GEMINI_API_KEY) are missing.")
56+
return
57+
58+
# Build a content string for Gemini API
59+
contents = (
60+
f"PR Number: {pr_number}\n"
61+
f"Title: {pr_title}\n"
62+
f"Body: {pr_body}\n"
63+
f"Documentation:\n{docs}"
64+
)
65+
66+
try:
67+
print("Sending payload to Gemini API using google-genai client...")
68+
client = genai.Client(api_key=gemini_api_key)
69+
response = client.models.generate_content(
70+
model="gemini-2.0-flash",
71+
contents=contents
72+
)
73+
print(f"Gemini API response: {response.text}")
74+
75+
# If GitHub credentials are provided, post a comment with the Gemini API response
76+
if github_token and repository:
77+
comment_body = (
78+
"Gemini API Review Result:\n\n"
79+
f"{response.text}"
80+
)
81+
post_comment_to_pr(repository, pr_number, comment_body, github_token)
82+
else:
83+
print("GitHub token or repository environment variable is missing; skipping posting comment on PR.")
84+
except Exception as e:
85+
print(f"An error occurred while calling Gemini API: {e}")
86+
87+
88+
if __name__ == "__main__":
89+
main()

0 commit comments

Comments
 (0)