Let’s take a tally on your PR this year!

0𝕏koji
2 min readDec 24, 2023

--

Photo by Richy Great on Unsplash

A co-worker posted how many lines she added/deleted to the repo in 2023. I just wrote a simple Python script to check PRs. The requirement is GitHub CLI.

The following script requires github id, repo owner, repo name and limit as input.

The limit option default number is 30 so without this the command cannot take all merged PRs data from GitHub.

  • Display Top 5 PRs (based on additions)
  • Display total addtions
  • Display total deletins
  • Display total merged
import json
import subprocess

# Run the gh command and get the output
user_id = input('Enter your github id: ')
repo_owner = input('Enter repo owner: ')
repo = input('Enter repo name: ')
limit = input('Enter limit num: ')
limit = int(limit)

gh_command = f'gh pr list --repo {repo_owner}/{repo} --author {user_id} --state merged --json url,number,title,createdAt,additions,deletions --search "created:2023-01-01..2023-12-31" --limit {limit}'
gh_output = subprocess.check_output(gh_command, shell=True)

# Parse the JSON output
pr_data = json.loads(gh_output)

# Filter PRs by year and sort by additions
filtered_prs = [pr for pr in pr_data if pr['createdAt'].startswith('2023')]
sorted_prs = sorted(filtered_prs, key=lambda pr: pr['additions'], reverse=True)

# Calculate total additions and deletions
total_additions = sum(pr['additions'] for pr in sorted_prs)
total_deletions = sum(pr['deletions'] for pr in sorted_prs)

# Print the sorted PRs
# print(json.dumps(sorted_prs, indent=2))

# Print the top 5 PRs with the most additions
print()
print(f'Your top 5 PR additions {repo_owner}/{repo}')
for i, pr in enumerate(sorted_prs[:5]):
print(f'PR #{i+1}:')
print(f'URL: {pr["url"]}')
print(f'Number: {pr["number"]}')
print(f'Title: {pr["title"]}')
print(f'Created at: {pr["createdAt"]}')
print(f'Additions: {pr["additions"]}')
print(f'Deletions: {pr["deletions"]}')
print()

# Print total additions and deletions
print(f'Total additions: {total_additions}')
print(f'Total deletions: {total_deletions}')

# Print total number of merged PRs
print(f'Total merged PRs: {len(sorted_prs)}')

--

--

0𝕏koji
0𝕏koji

Written by 0𝕏koji

software engineer works for a Biotechnology Research startup in Brooklyn. #CreativeCoding #Art #IoT #MachineLearning #python #typescript #javascript #reactjs

No responses yet