1+ import hmac
2+ import os
3+ import subprocess
4+ from pathlib import Path
5+ import requests
6+
7+ from dotenv import load_dotenv
8+ from flask import Flask
9+ from flask import request
10+ import git
11+ from ai_api_utils import generate_gemini_text
12+ import pamji_bot
13+ # Build paths inside the project like this: BASE_DIR / 'subdir'.
14+ BASE_DIR = Path (__file__ ).resolve ().parent
15+ load_dotenv (BASE_DIR / '.env' )
16+
17+
18+ app = Flask (__name__ )
19+ API_NAME = os .getenv ('API_NAME' )
20+ API_DESCRIPTION = os .getenv ('API_DESCRIPTION' )
21+ API_VERSION = os .getenv ('API_VERSION' )
22+ DEPLOY_COMMAND = os .getenv ('DEPLOY_COMMAND' )
23+ GITHUB_WEBHOOK_SECRET = os .getenv ('GITHUB_WEBHOOK_SECRET' )
24+
25+
26+ def verify_signature ():
27+ header_signature = request .headers .get ('X-Hub-Signature-256' )
28+
29+ if not header_signature :
30+ return False
31+
32+ sha_name , signature = header_signature .split ('=' )
33+ if sha_name != 'sha256' :
34+ return False
35+
36+ local_signature = hmac .new (GITHUB_WEBHOOK_SECRET .encode (), msg = request .get_data (), digestmod = 'sha256' )
37+ return hmac .compare_digest (local_signature .hexdigest (), signature )
38+
39+
40+ @app .route ('/github-webhook' , methods = ['POST' ])
41+ def github_webhook ():
42+ print (request .data )
43+ print (request .json )
44+ resp = request .json
45+ ref = resp ['ref' ]
46+ before = resp ['before' ]
47+ after = resp ['after' ]
48+ owner = resp ['repository' ]['owner' ]['name' ]
49+ url = resp ['repository' ]["html_url" ]
50+ description = resp ['repository' ]['description' ]
51+ push_time = resp ['repository' ]['updated_at' ]
52+ pusher = resp ['pusher' ]['name' ]
53+ comit_description = resp ["head_commit" ]["message" ]
54+ commit_url = resp ["commits" ][0 ]["url" ]
55+ repo = resp ["repository" ]["name" ]
56+ dif_url = commit_url + ".diff"
57+ url = "https://github.com/pam-ji/github-bot/commit/b3bbfc779831200522549c41a082a0dbab12d1e3.diff"
58+ response = requests .get (url )
59+ if response .status_code == 200 :
60+ diff = response .text
61+ print (diff )
62+ else :
63+ print ("Fehler:" , response .status_code )
64+ prompt = response .text
65+ instructions = "You are the github review bot. The ouput text needs to be in markdown format. highlight the important changes in the changes section. Output a maximum of 5 Lines for unimportant changes in the changes section. For important changes you can use a maximum of 10 lines changes in the changes section. Only give personal oppinion within the Copilot Advisery section if the changes are insecure, buggy, or shitcode."
66+ max_tokens = 256
67+ review = generate_gemini_text (prompt , instructions , max_tokens )
68+ print (review )
69+
70+
71+ @app .route ('/' )
72+ def index ():
73+ return {
74+ 'name' : API_NAME ,
75+ 'description' : API_DESCRIPTION ,
76+ 'version' : API_VERSION ,
77+ }
78+
79+
80+ @app .route ('/deploy' , methods = ['POST' ])
81+ def deploy ():
82+ verified = verify_signature ()
83+
84+ if not verified :
85+ return {
86+ 'message' : 'The request could not be verified. Signature missing or does not match.' ,
87+ 'verified' : False ,
88+ }, 400
89+
90+ subprocess .call (DEPLOY_COMMAND , shell = True )
91+
92+ return {
93+ 'message' : 'Deploying...' ,
94+ 'verified' : True ,
95+ }
96+ repo = 'https://github.com/pam-ji/github-bot'
97+ comit_id = "b3bbfc779831200522549c41a082a0dbab12d1e3"
98+ def get_commit_diff (repo , commit_id ):
99+ repo = git .Repo (repo )
100+ commit = repo .commit (commit_id )
101+ response = requests .get (url )
102+ if response .status_code == 200 :
103+ diff = response .text
104+ print (diff )
105+ else :
106+ print ("Fehler:" , response .status_code )
107+
108+ # Erhalte die Änderungen des Commits
109+ diff = commit .diff ()
110+
111+ # Durchlaufe die Änderungen
112+ for diff_file in diff :
113+ # Erhalte den Dateinamen und den Änderungstyp
114+ filename = diff_file .a_path
115+ change_type = diff_file .change_type
116+
117+ # Erhalte den Inhalt der Datei vor und nach dem Commit
118+ old_content = diff_file .a_blob .data_stream .read ().decode ('utf-8' )
119+ new_content = diff_file .b_blob .data_stream .read ().decode ('utf-8' )
120+
121+ # Durchlaufe die Zeilen der Datei und erhalte die Änderungen
122+ for line in diff_file .diff .decode ('utf-8' ).splitlines ():
123+ # Erhalte den Änderungstyp (z.B. '+', '-', ' ')
124+ change_type_line = line [0 ]
125+
126+ # Erhalte den Text der Zeile
127+ line_text = line [1 :]
128+
129+ # Verarbeite die Änderung
130+ if change_type_line == '+' :
131+ print (f'Added line: { line_text } ' )
132+ elif change_type_line == '-' :
133+ print (f'Removed line: { line_text } ' )
134+ else :
135+ print (f'Unchanged line: { line_text } ' )
136+
137+
138+
139+ if __name__ == '__main__' :
140+ app .run ()
141+ # url="https://github.com/pam-ji/github-bot/commit/b3bbfc779831200522549c41a082a0dbab12d1e3.diff"
142+ # response = requests.get(url)
143+ # if response.status_code == 200:
144+ # diff = response.text
145+ # print(diff)
146+ # else:
147+ # print("Fehler:", response.status_code)
148+ # prompt=response.text
149+ # instructions="You are the github review bot. The ouput text needs to be in markdown format. highlight the important changes in the changes section. Output a maximum of 5 Lines for unimportant changes in the changes section. For important changes you can use a maximum of 10 lines changes in the changes section. Only give personal oppinion within the Copilot Advisery section if the changes are insecure, buggy, or shitcode."
150+ # max_tokens=256
151+ # review=generate_gemini_text(prompt, instructions, max_tokens)
152+ # print(review)
0 commit comments