-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.py
91 lines (78 loc) · 2.71 KB
/
query.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
import argparse
import json
from github import Github
def get_page(paginated_list):
"""
Take a github.PaginatedList.PaginatedList and then iterate through the
pages to get all of its entries
Args:
paginated_list (github.PaginatedList.PaginatedList): PyGithub paginated
list object
Returns:
`list`: All entries in the paginated list
"""
idx = 0
_page_entries = paginated_list.get_page(idx)
page_entries = []
while _page_entries:
page_entries.extend(_page_entries)
idx += 1
_page_entries = paginated_list.get_page(idx)
return page_entries
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Query the GitHub API for information on repositories."
)
parser.add_argument(
"access_token",
metavar="ACCESS_TOKEN",
type=str,
help="GitHub access token",
)
parser.add_argument(
"--query-list",
dest="query_list",
type=str,
default="projects.json",
help="Path to JSON file containing the project names to query",
)
args = parser.parse_args()
with open(args.query_list) as read_file:
repo_names = json.load(read_file)["project_name"]
github_api = Github(args.access_token)
data = {}
# Want information on actual users, not bots
# https://pygithub.readthedocs.io/en/latest/github_objects/NamedUser.html
for repo_name in repo_names:
repo = github_api.get_repo(repo_name)
contributor_ids = [
contributor.id
for contributor in get_page(repo.get_contributors())
if contributor.type.lower() != "bot"
]
data[repo_name] = {
"star_count": repo.stargazers_count,
"watcher_count": repo.subscribers_count,
"fork_count": repo.forks_count,
"contributor_count": len(set(contributor_ids)),
"tags": repo.get_tags().totalCount,
"releases": repo.get_releases().totalCount,
"stargazer_ids": [
stargazer.id
for stargazer in get_page(repo.get_stargazers())
if stargazer.type.lower() != "bot"
],
"watcher_ids": [
watcher.id
for watcher in get_page(repo.get_subscribers())
if watcher.type.lower() != "bot"
],
"fork_owner_ids": [
fork.owner.id
for fork in get_page(repo.get_forks())
if fork.owner.type.lower() != "bot"
],
"contributor_ids": contributor_ids,
}
with open("repo_data.json", "w") as write_file:
write_file.write(json.dumps(data, indent=4))