Skip to content

Instantly share code, notes, and snippets.

@stever
Last active July 18, 2024 20:25
Show Gist options
  • Save stever/4b9e58ab4c65a1b79260e5830a9d20c9 to your computer and use it in GitHub Desktop.
Save stever/4b9e58ab4c65a1b79260e5830a9d20c9 to your computer and use it in GitHub Desktop.
Show the number of committers in a repository in the last 90 days
import requests
from datetime import datetime, timedelta
import os
# Environment Variables
TOKEN = os.getenv('GITHUB_TOKEN')
OWNER = os.getenv('GITHUB_OWNER')
REPO = os.getenv('GITHUB_REPO')
SINCE = (datetime.utcnow() - timedelta(days=90)).isoformat() + 'Z' # GitHub uses UTC time format
# API request to get commits
url = f'https://api.github.com/repos/{OWNER}/{REPO}/commits'
headers = {'Authorization': f'token {TOKEN}'}
params = {'since': SINCE, 'per_page': 100} # GitHub API pagination
committers = set()
commits = []
page = 1
while True:
response = requests.get(url, headers=headers, params={**params, 'page': page})
data = response.json()
if not data:
break
commits.extend(data)
for commit in data:
if commit.get('author'): # Check if 'author' is not None
committers.add(commit['author']['login'])
page += 1
# Extract the dates
commit_dates = [commit['commit']['author']['date'] for commit in commits]
commit_dates.sort()
earliest_commit = commit_dates[0] if commit_dates else 'No commits found'
latest_commit = commit_dates[-1] if commit_dates else 'No commits found'
print(f'Active committers in the last 90 days: {committers}')
print(f'Total active committers: {len(committers)}')
print(f'Total number of commits: {len(commits)}')
print(f'Earliest commit date: {earliest_commit}')
print(f'Latest commit date: {latest_commit}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment