Skip to content

Instantly share code, notes, and snippets.

@chrismytton
Created January 16, 2025 20:55
Show Gist options
  • Save chrismytton/d7780ea9706a7fff41e9a0645217755e to your computer and use it in GitHub Desktop.
Save chrismytton/d7780ea9706a7fff41e9a0645217755e to your computer and use it in GitHub Desktop.
Get a list of pull requests created by me in 2024
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests",
# ]
# ///
import requests
from datetime import datetime
import os
from typing import List, Dict
import json
import argparse
def fetch_2024_prs(github_token: str, username: str) -> List[Dict]:
"""
Fetch all pull requests created by a user in 2024 across all repositories.
Args:
github_token (str): GitHub personal access token
username (str): GitHub username
Returns:
List[Dict]: List of pull request information
"""
headers = {
'Authorization': f'token {github_token}',
'Accept': 'application/vnd.github.v3+json'
}
# Search query parameters
query = f'author:{username} is:pr created:2024-01-01..2024-12-31'
base_url = 'https://api.github.com/search/issues'
all_prs = []
page = 1
while True:
params = {
'q': query,
'per_page': 100,
'page': page
}
response = requests.get(base_url, headers=headers, params=params)
if response.status_code != 200:
raise Exception(f'API request failed: {response.status_code}\n{response.text}')
data = response.json()
items = data.get('items', [])
if not items:
break
for pr in items:
pr_info = {
'title': pr['title'],
'url': pr['html_url'],
'repo': pr['repository_url'].split('/')[-1],
'state': pr['state'],
'created_at': pr['created_at'],
'updated_at': pr['updated_at']
}
all_prs.append(pr_info)
page += 1
return all_prs
def main():
parser = argparse.ArgumentParser(description='Fetch GitHub PRs created by a user in 2024')
parser.add_argument('username', help='GitHub username to fetch PRs for')
args = parser.parse_args()
# Get GitHub token from environment variable
github_token = os.getenv('GITHUB_TOKEN')
if not github_token:
raise ValueError("Please set the GITHUB_TOKEN environment variable")
try:
prs = fetch_2024_prs(github_token, args.username)
# Sort all PRs by creation date
prs.sort(key=lambda x: x['created_at'])
# Print summary as markdown
print(f"# Pull Requests by {args.username} in 2024\n")
print(f"Total PRs: {len(prs)}\n")
# Print all PRs chronologically
for pr in prs:
created_date = datetime.fromisoformat(pr['created_at'].replace('Z', '+00:00')).strftime('%Y-%m-%d')
print(f"### {pr['title']}")
print(f"- **Repository:** {pr['repo']}")
print(f"- **Status:** {pr['state']}")
print(f"- **Created:** {created_date}")
print(f"- **URL:** {pr['url']}")
print()
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment