Skip to content

Instantly share code, notes, and snippets.

@ardaorkin
Created August 4, 2025 18:55
Show Gist options
  • Save ardaorkin/5d10268ffe1edf9925fb6b672a270629 to your computer and use it in GitHub Desktop.
Save ardaorkin/5d10268ffe1edf9925fb6b672a270629 to your computer and use it in GitHub Desktop.
Fetch GitHub PRs by User and Date Range

Fetch GitHub PRs by User and Date Range

This script retrieves pull requests created by a specific GitHub user within a date range across multiple repositories. It outputs a Markdown file containing:

  • PR Title & Link
  • Description
  • Full Diff (optional in script)
  • Review Comments (with file context)
  • Issue Discussion Threads

✅ Requirements


▶ Usage

  1. Make the script executable:
    chmod +x fetch_prs.sh
#!/bin/bash
# Script: fetch_prs.sh
# Description: Fetch pull requests created by a GitHub user in specified repositories and date range,
# and save them into a Markdown file with PR details, diffs, review comments, and discussions.
# Requirements: GitHub CLI (gh), jq
# Prompt for inputs
read -p "GitHub username: " GH_USER
read -p "Repository list (space-separated, e.g. org/repo1 org/repo2): " REPOS
read -p "Start date (YYYY-MM-DD): " START_DATE
read -p "End date (YYYY-MM-DD): " END_DATE
read -p "Output file name (default: prs_${START_DATE}_to_${END_DATE}.md): " OUTPUT_FILE
# Default output filename
OUTPUT_FILE=${OUTPUT_FILE:-prs_${START_DATE}_to_${END_DATE}.md}
# Validate date format (basic)
DATE_REGEX="^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
if [[ ! $START_DATE =~ $DATE_REGEX ]] || [[ ! $END_DATE =~ $DATE_REGEX ]]; then
echo "❌ Invalid date format. Use YYYY-MM-DD."
exit 1
fi
# Check if gh CLI exists
if ! command -v gh &> /dev/null; then
echo "❌ GitHub CLI (gh) is not installed. Install from https://cli.github.com/"
exit 1
fi
echo "# Pull Requests from $START_DATE to $END_DATE" > "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "Fetching PRs for $GH_USER between $START_DATE and $END_DATE..."
echo "Repos: $REPOS"
echo "Output: $OUTPUT_FILE"
echo ""
for repo in $REPOS; do
echo "Processing repository: $repo"
PR_URLS=$(gh api \
-H "Accept: application/vnd.github+json" \
"/search/issues?q=is:pr+author:$GH_USER+repo:$repo+created:$START_DATE..$END_DATE&per_page=100" \
--paginate --jq '.items[].pull_request.url')
if [ -z "$PR_URLS" ]; then
echo "No PRs found for $repo."
continue
fi
for url in $PR_URLS; do
echo "Fetching PR details: $url"
PR_DATA=$(gh api "$url")
NUMBER=$(echo "$PR_DATA" | jq .number)
TITLE=$(echo "$PR_DATA" | jq -r .title)
BODY=$(echo "$PR_DATA" | jq -r .body)
HTML_URL=$(echo "$PR_DATA" | jq -r .html_url)
echo -e "\n## [$repo] PR #$NUMBER: $TITLE" >> "$OUTPUT_FILE"
echo -e "**Link:** [$HTML_URL]($HTML_URL)" >> "$OUTPUT_FILE"
if [ -n "$BODY" ] && [ "$BODY" != "null" ]; then
echo -e "\n**Description:**\n$BODY\n" >> "$OUTPUT_FILE"
fi
# Fetch diff
echo -e "\n**Diff:**\n\`\`\`diff" >> "$OUTPUT_FILE"
gh api "$url" -H "Accept: application/vnd.github.v3.diff" >> "$OUTPUT_FILE"
echo -e "\n\`\`\`\n" >> "$OUTPUT_FILE"
# Fetch review comments
OWNER=$(echo $repo | cut -d'/' -f1)
REPO=$(echo $repo | cut -d'/' -f2)
COMMENTS=$(gh api "/repos/$OWNER/$REPO/pulls/$NUMBER/comments" --jq '.[] | "- \(.path): \(.body)"')
if [ ! -z "$COMMENTS" ]; then
echo -e "**Review Comments:**" >> "$OUTPUT_FILE"
echo "$COMMENTS" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
fi
# Fetch issue discussion comments (main PR thread)
DISCUSSIONS=$(gh api "/repos/$OWNER/$REPO/issues/$NUMBER/comments" --jq '.[] | "- \(.body)"')
if [ ! -z "$DISCUSSIONS" ]; then
echo -e "**Discussion Threads:**" >> "$OUTPUT_FILE"
echo "$DISCUSSIONS" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
fi
sleep 1 # avoid hitting rate limits
done
done
echo "✅ Done! All PR data saved to $OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment