Created
June 29, 2024 05:06
-
-
Save gyurisc/fd7eec10add26b0182d019a6f32b0965 to your computer and use it in GitHub Desktop.
Simple git repo stats
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
import os | |
import requests | |
from datetime import datetime | |
from dotenv import load_dotenv | |
# Load environment variables from .env file | |
load_dotenv() | |
# Get GitHub API token from environment variable | |
github_token = os.getenv('GITHUB_TOKEN') | |
def get_repo_stats(repo_url): | |
# Extract owner and repo name from the URL | |
_, _, _, owner, repo = repo_url.rstrip('/').split('/') | |
# Set up the API request | |
api_url = f"https://api.github.com/repos/{owner}/{repo}" | |
headers = { | |
'Authorization': f'token {github_token}', | |
'Accept': 'application/vnd.github.v3+json' | |
} | |
# Make the API request | |
response = requests.get(api_url, headers=headers) | |
if response.status_code != 200: | |
return f"Error: Unable to fetch repository data. Status code: {response.status_code}" | |
data = response.json() | |
# Calculate statistics | |
created_at = datetime.strptime(data['created_at'], '%Y-%m-%dT%H:%M:%SZ') | |
age_days = (datetime.now() - created_at).days | |
total_commits = data['size'] # Note: This is an approximation | |
avg_daily_commits = total_commits / age_days if age_days > 0 else 0 | |
stars = data['stargazers_count'] | |
# Fetch commit data for the last 30 days | |
commits_url = f"{api_url}/commits" | |
params = {'since': (datetime.now() - timedelta(days=30)).isoformat()} | |
commits_response = requests.get(commits_url, headers=headers, params=params) | |
recent_commits = len(commits_response.json()) if commits_response.status_code == 200 else 0 | |
commit_velocity = recent_commits / 30 | |
return { | |
'age_days': age_days, | |
'total_commits': total_commits, | |
'avg_daily_commits': round(avg_daily_commits, 2), | |
'stars': stars, | |
'commit_velocity': round(commit_velocity, 2) | |
} | |
# Example usage | |
repo_url = input("Enter the GitHub repository URL: ") | |
stats = get_repo_stats(repo_url) | |
print(stats) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment