Skip to content

Commit a53a08d

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

File tree

2 files changed

+126
-0
lines changed

2 files changed

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

hack/gemini_api_review.py

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

0 commit comments

Comments
 (0)