-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
303 lines (281 loc) · 15 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import http.server, socket, json, os, openai, argparse, requests, threading, subprocess, sys
import tools
import dotenv
dotenv.load_dotenv(dotenv.find_dotenv())
parser = argparse.ArgumentParser(description="Translation")
parser.add_argument("--listen", type=int, required=True, help="Listen port, for example, 2023")
parser.add_argument("--forward", type=str, required=True, help="Forward message to, for example, https://discord.com/x/y/z/xxx/github")
parser.add_argument("--token", type=str, required=False, help="GitHub access token, for example, github_pat_xxx_yyyyyy")
parser.add_argument("--proxy", type=str, required=False, help="OpenAI API proxy, for example, x.y.z")
parser.add_argument("--key", type=str, required=False, help="OpenAI API key, for example, xxxyyyzzz")
parser.add_argument("--secret", type=str, required=False, help="The secret in url, for example, xxxxxx")
parser.add_argument("--open-collective", type=str, required=False, help="Callback for the OpenCollective event.")
args = parser.parse_args()
tools.github_token_init(args.token)
tools.openai_init(args.key, args.proxy)
IGNORE_LOGIN='dependabot'
logs = []
logs.append(f"listen: {args.listen}")
logs.append(f"token: {len(os.environ.get('GITHUB_TOKEN'))}B")
logs.append(f"proxy: {len(openai.api_base)}B")
logs.append(f"key: {len(openai.api_key)}B")
logs.append(f"forward: {args.forward}")
if args.secret is not None:
logs.append(f"secret: {len(args.secret)}B")
if args.open_collective is not None:
logs.append(f"open_collective: {args.open_collective}")
print(f"run with {', '.join(logs)}")
def handle_oc_request(j_req, event, delivery, headers):
name = None
formattedAmountWithInterval = None
do_forward = False
j_discord = {}
if event == 'collective.member.created':
if 'data' in j_req and 'member' in j_req['data'] and 'memberCollective' in j_req['data']['member'] and 'name' in j_req['data']['member']['memberCollective']:
name = j_req['data']['member']['memberCollective']['name']
if 'data' in j_req and 'order' in j_req['data'] and 'formattedAmountWithInterval' in j_req['data']['order']:
formattedAmountWithInterval = j_req['data']['order']['formattedAmountWithInterval']
if name is not None and formattedAmountWithInterval is not None:
do_forward = True
j_discord['content'] = f":champagne_glass: New member {name} just joined SRS and contributed with {formattedAmountWithInterval}"
print(f"Thread: {delivery}: Got an {event} as {j_discord}")
elif event == 'collective.transaction.created':
if 'data' in j_req and 'fromCollective' in j_req['data'] and 'name' in j_req['data']['fromCollective']:
name = j_req['data']['fromCollective']['name']
if 'data' in j_req and 'transaction' in j_req['data'] and 'formattedAmountWithInterval' in j_req['data']['transaction']:
formattedAmountWithInterval = j_req['data']['transaction']['formattedAmountWithInterval']
if name is not None and formattedAmountWithInterval is not None:
do_forward = True
j_discord['content'] = f":pray: Financial contribution: {name} gave {formattedAmountWithInterval} to SRS!"
print(f"Thread: {delivery}: Got an {event} as {j_discord}")
else:
print(f"Thread: {delivery}: Ignore event {event}")
if do_forward and args.open_collective is not None:
# Without any Host set.
if 'Host' in headers:
del headers['Host']
# Reset the Content-Type to application/json.
if 'Content-Type' in headers:
del headers['Content-Type']
if 'content-type' in headers:
del headers['content-type']
headers['Content-Type'] = 'application/json'
res = requests.post(args.open_collective, json=j_discord, headers=headers)
print(f"Thread: {delivery}: Response {res.status_code} {res.reason} {len(res.text)}B {res.headers}")
print(f"Thread: {delivery}: Done")
def handle_github_request(j_req, event, delivery, headers):
action = j_req['action'] if 'action' in j_req else None
print(f"Thread: {delivery}: Got a event {event} {action}, {headers}")
if 'sender' in j_req and 'login' in j_req['sender']:
sender = j_req['sender']['login']
if IGNORE_LOGIN in sender:
print(f"Thread: {delivery}: Ignore sender {sender}")
return
do_forward = False
if event == 'ping':
print(f"Thread: {delivery}: Got a test ping")
elif event == 'issues':
if action != 'opened':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
title = j_req['issue']['title']
number = j_req['issue']['number']
html_url = j_req['issue']['html_url']
print(f"Thread: {delivery}: Got an issue #{number} {html_url} {title}")
command = ["bash", "srs/issue.sh", "--input", html_url]
subprocess.run(command, stdout=sys.stdout, stderr=sys.stderr, text=True, check=True)
parsed = tools.parse_issue_url(html_url)
trans = tools.query_issue(parsed['owner'], parsed['name'], parsed['number'])
j_req['issue']['title'] = trans['title']
j_req['issue']['body'] = trans['body']
elif event == 'issue_comment':
if action != 'created':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
html_url = j_req['comment']['html_url']
issue_url = j_req['issue']['html_url']
node_id = j_req['comment']['node_id']
body = j_req['comment']['body']
print(f"Thread: {delivery}: Got a comment {html_url} of {issue_url} {node_id} {body}")
(body_trans, body_trans_by_gpt, real_translated) = tools.gpt_translate(body, False)
if real_translated:
print(f"Thread: {delivery}: Body:\n{body_trans}\n")
try:
tools.update_issue_comment(node_id, tools.wrap_magic(body_trans))
print(f"Thread: {delivery}: Updated ok")
except tools.GithubGraphQLException as e:
if e.is_forbidden():
print(f"Thread: {delivery}: Warning!!! Ignore update comment {node_id} failed, forbidden, {e.errors}")
else:
raise e
j_req['comment']['body'] = body_trans
elif event == 'discussion':
if action != 'created':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
html_url = j_req['discussion']['html_url']
number = j_req['discussion']['number']
title = j_req['discussion']['title']
print(f"Thread: {delivery}: Got a discussion #{number} {html_url} {title}")
command = ["bash", "srs/discussion.sh", "--input", html_url]
subprocess.run(command, stdout=sys.stdout, stderr=sys.stderr, text=True, check=True)
parsed = tools.parse_discussion_url(html_url)
trans = tools.query_discussion(parsed['owner'], parsed['name'], parsed['number'])
j_req['discussion']['title'] = trans['title']
j_req['discussion']['body'] = trans['body']
elif event == 'discussion_comment':
if action != 'created':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
html_url = j_req['comment']['html_url']
discussion_url = j_req['discussion']['html_url']
node_id = j_req['comment']['node_id']
body = j_req['comment']['body']
print(f"Thread: {delivery}: Got a comment {html_url} of {discussion_url} {node_id} {body}")
(body_trans, body_trans_by_gpt, real_translated) = tools.gpt_translate(body, False)
if real_translated:
print(f"Thread: {delivery}: Body:\n{body_trans}\n")
try:
tools.update_discussion_comment(node_id, tools.wrap_magic(body_trans))
print(f"Thread: {delivery}: Updated ok")
except tools.GithubGraphQLException as e:
if e.is_forbidden():
print(f"Thread: {delivery}: Warning!!! Ignore update comment {node_id} failed, forbidden, {e.errors}")
else:
raise e
j_req['comment']['body'] = body_trans
elif event == 'pull_request':
if action != 'opened':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
html_url = j_req['pull_request']['html_url']
number = j_req['pull_request']['number']
title = j_req['pull_request']['title']
print(f"Thread: {delivery}: Got a pull request #{number} {html_url} {title}")
command = ["python", "pr-trans.py", "--input", html_url]
subprocess.run(command, stdout=sys.stdout, stderr=sys.stderr, text=True, check=True)
parsed = tools.parse_pullrequest_url(html_url)
trans = tools.query_pullrequest(parsed['owner'], parsed['name'], parsed['number'])
j_req['pull_request']['title'] = trans['title']
j_req['pull_request']['body'] = trans['body']
elif event == 'pull_request_review':
if action != 'submitted':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
html_url = j_req['review']['html_url']
pull_request_url = j_req['pull_request']['html_url']
node_id = j_req['review']['node_id']
body = j_req['review']['body']
print(f"Thread: {delivery}: Got a PR review {html_url} of {pull_request_url} {node_id} {body}")
(body_trans, body_trans_by_gpt, real_translated) = tools.gpt_translate(body, False)
if real_translated:
print(f"Thread: {delivery}: Body:\n{body_trans}\n")
try:
tools.update_pullrequest_review(node_id, tools.wrap_magic(body_trans))
print(f"Thread: {delivery}: Updated ok")
except tools.GithubGraphQLException as e:
if e.is_forbidden():
print(f"Thread: {delivery}: Warning!!! Ignore update PR review {node_id} failed, forbidden, {e.errors}")
else:
raise e
j_req['review']['body'] = body_trans
elif event == 'pull_request_review_comment':
if action != 'created':
print(f"Thread: {delivery}: Ignore action {action}")
else:
do_forward = True
html_url = j_req['comment']['html_url']
pull_request_url = j_req['pull_request']['html_url']
node_id = j_req['comment']['node_id']
body = j_req['comment']['body']
print(f"Thread: {delivery}: Got a PR Review comment {html_url} of {pull_request_url} {node_id} {body}")
(body_trans, body_trans_by_gpt, real_translated) = tools.gpt_translate(body, False)
if real_translated:
print(f"Thread: {delivery}: Body:\n{body_trans}\n")
try:
tools.update_pullrequest_review_comment(node_id, tools.wrap_magic(body_trans))
print(f"Thread: {delivery}: Updated ok")
except tools.GithubGraphQLException as e:
if e.is_forbidden():
print(f"Thread: {delivery}: Warning!!! Ignore update PR Review comment {node_id} failed, forbidden, {e.errors}")
else:
raise e
j_req['comment']['body'] = body_trans
else:
print(f"Thread: {delivery}: Ignore event {event}")
if do_forward and args.forward is not None:
# Without any Host set.
if 'Host' in headers:
del headers['Host']
# Reset the Content-Type to application/json.
if 'Content-Type' in headers:
del headers['Content-Type']
if 'content-type' in headers:
del headers['content-type']
headers['Content-Type'] = 'application/json'
headers['User-Agent'] = 'GitHub-Hookshot/4689486'
res = requests.post(args.forward, json=j_req, headers=headers)
print(f"Thread: {delivery}: Response {res.status_code} {res.reason} {len(res.text)}B {res.headers}")
print(f"Thread: {delivery}: Done")
class Server(http.server.HTTPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
super().server_bind()
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path != '/api/v1/echo':
return self.send_error(404, 'Not Found')
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write(b'HelloWorld')
def do_POST(self):
if '/api/v1/hooks' not in self.path:
return self.send_error(404, 'Not Found')
if args.secret is not None and args.secret not in self.path:
return self.send_error(403, 'Forbidden')
# Read the message and convert it to a JSON object.
content_length = int(self.headers.get('Content-Length'))
req_body = self.rfile.read(content_length)
j_req = json.loads(req_body.decode('utf-8'))
headers = {}
for key in self.headers.keys():
headers[key] = self.headers.get(key)
print(f"Got a request {self.path} {len(req_body)}B {headers}")
# For GitHub.
if 'X-GitHub-Event' in self.headers and 'X-GitHub-Delivery' in self.headers:
event = self.headers.get('X-GitHub-Event')
delivery = self.headers.get('X-GitHub-Delivery')
hook = None
if 'hook' in j_req and 'config' in j_req['hook'] and 'url' in j_req['hook']['config']:
hook = j_req['hook']['config']['url']
if hook is not None:
j_req['hook']['config']['url'] = args.forward
print(f"{delivery}: Get POST body {len(req_body)}B, event={event}, hook={hook}, headers={self.headers}")
# Deliver to thread.
print(f"{delivery}: Start a thread to handle {event}")
thread = threading.Thread(target=handle_github_request, args=(j_req, event, delivery, headers))
thread.start()
# For OpenCollective.
elif 'type' in j_req and 'CollectiveId' in j_req:
event = j_req['type']
delivery = j_req['CollectiveId']
print(f"Got a request {self.path} {headers} {req_body}")
# Deliver to thread.
print(f"{delivery}: Start a thread to handle {event}")
thread = threading.Thread(target=handle_oc_request, args=(j_req, event, delivery, headers))
thread.start()
else:
return self.send_error(404, 'Not Found')
self.send_response(204)
self.end_headers()
print(f"{delivery}: Done")
httpd = Server(("", args.listen), Handler)
print(f"Serving on port {args.listen}")
httpd.serve_forever()