-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithub_adapter.py
More file actions
232 lines (187 loc) · 9.66 KB
/
Copy pathgithub_adapter.py
File metadata and controls
232 lines (187 loc) · 9.66 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
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
from utils.github_api import GithubAPI
import aiohttp, sys,os,httpx
from utils.jwt_generator import GenerateJWT
from events.ticketFeedbackHandler import TicketFeedbackHandler
from utils.logging_file import logger
class GithubAdapter:
def __init__(self):
return
async def get_github_data(code):
try:
github_url_for_access_token = 'https://github.com/login/oauth/access_token'
data = {
"client_id": os.getenv("GITHUB_CLIENT_ID"),
"client_secret": os.getenv("GITHUB_CLIENT_SECRET"),
"code": code
}
headers = {
"Accept": "application/json"
}
async with aiohttp.ClientSession() as session:
async with session.post(github_url_for_access_token, data=data, headers=headers) as response:
res = await response.json()
return res
except Exception as e:
raise Exception
async def get_github_user(header):
try:
async with aiohttp.ClientSession() as session:
async with session.get("https://api.github.com/user", headers=header) as user_response:
response = await user_response.json()
return response
except Exception as e:
return []
async def fetch_github_issues_from_repo(owner,repo):
try:
url = f"https://api.github.com/repos/{owner}/{repo}/issues"
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {os.getenv("GithubPAT")}',
'X-GitHub-Api-Version': '2022-11-28'
}
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
if response.status == 200:
issues = await response.json()
return issues
else:
print(f"Failed to get issues: {response.status}")
return None
except Exception as e:
logger.info(e)
return None
async def get_classroom_data(assignment_id,page):
github_api_url = f'https://api.github.com/assignments/{assignment_id}/accepted_assignments?page={page}'
# Define request headers
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': 'Bearer'+' '+ os.getenv('API_TOKEN'),
'X-GitHub-Api-Version': '2022-11-28'
}
async with httpx.AsyncClient() as client:
try:
# Make the request to the GitHub API
response = await client.get(github_api_url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Return the response from the GitHub API
response,code = response.json(),200
except :
response = [],400
return response,code
async def createComment(self, owner, repo, issue_number, markdown_dict):
token = await GithubAPI().authenticate_app_as_installation(repo_owner=owner)
url = f'https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/comments'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
data = {
'body': f'{TicketFeedbackHandler.feedBackMessageCreator(markdown_dict=markdown_dict)}'
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
if response.status == 201:
print('Comment created successfully.')
return await response.json()
else:
print(f'Error creating comment. Status code: {response.status}', sys.stderr)
response_text = await response.text()
print(f'Response body: {response_text}', file=sys.stderr)
async def updateComment(self, owner, repo, comment_id, markdown_dict):
token = await GithubAPI().authenticate_app_as_installation(repo_owner=owner)
url = f'https://api.github.com/repos/{owner}/{repo}/issues/comments/{comment_id}'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
data = {
'body': f'{TicketFeedbackHandler.feedBackMessageCreator(markdown_dict)}'
}
async with aiohttp.ClientSession() as session:
async with session.patch(url, headers=headers, json=data) as response:
if response.status == 200:
print('Comment updated successfully.')
return await response.json()
else:
print(f'Error updating comment. Status code: {response.status}', file=sys.stderr)
response_text = await response.text()
print(f'Response body: {response_text}',file=sys.stderr)
async def deleteComment(self, owner, repo, comment_id):
token = await GithubAPI().authenticate_app_as_installation(repo_owner=owner)
url = f'https://api.github.com/repos/{owner}/{repo}/issues/comments/{comment_id}'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
async with aiohttp.ClientSession() as session:
async with session.delete(url, headers=headers) as response:
if response.status == 204:
print('Comment deleted successfully.')
else:
print(f'Error deleting comment. Status code: {response.status}', file=sys.stderr)
response_text = await response.text()
print(f'Response body: {response_text}', file=sys.stderr)
async def bot_comments(self):
async def get_installations():
token = GenerateJWT().__call__()
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.github.machine-man-preview+json' # Required for accessing GitHub app APIs
}
installations_url = 'https://api.github.com/app/installations'
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(installations_url) as response:
if response.status == 200:
installations = await response.json()
return installations
async def get_repositories(installation):
repositories_url = f'https://api.github.com/installation/repositories'
token = await GithubAPI().authenticate_app_as_installation(installation["account"]["login"])
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/vnd.github.machine-man-preview+json' # Required for accessing GitHub app APIs
}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(repositories_url) as response:
# print(await response.json(), file=sys.stderr)
if response.status == 200:
data = await response.json()
return data["repositories"]
async def get_comments(repository):
# print(repository, file=sys.stderr)
repository_owner = repository['owner']['login']
repository_name = repository['name']
comments_url = f'https://api.github.com/repos/{repository_owner}/{repository_name}/comments'
headers = {
'Authorization': f'Bearer {os.getenv("GithubPAT")}',
'Accept': 'application/vnd.github.machine-man-preview+json' # Required for accessing GitHub app APIs
}
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(comments_url) as response:
if response.status == 200:
comments = await response.json()
# print(comments, file=sys.stderr)
return comments
comments = []
app_installations = await get_installations()
for installation in app_installations:
repositories = await get_repositories(installation)
if repositories:
print("----RePO-----", file=sys.stderr)
for repo in repositories:
print(installation["account"]["login"]+'/'+repo["name"], file=sys.stderr)
# print(repo, file=sys.stderr)
# data = await get_comments(repo)
# if data:
# comments+=data
count = 0
for comment in comments:
# print(comment)
if comment["user"]["login"] == "c4gt-community-support[bot]":
count+=1
print(count, file=sys.stderr)
return app_installations