Last active
March 20, 2024 00:47
-
-
Save acabreragnz/8d133c8ebecc4f84505f240cd46c4e03 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# This script, named generate-pr-report.sh, generates a Markdown report of pull requests for specified GitHub users and date ranges within a GitHub project. | |
# It must be executed within the root directory of the project you want to analyze. | |
# | |
# Usage: | |
# ./generate-pr-report.sh "user1,user2" "2024-02,2024-03" | |
# This command generates a report for pull requests created by user1 and user2 between February 2024 and March 2024. | |
# | |
# ./generate-pr-report.sh "user1" | |
# This command generates a report for pull requests created by user1 in the current month. | |
# | |
# Note: If 'gh pr list' or 'gh pr view' commands return an error (e.g., no pull requests found), the script will skip the problematic user or pull request and continue. | |
# | |
# Example of generated report structure: | |
# | |
# # Pull Requests Report | |
# ### user1 - 2024-02 | |
# - PR #123 | |
# - Title: Example PR title | |
# - Created Date: Feb 10, 2024 10:00 AM | |
# - Merged Date: Feb 12, 2024 02:00 PM | |
# - Days Open: 2 | |
# - Lines of Codes: +100/-50 | |
# - URL: [https://github.com/yourproject/pull/123](https://github.com/yourproject/pull/123) | |
# | |
# Please ensure 'gh' (GitHub CLI) is installed and configured properly before running this script. | |
# Script content continues below... | |
# Check if users were passed as an argument and separate them | |
if [ -n "$1" ]; then | |
IFS=',' read -r -a users <<< "$1" | |
else | |
echo "Error: No users specified. Please provide at least one user." | |
exit 1 | |
fi | |
# Check if dates were passed as an argument and separate them | |
if [ -n "$2" ]; then | |
IFS=',' read -r start_date end_date <<< "$2" | |
# Generate an array of months based on the given range | |
months=() | |
current="$start_date" | |
while [ "$current" != "$end_date" ]; do | |
months+=("$current") | |
current=$(date -d "$current-15 +1 month" +%Y-%m) | |
done | |
months+=("$end_date") # Ensure the end month is included in the range | |
else | |
# Use the current month if no range is specified | |
current_month=$(date "+%Y-%m") | |
months=("$current_month") | |
fi | |
# Create a Markdown file | |
output_file="PR_report.md" | |
echo "# Pull Requests Report" > "$output_file" | |
# Function to generate the PR section for a given user and month | |
generate_pr_section() { | |
local user=$1 | |
local month=$2 | |
echo "### ${user} - $month" >> "$output_file" | |
# Adjust the command based on specified users | |
user_filter="--author $user" | |
pr_numbers=$(gh pr list --state all $user_filter --search "created:$month sort:created-asc" --json number --jq '.[] | "\(.number) "') | |
if [ -z "$pr_numbers" ]; then | |
echo "Skipping $user for $month, no pull requests found or an error occurred." >&2 | |
return | |
fi | |
echo "$pr_numbers" | while read pr_number; do | |
pr_details=$(gh pr view "$pr_number" --json additions,deletions,url,title,createdAt,mergedAt) | |
if [ -z "$pr_details" ]; then | |
echo "Error retrieving details for PR #$pr_number, skipping." >&2 | |
continue | |
fi | |
# Extract PR details and display them | |
title=$(echo "$pr_details" | jq -r '.title') | |
created_at=$(echo "$pr_details" | jq -r '.createdAt') | |
merged_at=$(echo "$pr_details" | jq -r '.mergedAt') | |
if [ "$merged_at" == "null" ]; then | |
days_opened=$(echo $(( ($(date +%s) - $(date -d "$created_at" +%s) )/(60*60*24) ))) | |
else | |
days_opened=$(echo $(( ($(date -d "$merged_at" +%s) - $(date -d "$created_at" +%s) )/(60*60*24) ))) | |
fi | |
additions=$(echo "$pr_details" | jq '.additions') | |
deletions=$(echo "$pr_details" | jq '.deletions') | |
url=$(echo "$pr_details" | jq -r '.url') | |
echo "- PR #$pr_number" >> "$output_file" | |
echo " - Title: $title" >> "$output_file" | |
echo " - Created Date: $(LC_TIME=en_US.UTF-8 date -d "$created_at" +"%b %d, %Y %I:%M %p")" >> "$output_file" | |
if [ "$merged_at" != "null" ]; then | |
echo " - Merged Date: $(LC_TIME=en_US.UTF-8 date -d "$merged_at" +"%b %d, %Y %I:%M %p")" >> "$output_file" | |
else | |
echo " - Merged Date: Not merged yet" >> "$output_file" | |
fi | |
echo " - Days Open: $days_opened" >> "$output_file" | |
echo " - Lines of Codes: +$additions/-$deletions" >> "$output_file" | |
echo " - URL: [$url]($url)" >> "$output_file" | |
done | |
} | |
# Generate sections for each user and month | |
for user in "${users[@]}"; do | |
for month in "${months[@]}"; do | |
generate_pr_section "$user" "$month" | |
done | |
done | |
echo "Report generated in $output_file." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment