Path: blob/master/internal/contributors/contributors.py
1192 views
import pandas as pd12## Config3owner = "probml"4repo = "pyprobml"5min_PRs = 26min_commits = 27fig_width = 5089## Get all contributors from a repo10api_call = f"https://api.github.com/repos/{owner}/{repo}/contributors?per_page=100"11contributors = pd.read_json(api_call).set_index("login")1213## Fetch all PRs from a repo14page_no = 115pr_df_list = []16while True:17api_call = f"https://api.github.com/repos/{owner}/{repo}/pulls?state=all&per_page=100&page={page_no}"18df = pd.read_json(api_call)19if len(df) == 0:20break21pr_df_list.append(df)22page_no += 123pull_requests = pd.concat(pr_df_list)2425## Get count of PRs per contributor2627pull_requests["login"] = pull_requests["user"].apply(lambda x: x["login"])28contributor_pr = pull_requests.groupby("login").agg({"url": len}).sort_values(by="url", ascending=False)29contributor_pr.rename(columns={"url": "Number of PRs"}, inplace=True)3031# Printing32all_users = contributors.index.union(contributor_pr.index).to_list()33tmp_df = pd.DataFrame(index=all_users)34tmp_df["Number of PRs"] = contributor_pr["Number of PRs"].fillna(0)35tmp_df["contributions"] = contributors["contributions"].fillna(0)36print(f"# Contributors of {repo} are {len(all_users)}")37print(tmp_df.to_string())38print("#####################################################################")3940# Filtering41atleast_n_pr = contributor_pr[contributor_pr["Number of PRs"] >= min_PRs]42atleast_n_commits = contributors[contributors["contributions"] >= min_commits]4344union_users = atleast_n_pr.index.union(atleast_n_commits.index).to_list()45union_users = sorted(union_users, key=lambda x: x.lower())4647## Create a dashboard48def get_href_user(user):49return f"[{user}](https://github.com/{user})"505152dashboard = pd.DataFrame(index=union_users)53dashboard["login"] = dashboard.index54dashboard["Avatar"] = dashboard.login.apply(55lambda x: f'<img width="{fig_width}" alt="image" src="https://github.com/{x}.png">'56)57dashboard["Contributor"] = dashboard.login.apply(get_href_user)58md_strings = dashboard[["Avatar", "Contributor"]].T.to_markdown().split("\n")5960# Print other stats61print("#####################################################################")62print(63f"# Contributors with at least {min_PRs} PRs or at least {min_commits} commits are: {len(union_users)} out of {len(all_users)}"64)65print("#####################################################################")66print("\n\nCopy paste below string in the README.md file:\n\n")6768# Little formatting69print("| " + md_strings[2].split("|", 2)[-1])70print("| " + md_strings[1].split("|", 2)[-1].replace("-|", ":|"))71print("| " + md_strings[3].split("|", 2)[-1])727374