Skip to content

Instantly share code, notes, and snippets.

@ejoubaud
Created June 9, 2023 08:33
Show Gist options
  • Save ejoubaud/3182d4a949a302b331f2ac3c48030ed5 to your computer and use it in GitHub Desktop.
Save ejoubaud/3182d4a949a302b331f2ac3c48030ed5 to your computer and use it in GitHub Desktop.
Get average and median lifetimes of merged Pull Requests using the gh CLI
#!/bin/bash
REPO=Go-Electra/electra-backend
prs=$(gh pr list --state merged --repo $REPO --json createdAt,mergedAt --limit 100)
count=0
total=0
lifetimes=()
for pr in $(echo "${prs}" | jq -c '.[]'); do
created=$(echo $pr | jq -r '.createdAt')
merged=$(echo $pr | jq -r '.mergedAt')
# Convert the dates to seconds since the Unix epoch
created_seconds=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$created" +"%s")
merged_seconds=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$merged" +"%s")
# Calculate the difference in secs
diff=$((merged_seconds - created_seconds))
# Add to totals
total=$((total + diff))
count=$((count + 1))
lifetimes+=($diff)
done
# Calculate and print the median
# Sort the lifetimes array
IFS=$'\n' sorted=($(sort -n <<<"${lifetimes[*]}"))
middle=$((count/2))
if (( $count % 2 == 0 )); then
median=$(((${sorted[$middle]} + ${sorted[$middle-1]}) / 2))
else
median=${sorted[$middle]}
fi
echo "Median lifetime of a Pull Request is $(($median / 3600)) hours"
# Calculate and print the average
average=$((total / count))
echo "Average lifetime of a Pull Request is $(($average / 3600)) hours"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment