-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_client.py
More file actions
99 lines (73 loc) · 1.32 KB
/
Copy pathgithub_client.py
File metadata and controls
99 lines (73 loc) · 1.32 KB
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
import json
import os
import requests
def load_event():
event_path = os.environ.get(
"GITHUB_EVENT_PATH"
)
if not event_path:
return {}
with open(
event_path,
"r",
encoding="utf-8"
) as f:
return json.load(f)
def extract_question(
question_input
):
if question_input:
return question_input.strip()
event = load_event()
comment = (
event.get("comment", {})
.get("body", "")
)
if comment.startswith(
"/gitgpt"
):
return comment.replace(
"/gitgpt",
"",
1
).strip()
return ""
def post_comment(
github_token,
repo,
answer,
sources
):
event = load_event()
issue = event.get(
"issue",
{}
)
issue_number = issue.get(
"number"
)
if not issue_number:
return
body = f"""
## GitGPT Answer
{answer}
### Sources
""" + "\n".join(
f"- `{s}`"
for s in sources
)
url = (
f"https://api.github.com/repos/"
f"{repo}/issues/"
f"{issue_number}/comments"
)
requests.post(
url,
headers={
"Authorization":
f"Bearer {github_token}"
},
json={
"body": body
}
)