Skip to content

Instantly share code, notes, and snippets.

@dulltz
Last active March 8, 2024 09:42
Show Gist options
  • Select an option

  • Save dulltz/3b796df0a868fed0299809b999d98d17 to your computer and use it in GitHub Desktop.

Select an option

Save dulltz/3b796df0a868fed0299809b999d98d17 to your computer and use it in GitHub Desktop.
Count unique GitHub users committed to a specified repositories within 90 days, to check the required number of licenses for GitHub Advanced Security or Snyk.
#!/usr/bin/env python3
import json
import os
import sys
from datetime import datetime, timedelta
import requests
TOKEN = os.environ.get('GITHUB_TOKEN')
repositories = {"github.com/aaa/bbb", "github.com/aaa/ccc"}
unique_emails = set()
three_months_ago = (datetime.now() - timedelta(days=90)).isoformat()
headers = {'Authorization': f'token {TOKEN}'}
print('Counting active users...', file=sys.stderr)
for repoURL in repositories:
org, repo = repoURL.split('/')[1:]
print(f'{repo}: ', file=sys.stderr, end='')
url = f'https://api.github.com/repos/{org}/{repo}/commits?since={three_months_ago}'
response = requests.get(url, headers=headers)
commits = response.json()
repo_emails = set()
for commit in commits:
email = commit['commit']['author']['email']
if '[bot]' in email:
continue
repo_emails.add(email)
repo_emails.discard('')
print(len(repo_emails), file=sys.stderr)
unique_emails = unique_emails.union(repo_emails)
unique_emails.discard('')
print(json.dumps(list(sorted(unique_emails)), indent=2))
print(f'Unique users in the last 90 days: {len(unique_emails)}', file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment