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 ---\n Content 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
+ # TODO: Add logic to check the diff of the PR and the actual code changes
59
+
60
+ # Build a content string for Gemini API
61
+ contents = (
62
+ f"PR Number: { pr_number } \n "
63
+ f"Title: { pr_title } \n "
64
+ f"Body: { pr_body } \n "
65
+ f"Documentation:\n { docs } "
66
+ )
67
+
68
+ try :
69
+ print ("Sending payload to Gemini API using google-genai client..." )
70
+ client = genai .Client (api_key = gemini_api_key )
71
+ response = client .models .generate_content (
72
+ model = "gemini-2.0-flash" ,
73
+ contents = contents
74
+ )
75
+ print (f"Gemini API response: { response .text } " )
76
+
77
+ # If GitHub credentials are provided, post a comment with the Gemini API response
78
+ if github_token and repository :
79
+ comment_body = (
80
+ "Gemini API Review Result:\n \n "
81
+ f"{ 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