Skip to content

Instantly share code, notes, and snippets.

@Jonty
Last active July 10, 2024 17:30
Show Gist options
  • Save Jonty/8e2bfb39e0af669e479f130f2bcf6f1b to your computer and use it in GitHub Desktop.
Save Jonty/8e2bfb39e0af669e479f130f2bcf6f1b to your computer and use it in GitHub Desktop.
Outputs the number of people who have committed to each repo in a github org in the last 90 days, as well as the total count of unique people who have committed to the org in the last 90 days. Useful for estimating seat-counts for tools like Snyk.
import os
import github
from datetime import datetime, timedelta, timezone
START_TIME = datetime.now(timezone.utc) - timedelta(days=90)
# USAGE:
# IGNORE_REPOS=repo1,repo2,repo3 GITHUB_TOKEN=foobarbaz GITHUB_ORG=gchq,nsa python github_org_active_users.py
g = github.Github(os.environ["GITHUB_TOKEN"])
orgs = os.environ["GITHUB_ORG"].split(",")
ignore_repos = os.environ.get("IGNORE_REPOS", "").split(",")
contributors = set()
for org_name in orgs:
org = g.get_organization(org_name)
for repo in org.get_repos(sort="pushed", direction="desc"):
if repo.name in ignore_repos:
continue
# We fetch repos in most-recent-updated order, so when there are no commits
# since START_TIME we know every repo after this hasn't been touched
if repo.pushed_at < START_TIME:
break
repo_contributors = set()
commit_count = 0
commits = repo.get_commits(since=START_TIME)
try:
for commit in commits:
commit_count += 1
if commit.author:
repo_contributors.add(commit.author.login)
except github.GithubException as e:
if e.status == 409:
continue
raise (e)
print(
f"{repo.full_name}: {commit_count} commits by {len(repo_contributors)} contributors"
)
contributors.update(repo_contributors)
print(f"\n\nTOTAL UNIQUE CONTRIBUTORS: {len(contributors)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment