Skip to content

Instantly share code, notes, and snippets.

@dafurman
Last active February 11, 2024 05:08
Show Gist options
  • Save dafurman/d267c26b53c27450184d467824080bcf to your computer and use it in GitHub Desktop.
Save dafurman/d267c26b53c27450184d467824080bcf to your computer and use it in GitHub Desktop.
Print the top 10 Github contributors to a repo since a given date
#!/usr/bin/env python3
import sys
import time
from collections import defaultdict
from ghapi.all import GhApi
if len(sys.argv) < 5 or len(sys.argv) > 6:
print("Usage: {} token owner repo start_date [verbose]".format(sys.argv[0]))
sys.exit(1)
TOKEN = sys.argv[1]
OWNER = sys.argv[2]
REPO = sys.argv[3]
CREATED = sys.argv[4]
verbose = len(sys.argv) == 6 and sys.argv[5] == "verbose"
api = GhApi(token=TOKEN)
try:
contributors = api.repos.list_contributors(owner=OWNER, repo=REPO)
except Exception as e:
print("Error fetching contributors:", e)
sys.exit(1)
contributors_logins = [contributor['login'] for contributor in contributors]
print("Gathering data...")
contributions_by_login = defaultdict(int)
for contributor in contributors_logins:
if contributor.endswith("bot"):
continue # Skip contributors with "bot" suffix
try:
search_result = api.search.issues_and_pull_requests(
q=f"repo:{OWNER}/{REPO} is:merged author:{contributor} created:>{CREATED}"
)
contributions = search_result['total_count']
except Exception as e:
print(f"Error fetching contributions for {contributor}:", e)
contributions = 0
if verbose:
print("{}: {}".format(contributor, contributions))
contributions_by_login[contributor] = contributions
time.sleep(5) # dumb hack to avoid API rate limiting
print("Top 10 contributors from {} onwards:".format(CREATED))
sorted_contributions = sorted(contributions_by_login.items(), key=lambda x: x[1], reverse=True)[:10]
max_len_contributor = max(len(contributor) for contributor, _ in sorted_contributions)
print("Rank\tContributor\tContributions")
for i, (contributor, count) in enumerate(sorted_contributions, start=1):
print(f"{i}\t{contributor.ljust(max_len_contributor)}\t{count}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment