Skip to content

Instantly share code, notes, and snippets.

@lcssanches
Created September 17, 2025 14:39
Show Gist options
  • Select an option

  • Save lcssanches/4d4278cc8bc344483c9198341b9ca459 to your computer and use it in GitHub Desktop.

Select an option

Save lcssanches/4d4278cc8bc344483c9198341b9ca459 to your computer and use it in GitHub Desktop.
#!/bin/bash
# --- VARIABLES ---
GITHUB_TOKEN="pat" # ⚠️ Replace with your actual token
ORG_NAME="orgname" # ⚠️ Replace with your organization name
BRANCH_SUBSTRING="shai" # The substring to search for (case-insensitive)
API_URL="https://api.github.com"
PAGE=1
PER_PAGE=100
echo "Searching for branch names containing '$BRANCH_SUBSTRING' in all repositories of '$ORG_NAME'..."
echo "---"
while true; do
# Get a list of repositories for the current page
REPOS_RESPONSE=$(curl -s -L \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$API_URL/orgs/$ORG_NAME/repos?per_page=$PER_PAGE&page=$PAGE")
# Check if there are no more repositories
if [ "$(echo "$REPOS_RESPONSE" | jq 'length')" -eq 0 ]; then
break
fi
# Loop through each repository on the current page
echo "$REPOS_RESPONSE" | jq -r '.[].full_name' | while read -r repo_full_name; do
# Get all branches for the current repository
echo "Checking repository: $repo_full_name"
BRANCHES_RESPONSE=$(curl -s -L \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$API_URL/repos/$repo_full_name/branches?per_page=100")
# Check if any branch name contains the substring (case-insensitive)
MATCHING_BRANCHES=$(echo "$BRANCHES_RESPONSE" | jq -r --arg substr "$BRANCH_SUBSTRING" '.[].name | select(test($substr; "i"))')
# If any branches are found, report them
if [ -n "$MATCHING_BRANCHES" ]; then
echo "$MATCHING_BRANCHES" | while read -r branch_name; do
echo "✅ Found branch '$branch_name' in: $repo_full_name"
done
fi
done
# Increment the page number for the next iteration
((PAGE++))
done
echo "---"
echo "Search complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment