Last active
July 21, 2023 04:39
-
-
Save johnlettman/611d20639ef6e722dacddcf7b7a4f7f2 to your computer and use it in GitHub Desktop.
Grab recent popular projects from a GitHub organization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Grab token from here: <https://github.com/settings/tokens> | |
from github import Auth | |
from github import Github | |
from re import search | |
from datetime import datetime, timedelta | |
from multiprocessing import cpu_count | |
from pytablewriter import MarkdownTableWriter | |
org_name = 'canonical' | |
stale_days = (4 * 365) # cutoff, in days, for old repos | |
token_file = './gh-canonical-token' # file containing GitHub app token | |
filters = [ | |
# flutter packages | |
r'\.dart$', | |
# snaps | |
r'-snap$', | |
r'^snap-', | |
# rockcraft | |
r'^rock-', | |
r'-rock$', | |
# charms | |
r'-charm$', | |
r'^charm-', | |
r'-operator(-\d+)?$', | |
# IoT / telco / edge packages | |
r'^edgex-', | |
r'^sdcore-', | |
r'^magma-', | |
# websites | |
r'\.io$', | |
r'\.com$', | |
r'^canonicalwebteam\.' | |
] | |
emoji_star = "\U00002B50" | |
emoji_scope = "\U0001F52D" | |
with open(token_file, 'r') as fd: | |
auth = Auth.Token(fd.readline()) | |
gh = Github(auth=auth) | |
org = gh.get_organization(org_name) | |
all_repos = org.get_repos(type='public') | |
# calculate the update date to cut off stale repositories | |
cutoff_date = datetime.utcnow() - timedelta(stale_days) | |
# skim off old or filtered repositories | |
filtered_repos = [ | |
repo for repo in all_repos | |
if repo.updated_at > cutoff_date and | |
not any(search(filter, repo.name) for filter in filters) | |
] | |
# sort the fresh repositories by popularity | |
popular_repos = sorted(filtered_repos, | |
key=lambda r: (r.stargazers_count, r.watchers_count), | |
reverse=True) | |
# build the table | |
writer = MarkdownTableWriter( | |
table_name=f"GitHub packages in the [`{org_name}`]({org.html_url}) org", | |
headers=["Repository", "Stargazers", "Watchers"], | |
max_workers=cpu_count(), | |
margin=2, | |
value_matrix=[ | |
[ | |
f"[{repo.name}]({repo.html_url})", | |
f"{emoji_star} {repo.stargazers_count}", | |
f"{emoji_scope} {repo.watchers_count}" | |
] for repo in popular_repos | |
] | |
) | |
writer.write_table() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment