Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/gemini-api-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Gemini API Review

on:
pull_request:
types: [labeled]
branches: [ "gke-ai-hackathon-api-review-bot" ]

jobs:
pr-review:
if: ${{ github.event.label.name == 'api-review' }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: '3.9'

- name: Install dependencies
run: |
pip install -q -U google-genai requests
- name: Run Gemini API review script
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
GITHUB_TOKEN: ${{ secrets.PULL_REQUEST_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: python hack/gemini_api_review.py
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ The [Kubernetes Steering community repo](https://github.com/kubernetes/steering)
## Roadmap

The [Kubernetes Enhancements repo](https://github.com/kubernetes/enhancements) provides information about Kubernetes releases, as well as feature tracking and backlogs.

fake readme
89 changes: 89 additions & 0 deletions hack/gemini_api_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
import os
import json
import requests
from google import genai


def gather_markdown_files(file_paths):
combined = ""
for file in file_paths:
if os.path.exists(file):
try:
with open(file, 'r') as f:
content = f.read()
combined += f"\n\n---\nContent of {file}:\n{content}\n"
except Exception as e:
print(f"Error reading {file}: {e}")
else:
print(f"File {file} not found, skipping.")
return combined


def post_comment_to_pr(repository, pr_number, comment_body, github_token):
url = f"https://api.github.com/repos/{repository}/issues/{pr_number}/comments"
headers = {
"Authorization": f"token {github_token}",
"Content-Type": "application/json",
"Accept": "application/vnd.github.v3+json"
}
payload = {"body": comment_body}
response = requests.post(url, headers=headers, json=payload)
if response.status_code in (200, 201):
print("Successfully posted comment to PR.")
else:
print(f"Failed to post comment to PR (status code {response.status_code}): {response.text}")


def main():
# Define the markdown files to be gathered
# file_paths = ["README.md", "docs/api-guidelines.md", "docs/api-changes.md"]
file_paths = ["README.md"]
docs = gather_markdown_files(file_paths)

# Retrieve environment variables for PR data and Gemini API credentials
pr_number = os.environ.get("PR_NUMBER")
pr_title = os.environ.get("PR_TITLE")
pr_body = os.environ.get("PR_BODY")
gemini_api_key = os.environ.get("GEMINI_API_KEY") # Now used with google-genai

# Retrieve GitHub info for posting a comment
github_token = os.environ.get("GITHUB_TOKEN")
repository = os.environ.get("GITHUB_REPOSITORY")

if not all([pr_number, pr_title, pr_body, gemini_api_key]):
print("Error: One or more required environment variables (PR_NUMBER, PR_TITLE, PR_BODY, GEMINI_API_KEY) are missing.")
return

# Build a content string for Gemini API
contents = (
f"PR Number: {pr_number}\n"
f"Title: {pr_title}\n"
f"Body: {pr_body}\n"
f"Documentation:\n{docs}"
)

try:
print("Sending payload to Gemini API using google-genai client...")
client = genai.Client(api_key=gemini_api_key)
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=contents
)
print(f"Gemini API response: {response.text}")

# If GitHub credentials are provided, post a comment with the Gemini API response
if github_token and repository:
comment_body = (
"Gemini API Review Result:\n\n"
f"{response.text}"
)
post_comment_to_pr(repository, pr_number, comment_body, github_token)
else:
print("GitHub token or repository environment variable is missing; skipping posting comment on PR.")
except Exception as e:
print(f"An error occurred while calling Gemini API: {e}")


if __name__ == "__main__":
main()