Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/internal/contributors/contributors.py
1192 views
1
import pandas as pd
2
3
## Config
4
owner = "probml"
5
repo = "pyprobml"
6
min_PRs = 2
7
min_commits = 2
8
fig_width = 50
9
10
## Get all contributors from a repo
11
api_call = f"https://api.github.com/repos/{owner}/{repo}/contributors?per_page=100"
12
contributors = pd.read_json(api_call).set_index("login")
13
14
## Fetch all PRs from a repo
15
page_no = 1
16
pr_df_list = []
17
while True:
18
api_call = f"https://api.github.com/repos/{owner}/{repo}/pulls?state=all&per_page=100&page={page_no}"
19
df = pd.read_json(api_call)
20
if len(df) == 0:
21
break
22
pr_df_list.append(df)
23
page_no += 1
24
pull_requests = pd.concat(pr_df_list)
25
26
## Get count of PRs per contributor
27
28
pull_requests["login"] = pull_requests["user"].apply(lambda x: x["login"])
29
contributor_pr = pull_requests.groupby("login").agg({"url": len}).sort_values(by="url", ascending=False)
30
contributor_pr.rename(columns={"url": "Number of PRs"}, inplace=True)
31
32
# Printing
33
all_users = contributors.index.union(contributor_pr.index).to_list()
34
tmp_df = pd.DataFrame(index=all_users)
35
tmp_df["Number of PRs"] = contributor_pr["Number of PRs"].fillna(0)
36
tmp_df["contributions"] = contributors["contributions"].fillna(0)
37
print(f"# Contributors of {repo} are {len(all_users)}")
38
print(tmp_df.to_string())
39
print("#####################################################################")
40
41
# Filtering
42
atleast_n_pr = contributor_pr[contributor_pr["Number of PRs"] >= min_PRs]
43
atleast_n_commits = contributors[contributors["contributions"] >= min_commits]
44
45
union_users = atleast_n_pr.index.union(atleast_n_commits.index).to_list()
46
union_users = sorted(union_users, key=lambda x: x.lower())
47
48
## Create a dashboard
49
def get_href_user(user):
50
return f"[{user}](https://github.com/{user})"
51
52
53
dashboard = pd.DataFrame(index=union_users)
54
dashboard["login"] = dashboard.index
55
dashboard["Avatar"] = dashboard.login.apply(
56
lambda x: f'<img width="{fig_width}" alt="image" src="https://github.com/{x}.png">'
57
)
58
dashboard["Contributor"] = dashboard.login.apply(get_href_user)
59
md_strings = dashboard[["Avatar", "Contributor"]].T.to_markdown().split("\n")
60
61
# Print other stats
62
print("#####################################################################")
63
print(
64
f"# Contributors with at least {min_PRs} PRs or at least {min_commits} commits are: {len(union_users)} out of {len(all_users)}"
65
)
66
print("#####################################################################")
67
print("\n\nCopy paste below string in the README.md file:\n\n")
68
69
# Little formatting
70
print("| " + md_strings[2].split("|", 2)[-1])
71
print("| " + md_strings[1].split("|", 2)[-1].replace("-|", ":|"))
72
print("| " + md_strings[3].split("|", 2)[-1])
73
74