forked from elizaOS/elizaos.github.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch_contributors.py
206 lines (176 loc) · 7.38 KB
/
fetch_contributors.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
import requests
import json
import os
import argparse
import sys
from datetime import datetime
from collections import defaultdict
import time
def check_rate_limit(headers):
"""Check GitHub API rate limit status."""
response = requests.get('https://api.github.com/rate_limit', headers=headers)
data = response.json()
remaining = data['resources']['core']['remaining']
reset_time = datetime.fromtimestamp(data['resources']['core']['reset'])
if remaining < 100:
wait_time = (reset_time - datetime.now()).total_seconds()
if wait_time > 0:
print(f"\nRate limit low ({remaining} remaining). Waiting {wait_time:.0f} seconds...")
time.sleep(wait_time + 1)
return remaining
def get_activity_summary(items, date_key="created_at"):
"""Summarize activity by year and month."""
activity = defaultdict(lambda: defaultdict(int))
for item in items:
if date_key in item:
date = datetime.strptime(item[date_key], "%Y-%m-%dT%H:%M:%SZ")
activity[date.year][date.month] += 1
return dict(activity)
def fetch_paginated(url, headers, is_search=False, max_pages=10):
"""Fetch paginated results from GitHub API."""
results = []
page = 1
while page <= max_pages:
paginated_url = f"{url}{'&' if '?' in url else '?'}page={page}&per_page=100"
check_rate_limit(headers)
try:
response = requests.get(paginated_url, headers=headers)
response.raise_for_status()
items = response.json()
if is_search:
items = items.get('items', [])
elif not isinstance(items, list):
results.append(items)
break
if not items:
break
results.extend(items)
if len(items) < 100:
break
page += 1
except requests.exceptions.HTTPError as e:
print(f"Error fetching {url}: {str(e)}")
break
return results
def get_contributor_data(repo_owner, repo_name, output_dir, headers, force=False):
"""Fetch detailed contributor activity data."""
os.makedirs(output_dir, exist_ok=True)
contributors = fetch_paginated(
f"https://api.github.com/repos/{repo_owner}/{repo_name}/contributors",
headers
)
print(f"\nProcessing {len(contributors)} contributors...")
for index, contributor in enumerate(contributors, 1):
username = contributor["login"]
output_file = os.path.join(output_dir, f"{username}.json")
if os.path.exists(output_file) and not force:
print(f"Skipping {username} ({index}/{len(contributors)}) - existing data")
continue
print(f"\nFetching data for {username} ({index}/{len(contributors)})...")
user_data = {
"username": username,
"avatar_url": contributor["avatar_url"],
"total_contributions": contributor["contributions"],
"activity": {
"code": {},
"issues": {},
"engagement": {}
}
}
# Fetch detailed commit data
commits = fetch_paginated(
f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits?author={username}",
headers
)
commit_data = [{
"sha": commit["sha"],
"date": commit["commit"]["author"]["date"],
"message": commit["commit"]["message"],
"url": commit.get("html_url", ""),
} for commit in commits]
# Fetch PRs with details
prs = fetch_paginated(
f"https://api.github.com/search/issues?q=repo:{repo_owner}/{repo_name}+author:{username}+type:pr",
headers,
is_search=True
)
pr_data = [{
"number": pr["number"],
"title": pr["title"],
"state": pr["state"],
"created_at": pr["created_at"],
"url": pr.get("html_url", ""),
"labels": [label["name"] for label in pr.get("labels", [])],
"comments": pr.get("comments", 0)
} for pr in prs]
# Fetch issues with details
issues = fetch_paginated(
f"https://api.github.com/search/issues?q=repo:{repo_owner}/{repo_name}+author:{username}+type:issue",
headers,
is_search=True
)
issue_data = [{
"number": issue["number"],
"title": issue["title"],
"state": issue["state"],
"created_at": issue["created_at"],
"url": issue.get("html_url", ""),
"labels": [label["name"] for label in issue.get("labels", [])],
"comments": issue.get("comments", 0)
} for issue in issues if "pull_request" not in issue]
# Fetch detailed comments
comments = fetch_paginated(
f"https://api.github.com/repos/{repo_owner}/{repo_name}/issues/comments",
headers
)
comment_data = [{
"id": comment["id"],
"body": comment["body"],
"created_at": comment["created_at"],
"url": comment.get("html_url", ""),
"type": "issue" if "/issues/" in comment.get("html_url", "") else "pr",
"issue_number": comment["issue_url"].split("/")[-1] if comment.get("issue_url") else None
} for comment in comments if comment["user"]["login"] == username]
# Organize activity data
user_data["activity"]["code"] = {
"commits": commit_data,
"pull_requests": pr_data,
"total_commits": len(commit_data),
"total_prs": len(pr_data),
"commit_activity": get_activity_summary(commit_data, "date"),
"pr_activity": get_activity_summary(pr_data)
}
user_data["activity"]["issues"] = {
"opened": issue_data,
"total_opened": len(issue_data),
"issue_activity": get_activity_summary(issue_data)
}
user_data["activity"]["engagement"] = {
"issue_comments": [c for c in comment_data if c["type"] == "issue"],
"pr_comments": [c for c in comment_data if c["type"] == "pr"],
"total_comments": len(comment_data),
"comment_activity": get_activity_summary(comment_data)
}
with open(output_file, "w") as f:
json.dump(user_data, f, indent=2)
print(f"Saved data for {username}")
if __name__ == "__main__":
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
if not GITHUB_TOKEN:
print("Error: Please set the GITHUB_TOKEN environment variable.")
sys.exit(1)
parser = argparse.ArgumentParser(description="Fetch GitHub contributor activity data.")
parser.add_argument("repo_owner", help="Repository owner")
parser.add_argument("repo_name", help="Repository name")
parser.add_argument("-o", "--output", default="./data/",
help="Output directory")
parser.add_argument("-f", "--force", action="store_true",
help="Force refresh existing data")
args = parser.parse_args()
get_contributor_data(
args.repo_owner,
args.repo_name,
args.output,
{"Authorization": f"Bearer {GITHUB_TOKEN}"},
args.force
)