Created
March 7, 2024 23:26
-
-
Save yonigoldberg/6a64da35ad17f4e6a50a5e9444622d85 to your computer and use it in GitHub Desktop.
A simple python script to print all the aggregated open PRs by user
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 requests | |
# Configuration | |
TOKEN = '' # Replace with your personal token | |
OWNER = '' # Replace with the repository owner's username | |
REPO = '' # Replace with the repository name | |
HEADERS = { | |
'Authorization': f'token {TOKEN}', | |
'Accept': 'application/vnd.github.v3+json' | |
} | |
ENDPOINT = f'https://api.github.com/repos/{OWNER}/{REPO}/pulls?state=open&per_page=100' | |
def get_all_open_prs(): | |
prs = [] | |
response = requests.get(ENDPOINT, headers=HEADERS) | |
if response.status_code == 200: | |
prs.extend(response.json()) | |
while 'next' in response.links: | |
response = requests.get(response.links['next']['url'], headers=HEADERS) | |
prs.extend(response.json()) | |
else: | |
print(f"Error {response.status_code}: {response.text}") | |
return prs | |
def group_by_author(prs): | |
grouped_prs = {} | |
for pr in prs: | |
author = pr['user']['login'] | |
date = pr['created_at'] | |
if author not in grouped_prs: | |
grouped_prs[author] = { | |
'prs': [], | |
'draft_prs': 0, | |
'non_draft_prs': 0, | |
'earliest_pr_date': date | |
} | |
grouped_prs[author]['prs'].append(pr) | |
if pr['draft']: | |
grouped_prs[author]['draft_prs'] += 1 | |
else: | |
grouped_prs[author]['non_draft_prs'] += 1 | |
if date < grouped_prs[author]['earliest_pr_date']: | |
grouped_prs[author]['earliest_pr_date'] = date | |
return grouped_prs | |
def main(): | |
prs = get_all_open_prs() | |
grouped_prs = group_by_author(prs) | |
# Sorting authors by the total number of PRs (draft + non-draft, in descending order) | |
sorted_authors = sorted(grouped_prs.keys(), key=lambda x: (grouped_prs[x]['draft_prs'] + grouped_prs[x]['non_draft_prs']), reverse=True) | |
total_draft_prs = 0 | |
total_non_draft_prs = 0 | |
for author in sorted_authors: | |
data = grouped_prs[author] | |
total_draft_prs += data['draft_prs'] | |
total_non_draft_prs += data['non_draft_prs'] | |
print ("Summary:\n") | |
print(f"{'Author':20} | {f'# of Non-Draft PRs [{total_non_draft_prs}]':20} | {f'# of Draft PRs [{total_draft_prs}]':15} | {'Earliest PR Date'}") | |
print("-" * 85) | |
for author in sorted_authors: | |
data = grouped_prs[author] | |
print(f"{author:20} | {data['non_draft_prs']:20} | {data['draft_prs']:15} | {data['earliest_pr_date']}") | |
print("\nDetailed PRs:\n") | |
for author in sorted_authors: | |
data = grouped_prs[author] | |
print(f"Author: {author}") | |
for pr in data['prs']: | |
draft_status = 'Draft' if pr['draft'] else 'Non-Draft' | |
print(f" - PR [{draft_status}]: {pr['title']} (Date: {pr['created_at']})") | |
print("----------------------") | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment