-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
267 lines (193 loc) · 7.93 KB
/
utils.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
import requests,re,markdown,os
from collections import defaultdict
from datetime import datetime, timedelta
from dateutil import parser
from flask import jsonify,request
from functools import wraps
GITHUB_TOKEN =os.getenv('GITHUB_TOKEN')
SECRET_KEY =os.getenv('SECRET_KEY')
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {GITHUB_TOKEN}",
"X-GitHub-Api-Version": "2022-11-28"
}
# Custom decorator to validate secret key
def require_secret_key(f):
@wraps(f)
def decorated_function(*args, **kwargs):
secret_key = request.headers.get('X-Secret-Key')
if secret_key != SECRET_KEY:
return jsonify({'message': 'Unauthorized access'}), 401
return f(*args, **kwargs)
return decorated_function
def find_org_data(url):
try:
url_parts = url.split("/")
owner = url_parts[4]
repo = url_parts[5]
# Fetch repository details to get organization info
repo_url = f"https://api.github.com/repos/{owner}/{repo}"
repo_response = requests.get(repo_url, headers=headers)
repo_data = repo_response.json()
if repo_data:
org_name = repo_data['owner']['login']
org_id = repo_data['owner']['id']
else:
org_name = None
org_id = None
return {"org_id":org_id,"org_name":org_name}
except Exception as e:
return {"org_id":None,"org_name":None}
def get_issue_details(issue_url):
url_parts = issue_url.split("/")
owner = url_parts[4]
repo = url_parts[5]
issue_number = url_parts[6]
# GitHub API endpoint to get the issue details
issue_api_url = f"https://api.github.com/repos/{owner}/{repo}/issues"
# Send GET request to GitHub API with authentication
response = requests.get(issue_api_url, headers=headers)
if response.status_code == 200:
issue_data = response.json()
return [{'id': issue['id'], 'name': issue['title'],'html_url':issue['html_url'],'issue_number':issue['number']} for issue in issue_data if "pull_request" not in issue]
else:
return {'id': None, 'name': None ,'html_url':None,'issue_number':None}
def group_by_owner(data):
res = []
for record in data:
org_data = find_org_data(record['issue_url'])
dict_ = {}
dict_['org_name'] = org_data['org_name']
dict_['org_id'] = org_data['org_id']
dict_['issues'] = get_issue_details(record['issue_url'])
res.append(dict_)
# org_dict = defaultdict(lambda: {'issues': [], 'org_id': None, 'org_name': None})
# for entry in res:
# org_id = entry['org_id']
# org_name = entry['org_name']
# org_dict[org_id]['issues'].extend(entry['issues'])
# org_dict[org_id]['org_id'] = org_id
# org_dict[org_id]['org_name'] = org_name
# return list(org_dict.values())
return res
def find_week_data(issue_details):
try:
#find how many weeks in reponse
weekly_updates = []
for item in issue_details:
if "Weekly Goals" in item["body"]:
week_match = re.search(r'Week \d+', item["body"])
if week_match:
weekly_updates.append({
"id": item["id"],
"val":item,
"week": week_match.group(0)
})
val = []
for week in weekly_updates:
plain_text_body = markdown.markdown(week['val']['body'])
tasks = re.findall(r'\[(x| )\]', plain_text_body)
total_tasks = len(tasks)
completed_tasks = tasks.count('x')
avg = round((completed_tasks/total_tasks)*100) if total_tasks!=0 else 0
# week['avg'] = avg
# week['val'] = None
week[str(week['week'])+' percentage'] = avg
del week['val']
del week['id']
del week['week']
val.append(week)
return val
except Exception as e:
return {}
def find_week_avg(url):
response = requests.get(url,headers=headers)
if response.status_code == 200:
issue_details = response.json()
# week_avgs = find_week_data(issue_details) phase 2
week_avgs = None
w_learn_url = None
w_goal_url = None
avg = 0
for item in issue_details:
if "Weekly Goals" in item['body']:
w_goal_url = item['body']
plain_text_body = markdown.markdown(issue_details[0]['body'])
tasks = re.findall(r'\[(x| )\]', plain_text_body)
total_tasks = len(tasks)
completed_tasks = tasks.count('x')
avg = round((completed_tasks/total_tasks)*100) if total_tasks!=0 else 0
if "Weekly Learnings" in item['body']:
w_learn_url = item['body']
return avg,issue_details[0]['user']['login'],issue_details[0]['user']['id'],w_goal_url,w_learn_url,week_avgs,issue_details[0]['user']['html_url']
def find_mentors(url):
response = requests.get(url,headers=headers)
if response.status_code == 200:
issue_details = response.json()
issue_body = issue_details['body']
pattern = r"## Mentors\s*([\s\S]+?)\s*##"
disc_pattern = r"## Desc 1\s*([\s\S]+?)\s*##"
disc_match = re.search(disc_pattern, issue_body)
disc_text = disc_match.group(1).strip() if disc_match else None
match = re.search(pattern, issue_body)
if match:
mentors_text = match.group(1).strip()
# Extract individual mentor usernames
mentors = [mentor.strip() for mentor in mentors_text.split(',')]
else:
mentors = []
api_base_url = "https://api.github.com/users/"
ment_username = []
for val in mentors:
url = f"{api_base_url}{val[1:]}"
username = requests.get(url,headers=headers)
ment_username.append(username.json()['login'])
return {
'mentors': mentors,
'mentor_usernames': ment_username,
'desc':disc_text
}
else:
return {
'mentors': [],
'mentor_usernames': [],
'desc':None
}
def get_pr_details(url):
try:
issue_url = url
url_parts = issue_url.split("/")
owner = url_parts[4]
repo = url_parts[5]
issue_number = url_parts[7]
# GitHub API endpoint to get pull requests for the repository
pulls_url = f"https://api.github.com/repos/{owner}/{repo}/pulls"
# Send GET request to GitHub API with authentication
response = requests.get(pulls_url, headers=headers)
if response.status_code == 200:
pulls = response.json()
return pulls
else:
return []
except Exception as e:
raise Exception
def get_repo_details(owner, repo):
url = f"https://api.github.com/repos/{owner}/{repo}"
response = requests.get(url,headers=headers)
if response.status_code == 200:
return response.json()
else:
return None
def determine_week(input_date_str, start_date_str='2024-06-11'):
try:
# Convert the start date string to a datetime object
start_date = datetime.strptime(start_date_str, '%Y-%m-%d')
input_date = parser.parse(input_date_str).replace(tzinfo=None)
# Calculate the difference in days
difference_in_days = (input_date - start_date).days
if difference_in_days < 0:
return "Week 0"
week_number = (difference_in_days // 7) + 1
return f"Week {week_number}"
except Exception as e:
return "Week -1"