import requests import time import json from datetime import datetime # Replace with your GitHub token and organization details token = '***********' organization = 'theorchard' # Set the headers with the correct API version headers = { 'Authorization': f'token {token}', 'Accept': 'application/vnd.github+json', 'X-GitHub-API-Version': '2022-11-28' } # Function to get the list of repositories for the organization def get_repos(org): url = f'https://api.github.com/orgs/{org}/repos' response = requests.get(url, headers=headers) if response.status_code == 200: return response.json() else: print(f"Error fetching repositories: {response.status_code}") return [] # Function to get commit activity for a repository def get_commit_activity(owner, repo): url = f'https://api.github.com/repos/{owner}/{repo}/stats/commit_activity' while True: response = requests.get(url, headers=headers) if response.status_code == 200: return response.json() elif response.status_code == 202: print(f"Statistics for {repo} are being computed. Retrying in 10 seconds...") time.sleep(10) else: print(f"Error fetching commit activity for {repo}: {response.status_code}") return [] # Function to print the summary report for a repository def print_summary_report(repo, stats, file): total_commits = 0 monthly_commits = {} for entry in stats: week_start = datetime.utcfromtimestamp(entry['week']).strftime('%B %Y') total_commits += entry['total'] if week_start in monthly_commits: monthly_commits[week_start] += entry['total'] else: monthly_commits[week_start] = entry['total'] file.write(f"\nSummary Report for Repository: {repo}\n") file.write(f"Total Commits: {total_commits}\n") file.write("Monthly Commits:\n") for month, commits in monthly_commits.items(): file.write(f"{month}: {commits} commits\n") # Main script to loop through all repositories and print reports repos = get_repos(organization) with open('summary_report.txt', 'w') as file: for repo in repos: repo_name = repo['name'] stats = get_commit_activity(organization, repo_name) if stats: print_summary_report(repo_name, stats, file)