Created
May 12, 2025 12:44
-
-
Save lovromazgon/d04f9f167ccc99cbbc03199c2eee93db to your computer and use it in GitHub Desktop.
Find GitHub PRs in all repositories of an organization based on title
This file contains hidden or 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 | |
ORG_NAME="$1" | |
PR_TITLE="$2" | |
if [ -z "$ORG_NAME" ] || [ -z "$PR_TITLE" ]; then | |
echo "Usage: $0 <organization_name> <pull_request_title>" | |
exit 1 | |
fi | |
echo "Searching for PRs with title \"$PR_TITLE\" in organization \"$ORG_NAME\"..." | |
echo "--------------------------------------------------" | |
# Get list of repositories in the organization | |
# We use --json to easily parse the repository names and handle pagination | |
# Increased limit as before, consider more robust pagination for very large orgs | |
REPO_LIST=$(gh repo list "$ORG_NAME" --json name --limit 1000 | jq -r '.[].name') | |
if [ -z "$REPO_LIST" ]; then | |
echo "No repositories found in organization \"$ORG_NAME\"." | |
exit 0 | |
fi | |
FOUND_PRS=() | |
TOTAL_REPOS_CHECKED=0 | |
# Iterate through each repository and search for the PR title | |
for REPO_NAME in $REPO_LIST; do | |
TOTAL_REPOS_CHECKED=$((TOTAL_REPOS_CHECKED + 1)) | |
# Search for pull requests in the repository with the matching title | |
# We use --json to easily parse the PR URL and handle potential multiple matches | |
# The search query uses 'is:pr' and 'in:title' to filter by PRs with the title match | |
# Added .[]? to handle cases where jq might return null if no PRs are found | |
PR_URLS=$(gh pr list --repo "$ORG_NAME/$REPO_NAME" --search "is:pr in:title \"$PR_TITLE\"" --json url --limit 100 2>/dev/null | jq -r '.[].url // empty') | |
# Count the number of found PRs in this repository | |
# Read the URLs into an array to count them easily | |
REPO_FOUND_PR_URLS=() | |
if [ -n "$PR_URLS" ]; then | |
while IFS= read -r url; do | |
REPO_FOUND_PR_URLS+=("$url") | |
FOUND_PRS+=("$url") # Also add to the global list | |
done <<< "$PR_URLS" | |
fi | |
NUM_FOUND_IN_REPO=${#REPO_FOUND_PR_URLS[@]} | |
# Output the count for the current repository | |
echo "Checking repository: $ORG_NAME/$REPO_NAME ... found $NUM_FOUND_IN_REPO PRs" | |
done | |
echo "--------------------------------------------------" | |
echo "Finished checking $TOTAL_REPOS_CHECKED repositories." | |
# Print the found PR URLs | |
if [ ${#FOUND_PRS[@]} -eq 0 ]; then | |
echo -e "\nNo PRs found with the title \"$PR_TITLE\" in organization \"$ORG_NAME\"." | |
else | |
echo -e "\nAll found PR URLs:" | |
for pr_url in "${FOUND_PRS[@]}"; do | |
echo "$pr_url" | |
done | |
echo -e "\nTotal PRs found across all repositories: ${#FOUND_PRS[@]}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment