Skip to content

Instantly share code, notes, and snippets.

@elgehelge
Created March 1, 2024 13:44
Show Gist options
  • Save elgehelge/4eeb2c877f0ef19c9c7d0d50645f7f87 to your computer and use it in GitHub Desktop.
Save elgehelge/4eeb2c877f0ef19c9c7d0d50645f7f87 to your computer and use it in GitHub Desktop.
Produce GitHub "scoreboard" highlighting users with many stale branches and pull requests
import os
from collections import defaultdict
from datetime import datetime
from datetime import timedelta
import pandas as pd
from github import Github
from tqdm import tqdm
# Replace 'YOUR_GITHUB_TOKEN' with your actual GitHub token
GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
# Replace 'recoord' with the actual organization name
ORG_NAME = "recoord"
# Replace 'sunstone' with the actual repository name
REPO_NAME = "sunstone"
def count_branches_and_pull_requests(repo):
branches = repo.get_branches()
pull_requests = repo.get_pulls(state="open")
branch_counts = defaultdict(int)
stale_counts = defaultdict(int)
pr_counts = defaultdict(int)
for branch in tqdm(branches, desc="Processing branches", total=branches.totalCount):
branch_owner = branch.commit.author.login
branch_date = branch.commit.commit.committer.date
branch_counts[branch_owner] += 1
if branch_date < datetime.now() - timedelta(days=90):
stale_counts[branch_owner] += 1
for pr in tqdm(pull_requests, desc="Processing pull requests", total=pull_requests.totalCount):
pr_owner = pr.user.login
pr_counts[pr_owner] += 1
all_users = set().union(branch_counts).union(stale_counts).union(pr_counts)
counts = {
user: {
"branches": branch_counts[user],
"stale_branches": stale_counts[user],
"pull_requests": pr_counts[user],
}
for user in all_users
}
return counts
def create_dataframe(branch_owners):
data = []
for owner, counts in branch_owners.items():
branches_count = counts["branches"]
pr_count = counts["pull_requests"]
stale_branches_count = counts["stale_branches"]
data.append([owner, stale_branches_count, branches_count, pr_count])
df = pd.DataFrame(data, columns=["User", "Stale Branches", "Branches", "Pull Requests"])
df.set_index("User", inplace=True)
df.sort_values(by=["Stale Branches", "Branches", "Pull Requests"], ascending=False, inplace=True)
# Add medals to top 3 users
top_3_users = df.index[:3]
medals = ["🥇", "🥈", "🥉"]
df.index = df.index.where(
~df.index.isin(top_3_users), [f"{medals[i]} {user}" for i, user in enumerate(top_3_users)]
)
return df
def main():
g = Github(GITHUB_TOKEN)
org = g.get_organization(ORG_NAME)
repo = org.get_repo(REPO_NAME)
branch_owners = count_branches_and_pull_requests(repo)
df = create_dataframe(branch_owners)
print(df)
if __name__ == "__main__":
main()
@elgehelge
Copy link
Author

elgehelge commented Mar 1, 2024

Had a little fun coding with ChatGPT 😂

                 Stale Branches  Branches  Pull Requests
User                                                    
🥇 rasmusroeg                 3        11              2
🥈 tobiasolo                  3         7              0
🥉 AdriaLlopar                3         6              3
pprie                         2         4              0
vladd                         1        13              2
elgehelge                     1         4              2
ilya-ve                       1         1              0
alexflorez                    0         6              0
hesh                          0         2              1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment