Skip to content

Instantly share code, notes, and snippets.

@celeron55
Created February 29, 2024 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celeron55/7978a0e790205b9bf5581fee1f13f17b to your computer and use it in GitHub Desktop.
Save celeron55/7978a0e790205b9bf5581fee1f13f17b to your computer and use it in GitHub Desktop.
import subprocess
import json
from datetime import datetime, timedelta
# Function to run gh CLI commands and return JSON output
def run_gh_command(command):
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode != 0:
print(f"Error executing command: {command}")
print(result.stderr)
return None
return json.loads(result.stdout)
# Calculate the date 3 months ago from today
three_months_ago = datetime.now() - timedelta(days=90)
three_months_ago_str = three_months_ago.strftime('%Y-%m-%d')
# Fetch open PRs updated in the last 3 months
pr_list_command = ['gh', 'pr', 'list', '--repo', 'minetest/minetest', '--state', 'open', '--json', 'number,updatedAt', '--search', f'updated:>{three_months_ago_str}']
prs = run_gh_command(pr_list_command)
if prs is None:
print("Failed to fetch pull requests.")
exit(1)
# Fetch lines changed for each PR and store in a list
pr_changes = []
for pr in prs:
pr_view_command = ['gh', 'pr', 'view', str(pr['number']), '--repo', 'minetest/minetest', '--json', 'additions,deletions']
pr_details = run_gh_command(pr_view_command)
if pr_details:
total_changes = pr_details['additions'] + pr_details['deletions']
pr_changes.append({'number': pr['number'], 'totalChanges': total_changes})
# Sort PRs by total lines changed
sorted_prs = sorted(pr_changes, key=lambda x: x['totalChanges'])
# Print sorted PRs
for pr in sorted_prs:
print(f"PR #{pr['number']} - Total Lines Changed: {pr['totalChanges']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment