-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
228 lines (190 loc) · 7.49 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
import random
import requests
import logging
from flask import redirect
import datetime
import time
from configs.globals import CRON_TOKEN
logging.basicConfig(level=logging.DEBUG)
def calculate_leaderboard(client):
pr_list = client.execute("""
SELECT id, repo_name, github_user
FROM pull_requests
JOIN users ON pull_requests.id = users.id
GROUP BY users.name
ORDER BY pr_count DESC;
""")
return list(pr_list)
def fetch_user_repos(username, client):
try:
return list(
client.execute("""
SELECT repo_name
FROM pull_requests
WHERE github_user = ?""",(username,))
)
except Exception as e:
logging.error(f"Error updating leaderboard: {str(e)}")
def update_leaderboard(client):
try:
cursor = client.cursor()
# Ensure all users exist in leaderboard (initialize with 0s)
cursor.execute("""
INSERT OR IGNORE INTO leaderboard (user_id, total_prs, total_commits, total_lines, points)
SELECT id, 0, 0, 0, 0 FROM users
""")
# Update leaderboard with filtered PRs and status points
cursor.execute("""
INSERT INTO leaderboard (user_id, total_prs, total_commits, total_lines, points)
SELECT users.id,
COUNT(DISTINCT pull_requests.repo_name),
SUM(pull_requests.total_commits),
SUM(pull_requests.total_lines),
SUM(CASE WHEN pull_requests.status = 'merged' THEN 10 ELSE 5 END)
FROM users
JOIN pull_requests ON users.github_id = pull_requests.github_login
WHERE pull_requests.pr_id IN (
SELECT MAX(pr_id)
FROM pull_requests
GROUP BY repo_name, github_login
)
GROUP BY users.id
ON CONFLICT(user_id) DO UPDATE SET
total_prs = excluded.total_prs,
total_commits = excluded.total_commits,
total_lines = excluded.total_lines,
points = excluded.points;
""")
logging.debug("Leaderboard updated.")
client.commit()
cursor.close()
except Exception as e:
logging.error(f"Error updating leaderboard: {str(e)}")
def load_filter_list():
with open('filter.txt', 'r') as f:
return [line.strip() for line in f if line.strip()]
def fetch_filtered_prs(client):
repos = load_filter_list()
pr_count = 0
for repo in repos:
prs = fetch_recent_prs(repo, client)
if not prs:
logging.info(f"No PRs found for {repo}. Skipping...")
continue
for pr in prs:
github_login = pr['user']['login'] if 'user' in pr else None
if not github_login:
logging.warning(f"PR {pr['id']} missing user login. Skipping...")
continue
pr_details = fetch_pr_details(repo, pr['id'], client)
if not pr_details:
logging.warning(f"Skipping PR {pr['id']} - Failed to fetch details.")
continue
total_commits = pr_details.get('commits', 0)
total_lines = pr_details.get('additions', 0) - pr_details.get('deletions', 0)
status = pr.get('state', 'open')
user_exists = client.execute(
"SELECT 1 FROM users WHERE github_id = ?",
(github_login,)
).fetchone()
if not user_exists:
logging.warning(f"User {github_login} not found. Skipping PR {pr['id']}.")
continue
# Check for existing PR from the same repo and user
existing_pr = client.execute(
"SELECT pr_id FROM pull_requests WHERE pr_id = ?",
(pr['id'],)
).fetchone()
if existing_pr:
logging.debug(f"Updating existing PR {existing_pr[0]}")
client.execute("""
UPDATE pull_requests
SET total_commits = ?, total_lines = ?, status = ?
WHERE pr_id = ?
""", (total_commits, total_lines, status, existing_pr[0]))
else:
logging.info(f"Inserting new PR {pr['id']}")
client.execute("""
INSERT INTO pull_requests (pr_id, repo_name, github_login, total_commits, total_lines, status)
VALUES (?, ?, ?, ?, ?, ?)
""", (
pr['id'],
repo,
github_login,
total_commits,
total_lines,
status
))
pr_count += 1
client.commit()
logging.info(f"Fetched {pr_count} new PRs.")
return pr_count
def fetch_recent_prs(repo, client):
token = CRON_TOKEN
url = f"https://api.github.com/repos/{repo}/pulls?state=open"
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
elif response.status_code == 403:
retry_after = response.headers.get('Retry-After')
if retry_after:
wait_time = int(retry_after)
else:
reset_time = int(response.headers.get('X-RateLimit-Reset', time.time()))
wait_time = max(reset_time - int(datetime.datetime.now().timestamp()), 60)
logging.warning(f"Rate limit hit. Sleeping for {wait_time} seconds...")
time.sleep(wait_time + 1)
return fetch_recent_prs(repo, client)
def insert_pull_request(client, pr, repo):
github_login = pr['user']['login']
pr_details = fetch_pr_details(repo, pr['id'], client)
if pr_details:
client.execute("""
INSERT INTO pull_requests (pr_id, repo_name, github_login, total_commits, total_lines)
VALUES (?, ?, ?, ?, ?)
""", (
pr['id'],
repo,
github_login,
pr_details['commits'],
pr_details['additions'] - pr_details['deletions']
))
logging.info(f"Inserted PR {pr['id']} by {github_login} for {repo}")
else:
logging.warning(f"Skipping PR {pr['id']} due to missing details.")
client.commit()
def fetch_pr_details(repo, pr_id, client, token):
# Fetch all open PRs and match the pr_id manually
url = f"https://api.github.com/repos/{repo}/pulls"
headers = {
'Authorization': f'token {token}',
'Accept': 'application/vnd.github.v3+json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
pr_list = response.json()
# Search for matching PR by ID
matched_pr = next((pr for pr in pr_list if pr['id'] == pr_id), None)
if not matched_pr:
logging.warning(f"PR {pr_id} not found in the open PR list for {repo}")
return None
# Fetch detailed info using the pulls_url
detail_url = matched_pr['url'] # This is the correct URL for the PR
details_response = requests.get(detail_url, headers=headers)
if details_response.status_code == 200:
pr_details = details_response.json()
return {
'commits': pr_details['commits'],
'additions': pr_details['additions'],
'deletions': pr_details['deletions']
}
else:
logging.warning(f"Failed to fetch detailed PR data: {details_response.status_code}")
return None
else:
logging.error(f"Failed to fetch open PRs for {repo}: {response.status_code}")
return None