Skip to content

Instantly share code, notes, and snippets.

@Divide-By-0
Created November 3, 2024 22:22
Show Gist options
  • Save Divide-By-0/c27819bd530b441445285818b5a5be2c to your computer and use it in GitHub Desktop.
Save Divide-By-0/c27819bd530b441445285818b5a5be2c to your computer and use it in GitHub Desktop.
List All Github Organization Commit Messages from Last N Days Script
import requests
from datetime import datetime, timedelta
import os
from dotenv import load_dotenv
import time
# GitHub API configuration
GITHUB_API_URL = "https://api.github.com"
ORG_NAME = "zkemail" # Replace with your organization name
LAST_N_DAYS = 30
load_dotenv() # Load environment variables from .env file
TOKEN = os.getenv('GITHUB_TOKEN') # Read GitHub token from .env file
if TOKEN is None or TOKEN == "":
print("GITHUB_TOKEN not found in .env file. Note that private repos will not be checked.")
# Set up headers for authentication
headers = {
"Accept": "application/vnd.github.v3+json"
}
if TOKEN is not None and TOKEN != "":
headers["Authorization"] = f"token {TOKEN}"
# Function to fetch all repositories in the organization
def get_org_repos():
repos = []
page = 1
while True:
response = requests.get(f"{GITHUB_API_URL}/orgs/{ORG_NAME}/repos?page={page}&per_page=100", headers=headers)
if response.status_code == 200:
page_repos = response.json()
if not page_repos:
break
repos.extend(page_repos)
page += 1
else:
print(f"Error fetching repositories: {response.status_code}")
break
return repos
# Function to fetch recent commits for a repository
def get_recent_commits(repo_name, since_date):
commits = []
page = 1
while True:
response = requests.get(f"{GITHUB_API_URL}/repos/{ORG_NAME}/{repo_name}/commits?since={since_date}&page={page}&per_page=100", headers=headers)
time.sleep(1) # Add a 1-second delay after each request
if response.status_code == 200:
page_commits = response.json()
if not page_commits:
break
commits.extend(page_commits)
page += 1
else:
print(f"Error fetching commits for {repo_name}: {response.status_code}")
break
return commits
# Main function to list recent commits across all repositories
def list_recent_commits():
repos = get_org_repos()
since_date = (datetime.now() - timedelta(days=LAST_N_DAYS)).isoformat() # Fetch commits from the last 30 days
for repo in repos:
repo_name = repo['name']
print(f"\nRepository: {repo_name}")
commits = get_recent_commits(repo_name, since_date)
for commit in commits:
author = commit['commit']['author']['name']
date = commit['commit']['author']['date']
message = commit['commit']['message'].split('\n')[0] # Get first line of commit message
print(f" {date} - {author}: {message}")
if __name__ == "__main__":
list_recent_commits()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment