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 ---\n Content 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