Skip to content

Instantly share code, notes, and snippets.

@m7kvqbe1
Created May 16, 2024 10:45
Show Gist options
  • Select an option

  • Save m7kvqbe1/81c931719734feefe7ad4bcb8cf903d5 to your computer and use it in GitHub Desktop.

Select an option

Save m7kvqbe1/81c931719734feefe7ad4bcb8cf903d5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <repository> <username>"
echo "Example: $0 octocat/Hello-World johndoe"
exit 1
fi
# Variables from command-line arguments
REPO=$1
AUTHOR=$2
# Fetch the full name of the author
author_name=$(gh api users/$AUTHOR --jq '.name')
# Fetch closed PRs by the specified user and sort by merged date in descending order
prs=$(gh pr list --repo $REPO --state closed --author $AUTHOR --json number,mergedAt --jq '.[] | select(.mergedAt != null) | {number: .number, mergedAt: .mergedAt} | @base64' | sort -t, -k2,2r)
# Decode base64 encoded JSON
decode_base64() {
echo $1 | base64 --decode
}
# Initialize an array to store PR details
pr_details=()
# Function to calculate the duration between two dates
calculate_duration() {
local start=$1
local end=$2
local start_sec=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$start" +%s)
local end_sec=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$end" +%s)
local diff_sec=$((end_sec - start_sec))
local days=$((diff_sec / 86400))
local hours=$(( (diff_sec % 86400) / 3600 ))
local minutes=$(( (diff_sec % 3600) / 60 ))
echo "${days}d ${hours}h ${minutes}m"
}
# Loop through each PR to get details
for pr in $prs; do
pr_json=$(decode_base64 $pr)
number=$(echo $pr_json | jq -r '.number')
merged_at=$(echo $pr_json | jq -r '.mergedAt')
pr_info=$(gh pr view $number --repo $REPO --json title,url,createdAt,files --jq '{title: .title, url: .url, createdAt: .createdAt, files: .files}')
title=$(echo $pr_info | jq -r '.title')
url=$(echo $pr_info | jq -r '.url')
created_at=$(echo $pr_info | jq -r '.createdAt')
files=$(echo $pr_info | jq -r '.files | length')
additions=$(echo $pr_info | jq -r '[.files[].additions] | add')
deletions=$(echo $pr_info | jq -r '[.files[].deletions] | add')
# Calculate the time the PR was open before merging
open_duration=$(calculate_duration "$created_at" "$merged_at")
pr_detail="-----------------------------------------\nPR #$number: $title\nAuthor: $author_name ($AUTHOR)\nURL: $url\nMerged at: $merged_at\nTime open before merge: $open_duration\nFiles changed: $files\nAdditions: $additions, Deletions: $deletions\n"
pr_details+=("$pr_detail")
done
# Print the PR details in reverse order
for (( idx=${#pr_details[@]}-1 ; idx>=0 ; idx-- )) ; do
echo -e "${pr_details[idx]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment